Skip to content

Commit

Permalink
Issue 559: NullPointerException when @nonnull is used in abstract method
Browse files Browse the repository at this point in the history
- Prevent NPE in javac and give a proper warning in both eclipse and javac
- Add test cases
  • Loading branch information
askoning committed Sep 20, 2013
1 parent cf7290f commit 72b55dc
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/lombok/eclipse/handlers/NonNullHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public class NonNullHandler extends EclipseAnnotationHandler<NonNull> {

if (isGenerated(declaration)) return;

if (declaration.isAbstract()) {
annotationNode.addWarning("@NonNull is meaningless on a parameter of an abstract method.");
return;
}

// Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter,
// and if they exist, create a new method in the class: 'private static <T> T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and
// wrap all references to it in the super/this to a call to this method.
Expand Down
5 changes: 5 additions & 0 deletions src/core/lombok/javac/handlers/NonNullHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public class NonNullHandler extends JavacAnnotationHandler<NonNull> {

if (JavacHandlerUtil.isGenerated(declaration)) return;

if (declaration.body == null) {
annotationNode.addWarning("@NonNull is meaningless on a parameter of an abstract method.");
return;
}

// Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter,
// and if they exist, create a new method in the class: 'private static <T> T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and
// wrap all references to it in the super/this to a call to this method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
abstract class NonNullOnParameterAbstract {
public void test(@lombok.NonNull String arg) {
if (arg == null) {
throw new java.lang.NullPointerException("arg");
}
System.out.println("Hey");
}

public abstract void test2(@lombok.NonNull String arg);
}
13 changes: 13 additions & 0 deletions test/transform/resource/after-ecj/NonNullOnParameterAbstract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
abstract class NonNullOnParameterAbstract {
NonNullOnParameterAbstract() {
super();
}
public void test(@lombok.NonNull String arg) {
if ((arg == null))
{
throw new java.lang.NullPointerException("arg");
}
System.out.println("Hey");
}
public abstract void test2(@lombok.NonNull String arg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
abstract class NonNullOnParameterAbstract {
public void test(@lombok.NonNull String arg) {
System.out.println("Hey");
}

public abstract void test2(@lombok.NonNull String arg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6:36 @NonNull is meaningless on a parameter of an abstract method.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6:154 @NonNull is meaningless on a parameter of an abstract method.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9:36 @NonNull is meaningless on a parameter of an abstract method.

0 comments on commit 72b55dc

Please sign in to comment.