diff --git a/README.md b/README.md index 390ec9a..7650414 100755 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ To use this product, please configure your findbugs-maven-plugin like below. - added ImplicitLengthDetector - added ImplicitNullnessDetector - added ColumnDefinitionDetector +- added NullablePrimitiveDetector ## 0.0.1 diff --git a/src/main/java/jp/co/worksap/oss/findbugs/jpa/NullablePrimitiveDetector.java b/src/main/java/jp/co/worksap/oss/findbugs/jpa/NullablePrimitiveDetector.java new file mode 100644 index 0000000..6aeaac3 --- /dev/null +++ b/src/main/java/jp/co/worksap/oss/findbugs/jpa/NullablePrimitiveDetector.java @@ -0,0 +1,58 @@ +package jp.co.worksap.oss.findbugs.jpa; + +import java.util.Map; + +import org.apache.bcel.classfile.ElementValue; +import org.apache.bcel.generic.ObjectType; +import org.apache.bcel.generic.Type; + +import edu.umd.cs.findbugs.BugInstance; +import edu.umd.cs.findbugs.BugReporter; + +public class NullablePrimitiveDetector extends AbstractColumnDetector { + + public NullablePrimitiveDetector(BugReporter bugReporter) { + super(bugReporter); + } + + @Override + protected void verifyColumn(Type columnType, + Map elements) { + if (! isPrimitive(columnType)) { + return; + } + + boolean isNullableColumn = detectNullability(elements); + if (isNullableColumn) { + reportNullablePrimitive(columnType); + } + } + + private void reportNullablePrimitive(Type columnType) { + BugInstance bug = new BugInstance(this, "NULLABLE_PRIMITIVE", NORMAL_PRIORITY); + if (visitingMethod()) { + bug.addMethod(this); + } + getBugReporter().reportBug(bug); + + } + + private boolean detectNullability(Map elements) { + if (! elements.containsKey("nullable")) { + // in JPA 1.0 specification, default value of 'nullable' parameter is true + // note that this case will be reported by ImplicitNullnessDetector. + return true; + } + + String nullability = elements.get("nullable").stringifyValue(); + return "true".equals(nullability); + } + + /** + * @return true if column type is primitive value (not reference type). + */ + private boolean isPrimitive(Type columnType) { + return ! (columnType instanceof ObjectType); // looks bad, but simple way to check primitive or not. + } + +} diff --git a/src/main/meta/bugrank.txt b/src/main/meta/bugrank.txt index 832ee33..5bbbeb5 100755 --- a/src/main/meta/bugrank.txt +++ b/src/main/meta/bugrank.txt @@ -11,3 +11,4 @@ 0 BugPattern IMPLICIT_LENGTH 0 BugPattern ILLEGAL_LENGTH 0 BugPattern USE_COLUMN_DEFINITION +0 BugPattern NULLABLE_PRIMITIVE diff --git a/src/main/meta/findbugs.xml b/src/main/meta/findbugs.xml index 15afa6d..0b8b881 100755 --- a/src/main/meta/findbugs.xml +++ b/src/main/meta/findbugs.xml @@ -54,6 +54,11 @@ +