diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-21 10:51:09 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-21 10:51:09 +0200 |
commit | 91bb3455da2913c81745d2a7f7e5b42839964f58 (patch) | |
tree | d01d7683b1ad91ada20df34d69ef648aaceb0815 /src/core/lombok/eclipse | |
parent | c06660bd186c7ae8215a822c4eceab097407eeda (diff) | |
download | lombok-91bb3455da2913c81745d2a7f7e5b42839964f58.tar.gz lombok-91bb3455da2913c81745d2a7f7e5b42839964f58.tar.bz2 lombok-91bb3455da2913c81745d2a7f7e5b42839964f58.zip |
Added using .getX() instead of using .x in equals, hashCode, and toString. Also updated changelog as well as the docs.
Also updated usage examples for @EqualsAndHashCode, @ToString, and @Data, which also contained some other minor issues (such as missing this. qualifiers).
Still to do is to detect that getters don't exist _yet_ but will later due to @Getter or @Data.
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 82 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java | 2 |
2 files changed, 69 insertions, 15 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 337ae3a8..29e44781 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -46,6 +46,7 @@ import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; +import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.NameReference; import org.eclipse.jdt.internal.compiler.ast.NullLiteral; @@ -98,34 +99,87 @@ public class EclipseHandlerUtil { } } + private static AbstractMethodDeclaration findGetter(EclipseNode field) { + TypeReference fieldType = ((FieldDeclaration)field.get()).type; + boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; + EclipseNode typeNode = field.up(); + for (String potentialGetterName : TransformationsUtil.toAllGetterNames(field.getName(), isBoolean)) { + switch (methodExists(potentialGetterName, typeNode, false)) { + case EXISTS_BY_LOMBOK: + case EXISTS_BY_USER: + for (EclipseNode potentialGetter : typeNode.down()) { + if (potentialGetter.getKind() != Kind.METHOD) continue; + AbstractMethodDeclaration method = (AbstractMethodDeclaration) potentialGetter.get(); + /** static getX() methods don't count. */ + if ((method.modifiers & ClassFileConstants.AccStatic) != 0) continue; + /** Nor do getters with a non-empty parameter list. */ + if (method.arguments != null && method.arguments.length > 0) continue; + return method; + } + } + } + + return null; + } + static TypeReference getFieldType(EclipseNode field, boolean useFieldsDirectly) { - return ((FieldDeclaration)field.get()).type; + AbstractMethodDeclaration getter = useFieldsDirectly ? null : findGetter(field); + if (!(getter instanceof MethodDeclaration)) { + return ((FieldDeclaration)field.get()).type; + } + + return ((MethodDeclaration)getter).returnType; } static Expression createFieldAccessor(EclipseNode field, boolean useFieldsDirectly, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; - FieldReference thisX = new FieldReference(field.getName().toCharArray(), p); - Eclipse.setGeneratedBy(thisX, source); - thisX.receiver = new ThisReference(pS, pE); - Eclipse.setGeneratedBy(thisX.receiver, source); - return thisX; + + AbstractMethodDeclaration getter = useFieldsDirectly ? null : findGetter(field); + + if (getter == null) { + FieldReference thisX = new FieldReference(field.getName().toCharArray(), p); + Eclipse.setGeneratedBy(thisX, source); + thisX.receiver = new ThisReference(pS, pE); + Eclipse.setGeneratedBy(thisX.receiver, source); + return thisX; + } + + MessageSend call = new MessageSend(); + Eclipse.setGeneratedBy(call, source); + call.sourceStart = pS; call.sourceEnd = pE; + call.receiver = new ThisReference(pS, pE); + Eclipse.setGeneratedBy(call.receiver, source); + call.selector = getter.selector; + return call; } static Expression createFieldAccessor(EclipseNode field, boolean useFieldsDirectly, ASTNode source, char[] receiver) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; - NameReference ref; + AbstractMethodDeclaration getter = useFieldsDirectly ? null : findGetter(field); - char[][] tokens = new char[2][]; - tokens[0] = receiver; - tokens[1] = field.getName().toCharArray(); - long[] poss = {p, p}; + if (getter == null) { + NameReference ref; + + char[][] tokens = new char[2][]; + tokens[0] = receiver; + tokens[1] = field.getName().toCharArray(); + long[] poss = {p, p}; + + ref = new QualifiedNameReference(tokens, poss, pS, pE); + Eclipse.setGeneratedBy(ref, source); + return ref; + } - ref = new QualifiedNameReference(tokens, poss, pS, pE); - Eclipse.setGeneratedBy(ref, source); - return ref; + MessageSend call = new MessageSend(); + Eclipse.setGeneratedBy(call, source); + call.sourceStart = pS; call.sourceEnd = pE; + call.receiver = new SingleNameReference(receiver, p); + Eclipse.setGeneratedBy(call.receiver, source); + call.selector = getter.selector; + return call; } /** diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index b7c6cda6..2142618f 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -353,7 +353,7 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA MessageSend hashCodeCall = new MessageSend(); hashCodeCall.sourceStart = pS; hashCodeCall.sourceEnd = pE; Eclipse.setGeneratedBy(hashCodeCall, source); - hashCodeCall.receiver = fieldAccessor; + hashCodeCall.receiver = createFieldAccessor(field, useFieldsDirectly, source); hashCodeCall.selector = "hashCode".toCharArray(); NullLiteral nullLiteral = new NullLiteral(pS, pE); Eclipse.setGeneratedBy(nullLiteral, source); |