Updated findMethod with paramcount.

This commit is contained in:
Reinier Zwitserloot committed 2012-03-26 20:06:35 +02:00
1 parent 4689d2a9bf
commit 0c927175af
10 files changed
+59 -21

No files matched your search

@@ -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;
}
}
}
}
@@ -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;
@@ -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:
@@ -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:
@@ -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);
@@ -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;
@@ -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:
@@ -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:
@@ -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);
@@ -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;
}
}
}
}