Skip to content

Commit

Permalink
More work on fully addressing the David Lynch bug (issue #41) - the a…
Browse files Browse the repository at this point in the history
…nnotation @NotNull/@NonNull/@nullable that is copied over by @getter should no longer be causing the David Lynch bug.
  • Loading branch information
Reinier Zwitserloot committed Sep 1, 2009
1 parent 25e33d0 commit da36708
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
31 changes: 20 additions & 11 deletions src/lombok/eclipse/Eclipse.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,38 +254,47 @@ public static TypeReference copyType(TypeReference ref) {
return ref;
}

public static Annotation[] copyAnnotations(Annotation[] annotations) {
return copyAnnotations(annotations, null);
public static Annotation[] copyAnnotations(Annotation[] annotations, long p) {
return copyAnnotations(annotations, null, p);
}

public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2) {
public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2, long p) {
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 : annotations1 ) {
outs[idx++] = copyAnnotation(annotation);
outs[idx++] = copyAnnotation(annotation, p);
}
for ( Annotation annotation : annotations2 ) {
outs[idx++] = copyAnnotation(annotation);
outs[idx++] = copyAnnotation(annotation, p);
}
return outs;
}

public static Annotation copyAnnotation(Annotation annotation) {
public static Annotation copyAnnotation(Annotation annotation, long p) {
int pS = (int)(p >> 32), pE = (int)p;

if (annotation instanceof MarkerAnnotation) {
return new MarkerAnnotation(copyType(annotation.type), 0);
MarkerAnnotation ann = new MarkerAnnotation(copyType(annotation.type), pS);
ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE;
return ann;
}

if (annotation instanceof SingleMemberAnnotation) {
SingleMemberAnnotation result = new SingleMemberAnnotation(copyType(annotation.type), 0);
result.memberValue = ((SingleMemberAnnotation)annotation).memberValue;
SingleMemberAnnotation ann = new SingleMemberAnnotation(copyType(annotation.type), pS);
ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE;
//TODO memberValue(s) need to be copied as well (same for copying a NormalAnnotation as below).
ann.memberValue = ((SingleMemberAnnotation)annotation).memberValue;
return ann;
}

if (annotation instanceof NormalAnnotation) {
NormalAnnotation result = new NormalAnnotation(copyType(annotation.type), 0);
result.memberValuePairs = ((NormalAnnotation)annotation).memberValuePairs;
NormalAnnotation ann = new NormalAnnotation(copyType(annotation.type), pS);
ann.declarationSourceEnd = ann.statementEnd = ann.sourceEnd = pE;
ann.memberValuePairs = ((NormalAnnotation)annotation).memberValuePairs;
return ann;
}

return annotation;
Expand Down
4 changes: 2 additions & 2 deletions src/lombok/eclipse/handlers/HandleData.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private ConstructorDeclaration createConstructor(boolean isPublic, Node type, Co
Statement nullCheck = generateNullCheck(field);
if (nullCheck != null) nullChecks.add(nullCheck);
}
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables);
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables, p);
if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations;
args.add(argument);
}
Expand Down Expand Up @@ -205,7 +205,7 @@ private MethodDeclaration createStaticConstructor(String name, Node type, Collec

Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0);
Annotation[] copiedAnnotations = copyAnnotations(
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN));
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), p);
if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations;
args.add(new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL));
}
Expand Down
6 changes: 4 additions & 2 deletions src/lombok/eclipse/handlers/HandleGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public boolean handle(AnnotationValues<Getter> annotation, Annotation ast, Node
}

private boolean createGetterForField(AccessLevel level, Node fieldNode, Node errorNode, ASTNode pos, boolean whineIfExists) {
int pS = pos.sourceStart(), pE = pos.sourceEnd();
long p = (long)pS << 32 | pE;
if ( fieldNode.getKind() != Kind.FIELD ) {
errorNode.addError("@Getter is only supported on a field.");
return true;
Expand All @@ -103,7 +105,7 @@ private boolean createGetterForField(AccessLevel level, Node fieldNode, Node err
String altNameExpl = "";
if ( !altName.equals(getterName) ) altNameExpl = String.format(" (%s)", altName);
errorNode.addWarning(
String.format("Not generating %s(): A method with that name already exists%s", getterName, altNameExpl));
String.format("Not generating %s(): A method with that name already exists%s", getterName, altNameExpl));
}
return true;
default:
Expand All @@ -114,7 +116,7 @@ private boolean createGetterForField(AccessLevel level, Node fieldNode, Node err

MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos);
Annotation[] copiedAnnotations = copyAnnotations(
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN));
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), p);
if (copiedAnnotations.length != 0) {
method.annotations = copiedAnnotations;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lombok/eclipse/handlers/HandleSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private MethodDeclaration generateSetter(TypeDeclaration parent, FieldDeclaratio
if (nullCheck != null) method.statements = new Statement[] { nullCheck, assignment };
else method.statements = new Statement[] { assignment };
}
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables);
Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables, pos);
if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations;
return method;
}
Expand Down

0 comments on commit da36708

Please sign in to comment.