Now @Nullable is also copied over.

This commit is contained in:
Reinier Zwitserloot committed 2009-08-27 23:05:14 +02:00
1 parent e6bb460e2b
commit fe0da3f53f
11 files changed
+67 -56

No files matched your search

+12 -3
View File
@@ -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;
+9 -10
View File
@@ -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));
}
@@ -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.");
}
@@ -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);
+7 -13
View File
@@ -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;
}
}
+6 -3
View File
@@ -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});
+8 -4
View File
@@ -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));
}
@@ -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.");
}
+3 -2
View File
@@ -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);
}
}
+4 -8
View File
@@ -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();
+6 -2
View File
@@ -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);
}
}