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/javac/handlers | |
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/javac/handlers')
5 files changed, 61 insertions, 17 deletions
diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 0a9d4cc2..00601b56 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -177,9 +177,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas boolean isFinal = (((JCClassDecl)typeNode.get()).mods.flags & Flags.FINAL) != 0; boolean needsCanEqual = !isFinal || !isDirectDescendantOfObject; 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/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index e49b0ccb..dd0a99ad 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -74,11 +74,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { public void generateGetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelGetter) { if (checkForTypeLevelGetter) { if (typeNode != null) for (JavacNode child : typeNode.down()) { - if (child.getKind() == Kind.ANNOTATION) { - if (annotationTypeMatches(Getter.class, child)) { - //The annotation will make it happen, so we can skip it. - return; - } + if (annotationTypeMatches(Getter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; } } } @@ -198,7 +196,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } for (String altName : toAllGetterNames(fieldNode)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: @@ -249,6 +247,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field); List<JCAnnotation> annsOnMethod = nonNulls.appendList(nullables); + if (isFieldDeprecated(field)) { + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + } JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 93ea5a5b..2a91fb37 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -170,7 +170,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { } for (String altName : toAllSetterNames(fieldNode)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: @@ -217,6 +217,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(setterName); List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables); + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. JCExpression methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID"))); @@ -226,7 +227,11 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType, + List<JCAnnotation> annsOnMethod = List.nil(); + if (isFieldDeprecated(field)) { + annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil())); + } + return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); } diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index a5fb099b..016a7972 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -151,7 +151,7 @@ public class HandleToString extends JavacAnnotationHandler<ToString> { } } - switch (methodExists("toString", typeNode)) { + switch (methodExists("toString", typeNode, 0)) { case NOT_EXISTS: JCMethodDecl method = createToString(typeNode, nodesForToString.toList(), includeFieldNames, callSuper, fieldAccess, source.get()); injectMethod(typeNode, method); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index c8538296..92629a3c 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -144,6 +144,24 @@ public class JavacHandlerUtil { } /** + * Returns if a field is marked deprecated, either by {@code @Deprecated} or in javadoc + * @param field the field to check + * @return {@code true} if a field is marked deprecated, either by {@code @Deprecated} or in javadoc, otherwise {@code false} + */ + public static boolean isFieldDeprecated(JavacNode field) { + JCVariableDecl fieldNode = (JCVariableDecl) field.get(); + if ((fieldNode.mods.flags & Flags.DEPRECATED) != 0) { + return true; + } + for (JavacNode child : field.down()) { + if (annotationTypeMatches(Deprecated.class, child)) { + return true; + } + } + return false; + } + + /** * Creates an instance of {@code AnnotationValues} for the provided AST Node. * * @param type An annotation class type, such as {@code lombok.Getter.class}. @@ -372,8 +390,8 @@ public class JavacHandlerUtil { return MemberExistsResult.NOT_EXISTS; } - public static MemberExistsResult methodExists(String methodName, JavacNode node) { - return methodExists(methodName, node, true); + public static MemberExistsResult methodExists(String methodName, JavacNode node, int params) { + return methodExists(methodName, node, true, params); } /** @@ -383,8 +401,9 @@ public class JavacHandlerUtil { * @param methodName the method name to check for. * @param node Any node that represents the Type (JCClassDecl) 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, JavacNode node, boolean caseSensitive) { + public static MemberExistsResult methodExists(String methodName, JavacNode node, boolean caseSensitive, int params) { while (node != null && !(node.get() instanceof JCClassDecl)) { node = node.up(); } @@ -392,9 +411,28 @@ public class JavacHandlerUtil { if (node != null && node.get() instanceof JCClassDecl) { for (JCTree def : ((JCClassDecl)node.get()).defs) { if (def instanceof JCMethodDecl) { - String name = ((JCMethodDecl)def).name.toString(); + JCMethodDecl md = (JCMethodDecl) def; + String name = md.name.toString(); boolean matches = caseSensitive ? name.equals(methodName) : name.equalsIgnoreCase(methodName); - if (matches) return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; + if (matches) { + if (params > -1) { + List<JCVariableDecl> ps = md.params; + int minArgs = 0; + int maxArgs = 0; + if (ps != null && ps.length() > 0) { + minArgs = ps.length(); + if ((ps.last().mods.flags & Flags.VARARGS) != 0) { + maxArgs = Integer.MAX_VALUE; + minArgs--; + } else { + maxArgs = minArgs; + } + } + + if (params < minArgs || params > maxArgs) continue; + } + return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; + } } } } |