Skip to content

Commit

Permalink
refs WorksApplications#17: implement NullablePrimitiveDetector.
Browse files Browse the repository at this point in the history
  • Loading branch information
worksap-bot committed Mar 17, 2014
1 parent 336179f commit d7b992f
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ To use this product, please configure your findbugs-maven-plugin like below.
- added ImplicitLengthDetector
- added ImplicitNullnessDetector
- added ColumnDefinitionDetector
- added NullablePrimitiveDetector

## 0.0.1

Expand Down
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.
}

}
1 change: 1 addition & 0 deletions src/main/meta/bugrank.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
0 BugPattern IMPLICIT_LENGTH
0 BugPattern ILLEGAL_LENGTH
0 BugPattern USE_COLUMN_DEFINITION
0 BugPattern NULLABLE_PRIMITIVE
5 changes: 5 additions & 0 deletions src/main/meta/findbugs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<BugPattern type="USE_COLUMN_DEFINITION" abbrev="JPA"
category="BAD_PRACTICE" />

<Detector class="jp.co.worksap.oss.findbugs.jpa.NullablePrimitiveDetector"
speed="fast" hidden="false" />
<BugPattern type="NULLABLE_PRIMITIVE" abbrev="JPA"
category="CORRECTNESS" />

<Detector class="jp.co.worksap.oss.findbugs.junit.UndocumentedIgnoreDetector"
speed="fast" hidden="false" />
<BugPattern type="UNDOCUMENTED_IGNORE" abbrev="JUNIT"
Expand Down
21 changes: 21 additions & 0 deletions src/main/meta/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@
</Details>
</BugPattern>

<Detector class="jp.co.worksap.oss.findbugs.jpa.NullablePrimitiveDetector">
<Details>
This detector will find illegal nullable property of primitive type.
When developer changes type of field from primitive type to reference type (ex. from int to Integer),
he/she might forget to care about this property. This detector helps developer to find this human-error.
</Details>
</Detector>

<BugPattern type="NULLABLE_PRIMITIVE">
<ShortDescription>Nullable property of primitive type should be false.
</ShortDescription>
<LongDescription>
Nullable property of primitive type should be false.
</LongDescription>
<Details>
<![CDATA[
<p>Nullable property of primitive type should be false.</p>
]]>
</Details>
</BugPattern>

<Detector class="jp.co.worksap.oss.findbugs.junit.UndocumentedIgnoreDetector">
<Details>
This detector will find ignored test case which has no explanation about why it is ignored.
Expand Down
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
}

}
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;
}

0 comments on commit d7b992f

Please sign in to comment.