diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 20:09:10 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 20:09:10 +0200 |
commit | a514af4dcdd87cdae64e87b9d8a8d1a489a8e474 (patch) | |
tree | 1e5f487e4aee08fe89cbe5ae919dd86fdc498d58 /src/core/lombok/eclipse | |
parent | aa5d3b8bb2cb2bf068f4b4728a9e765968c673d4 (diff) | |
parent | 0c927175af39f2b8d66d25b735ee0e5249107286 (diff) | |
download | lombok-a514af4dcdd87cdae64e87b9d8a8d1a489a8e474.tar.gz lombok-a514af4dcdd87cdae64e87b9d8a8d1a489a8e474.tar.bz2 lombok-a514af4dcdd87cdae64e87b9d8a8d1a489a8e474.zip |
Merge branch 'acc2' into accessors
Conflicts:
src/core/lombok/eclipse/handlers/HandleGetter.java
src/core/lombok/eclipse/handlers/HandleSetter.java
src/core/lombok/javac/handlers/HandleGetter.java
src/core/lombok/javac/handlers/HandleSetter.java
Diffstat (limited to 'src/core/lombok/eclipse')
5 files changed, 60 insertions, 13 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 3c2ab904..ff743601 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -246,6 +246,27 @@ public class EclipseHandlerUtil { return node; } + public static MarkerAnnotation generateDeprecatedAnnotation(ASTNode source) { + QualifiedTypeReference qtr = new QualifiedTypeReference(new char[][] { + {'j', 'a', 'v', 'a'}, {'l', 'a', 'n', 'g'}, {'D', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd'}}, poss(source, 3)); + setGeneratedBy(qtr, source); + return new MarkerAnnotation(qtr, source.sourceStart); + } + + public static boolean isFieldDeprecated(EclipseNode fieldNode) { + FieldDeclaration field = (FieldDeclaration) fieldNode.get(); + if ((field.modifiers & ClassFileConstants.AccDeprecated) != 0) { + return true; + } + if (field.annotations == null) return false; + for (Annotation annotation : field.annotations) { + if (typeMatches(Deprecated.class, fieldNode, annotation.type)) { + return true; + } + } + return false; + } + /** * Checks if the given TypeReference node is likely to be a reference to the provided class. * @@ -1024,8 +1045,8 @@ public class EclipseHandlerUtil { /** * Wrapper for {@link #methodExists(String, EclipseNode, boolean)} with {@code caseSensitive} = {@code true}. */ - public static MemberExistsResult methodExists(String methodName, EclipseNode node) { - return methodExists(methodName, node, true); + public static MemberExistsResult methodExists(String methodName, EclipseNode node, int params) { + return methodExists(methodName, node, true, params); } /** @@ -1035,8 +1056,9 @@ public class EclipseHandlerUtil { * @param methodName the method name to check for. * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof. * @param caseSensitive If the search should be case sensitive. + * @param params The number of parameters the method should have; varargs count as 0-*. Set to -1 to find any method with the appropriate name regardless of parameter count. */ - public static MemberExistsResult methodExists(String methodName, EclipseNode node, boolean caseSensitive) { + public static MemberExistsResult methodExists(String methodName, EclipseNode node, boolean caseSensitive, int params) { while (node != null && !(node.get() instanceof TypeDeclaration)) { node = node.up(); } @@ -1048,7 +1070,24 @@ public class EclipseHandlerUtil { char[] mName = def.selector; if (mName == null) continue; boolean nameEquals = caseSensitive ? methodName.equals(new String(mName)) : methodName.equalsIgnoreCase(new String(mName)); - if (nameEquals) return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; + if (nameEquals) { + if (params > -1) { + int minArgs = 0; + int maxArgs = 0; + if (def.arguments != null && def.arguments.length > 0) { + minArgs = def.arguments.length; + if ((def.arguments[def.arguments.length - 1].type.bits & ASTNode.IsVarArgs) != 0) { + minArgs--; + maxArgs = Integer.MAX_VALUE; + } else { + maxArgs = minArgs; + } + } + + if (params < minArgs || params > maxArgs) continue; + } + return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; + } } } } diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index ef01835c..82a8f00e 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -204,9 +204,9 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH boolean isFinal = (typeDecl.modifiers & ClassFileConstants.AccFinal) != 0; boolean needsCanEqual = !isDirectDescendantOfObject || !isFinal; java.util.List<MemberExistsResult> existsResults = new ArrayList<MemberExistsResult>(); - existsResults.add(methodExists("equals", typeNode)); - existsResults.add(methodExists("hashCode", typeNode)); - existsResults.add(methodExists("canEqual", typeNode)); + existsResults.add(methodExists("equals", typeNode, 1)); + existsResults.add(methodExists("hashCode", typeNode, 0)); + existsResults.add(methodExists("canEqual", typeNode, 1)); switch (Collections.max(existsResults)) { case EXISTS_BY_LOMBOK: return; diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index c631e583..d53bf89b 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -198,7 +198,7 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); for (String altName : toAllGetterNames(fieldNode, isBoolean)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: @@ -216,14 +216,20 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { } MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), fieldNode, getterName, modifier, source, lazy); - Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode)); + + Annotation[] deprecated = null; + if (isFieldDeprecated(fieldNode)) { + deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; + } + + Annotation[] copiedAnnotations = copyAnnotations(source, findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode), deprecated); if (copiedAnnotations.length != 0) { method.annotations = copiedAnnotations; } injectMethod(fieldNode.up(), method); } - + private static Annotation[] findDelegatesAndMarkAsHandled(EclipseNode fieldNode) { List<Annotation> delegates = new ArrayList<Annotation>(); for (EclipseNode child : fieldNode.down()) { diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 810096ab..3fa872f9 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -157,7 +157,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); for (String altName : toAllSetterNames(fieldNode, isBoolean)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: @@ -188,7 +188,9 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; setGeneratedBy(method.returnType, source); - method.annotations = null; + if (isFieldDeprecated(fieldNode)) { + method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; + } Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL); param.sourceStart = pS; param.sourceEnd = pE; setGeneratedBy(param, source); diff --git a/src/core/lombok/eclipse/handlers/HandleToString.java b/src/core/lombok/eclipse/handlers/HandleToString.java index 07d88f5f..26f0e9be 100644 --- a/src/core/lombok/eclipse/handlers/HandleToString.java +++ b/src/core/lombok/eclipse/handlers/HandleToString.java @@ -159,7 +159,7 @@ public class HandleToString extends EclipseAnnotationHandler<ToString> { } } - switch (methodExists("toString", typeNode)) { + switch (methodExists("toString", typeNode, 0)) { case NOT_EXISTS: MethodDeclaration toString = createToString(typeNode, nodesForToString, includeFieldNames, callSuper, errorNode.get(), fieldAccess); injectMethod(typeNode, toString); |