aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r--src/core/lombok/javac/handlers/HandleBuilder.java6
-rw-r--r--src/core/lombok/javac/handlers/HandleConstructor.java9
-rw-r--r--src/core/lombok/javac/handlers/HandleNonNull.java4
-rw-r--r--src/core/lombok/javac/handlers/HandleSetter.java26
-rw-r--r--src/core/lombok/javac/handlers/HandleWither.java19
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java31
6 files changed, 57 insertions, 38 deletions
diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java
index bf3b39dc..1885b8b4 100644
--- a/src/core/lombok/javac/handlers/HandleBuilder.java
+++ b/src/core/lombok/javac/handlers/HandleBuilder.java
@@ -194,12 +194,12 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
java.util.List<JavacNode> fieldNodes = addFieldsToBuilder(builderType, namesOfParameters, typesOfParameters, ast);
java.util.List<JCMethodDecl> newMethods = new ArrayList<JCMethodDecl>();
for (JavacNode fieldNode : fieldNodes) {
- JCMethodDecl newMethod = makeSetterMethodForBuilder(builderType, fieldNode, ast, builderInstance.fluent(), builderInstance.chain());
+ JCMethodDecl newMethod = makeSetterMethodForBuilder(builderType, fieldNode, annotationNode, builderInstance.fluent(), builderInstance.chain());
if (newMethod != null) newMethods.add(newMethod);
}
if (constructorExists(builderType) == MemberExistsResult.NOT_EXISTS) {
- JCMethodDecl cd = HandleConstructor.createConstructor(AccessLevel.PACKAGE, List.<JCAnnotation>nil(), builderType, List.<JavacNode>nil(), null, ast);
+ JCMethodDecl cd = HandleConstructor.createConstructor(AccessLevel.PACKAGE, List.<JCAnnotation>nil(), builderType, List.<JavacNode>nil(), null, annotationNode);
if (cd != null) injectMethod(builderType, cd);
}
@@ -300,7 +300,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
}
- public JCMethodDecl makeSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JCTree source, boolean fluent, boolean chain) {
+ public JCMethodDecl makeSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JavacNode source, boolean fluent, boolean chain) {
Name fieldName = ((JCVariableDecl) fieldNode.get()).name;
for (JavacNode child : builderType.down()) {
diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java
index 0618706e..6043d1cb 100644
--- a/src/core/lombok/javac/handlers/HandleConstructor.java
+++ b/src/core/lombok/javac/handlers/HandleConstructor.java
@@ -214,7 +214,7 @@ public class HandleConstructor {
}
}
- JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, suppressConstructorProperties, source.get());
+ JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, suppressConstructorProperties, source);
injectMethod(typeNode, constr);
if (staticConstrRequired) {
JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, fields, source.get());
@@ -236,7 +236,7 @@ public class HandleConstructor {
mods.annotations = mods.annotations.append(annotation);
}
- public static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, Boolean suppressConstructorProperties, JCTree source) {
+ public static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, Boolean suppressConstructorProperties, JavacNode source) {
JavacTreeMaker maker = typeNode.getTreeMaker();
boolean isEnum = (((JCClassDecl) typeNode.get()).mods.flags & Flags.ENUM) != 0;
@@ -268,7 +268,7 @@ public class HandleConstructor {
assigns.append(maker.Exec(assign));
if (!nonNulls.isEmpty()) {
- JCStatement nullCheck = generateNullCheck(maker, fieldNode);
+ JCStatement nullCheck = generateNullCheck(maker, fieldNode, source);
if (nullCheck != null) nullChecks.append(nullCheck);
}
}
@@ -280,7 +280,8 @@ public class HandleConstructor {
if (onConstructor != null) mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor));
return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"),
- null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source, typeNode.getContext());
+ null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(),
+ maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source.get(), typeNode.getContext());
}
public static boolean isLocalType(JavacNode type) {
diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java
index 172e70b3..cd8e3402 100644
--- a/src/core/lombok/javac/handlers/HandleNonNull.java
+++ b/src/core/lombok/javac/handlers/HandleNonNull.java
@@ -84,8 +84,6 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> {
return;
}
-// if (JavacHandlerUtil.isGenerated(declaration)) return;
-
if (declaration.body == null) {
annotationNode.addWarning("@NonNull is meaningless on a parameter of an abstract method.");
return;
@@ -95,7 +93,7 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> {
// 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.
- JCStatement nullCheck = recursiveSetGeneratedBy(generateNullCheck(annotationNode.getTreeMaker(), annotationNode.up()), ast, annotationNode.getContext());
+ JCStatement nullCheck = recursiveSetGeneratedBy(generateNullCheck(annotationNode.getTreeMaker(), annotationNode.up(), annotationNode), ast, annotationNode.getContext());
if (nullCheck == null) {
// @NonNull applied to a primitive. Kinda pointless. Let's generate a warning.
diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java
index fbc9ef46..3c4329b2 100644
--- a/src/core/lombok/javac/handlers/HandleSetter.java
+++ b/src/core/lombok/javac/handlers/HandleSetter.java
@@ -41,7 +41,6 @@ import lombok.javac.handlers.JavacHandlerUtil.FieldAccess;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
-import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAssign;
import com.sun.tools.javac.tree.JCTree.JCBlock;
@@ -52,7 +51,6 @@ import com.sun.tools.javac.tree.JCTree.JCReturn;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
-import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
@@ -90,7 +88,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
//Skip final fields.
if ((fieldDecl.mods.flags & Flags.FINAL) != 0) continue;
- generateSetterForField(field, errorNode.get(), level);
+ generateSetterForField(field, errorNode, level);
}
}
@@ -109,13 +107,13 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
* @param fieldNode The node representing the field you want a setter for.
* @param pos The node responsible for generating the setter (the {@code @Data} or {@code @Setter} annotation).
*/
- public void generateSetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
+ public void generateSetterForField(JavacNode fieldNode, JavacNode sourceNode, AccessLevel level) {
if (hasAnnotation(Setter.class, fieldNode)) {
//The annotation will make it happen, so we can skip it.
return;
}
- createSetterForField(level, fieldNode, fieldNode, false, List.<JCAnnotation>nil(), List.<JCAnnotation>nil());
+ createSetterForField(level, fieldNode, sourceNode, false, List.<JCAnnotation>nil(), List.<JCAnnotation>nil());
}
@Override public void handle(AnnotationValues<Setter> annotation, JCAnnotation ast, JavacNode annotationNode) {
@@ -150,7 +148,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
}
- public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
+ public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode sourceNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
if (fieldNode.getKind() != Kind.FIELD) {
fieldNode.addError("@Setter is only supported on a class or a field.");
return;
@@ -160,12 +158,12 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
String methodName = toSetterName(fieldNode);
if (methodName == null) {
- source.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
+ fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
return;
}
if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
- source.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
+ fieldNode.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
return;
}
@@ -177,7 +175,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
if (whineIfExists) {
String altNameExpl = "";
if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
- source.addWarning(
+ fieldNode.addWarning(
String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
}
return;
@@ -189,17 +187,17 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
- JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), source.get(), onMethod, onParam);
+ JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
injectMethod(fieldNode.up(), createdSetter);
}
- public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
+ public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
String setterName = toSetterName(field);
boolean returnThis = shouldReturnThis(field);
return createSetter(access, field, treeMaker, setterName, returnThis, source, onMethod, onParam);
}
- public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, String setterName, boolean shouldReturnThis, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
+ public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, String setterName, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
if (setterName == null) return null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
@@ -220,7 +218,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
if (nonNulls.isEmpty()) {
statements.append(treeMaker.Exec(assign));
} else {
- JCStatement nullCheck = generateNullCheck(treeMaker, field);
+ JCStatement nullCheck = generateNullCheck(treeMaker, field, source);
if (nullCheck != null) statements.append(nullCheck);
statements.append(treeMaker.Exec(assign));
}
@@ -253,7 +251,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType,
- methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext());
+ methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
copyJavadoc(field, decl, CopyJavadoc.SETTER);
return decl;
}
diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java
index 7b55b671..8a7b49b2 100644
--- a/src/core/lombok/javac/handlers/HandleWither.java
+++ b/src/core/lombok/javac/handlers/HandleWither.java
@@ -41,7 +41,6 @@ import lombok.javac.handlers.JavacHandlerUtil.FieldAccess;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
-import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
@@ -161,22 +160,22 @@ public class HandleWither extends JavacAnnotationHandler<Wither> {
String methodName = toWitherName(fieldNode);
if (methodName == null) {
- source.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
+ fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
return;
}
if ((fieldDecl.mods.flags & Flags.STATIC) != 0) {
- source.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
+ fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
return;
}
if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) {
- source.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
+ fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
return;
}
if (fieldDecl.name.toString().startsWith("$")) {
- source.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
+ fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
return;
}
@@ -188,7 +187,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> {
if (whineIfExists) {
String altNameExpl = "";
if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
- source.addWarning(
+ fieldNode.addWarning(
String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
}
return;
@@ -200,11 +199,11 @@ public class HandleWither extends JavacAnnotationHandler<Wither> {
long access = toJavacModifier(level);
- JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source.get(), onMethod, onParam);
+ JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source, onMethod, onParam);
injectMethod(fieldNode.up(), createdWither);
}
- public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JCTree source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
+ public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
String witherName = toWitherName(field);
if (witherName == null) return null;
@@ -249,7 +248,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> {
if (nonNulls.isEmpty()) {
statements.append(returnStatement);
} else {
- JCStatement nullCheck = generateNullCheck(maker, field);
+ JCStatement nullCheck = generateNullCheck(maker, field, source);
if (nullCheck != null) statements.append(nullCheck);
statements.append(returnStatement);
}
@@ -268,7 +267,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> {
annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
}
JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType,
- methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source, field.getContext());
+ methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
copyJavadoc(field, decl, CopyJavadoc.WITHER);
return decl;
}
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index aa395e08..524af5de 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -898,6 +898,17 @@ public class JavacHandlerUtil {
return flags;
}
+ public static JCExpression genTypeRef(JavacNode node, String complexName) {
+ String[] parts = complexName.split("\\.");
+ if (parts.length > 2 && parts[0].equals("java") && parts[1].equals("lang")) {
+ String[] subParts = new String[parts.length - 2];
+ System.arraycopy(parts, 2, subParts, 0, subParts.length);
+ return genJavaLangTypeRef(node, subParts);
+ }
+
+ return chainDots(node, parts);
+ }
+
public static JCExpression genJavaLangTypeRef(JavacNode node, String... simpleNames) {
if (LombokOptionsFactory.getDelombokOptions(node.getContext()).getFormatPreferences().javaLangAsFqn()) {
return chainDots(node, "java", "lang", simpleNames);
@@ -1029,15 +1040,27 @@ public class JavacHandlerUtil {
}
/**
- * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the
+ * Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the
* variable name as message.
+ *
+ * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
- public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable) {
+ public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) {
+ String exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
+ if (exceptionType == null) {
+ exceptionType = HandlerUtil.DEFAULT_EXCEPTION_FOR_NON_NULL;
+ } else {
+ if (!HandlerUtil.isLegalBasicClassReference(exceptionType)) {
+ source.addWarning("Configuration key contains invalid java type reference '" + exceptionType + "'; use something like 'java.lang.NullPointerException' as value for this key.");
+ exceptionType = HandlerUtil.DEFAULT_EXCEPTION_FOR_NON_NULL;
+ }
+ }
+
JCVariableDecl varDecl = (JCVariableDecl) variable.get();
if (isPrimitive(varDecl.vartype)) return null;
Name fieldName = varDecl.name;
- JCExpression npe = genJavaLangTypeRef(variable, "NullPointerException");
- JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(maker.Literal(fieldName.toString())), null);
+ JCExpression exType = genTypeRef(variable, exceptionType);
+ JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(fieldName.toString())), null);
JCStatement throwStatement = maker.Throw(exception);
JCBlock throwBlock = maker.Block(0, List.of(throwStatement));
return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), throwBlock, null);