Skip to content

Commit

Permalink
Add ability to ignore properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sykesd committed Nov 2, 2018
1 parent fd49938 commit cd5e9a5
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/org/dt/japper/JapperIgnore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.dt.japper;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marker annotation to tell the mapper to ignore this property.
* <p>
* This should be placed on either the getter or the setter method of the property.
* Placing it on either one will tell Japper to ignore this property during matching.
* </p>
* <p>
* The initial use-case for this is to allow for the prevention of infinite loops.
* </p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JapperIgnore {
}
38 changes: 36 additions & 2 deletions src/org/dt/japper/PropertyMatcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.dt.japper;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
Expand Down Expand Up @@ -504,7 +505,8 @@ private void buildPropertyList(Class<?> type, Stack<PropertyDescriptor> path) {

for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(type)) {
if ("class".equals(descriptor.getName())) continue;

if (isIgnored(descriptor)) continue;

path.push(descriptor);

if (MapperUtils.isSimpleType(descriptor.getPropertyType())) {
Expand All @@ -517,7 +519,39 @@ private void buildPropertyList(Class<?> type, Stack<PropertyDescriptor> path) {
path.pop();
}
}


/**
* Should we ignore the given property?
* <p>
* Some properties can be annotated to tell Japper to ignore them. This
* method checks for this case.
* </p>
*
* @param descriptor the {@link PropertyDescriptor} describing the property to check
* @return {@code true} if it should be ignored, {@code false} otherwise
*/
private boolean isIgnored(PropertyDescriptor descriptor) {
Method readMethod = descriptor.getReadMethod();
if (isIgnored(readMethod)) {
return true;
}

Method writeMethod = descriptor.getWriteMethod();
if (isIgnored(writeMethod)) {
return true;
}

return false;
}

private boolean isIgnored(Method method) {
if (method == null) {
return false;
}

return method.getAnnotation(JapperIgnore.class) != null;
}

private static String pathToPrefix(List<PropertyDescriptor> path) {
StringBuilder sb = new StringBuilder();
for (PropertyDescriptor descriptor : path) {
Expand Down
76 changes: 76 additions & 0 deletions test-src/org/dt/japper/SimpleRecursiveModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.dt.japper;

/*
* Copyright (c) 2018, David Sykes and Tomasz Orzechowski
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name David Sykes nor Tomasz Orzechowski may be used to endorse
* or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/


import org.dt.japper.testmodel.RecursiveIgnoredPartModel;
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.Connection;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SimpleRecursiveModelTest {

private static TestData testData;

@BeforeClass
public static void setupDB() throws Exception {
testData = new TestData();
testData.create();
}

private static final String SQL_PARTS =
" SELECT *"
+ " FROM part"
+ " ORDER BY partno"
;

@Test
public void mapPartsIgnoringRecursion() throws Exception {
Connection conn = testData.connect();

List<RecursiveIgnoredPartModel> parts = Japper.query(conn, RecursiveIgnoredPartModel.class, SQL_PARTS);
assertEquals(3, parts.size());

RecursiveIgnoredPartModel part = parts.get(0);
assertEquals("123456", part.getPartno());
assertEquals("FAB", part.getPartType());

conn.close();
}

}
87 changes: 87 additions & 0 deletions test-src/org/dt/japper/testmodel/RecursiveIgnoredPartModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.dt.japper.testmodel;

/*
* Copyright (c) 2018, David Sykes and Tomasz Orzechowski
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name David Sykes nor Tomasz Orzechowski may be used to endorse
* or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/

import org.dt.japper.JapperIgnore;

import java.math.BigDecimal;
import java.util.Map;

public class RecursiveIgnoredPartModel {

private String partno;
private String description;
private String partType;
private Map<String, BigDecimal> flexFields;

// This is the recursive part!
private RecursiveIgnoredPartModel otherPart;

public String getPartno() {
return partno;
}
public void setPartno(String partno) {
this.partno = partno;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPartType() {
return partType;
}
public void setPartType(String partType) {
this.partType = partType;
}


public Map<String, BigDecimal> getFlexFields() {
return flexFields;
}

public void setFlexFields(Map<String, BigDecimal> flexFields) {
this.flexFields = flexFields;
}

public RecursiveIgnoredPartModel getOtherPart() {
return otherPart;
}

@JapperIgnore
public void setOtherPart(RecursiveIgnoredPartModel otherPart) {
this.otherPart = otherPart;
}
}

0 comments on commit cd5e9a5

Please sign in to comment.