diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 20:06:35 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 20:06:35 +0200 |
commit | 0c927175af39f2b8d66d25b735ee0e5249107286 (patch) | |
tree | 0428f1deba1cd6b0eec9e4f7c74bdd28c6eeb68a | |
parent | 4689d2a9bf79f592690a71e7ad7d25cb38b2344b (diff) | |
download | lombok-0c927175af39f2b8d66d25b735ee0e5249107286.tar.gz lombok-0c927175af39f2b8d66d25b735ee0e5249107286.tar.bz2 lombok-0c927175af39f2b8d66d25b735ee0e5249107286.zip |
Updated findMethod with paramcount.
10 files changed, 59 insertions, 21 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 67a2d07c..56f51573 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -977,8 +977,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); } /** @@ -988,8 +988,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(); } @@ -1001,7 +1002,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 3bdba74e..5aeda5a5 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -194,7 +194,7 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); for (String altName : TransformationsUtil.toAllGetterNames(fieldName, isBoolean)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 8599fca7..01926155 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -153,7 +153,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); for (String altName : TransformationsUtil.toAllSetterNames(new String(field.name), isBoolean)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: 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); 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 b3421f86..09da2d4b 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -191,7 +191,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { String methodName = toGetterName(fieldDecl); for (String altName : toAllGetterNames(fieldDecl)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 0298311e..20b8375b 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -165,7 +165,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { String methodName = toSetterName(fieldDecl); for (String altName : toAllSetterNames(fieldDecl)) { - switch (methodExists(altName, fieldNode, false)) { + switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; case EXISTS_BY_USER: 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 32b17322..9234812e 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -313,8 +313,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); } /** @@ -324,8 +324,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(); } @@ -333,9 +334,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; + } } } } |