forked from WorksApplications/findbugs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WorksApplications#18 from eller86/nullable-primitive
Detector for nullable primitive
- Loading branch information
Showing
14 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/jp/co/worksap/oss/findbugs/jpa/NullablePrimitiveDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, ElementValue> 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<String, ElementValue> 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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/jp/co/worksap/oss/findbugs/jpa/NonNullablePrimitiveColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NonNullablePrimitiveColumn { | ||
@Column(nullable = false) | ||
private boolean booleanValue; | ||
|
||
@Column(nullable = false) | ||
private byte byteValue; | ||
|
||
@Column(nullable = false) | ||
private short shortValue; | ||
|
||
@Column(nullable = false) | ||
private int intValue; | ||
|
||
@Column(nullable = false) | ||
private long longValue; | ||
|
||
@Column(nullable = false) | ||
private float floatValue; | ||
|
||
@Column(nullable = false) | ||
private double doubleValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableBooleanColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableBooleanColumn { | ||
@Column(nullable = true) | ||
private boolean booleanValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableByteColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableByteColumn { | ||
@Column(nullable = true) | ||
private byte byteValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableDoubleColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableDoubleColumn { | ||
@Column(nullable = true) | ||
private double doubleValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableFloatColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableFloatColumn { | ||
@Column(nullable = true) | ||
private float floatValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableIntColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableIntColumn { | ||
@Column(nullable = true) | ||
private int intValue; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableLongColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableLongColumn { | ||
@Column(nullable = true) | ||
private long longValue; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullablePrimitiveDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import static com.youdevise.fbplugins.tdd4fb.DetectorAssert.assertBugReported; | ||
import static com.youdevise.fbplugins.tdd4fb.DetectorAssert.assertNoBugsReported; | ||
import static com.youdevise.fbplugins.tdd4fb.DetectorAssert.bugReporterForTesting; | ||
import static com.youdevise.fbplugins.tdd4fb.DetectorAssert.ofType; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.BugReporter; | ||
|
||
public class NullablePrimitiveDetectorTest { | ||
private BugReporter bugReporter; | ||
private NullablePrimitiveDetector detector; | ||
|
||
@Before | ||
public void before() { | ||
this.bugReporter = bugReporterForTesting(); | ||
this.detector = new NullablePrimitiveDetector(bugReporter); | ||
} | ||
|
||
@Test | ||
public void testNullableObject() throws Exception { | ||
assertNoBugsReported(UseColumnDefinition.class, detector, bugReporter); | ||
} | ||
|
||
@Test | ||
public void testNullableInt() throws Exception { | ||
assertBugReported(NullableBooleanColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableByteColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableShortColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableIntColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableLongColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableFloatColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
assertBugReported(NullableDoubleColumn.class, detector, | ||
bugReporter, ofType("NULLABLE_PRIMITIVE")); | ||
} | ||
|
||
@Test | ||
public void testNonNullableObject() throws Exception { | ||
assertNoBugsReported(ColumnWithNullable.class, detector, bugReporter); | ||
} | ||
|
||
@Test | ||
public void testNonNullableInt() throws Exception { | ||
assertNoBugsReported(NonNullablePrimitiveColumn.class, detector, bugReporter); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/jp/co/worksap/oss/findbugs/jpa/NullableShortColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package jp.co.worksap.oss.findbugs.jpa; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class NullableShortColumn { | ||
@Column(nullable = true) | ||
private short shortValue; | ||
} |