diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-08-27 23:05:14 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-08-27 23:05:14 +0200 |
commit | fe0da3f53f1e88b704e21463cc5fea3d998e394a (patch) | |
tree | fa97c0d94a562dd7dd862a5d8e6e2f1ac432d060 | |
parent | e6bb460e2b1ba49698e5306ddcb5ce38748e2dfc (diff) | |
download | lombok-fe0da3f53f1e88b704e21463cc5fea3d998e394a.tar.gz lombok-fe0da3f53f1e88b704e21463cc5fea3d998e394a.tar.bz2 lombok-fe0da3f53f1e88b704e21463cc5fea3d998e394a.zip |
Now @Nullable is also copied over.
-rw-r--r-- | src/lombok/eclipse/Eclipse.java | 15 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleData.java | 19 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 8 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleGetter.java | 7 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleSetter.java | 20 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/PKG.java | 9 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 12 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleEqualsAndHashCode.java | 8 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 5 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 12 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 8 |
11 files changed, 67 insertions, 56 deletions
diff --git a/src/lombok/eclipse/Eclipse.java b/src/lombok/eclipse/Eclipse.java index 33164ab5..017affa1 100644 --- a/src/lombok/eclipse/Eclipse.java +++ b/src/lombok/eclipse/Eclipse.java @@ -255,10 +255,19 @@ public class Eclipse { } public static Annotation[] copyAnnotations(Annotation[] annotations) { - if (annotations == null) return null; - Annotation[] outs = new Annotation[annotations.length]; + return copyAnnotations(annotations, null); + } + + public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2) { + if (annotations1 == null && annotations2 == null) return null; + if (annotations1 == null) annotations1 = new Annotation[0]; + if (annotations2 == null) annotations2 = new Annotation[0]; + Annotation[] outs = new Annotation[annotations1.length + annotations2.length]; int idx = 0; - for ( Annotation annotation : annotations ) { + for ( Annotation annotation : annotations1 ) { + outs[idx++] = copyAnnotation(annotation); + } + for ( Annotation annotation : annotations2 ) { outs[idx++] = copyAnnotation(annotation); } return outs; diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index 0296d5e4..38e3135d 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -89,7 +89,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> { //Skip static fields. if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - boolean isNonNull = findNonNullAnnotations(fieldDecl).length != 0; + boolean isNonNull = findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0; if ( (isFinal || isNonNull) && fieldDecl.initialization == null ) nodesForConstructor.add(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); @@ -151,11 +151,11 @@ public class HandleData implements EclipseAnnotationHandler<Data> { assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p)); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - nullChecks.add(generateNullCheck(field)); - argument.annotations = copyAnnotations(nonNulls); - } + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); + if (nonNulls.length != 0) nullChecks.add(generateNullCheck(field)); + Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); + if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(argument); } @@ -200,10 +200,9 @@ public class HandleData implements EclipseAnnotationHandler<Data> { assigns.add(new SingleNameReference(field.name, fieldPos)); Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - argument.annotations = copyAnnotations(nonNulls); - } + Annotation[] copiedAnnotations = copyAnnotations( + findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(new Argument(field.name, fieldPos, copyType(field.type), 0)); } diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index d8e1562e..f837d4a6 100644 --- a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -153,19 +153,19 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA return false; } - boolean isDirectDescendentOfObject = true; + boolean isDirectDescendantOfObject = true; if ( typeDecl.superclass != null ) { String p = typeDecl.superclass.toString(); - isDirectDescendentOfObject = p.equals("Object") || p.equals("java.lang.Object"); + isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object"); } - if ( isDirectDescendentOfObject && callSuper ) { + if ( isDirectDescendantOfObject && callSuper ) { errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless."); return true; } - if ( !isDirectDescendentOfObject && !callSuper ) { + if ( !isDirectDescendantOfObject && !callSuper ) { errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object."); } diff --git a/src/lombok/eclipse/handlers/HandleGetter.java b/src/lombok/eclipse/handlers/HandleGetter.java index 6a4c5ceb..5fb7876a 100644 --- a/src/lombok/eclipse/handlers/HandleGetter.java +++ b/src/lombok/eclipse/handlers/HandleGetter.java @@ -113,9 +113,10 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> { } MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - method.annotations = copyAnnotations(nonNulls); + Annotation[] copiedAnnotations = copyAnnotations( + findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + if (copiedAnnotations.length != 0) { + method.annotations = copiedAnnotations; } injectMethod(fieldNode.up(), method); diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java index d850e549..6214c86d 100644 --- a/src/lombok/eclipse/handlers/HandleSetter.java +++ b/src/lombok/eclipse/handlers/HandleSetter.java @@ -21,15 +21,8 @@ */ package lombok.eclipse.handlers; -import static lombok.eclipse.Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; -import static lombok.eclipse.Eclipse.annotationTypeMatches; -import static lombok.eclipse.Eclipse.copyAnnotations; -import static lombok.eclipse.Eclipse.copyType; -import static lombok.eclipse.handlers.PKG.findNonNullAnnotations; -import static lombok.eclipse.handlers.PKG.generateNullCheck; -import static lombok.eclipse.handlers.PKG.injectMethod; -import static lombok.eclipse.handlers.PKG.methodExists; -import static lombok.eclipse.handlers.PKG.toModifier; +import static lombok.eclipse.Eclipse.*; +import static lombok.eclipse.handlers.PKG.*; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; @@ -146,14 +139,15 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd; - Annotation[] nonNulls = findNonNullAnnotations(field); + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.length == 0) { method.statements = new Statement[] { assignment }; - } - else { - param.annotations = copyAnnotations(nonNulls); + } else { method.statements = new Statement[] { generateNullCheck(field), assignment }; } + Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); + if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations; return method; } } diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java index 2eae1edf..17096b70 100644 --- a/src/lombok/eclipse/handlers/PKG.java +++ b/src/lombok/eclipse/handlers/PKG.java @@ -26,6 +26,7 @@ import static lombok.eclipse.Eclipse.fromQualifiedName; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.core.AST.Kind; @@ -231,14 +232,17 @@ class PKG { type.add(method, Kind.METHOD).recursiveSetHandled(); } - static Annotation[] findNonNullAnnotations(FieldDeclaration field) { + static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE); + static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE); + + static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) { List<Annotation> result = new ArrayList<Annotation>(); for (Annotation annotation : field.annotations) { TypeReference typeRef = annotation.type; if ( typeRef != null && typeRef.getTypeName()!= null ) { char[][] typeName = typeRef.getTypeName(); String suspect = new String(typeName[typeName.length - 1]); - if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) { + if ( namePattern.matcher(suspect).matches() ) { result.add(annotation); } } @@ -246,7 +250,6 @@ class PKG { return result.toArray(new Annotation[0]); } - static Statement generateNullCheck(AbstractVariableDeclaration variable) { AllocationExpression exception = new AllocationExpression(); exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0}); diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index e65462d8..648ff91e 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -79,7 +79,7 @@ public class HandleData implements JavacAnnotationHandler<Data> { if ( (fieldFlags & Flags.STATIC) != 0 ) continue; if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child); boolean isFinal = (fieldFlags & Flags.FINAL) != 0; - boolean isNonNull = !findNonNullAnnotations(child).isEmpty(); + boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty(); if ( (isFinal || isNonNull) && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); @@ -114,8 +114,9 @@ public class HandleData implements JavacAnnotationHandler<Data> { for ( Node fieldNode : fields ) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); - List<JCAnnotation> nonNulls = findNonNullAnnotations(fieldNode); - JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls), field.name, field.vartype, null); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls.appendList(nullables)), field.name, field.vartype, null); params = params.append(param); JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name); @@ -169,7 +170,10 @@ public class HandleData implements JavacAnnotationHandler<Data> { for ( JCExpression arg : typeApply.arguments ) tArgs = tArgs.append(arg); pType = maker.TypeApply(typeApply.clazz, tArgs); } else pType = field.vartype; - JCVariableDecl param = maker.VarDef(maker.Modifiers(0, findNonNullAnnotations(fieldNode)), field.name, pType, null); + + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls.appendList(nullables)), field.name, pType, null); params = params.append(param); args = args.append(maker.Ident(field.name)); } diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java index 1a1158eb..e3e7c1ac 100644 --- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -119,20 +119,20 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return false; } - boolean isDirectDescendentOfObject = true; + boolean isDirectDescendantOfObject = true; JCTree extending = ((JCClassDecl)typeNode.get()).extending; if ( extending != null ) { String p = extending.toString(); - isDirectDescendentOfObject = p.equals("Object") || p.equals("java.lang.Object"); + isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object"); } - if ( isDirectDescendentOfObject && callSuper ) { + if ( isDirectDescendantOfObject && callSuper ) { errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless."); return true; } - if ( !isDirectDescendentOfObject && !callSuper ) { + if ( !isDirectDescendantOfObject && !callSuper ) { errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object."); } diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 370a405b..774a9955 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -128,8 +128,9 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - List<JCAnnotation> annotations = findNonNullAnnotations(field); - return treeMaker.MethodDef(treeMaker.Modifiers(access, annotations), methodName, methodType, + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); + return treeMaker.MethodDef(treeMaker.Modifiers(access, nonNulls.appendList(nullables)), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue); } } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index 9fb995e4..c10e731c 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -21,12 +21,7 @@ */ package lombok.javac.handlers; -import static lombok.javac.handlers.PKG.findNonNullAnnotations; -import static lombok.javac.handlers.PKG.generateNullCheck; -import static lombok.javac.handlers.PKG.injectMethod; -import static lombok.javac.handlers.PKG.methodExists; -import static lombok.javac.handlers.PKG.toJavacModifier; -import static lombok.javac.handlers.PKG.toSetterName; +import static lombok.javac.handlers.PKG.*; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; @@ -125,7 +120,8 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name)); List<JCStatement> statements; - List<JCAnnotation> nonNulls = findNonNullAnnotations(field); + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.isEmpty()) { statements = List.<JCStatement>of(treeMaker.Exec(assign)); } @@ -136,7 +132,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(toSetterName(fieldDecl)); - JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls), fieldDecl.name, fieldDecl.vartype, null); + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls.appendList(nullables)), fieldDecl.name, fieldDecl.vartype, null); JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType); List<JCTypeParameter> methodGenericParams = List.nil(); diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 006e92a3..42cfed13 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -22,6 +22,7 @@ package lombok.javac.handlers; import java.lang.reflect.Modifier; +import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.core.TransformationsUtil; @@ -257,7 +258,10 @@ class PKG { return e; } - static List<JCAnnotation> findNonNullAnnotations(Node fieldNode) { + static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE); + static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE); + + static List<JCAnnotation> findAnnotations(Node fieldNode, Pattern namePattern) { List<JCAnnotation> result = List.nil(); for ( Node child : fieldNode.down() ) { if ( child.getKind() == Kind.ANNOTATION ) { @@ -265,7 +269,7 @@ class PKG { String name = annotation.annotationType.toString(); int idx = name.lastIndexOf("."); String suspect = idx == -1 ? name : name.substring(idx + 1); - if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) { + if (namePattern.matcher(suspect).matches()) { result = result.append(annotation); } } |