Skip to content

Commit c85aef3

Browse files
committed
Issue #154 - Enforce code style
Adding PMD
1 parent 878279b commit c85aef3

File tree

8 files changed

+209
-76
lines changed

8 files changed

+209
-76
lines changed

pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<slf4j-test.version>1.2.0</slf4j-test.version>
5050
<sqlite4java.version>1.0.392</sqlite4java.version>
5151

52+
<findbugs.version>3.0.5</findbugs.version>
5253
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5354
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5455
<maven.compiler.source>1.8</maven.compiler.source>
@@ -390,6 +391,33 @@
390391
<configLocation>checkstyle.xml</configLocation>
391392
</configuration>
392393
</plugin>
394+
<plugin>
395+
<groupId>org.codehaus.mojo</groupId>
396+
<artifactId>findbugs-maven-plugin</artifactId>
397+
<version>${findbugs.version}</version>
398+
<configuration>
399+
<!-- 2.5.2
400+
Enables analysis which takes more memory but finds more bugs.
401+
If you run out of memory, changes the value of the effort element
402+
to 'Low'.
403+
-->
404+
<effort>Max</effort>
405+
<!-- Reports all bugs (other values are medium and max) -->
406+
<threshold>Max</threshold>
407+
<!-- Produces XML report
408+
<xmlOutput>true</xmlOutput>
409+
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
410+
-->
411+
</configuration>
412+
<executions>
413+
<execution>
414+
<phase>compile</phase>
415+
<goals>
416+
<goal>check</goal>
417+
</goals>
418+
</execution>
419+
</executions>
420+
</plugin>
393421
<plugin>
394422
<groupId>org.apache.maven.plugins</groupId>
395423
<artifactId>maven-release-plugin</artifactId>
@@ -822,6 +850,35 @@
822850
<configLocation>checkstyle.xml</configLocation>
823851
</configuration>
824852
</plugin>
853+
<plugin>
854+
<groupId>org.codehaus.mojo</groupId>
855+
<artifactId>findbugs-maven-plugin</artifactId>
856+
<version>${findbugs.version}</version>
857+
<configuration>
858+
<effort>Max</effort>
859+
<!-- Reports all bugs (other values are medium and max) -->
860+
<threshold>Low</threshold>
861+
</configuration>
862+
</plugin>
863+
<plugin>
864+
<groupId>org.apache.maven.plugins</groupId>
865+
<artifactId>maven-jxr-plugin</artifactId>
866+
<version>2.3</version>
867+
</plugin>
868+
<plugin>
869+
<groupId>org.apache.maven.plugins</groupId>
870+
<artifactId>maven-pmd-plugin</artifactId>
871+
<version>3.9.0</version>
872+
<configuration>
873+
<linkXRef>true</linkXRef>
874+
<analysisCache>false</analysisCache>
875+
<skipEmptyReport>false</skipEmptyReport>
876+
<benchmark>true</benchmark>
877+
<rulesets>
878+
<ruleset>src/pmd.xml</ruleset>
879+
</rulesets>
880+
</configuration>
881+
</plugin>
825882
<plugin>
826883
<groupId>org.apache.maven.plugins</groupId>
827884
<artifactId>maven-surefire-report-plugin</artifactId>

src/main/java/org/socialsignin/spring/data/dynamodb/config/AbstractDynamoDBConfiguration.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
109109
componentProvider.addIncludeFilter(new AnnotationTypeFilter(DynamoDBTable.class));
110110

111111
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
112-
LOGGER.trace("getInitialEntitySet. candidate: {}", candidate.getBeanClassName());
113-
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
114-
AbstractDynamoDBConfiguration.class.getClassLoader()));
112+
String candidateClass = candidate.getBeanClassName();
113+
if (candidateClass != null) {
114+
LOGGER.trace("getInitialEntitySet. candidate: {}", candidateClass);
115+
initialEntitySet.add(ClassUtils.forName(candidateClass,
116+
AbstractDynamoDBConfiguration.class.getClassLoader()));
117+
} else {
118+
LOGGER.warn("getInitialEntitySet. candidate: {} did not provide a class", candidate);
119+
}
115120
}
116121
}
117122
}

