diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lombok/eclipse/Eclipse.java | 100 | ||||
| -rw-r--r-- | src/lombok/eclipse/EclipseASTVisitor.java | 31 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleCleanup.java | 6 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleData.java | 60 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 365 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleGetter.java | 23 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleSetter.java | 36 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleSneakyThrows.java | 58 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleSynchronized.java | 19 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/HandleToString.java | 72 | ||||
| -rw-r--r-- | src/lombok/eclipse/handlers/PKG.java | 36 |
11 files changed, 570 insertions, 236 deletions
diff --git a/src/lombok/eclipse/Eclipse.java b/src/lombok/eclipse/Eclipse.java index 6122d6bc..c96c9ce6 100644 --- a/src/lombok/eclipse/Eclipse.java +++ b/src/lombok/eclipse/Eclipse.java @@ -21,6 +21,7 @@ */ package lombok.eclipse; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -29,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import lombok.Lombok; import lombok.core.AnnotationValues; import lombok.core.TypeLibrary; import lombok.core.TypeResolver; @@ -145,17 +147,18 @@ public class Eclipse { * from another 'T', even for the same T in a single class file. Unfortunately the TypeParameter type hierarchy * is complicated and there's no clone method on TypeParameter itself. This method can clone them. */ - public static TypeParameter[] copyTypeParams(TypeParameter[] params) { + public static TypeParameter[] copyTypeParams(TypeParameter[] params, ASTNode source) { if ( params == null ) return null; TypeParameter[] out = new TypeParameter[params.length]; int idx = 0; for ( TypeParameter param : params ) { TypeParameter o = new TypeParameter(); + setGeneratedBy(o, source); o.annotations = param.annotations; o.bits = param.bits; o.modifiers = param.modifiers; o.name = param.name; - o.type = copyType(param.type); + o.type = copyType(param.type, source); o.sourceStart = param.sourceStart; o.sourceEnd = param.sourceEnd; o.declarationEnd = param.declarationEnd; @@ -164,7 +167,7 @@ public class Eclipse { if ( param.bounds != null ) { TypeReference[] b = new TypeReference[param.bounds.length]; int idx2 = 0; - for ( TypeReference ref : param.bounds ) b[idx2++] = copyType(ref); + for ( TypeReference ref : param.bounds ) b[idx2++] = copyType(ref, source); o.bounds = b; } out[idx++] = o; @@ -176,12 +179,12 @@ public class Eclipse { * Convenience method that creates a new array and copies each TypeReference in the source array via * {@link #copyType(TypeReference)}. */ - public static TypeReference[] copyTypes(TypeReference[] refs) { + public static TypeReference[] copyTypes(TypeReference[] refs, ASTNode source) { if ( refs == null ) return null; TypeReference[] outs = new TypeReference[refs.length]; int idx = 0; for ( TypeReference ref : refs ) { - outs[idx++] = copyType(ref); + outs[idx++] = copyType(ref, source); } return outs; } @@ -191,7 +194,7 @@ public class Eclipse { * Unfortunately the TypeReference type hierarchy is complicated and there's no clone * method on TypeReference itself. This method can clone them. */ - public static TypeReference copyType(TypeReference ref) { + public static TypeReference copyType(TypeReference ref, ASTNode source) { if ( ref instanceof ParameterizedQualifiedTypeReference ) { ParameterizedQualifiedTypeReference iRef = (ParameterizedQualifiedTypeReference) ref; TypeReference[][] args = null; @@ -204,23 +207,29 @@ public class Eclipse { TypeReference[] outRefArray = new TypeReference[inRefArray.length]; int idx2 = 0; for ( TypeReference inRef : inRefArray ) { - outRefArray[idx2++] = copyType(inRef); + outRefArray[idx2++] = copyType(inRef, source); } args[idx++] = outRefArray; } } } - return new ParameterizedQualifiedTypeReference(iRef.tokens, args, iRef.dimensions(), iRef.sourcePositions); + TypeReference typeRef = new ParameterizedQualifiedTypeReference(iRef.tokens, args, iRef.dimensions(), iRef.sourcePositions); + setGeneratedBy(typeRef, source); + return typeRef; } if ( ref instanceof ArrayQualifiedTypeReference ) { ArrayQualifiedTypeReference iRef = (ArrayQualifiedTypeReference) ref; - return new ArrayQualifiedTypeReference(iRef.tokens, iRef.dimensions(), iRef.sourcePositions); + TypeReference typeRef = new ArrayQualifiedTypeReference(iRef.tokens, iRef.dimensions(), iRef.sourcePositions); + setGeneratedBy(typeRef, source); + return typeRef; } if ( ref instanceof QualifiedTypeReference ) { QualifiedTypeReference iRef = (QualifiedTypeReference) ref; - return new QualifiedTypeReference(iRef.tokens, iRef.sourcePositions); + TypeReference typeRef = new QualifiedTypeReference(iRef.tokens, iRef.sourcePositions); + setGeneratedBy(typeRef, source); + return typeRef; } if ( ref instanceof ParameterizedSingleTypeReference ) { @@ -231,62 +240,72 @@ public class Eclipse { int idx = 0; for ( TypeReference inRef : iRef.typeArguments ) { if ( inRef == null ) args[idx++] = null; - else args[idx++] = copyType(inRef); + else args[idx++] = copyType(inRef, source); } } - return new ParameterizedSingleTypeReference(iRef.token, args, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + + TypeReference typeRef = new ParameterizedSingleTypeReference(iRef.token, args, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + setGeneratedBy(typeRef, source); + return typeRef; } if ( ref instanceof ArrayTypeReference ) { ArrayTypeReference iRef = (ArrayTypeReference) ref; - return new ArrayTypeReference(iRef.token, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + TypeReference typeRef = new ArrayTypeReference(iRef.token, iRef.dimensions(), (long)iRef.sourceStart << 32 | iRef.sourceEnd); + setGeneratedBy(typeRef, source); + return typeRef; } if ( ref instanceof Wildcard ) { Wildcard wildcard = new Wildcard(((Wildcard)ref).kind); wildcard.sourceStart = ref.sourceStart; wildcard.sourceEnd = ref.sourceEnd; + setGeneratedBy(wildcard, source); return wildcard; } if ( ref instanceof SingleTypeReference ) { SingleTypeReference iRef = (SingleTypeReference) ref; - return new SingleTypeReference(iRef.token, (long)iRef.sourceStart << 32 | iRef.sourceEnd); + TypeReference typeRef = new SingleTypeReference(iRef.token, (long)iRef.sourceStart << 32 | iRef.sourceEnd); + setGeneratedBy(typeRef, source); + return typeRef; } return ref; } - public static Annotation[] copyAnnotations(Annotation[] annotations, long p) { - return copyAnnotations(annotations, null, p); + public static Annotation[] copyAnnotations(Annotation[] annotations, ASTNode source) { + return copyAnnotations(annotations, null, source); } - public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2, long p) { + public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2, ASTNode source) { 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, p); + outs[idx++] = copyAnnotation(annotation, source); } for ( Annotation annotation : annotations2 ) { - outs[idx++] = copyAnnotation(annotation, p); + outs[idx++] = copyAnnotation(annotation, source); } return outs; } - public static Annotation copyAnnotation(Annotation annotation, long p) { - int pS = (int)(p >> 32), pE = (int)p; + public static Annotation copyAnnotation(Annotation annotation, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; if (annotation instanceof MarkerAnnotation) { - MarkerAnnotation ann = new MarkerAnnotation(copyType(annotation.type), pS); + MarkerAnnotation ann = new MarkerAnnotation(copyType(annotation.type, source), pS); + setGeneratedBy(ann, source); ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE; return ann; } if (annotation instanceof SingleMemberAnnotation) { - SingleMemberAnnotation ann = new SingleMemberAnnotation(copyType(annotation.type), pS); + SingleMemberAnnotation ann = new SingleMemberAnnotation(copyType(annotation.type, source), pS); + setGeneratedBy(ann, source); 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; @@ -294,7 +313,8 @@ public class Eclipse { } if (annotation instanceof NormalAnnotation) { - NormalAnnotation ann = new NormalAnnotation(copyType(annotation.type), pS); + NormalAnnotation ann = new NormalAnnotation(copyType(annotation.type, source), pS); + setGeneratedBy(ann, source); ann.declarationSourceEnd = ann.statementEnd = ann.sourceEnd = pE; ann.memberValuePairs = ((NormalAnnotation)annotation).memberValuePairs; return ann; @@ -425,4 +445,36 @@ public class Eclipse { return null; } + + private static Field generatedByField; + + static { + try { + generatedByField = ASTNode.class.getDeclaredField("$generatedBy"); + } catch ( Throwable t ) { + throw Lombok.sneakyThrow(t); + } + } + + public static ASTNode getGeneratedBy(ASTNode node) { + try { + return (ASTNode) generatedByField.get(node); + } catch ( Exception t ) { + throw Lombok.sneakyThrow(t); + } + } + + public static boolean isGenerated(ASTNode node) { + return getGeneratedBy(node) != null; + } + + public static ASTNode setGeneratedBy(ASTNode node, ASTNode source) { + try { + generatedByField.set(node, source); + } catch ( Exception t ) { + throw Lombok.sneakyThrow(t); + } + + return node; + } } diff --git a/src/lombok/eclipse/EclipseASTVisitor.java b/src/lombok/eclipse/EclipseASTVisitor.java index 6cc4130f..39c785d1 100644 --- a/src/lombok/eclipse/EclipseASTVisitor.java +++ b/src/lombok/eclipse/EclipseASTVisitor.java @@ -165,7 +165,7 @@ public interface EclipseASTVisitor { out.println("---------------------------------------------------------"); out.println(node.isCompleteParse() ? "COMPLETE" : "incomplete"); - print("<CUD %s>", node.getFileName()); + print("<CUD %s%s>", node.getFileName(), Eclipse.isGenerated(unit) ? " (GENERATED)" : ""); indent++; } @@ -175,7 +175,7 @@ public interface EclipseASTVisitor { } public void visitType(Node node, TypeDeclaration type) { - print("<TYPE %s>", str(type.name)); + print("<TYPE %s%s>", str(type.name), Eclipse.isGenerated(type) ? " (GENERATED)" : ""); indent++; if ( printContent ) { print("%s", type); @@ -184,7 +184,7 @@ public interface EclipseASTVisitor { } public void visitAnnotationOnType(TypeDeclaration type, Node node, Annotation annotation) { - forcePrint("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION%s: %s />", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } public void endVisitType(Node node, TypeDeclaration type) { @@ -196,9 +196,10 @@ public interface EclipseASTVisitor { public void visitInitializer(Node node, Initializer initializer) { Block block = initializer.block; boolean s = (block != null && block.statements != null); - print("<%s INITIALIZER: %s>", + print("<%s INITIALIZER: %s%s>", (initializer.modifiers & Modifier.STATIC) != 0 ? "static" : "instance", - s ? "filled" : "blank"); + s ? "filled" : "blank", + Eclipse.isGenerated(initializer) ? " (GENERATED)" : ""); indent++; if ( printContent ) { if ( initializer.block != null ) print("%s", initializer.block); @@ -213,7 +214,8 @@ public interface EclipseASTVisitor { } public void visitField(Node node, FieldDeclaration field) { - print("<FIELD %s %s = %s>", str(field.type), str(field.name), field.initialization); + print("<FIELD%s %s %s = %s>", Eclipse.isGenerated(field) ? " (GENERATED)" : "", + str(field.type), str(field.name), field.initialization); indent++; if ( printContent ) { if ( field.initialization != null ) print("%s", field.initialization); @@ -222,7 +224,7 @@ public interface EclipseASTVisitor { } public void visitAnnotationOnField(FieldDeclaration field, Node node, Annotation annotation) { - forcePrint("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION%s: %s />", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } public void endVisitField(Node node, FieldDeclaration field) { @@ -233,7 +235,8 @@ public interface EclipseASTVisitor { public void visitMethod(Node node, AbstractMethodDeclaration method) { String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; - print("<%s %s: %s>", type, str(method.selector), method.statements != null ? "filled" : "blank"); + print("<%s %s: %s%s>", type, str(method.selector), method.statements != null ? "filled" : "blank", + Eclipse.isGenerated(method) ? " (GENERATED)" : ""); indent++; if ( printContent ) { if ( method.statements != null ) print("%s", method); @@ -242,7 +245,7 @@ public interface EclipseASTVisitor { } public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node node, Annotation annotation) { - forcePrint("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION%s: %s />", Eclipse.isGenerated(method) ? " (GENERATED)" : "", annotation); } public void endVisitMethod(Node node, AbstractMethodDeclaration method) { @@ -253,12 +256,12 @@ public interface EclipseASTVisitor { } public void visitMethodArgument(Node node, Argument arg, AbstractMethodDeclaration method) { - print("<METHODARG %s %s = %s>", str(arg.type), str(arg.name), arg.initialization); + print("<METHODARG%s %s %s = %s>", Eclipse.isGenerated(arg) ? " (GENERATED)" : "", str(arg.type), str(arg.name), arg.initialization); indent++; } public void visitAnnotationOnMethodArgument(Argument arg, AbstractMethodDeclaration method, Node node, Annotation annotation) { - print("<ANNOTATION: %s />", annotation); + print("<ANNOTATION%s: %s />", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } public void endVisitMethodArgument(Node node, Argument arg, AbstractMethodDeclaration method) { @@ -267,12 +270,12 @@ public interface EclipseASTVisitor { } public void visitLocal(Node node, LocalDeclaration local) { - print("<LOCAL %s %s = %s>", str(local.type), str(local.name), local.initialization); + print("<LOCAL%s %s %s = %s>", Eclipse.isGenerated(local) ? " (GENERATED)" : "", str(local.type), str(local.name), local.initialization); indent++; } public void visitAnnotationOnLocal(LocalDeclaration local, Node node, Annotation annotation) { - print("<ANNOTATION: %s />", annotation); + print("<ANNOTATION%s: %s />", Eclipse.isGenerated(annotation) ? " (GENERATED)" : "", annotation); } public void endVisitLocal(Node node, LocalDeclaration local) { @@ -281,7 +284,7 @@ public interface EclipseASTVisitor { } public void visitStatement(Node node, Statement statement) { - print("<%s>", statement.getClass()); + print("<%s%s>", statement.getClass(), Eclipse.isGenerated(statement) ? " (GENERATED)" : ""); indent++; print("%s", statement); } diff --git a/src/lombok/eclipse/handlers/HandleCleanup.java b/src/lombok/eclipse/handlers/HandleCleanup.java index 9621e81c..07c921ab 100644 --- a/src/lombok/eclipse/handlers/HandleCleanup.java +++ b/src/lombok/eclipse/handlers/HandleCleanup.java @@ -26,6 +26,7 @@ import java.util.Arrays; import lombok.Cleanup; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; +import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseAST.Node; @@ -134,15 +135,18 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { doAssignmentCheck(annotationNode, tryBlock, decl.name); TryStatement tryStatement = new TryStatement(); + Eclipse.setGeneratedBy(tryStatement, ast); tryStatement.tryBlock = new Block(0); tryStatement.tryBlock.statements = tryBlock; newStatements[start] = tryStatement; Statement[] finallyBlock = new Statement[1]; MessageSend unsafeClose = new MessageSend(); + Eclipse.setGeneratedBy(unsafeClose, ast); unsafeClose.sourceStart = ast.sourceStart; unsafeClose.sourceEnd = ast.sourceEnd; SingleNameReference receiver = new SingleNameReference(decl.name, 0); + Eclipse.setGeneratedBy(receiver, ast); unsafeClose.receiver = receiver; long nameSourcePosition = (long)ast.sourceStart << 32 | ast.sourceEnd; if ( ast.memberValuePairs() != null ) for ( MemberValuePair pair : ast.memberValuePairs() ) { @@ -155,6 +159,7 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { unsafeClose.selector = cleanupName.toCharArray(); finallyBlock[0] = unsafeClose; tryStatement.finallyBlock = new Block(0); + Eclipse.setGeneratedBy(tryStatement.finallyBlock, ast); tryStatement.finallyBlock.statements = finallyBlock; tryStatement.catchArguments = null; @@ -170,7 +175,6 @@ public class HandleCleanup implements EclipseAnnotationHandler<Cleanup> { ancestor.rebuild(); - return true; } diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index 385f56d4..2761c20b 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -122,21 +122,23 @@ public class HandleData implements EclipseAnnotationHandler<Data> { return false; } - private ConstructorDeclaration createConstructor(boolean isPublic, Node type, Collection<Node> fields, ASTNode pos) { - long p = (long)pos.sourceStart << 32 | pos.sourceEnd; + private ConstructorDeclaration createConstructor(boolean isPublic, Node type, Collection<Node> fields, ASTNode source) { + long p = (long)source.sourceStart << 32 | source.sourceEnd; ConstructorDeclaration constructor = new ConstructorDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); + Eclipse.setGeneratedBy(constructor, source); constructor.modifiers = PKG.toModifier(isPublic ? AccessLevel.PUBLIC : AccessLevel.PRIVATE); constructor.annotations = null; constructor.selector = ((TypeDeclaration)type.get()).name; constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper); + Eclipse.setGeneratedBy(constructor.constructorCall, source); constructor.thrownExceptions = null; constructor.typeParameters = null; constructor.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; - constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = pos.sourceStart; - constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = pos.sourceEnd; + constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart; + constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd; constructor.arguments = null; List<Argument> args = new ArrayList<Argument>(); @@ -146,19 +148,26 @@ public class HandleData implements EclipseAnnotationHandler<Data> { for ( Node fieldNode : fields ) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); FieldReference thisX = new FieldReference(("this." + new String(field.name)).toCharArray(), p); + Eclipse.setGeneratedBy(thisX, source); thisX.receiver = new ThisReference((int)(p >> 32), (int)p); + Eclipse.setGeneratedBy(thisX.receiver, source); thisX.token = field.name; - assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p)); + SingleNameReference assignmentNameRef = new SingleNameReference(field.name, p); + Eclipse.setGeneratedBy(assignmentNameRef, source); + Assignment assignment = new Assignment(thisX, assignmentNameRef, (int)p); + Eclipse.setGeneratedBy(assignment, source); + assigns.add(assignment); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; - Argument argument = new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL); + Argument argument = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL); + Eclipse.setGeneratedBy(argument, source); Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); if (nonNulls.length != 0) { - Statement nullCheck = generateNullCheck(field); + Statement nullCheck = generateNullCheck(field, source); if (nullCheck != null) nullChecks.add(nullCheck); } - Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables, p); + Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables, source); if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(argument); } @@ -169,11 +178,13 @@ public class HandleData implements EclipseAnnotationHandler<Data> { return constructor; } - private MethodDeclaration createStaticConstructor(String name, Node type, Collection<Node> fields, ASTNode pos) { - long p = (long)pos.sourceStart << 32 | pos.sourceEnd; + private MethodDeclaration createStaticConstructor(String name, Node type, Collection<Node> fields, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; + long p = (long)pS << 32 | pE; MethodDeclaration constructor = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); + Eclipse.setGeneratedBy(constructor, source); constructor.modifiers = PKG.toModifier(AccessLevel.PUBLIC) | Modifier.STATIC; TypeDeclaration typeDecl = (TypeDeclaration) type.get(); @@ -181,38 +192,49 @@ public class HandleData implements EclipseAnnotationHandler<Data> { TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length]; int idx = 0; for ( TypeParameter param : typeDecl.typeParameters ) { - refs[idx++] = new SingleTypeReference(param.name, (long)param.sourceStart << 32 | param.sourceEnd); + TypeReference typeRef = new SingleTypeReference(param.name, (long)param.sourceStart << 32 | param.sourceEnd); + Eclipse.setGeneratedBy(typeRef, source); + refs[idx++] = typeRef; } constructor.returnType = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p); } else constructor.returnType = new SingleTypeReference(((TypeDeclaration)type.get()).name, p); + Eclipse.setGeneratedBy(constructor.returnType, source); constructor.annotations = null; constructor.selector = name.toCharArray(); constructor.thrownExceptions = null; - constructor.typeParameters = copyTypeParams(((TypeDeclaration)type.get()).typeParameters); + constructor.typeParameters = copyTypeParams(((TypeDeclaration)type.get()).typeParameters, source); constructor.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; - constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = pos.sourceStart; - constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = pos.sourceEnd; + constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart; + constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd; List<Argument> args = new ArrayList<Argument>(); List<Expression> assigns = new ArrayList<Expression>(); AllocationExpression statement = new AllocationExpression(); - statement.type = copyType(constructor.returnType); + statement.sourceStart = pS; statement.sourceEnd = pE; + Eclipse.setGeneratedBy(statement, source); + statement.type = copyType(constructor.returnType, source); for ( Node fieldNode : fields ) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; - assigns.add(new SingleNameReference(field.name, fieldPos)); + SingleNameReference nameRef = new SingleNameReference(field.name, fieldPos); + Eclipse.setGeneratedBy(nameRef, source); + assigns.add(nameRef); - Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); + Argument argument = new Argument(field.name, fieldPos, copyType(field.type, source), 0); + Eclipse.setGeneratedBy(argument, source); + Annotation[] copiedAnnotations = copyAnnotations( - findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), p); + findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), + findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), source); if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; - args.add(new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL)); + args.add(new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL)); } statement.arguments = assigns.isEmpty() ? null : assigns.toArray(new Expression[assigns.size()]); constructor.arguments = args.isEmpty() ? null : args.toArray(new Argument[args.size()]); constructor.statements = new Statement[] { new ReturnStatement(statement, (int)(p >> 32), (int)p) }; + Eclipse.setGeneratedBy(constructor.statements[0], source); return constructor; } } diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 46792236..209c095c 100644 --- a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -234,22 +234,24 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA return true; } - private MethodDeclaration createHashCode(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) { - int pS = pos.sourceStart, pE = pos.sourceEnd; + private MethodDeclaration createHashCode(Node type, Collection<Node> fields, boolean callSuper, ASTNode source) { + int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); + Eclipse.setGeneratedBy(method, source); method.modifiers = PKG.toModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0); - method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, p)}; + Eclipse.setGeneratedBy(method.returnType, source); + method.annotations = new Annotation[] {makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source)}; method.selector = "hashCode".toCharArray(); method.thrownExceptions = null; method.typeParameters = null; method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; - method.bodyStart = method.declarationSourceStart = method.sourceStart = pos.sourceStart; - method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = pos.sourceEnd; + method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; + method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; method.arguments = null; List<Statement> statements = new ArrayList<Statement>(); @@ -263,23 +265,34 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */ if ( !isEmpty || callSuper ) { LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE); + Eclipse.setGeneratedBy(primeDecl, source); primeDecl.modifiers |= Modifier.FINAL; primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); + primeDecl.type.sourceStart = pS; primeDecl.type.sourceEnd = pE; + Eclipse.setGeneratedBy(primeDecl.type, source); primeDecl.initialization = new IntLiteral("31".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(primeDecl.initialization, source); statements.add(primeDecl); } } /* int result = 1; */ { LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); + Eclipse.setGeneratedBy(resultDecl, source); resultDecl.initialization = new IntLiteral("1".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(resultDecl.initialization, source); resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); + resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; + Eclipse.setGeneratedBy(resultDecl.type, source); statements.add(resultDecl); } if ( callSuper ) { MessageSend callToSuper = new MessageSend(); + Eclipse.setGeneratedBy(callToSuper, source); + callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; callToSuper.receiver = new SuperReference(pS, pE); + Eclipse.setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "hashCode".toCharArray(); intoResult.add(callToSuper); } @@ -292,59 +305,79 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA if ( Arrays.equals(TypeConstants.FLOAT, token) ) { /* Float.floatToIntBits(fieldName) */ MessageSend floatToIntBits = new MessageSend(); - floatToIntBits.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA_LANG_FLOAT); + floatToIntBits.sourceStart = pS; floatToIntBits.sourceEnd = pE; + Eclipse.setGeneratedBy(floatToIntBits, source); + floatToIntBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_FLOAT); floatToIntBits.selector = "floatToIntBits".toCharArray(); - floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name, p) }; + floatToIntBits.arguments = new Expression[] { generateFieldReference(f.name, source) }; intoResult.add(floatToIntBits); } else if ( Arrays.equals(TypeConstants.DOUBLE, token) ) { /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */ MessageSend doubleToLongBits = new MessageSend(); - doubleToLongBits.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA_LANG_DOUBLE); + doubleToLongBits.sourceStart = pS; doubleToLongBits.sourceEnd = pE; + Eclipse.setGeneratedBy(doubleToLongBits, source); + doubleToLongBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_DOUBLE); doubleToLongBits.selector = "doubleToLongBits".toCharArray(); - doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name, p) }; + doubleToLongBits.arguments = new Expression[] { generateFieldReference(f.name, source) }; final char[] tempName = ("temp" + ++tempCounter).toCharArray(); LocalDeclaration tempVar = new LocalDeclaration(tempName, pS, pE); + Eclipse.setGeneratedBy(tempVar, source); tempVar.initialization = doubleToLongBits; tempVar.type = TypeReference.baseTypeReference(TypeIds.T_long, 0); + tempVar.type.sourceStart = pS; tempVar.type.sourceEnd = pE; + Eclipse.setGeneratedBy(tempVar.type, source); tempVar.modifiers = Modifier.FINAL; statements.add(tempVar); - intoResult.add(longToIntForHashCode( - new SingleNameReference(tempName, p), new SingleNameReference(tempName, p), p)); + SingleNameReference copy1 = new SingleNameReference(tempName, p); + Eclipse.setGeneratedBy(copy1, source); + SingleNameReference copy2 = new SingleNameReference(tempName, p); + Eclipse.setGeneratedBy(copy2, source); + intoResult.add(longToIntForHashCode(copy1, copy2, source)); } else if ( Arrays.equals(TypeConstants.BOOLEAN, token) ) { /* booleanField ? 1231 : 1237 */ - intoResult.add(new ConditionalExpression( - generateFieldReference(f.name, p), - new IntLiteral("1231".toCharArray(), pS, pE), - new IntLiteral("1237".toCharArray(), pS, pE))); + IntLiteral int1231 = new IntLiteral("1231".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int1231, source); + IntLiteral int1237 = new IntLiteral("1237".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int1237, source); + ConditionalExpression int1231or1237 = new ConditionalExpression( + generateFieldReference(f.name, source), int1231, int1237); + Eclipse.setGeneratedBy(int1231or1237, source); + intoResult.add(int1231or1237); } else if ( Arrays.equals(TypeConstants.LONG, token) ) { - intoResult.add(longToIntForHashCode(generateFieldReference(f.name, p), generateFieldReference(f.name, p), p)); + intoResult.add(longToIntForHashCode(generateFieldReference(f.name, source), generateFieldReference(f.name, source), source)); } else if ( BUILT_IN_TYPES.contains(new String(token)) ) { - intoResult.add(generateFieldReference(f.name, p)); + intoResult.add(generateFieldReference(f.name, source)); } else /* objects */ { /* this.fieldName == null ? 0 : this.fieldName.hashCode() */ MessageSend hashCodeCall = new MessageSend(); - hashCodeCall.receiver = generateFieldReference(f.name, p); + hashCodeCall.sourceStart = pS; hashCodeCall.sourceEnd = pE; + Eclipse.setGeneratedBy(hashCodeCall, source); + hashCodeCall.receiver = generateFieldReference(f.name, source); hashCodeCall.selector = "hashCode".toCharArray(); + NullLiteral nullLiteral = new NullLiteral(pS, pE); + Eclipse.setGeneratedBy(nullLiteral, source); EqualExpression objIsNull = new EqualExpression( - generateFieldReference(f.name, p), - new NullLiteral(pS, pE), - OperatorIds.EQUAL_EQUAL); - ConditionalExpression nullOrHashCode = new ConditionalExpression( - objIsNull, - new IntLiteral("0".toCharArray(), pS, pE), - hashCodeCall); + generateFieldReference(f.name, source), nullLiteral, OperatorIds.EQUAL_EQUAL); + Eclipse.setGeneratedBy(objIsNull, source); + IntLiteral int0 = new IntLiteral("0".toCharArray(), pS, pE); + Eclipse.setGeneratedBy(int0, source); + ConditionalExpression nullOrHashCode = new ConditionalExpression(objIsNull, int0, hashCodeCall); + nullOrHashCode.sourceStart = pS; nullOrHashCode.sourceEnd = pE; + Eclipse.setGeneratedBy(nullOrHashCode, source); intoResult.add(nullOrHashCode); } } else if ( f.type.dimensions() > 0 && token != null ) { /* Arrays.deepHashCode(array) //just hashCode for simple arrays */ MessageSend arraysHashCodeCall = new MessageSend(); - arraysHashCodeCall.receiver = generateQualifiedNameRef(p, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); + arraysHashCodeCall.sourceStart = pS; arraysHashCodeCall.sourceEnd = pE; + Eclipse.setGeneratedBy(arraysHashCodeCall, source); + arraysHashCodeCall.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); if ( f.type.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token)) ) { arraysHashCodeCall.selector = "deepHashCode".toCharArray(); } else { arraysHashCodeCall.selector = "hashCode".toCharArray(); } - arraysHashCodeCall.arguments = new Expression[] { generateFieldReference(f.name, p) }; + arraysHashCodeCall.arguments = new Expression[] { generateFieldReference(f.name, source) }; intoResult.add(arraysHashCodeCall); } } @@ -352,73 +385,117 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA /* fold each intoResult entry into: result = result * PRIME + (item); */ { for ( Expression ex : intoResult ) { - BinaryExpression multiplyByPrime = new BinaryExpression(new SingleNameReference(RESULT, p), - new SingleNameReference(PRIME, 0), OperatorIds.MULTIPLY); + SingleNameReference resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + SingleNameReference primeRef = new SingleNameReference(PRIME, p); + Eclipse.setGeneratedBy(primeRef, source); + BinaryExpression multiplyByPrime = new BinaryExpression(resultRef, primeRef, OperatorIds.MULTIPLY); + multiplyByPrime.sourceStart = pS; multiplyByPrime.sourceEnd = pE; + Eclipse.setGeneratedBy(multiplyByPrime, source); BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS); - statements.add(new Assignment(new SingleNameReference(RESULT, p), addItem, pE)); + addItem.sourceStart = pS; addItem.sourceEnd = pE; + Eclipse.setGeneratedBy(addItem, source); + resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + Assignment assignment = new Assignment(resultRef, addItem, pE); + assignment.sourceStart = pS; assignment.sourceEnd = pE; + Eclipse.setGeneratedBy(assignment, source); + statements.add(assignment); } } /* return result; */ { - statements.add(new ReturnStatement(new SingleNameReference(RESULT, p), pS, pE)); + SingleNameReference resultRef = new SingleNameReference(RESULT, p); + Eclipse.setGeneratedBy(resultRef, source); + ReturnStatement returnStatement = new ReturnStatement(resultRef, pS, pE); + Eclipse.setGeneratedBy(returnStatement, source); + statements.add(returnStatement); } method.statements = statements.toArray(new Statement[statements.size()]); return method; } - private MethodDeclaration createEquals(Node type, Collection<Node> fields, boolean callSuper, ASTNode pos) { - int pS = pos.sourceStart; int pE = pos.sourceEnd; + private MethodDeclaration createEquals(Node type, Collection<Node> fields, boolean callSuper, ASTNode source) { + int pS = source.sourceStart; int pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration me |
