The constructors will now also add non-final fields if they have a NonNull annotation

The constructor will test for null-values

The constructor and static constructor will copy the NonNull annotations from the fields
This commit is contained in:
Roel Spilker committed 2009-08-01 02:38:56 +02:00
1 parent c4d97b008a
commit dd593a3af0
2 files changed
+32 -7

No files matched your search

+19 -3
View File
@@ -89,7 +89,8 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
//Skip static fields.
if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue;
boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0;
if ( isFinal && fieldDecl.initialization == null ) nodesForConstructor.add(child);
boolean isNonNull = findNonNullAnnotations(fieldDecl).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());
}
@@ -139,18 +140,27 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
List<Argument> args = new ArrayList<Argument>();
List<Statement> assigns = new ArrayList<Statement>();
List<Statement> nullChecks = new ArrayList<Statement>();
for ( Node fieldNode : fields ) {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
FieldReference thisX = new FieldReference(("this." + new String(field.name)).toCharArray(), p);
thisX.receiver = new ThisReference((int)(p >> 32), (int)p);
thisX.token = field.name;
assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p));
long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd;
args.add(new Argument(field.name, fieldPos, copyType(field.type), 0));
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);
}
args.add(argument);
}
constructor.statements = assigns.isEmpty() ? null : assigns.toArray(new Statement[assigns.size()]);
nullChecks.addAll(assigns);
constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
constructor.arguments = args.isEmpty() ? null : args.toArray(new Argument[args.size()]);
return constructor;
}
@@ -188,6 +198,12 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd;
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);
}
args.add(new Argument(field.name, fieldPos, copyType(field.type), 0));
}
+13 -4
View File
@@ -79,7 +79,8 @@ 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;
if ( isFinal && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child);
boolean isNonNull = !findNonNullAnnotations(child).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());
}
@@ -106,21 +107,29 @@ public class HandleData implements JavacAnnotationHandler<Data> {
TreeMaker maker = typeNode.getTreeMaker();
JCClassDecl type = (JCClassDecl) typeNode.get();
List<JCStatement> nullChecks = List.nil();
List<JCStatement> assigns = List.nil();
List<JCVariableDecl> params = List.nil();
for ( Node fieldNode : fields ) {
JCVariableDecl field = (JCVariableDecl) fieldNode.get();
JCVariableDecl param = maker.VarDef(maker.Modifiers(0), field.name, field.vartype, null);
List<JCAnnotation> nonNulls = findNonNullAnnotations(fieldNode);
JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls), field.name, field.vartype, null);
params = params.append(param);
JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name);
JCAssign assign = maker.Assign(thisX, maker.Ident(field.name));
assigns = assigns.append(maker.Exec(assign));
if (!nonNulls.isEmpty()) {
nullChecks = nullChecks.append(generateNullCheck(maker, fieldNode));
}
}
JCModifiers mods = maker.Modifiers(isPublic ? Modifier.PUBLIC : Modifier.PRIVATE);
return maker.MethodDef(mods, typeNode.toName("<init>"),
null, type.typarams, params, List.<JCExpression>nil(), maker.Block(0L, assigns), null);
null, type.typarams, params, List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns)), null);
}
private JCMethodDecl createStaticConstructor(String name, Node typeNode, List<Node> fields) {
@@ -160,7 +169,7 @@ 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), field.name, pType, null);
JCVariableDecl param = maker.VarDef(maker.Modifiers(0, findNonNullAnnotations(fieldNode)), field.name, pType, null);
params = params.append(param);
args = args.append(maker.Ident(field.name));
}