src/main/java/org/socialsignin/spring/data/dynamodb/mapping/DynamoDBPersistentEntityImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,19 @@ protected DynamoDBPersistentProperty returnPropertyIfBetterIdPropertyCandidateOr
5151
return null;
5252
}
5353

54-
if (getIdProperty() != null) {
54+
DynamoDBPersistentProperty idProperty = getIdProperty();
55+
if (idProperty != null) {
5556

56-
if (getIdProperty().isCompositeIdProperty() && property.isHashKeyProperty()) {
57+
if (idProperty.isCompositeIdProperty() && property.isHashKeyProperty()) {
5758
// Do nothing - favour id annotated properties over hashkey
5859
return null;
59-
} else if (getIdProperty().isHashKeyProperty() && property.isCompositeIdProperty()) {
60+
} else if (idProperty.isHashKeyProperty() && property.isCompositeIdProperty()) {
6061
return property;
6162
} else {
6263
throw new MappingException(String.format(
6364
"Attempt to add id property %s but already have property %s registered "
6465
+ "as id. Check your mapping configuration!",
65-
property.getField(), getIdProperty().getField()));
66+
property.getField(), idProperty.getField()));
6667
}
6768
}
6869

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCriteria.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.domain.Sort;
3535
import org.springframework.data.domain.Sort.Direction;
3636
import org.springframework.data.domain.Sort.Order;
37+
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;
3839
import org.springframework.util.ClassUtils;
3940
import org.springframework.util.LinkedMultiValueMap;
@@ -480,7 +481,7 @@ protected <V extends Object> Object getPropertyAttributeValue(final String prope
480481
.getTypeConverterForProperty(propertyName);
481482

482483
if (converter != null) {
483-
return converter.convert((V) value);
484+
return converter.convert(value);
484485
}
485486

486487
DynamoDBMarshaller<V> marshaller = (DynamoDBMarshaller<V>) entityInformation
@@ -560,7 +561,11 @@ private List<String> getBooleanListAsStringList(List<Boolean> booleanList) {
560561
}
561562

562563
@SuppressWarnings("unchecked")
563-
private <P> List<P> getAttributeValueAsList(Object attributeValue) {
564+
@Nullable
565+
private <P> List<P> getAttributeValueAsList(@Nullable Object attributeValue) {
566+
if (attributeValue == null) {
567+
return null;
568+
}
564569
boolean isIterable = ClassUtils.isAssignable(Iterable.class, attributeValue.getClass());
565570
if (isIterable) {
566571
List<P> attributeValueAsList = new ArrayList<>();
@@ -573,8 +578,8 @@ private <P> List<P> getAttributeValueAsList(Object attributeValue) {
573578
return null;
574579
}
575580

576-
protected <P> List<AttributeValue> addAttributeValue(List<AttributeValue> attributeValueList, Object attributeValue,
577-
String propertyName, Class<P> propertyType, boolean expandCollectionValues) {
581+
protected <P> List<AttributeValue> addAttributeValue(List<AttributeValue> attributeValueList,
582+
@Nullable Object attributeValue, Class<P> propertyType, boolean expandCollectionValues) {
578583
AttributeValue attributeValueObject = new AttributeValue();
579584

580585
if (ClassUtils.isAssignable(String.class, propertyType)) {
@@ -637,7 +642,7 @@ protected Condition createSingleValueCondition(String propertyName, ComparisonOp
637642
Assert.notNull(o, "Creating conditions on null property values not supported: please specify a value for '"
638643
+ propertyName + "'");
639644

640-
List<AttributeValue> attributeValueList = new ArrayList<>();
645+
List<AttributeValue> attributeValueList = new ArrayList<>(1);
641646
Object attributeValue = !alreadyMarshalledIfRequired ? getPropertyAttributeValue(propertyName, o) : o;
642647
if (ClassUtils.isAssignableValue(AttributeValue.class, attributeValue)) {
643648
attributeValueList.add((AttributeValue) attributeValue);
@@ -646,8 +651,7 @@ protected Condition createSingleValueCondition(String propertyName, ComparisonOp
646651
&& !entityInformation.isCompositeHashAndRangeKeyProperty(propertyName);
647652

648653
Class<?> targetPropertyType = marshalled ? String.class : propertyType;
649-
attributeValueList = addAttributeValue(attributeValueList, attributeValue, propertyName, targetPropertyType,
650-
true);
654+
addAttributeValue(attributeValueList, attributeValue, targetPropertyType, true);
651655
}
652656

653657
return new Condition().withComparisonOperator(comparisonOperator).withAttributeValueList(attributeValueList);
@@ -670,8 +674,7 @@ protected Condition createCollectionCondition(String propertyName, ComparisonOpe
670674
&& !entityInformation.isCompositeHashAndRangeKeyProperty(propertyName);
671675
}
672676
Class<?> targetPropertyType = marshalled ? String.class : propertyType;
673-
attributeValueList = addAttributeValue(attributeValueList, attributeValue, propertyName,
674-
targetPropertyType, false);
677+
addAttributeValue(attributeValueList, attributeValue, targetPropertyType, false);
675678
}
676679
}
677680

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
2929
import org.springframework.util.Assert;
3030
import org.springframework.util.ReflectionUtils;
31-
import org.springframework.util.ReflectionUtils.FieldCallback;
32-
import org.springframework.util.ReflectionUtils.MethodCallback;
3331
import org.springframework.util.StringUtils;
3432

3533
import java.lang.annotation.Annotation;
@@ -55,7 +53,7 @@ public class DynamoDBEntityMetadataSupport<T, ID> implements DynamoDBHashKeyExtr
5553
private List<String> globalIndexRangeKeyPropertyNames;
5654

5755
private String dynamoDBTableName;
58-
private Map<String, String[]> globalSecondaryIndexNames = new HashMap<String, String[]>();
56+
private Map<String, String[]> globalSecondaryIndexNames = new HashMap<>();
5957

6058
@Override
6159
public String getDynamoDBTableName() {
@@ -73,50 +71,47 @@ public DynamoDBEntityMetadataSupport(final Class<T> domainType) {
7371

7472
Assert.notNull(domainType, "Domain type must not be null!");
7573
this.domainType = domainType;
74+
7675
DynamoDBTable table = this.domainType.getAnnotation(DynamoDBTable.class);
7776
Assert.notNull(table, "Domain type must by annotated with DynamoDBTable!");
77+
7878
this.dynamoDBTableName = table.tableName();
79+
this.hashKeyPropertyName = null;
7980
this.globalSecondaryIndexNames = new HashMap<>();
8081
this.globalIndexHashKeyPropertyNames = new ArrayList<>();
8182
this.globalIndexRangeKeyPropertyNames = new ArrayList<>();
82-
ReflectionUtils.doWithMethods(domainType, new MethodCallback() {
83-
@Override
84-
public void doWith(Method method) {
85-
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
86-
hashKeyPropertyName = getPropertyNameForAccessorMethod(method);
87-
}
88-
if (method.getAnnotation(DynamoDBRangeKey.class) != null) {
89-
hasRangeKey = true;
90-
}
91-
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = method.getAnnotation(DynamoDBIndexRangeKey.class);
92-
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = method.getAnnotation(DynamoDBIndexHashKey.class);
93-
94-
if (dynamoDBRangeKeyAnnotation != null) {
95-
addGlobalSecondaryIndexNames(method, dynamoDBRangeKeyAnnotation);
96-
}
97-
if (dynamoDBHashKeyAnnotation != null) {
98-
addGlobalSecondaryIndexNames(method, dynamoDBHashKeyAnnotation);
99-
}
83+
ReflectionUtils.doWithMethods(domainType, method -> {
84+
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
85+
hashKeyPropertyName = getPropertyNameForAccessorMethod(method);
86+
}
87+
if (method.getAnnotation(DynamoDBRangeKey.class) != null) {
88+
hasRangeKey = true;
89+
}
90+
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = method.getAnnotation(DynamoDBIndexRangeKey.class);
91+
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = method.getAnnotation(DynamoDBIndexHashKey.class);
92+
93+
if (dynamoDBRangeKeyAnnotation != null) {
94+
addGlobalSecondaryIndexNames(method, dynamoDBRangeKeyAnnotation);
95+
}
96+
if (dynamoDBHashKeyAnnotation != null) {
97+
addGlobalSecondaryIndexNames(method, dynamoDBHashKeyAnnotation);
10098
}
10199
});
102-
ReflectionUtils.doWithFields(domainType, new FieldCallback() {
103-
@Override
104-
public void doWith(Field field) {
105-
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
106-
hashKeyPropertyName = getPropertyNameForField(field);
107-
}
108-
if (field.getAnnotation(DynamoDBRangeKey.class) != null) {
109-
hasRangeKey = true;
110-
}
111-
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = field.getAnnotation(DynamoDBIndexRangeKey.class);
112-
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = field.getAnnotation(DynamoDBIndexHashKey.class);
113-
114-
if (dynamoDBRangeKeyAnnotation != null) {
115-
addGlobalSecondaryIndexNames(field, dynamoDBRangeKeyAnnotation);
116-
}
117-
if (dynamoDBHashKeyAnnotation != null) {
118-
addGlobalSecondaryIndexNames(field, dynamoDBHashKeyAnnotation);
119-
}
100+
ReflectionUtils.doWithFields(domainType, field -> {
101+
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
102+
hashKeyPropertyName = getPropertyNameForField(field);
103+
}
104+
if (field.getAnnotation(DynamoDBRangeKey.class) != null) {
105+
hasRangeKey = true;
106+
}
107+
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = field.getAnnotation(DynamoDBIndexRangeKey.class);
108+
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = field.getAnnotation(DynamoDBIndexHashKey.class);
109+
110+
if (dynamoDBRangeKeyAnnotation != null) {
111+
addGlobalSecondaryIndexNames(field, dynamoDBRangeKeyAnnotation);
112+
}
113+
if (dynamoDBHashKeyAnnotation != null) {
114+
addGlobalSecondaryIndexNames(field, dynamoDBHashKeyAnnotation);
120115
}
121116
});
122117
Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!");

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/FieldAndGetterReflectionEntityInformation.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.socialsignin.spring.data.dynamodb.repository.support;
1717

18-
import org.springframework.data.annotation.Id;
1918
import org.springframework.data.repository.core.support.AbstractEntityInformation;
19+
import org.springframework.lang.NonNull;
2020
import org.springframework.util.Assert;
2121
import org.springframework.util.ReflectionUtils;
2222

@@ -34,24 +34,9 @@
3434
*/
3535
public class FieldAndGetterReflectionEntityInformation<T, ID> extends AbstractEntityInformation<T, ID> {
3636

37-
private static final Class<Id> DEFAULT_ID_ANNOTATION = Id.class;
38-
3937
protected Method method;
4038
private Field field;
4139

42-
/**
43-
* Creates a new
44-
* {@link org.springframework.data.repository.core.support.ReflectionEntityInformation}
45-
* inspecting the given domain class for a getter carrying the {@link Id}
46-
* annotation.
47-
*
48-
* @param domainClass
49-
* must not be {@literal null}.
50-
*/
51-
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass) {
52-
this(domainClass, DEFAULT_ID_ANNOTATION);
53-
}
54-
5540
/**
5641
* Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting
5742
* the given domain class for a getter carrying the given annotation.
@@ -61,23 +46,24 @@ public FieldAndGetterReflectionEntityInformation(Class<T> domainClass) {
6146
* @param annotation
6247
* must not be {@literal null}.
6348
*/
64-
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass,
65-
final Class<? extends Annotation> annotation) {
49+
public FieldAndGetterReflectionEntityInformation(@NonNull Class<T> domainClass,
50+
@NonNull final Class<? extends Annotation> annotation) {
6651

6752
super(domainClass);
6853
Assert.notNull(annotation, "annotation must not be null!");
6954

7055
ReflectionUtils.doWithMethods(domainClass, (method) -> {
7156
if (method.getAnnotation(annotation) != null) {
72-
FieldAndGetterReflectionEntityInformation.this.method = method;
57+
this.method = method;
7358
return;
7459
}
7560
});
7661

7762
if (method == null) {
63+
field = null;
7864
ReflectionUtils.doWithFields(domainClass, (field) -> {
7965
if (field.getAnnotation(annotation) != null) {
80-
FieldAndGetterReflectionEntityInformation.this.field = field;
66+
this.field = field;
8167
return;
8268
}
8369
});

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/HashAndRangeKeyExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
*/
2222
public interface HashAndRangeKeyExtractor<ID, H> extends HashKeyExtractor<ID, H> {
2323

24-
public Object getRangeKey(ID id);
24+
Object getRangeKey(ID id);
2525

2626
}

0 commit comments

Comments
 (0)