- Revision
- 705
- Author
- rfscholte
- Date
- 2010-03-01 08:36:49 -0600 (Mon, 01 Mar 2010)
Log Message
fix for QDOX-201: methodSignature with varArg support
Modified Paths
Diff
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java (704 => 705)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java 2010-02-27 10:57:13 UTC (rev 704) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java 2010-03-01 14:36:49 UTC (rev 705) @@ -353,6 +353,7 @@ } /** + * * @param name method name * @param parameterTypes parameter types or null if there are no parameters. * @return the matching method or null if no match is found. @@ -361,7 +362,7 @@ JavaMethod[] methods = getMethods(); for (int i = 0; i < methods.length; i++) { - if (methods[i].signatureMatches(name, parameterTypes)) { + if (methods[i].signatureMatches(name, parameterTypes, false)) { return methods[i]; } } @@ -369,16 +370,56 @@ return null; } + /** + * + * @param name + * @param parameterTypes + * @param superclasses + * @return + */ public JavaMethod getMethodBySignature(String name, Type[] parameterTypes, boolean superclasses) { + return getMethodBySignature( name, parameterTypes, superclasses, false ); + } + + /** + * + * @param name + * @param parameterTypes + * @param superclasses + * @param varArg + * @return + */ + public JavaMethod getMethodBySignature(String name, Type[] parameterTypes, + boolean superclasses, boolean varArg) { JavaMethod[] result = getMethodsBySignature(name, parameterTypes, - superclasses); + superclasses, varArg); return (result.length > 0) ? result[0] : null; } + + /** + * + * @param name + * @param parameterTypes + * @param superclasses + * @return + */ + public JavaMethod[] getMethodsBySignature(String name, + Type[] parameterTypes, boolean superclasses) { + return getMethodsBySignature( name, parameterTypes, superclasses, false ); + } + /** + * + * @param name + * @param parameterTypes + * @param superclasses + * @param varArg + * @return + */ public JavaMethod[] getMethodsBySignature(String name, - Type[] parameterTypes, boolean superclasses) { + Type[] parameterTypes, boolean superclasses, boolean varArg) { List result = new ArrayList(); JavaMethod methodInThisClass = getMethodBySignature(name, parameterTypes); @@ -392,7 +433,7 @@ if (superclass != null) { JavaMethod method = superclass.getMethodBySignature(name, - parameterTypes, true); + parameterTypes, true, varArg ); // todo: ideally we should check on package privacy too. oh well. if ((method != null) && !method.isPrivate()) { @@ -404,7 +445,7 @@ for (int i = 0; i < implementz.length; i++) { JavaMethod method = implementz[i].getMethodBySignature(name, - parameterTypes, true); + parameterTypes, true, varArg ); if (method != null) { result.add(method);
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java (704 => 705)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java 2010-02-27 10:57:13 UTC (rev 704) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java 2010-03-01 14:36:49 UTC (rev 705) @@ -14,6 +14,7 @@ private Type[] exceptions = Type.EMPTY_ARRAY; private boolean constructor; private String sourceCode; + private boolean varArg; public JavaMethod() { } @@ -146,6 +147,7 @@ javaParameter.setParentMethod( this ); parameters.add( javaParameter ); parametersArray = null; + this.varArg = javaParameter.isVarArgs(); } public void setExceptions(Type[] exceptions) { @@ -175,15 +177,27 @@ if (!otherParams[i].equals(myParams[i])) return false; } - return true; + return this.varArg == m.varArg; } /** + * This method is NOT varArg aware. The overloaded method is. + * + * @param name + * @param parameterTypes + * @return + * @deprecated use overloaded method + */ + public boolean signatureMatches(String name, Type[] parameterTypes) { + return signatureMatches( name, parameterTypes, false ); + } + + /** * @param name method name * @param parameterTypes parameter types or null if there are no parameters. * @return true if the signature and parameters match. */ - public boolean signatureMatches(String name, Type[] parameterTypes) { + public boolean signatureMatches(String name, Type[] parameterTypes, boolean varArg) { if (!name.equals(this.name)) return false; parameterTypes = (parameterTypes == null ? new Type[0] : parameterTypes); if (parameterTypes.length != this.getParameters().length) return false; @@ -192,7 +206,7 @@ return false; } } - return true; + return (this.varArg == varArg); } public int hashCode() {
Modified: trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java (704 => 705)
--- trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java 2010-02-27 10:57:13 UTC (rev 704) +++ trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java 2010-03-01 14:36:49 UTC (rev 705) @@ -51,6 +51,12 @@ mth.addParameter(new JavaParameter(new Type("MyThing"), "t")); } +// public void testSignatureWithVarArgs() throws Exception { +// mth.setName( "method" ); +// mth.addParameter( new JavaParameter(new Type("java.lang.String"), "param", true) ); +// assertEquals( mth, clazz.getMethodBySignature( "method", new Type[] { new Type("java.lang.String", true)} ) ); +// } + public void testGetCodeBlockSimple() throws Exception { mth.setName("doSomething"); mth.setReturns(new Type("java.lang.String")); @@ -300,7 +306,36 @@ assertFalse(mth.signatureMatches("thing", wrongTypes1)); assertFalse(mth.signatureMatches("thing", wrongTypes2)); } + + public void testVarArgSignatureMatches() throws Exception { + mth.setName("thing"); + mth.addParameter(new JavaParameter(new Type("int"), "x")); + mth.addParameter(new JavaParameter(new Type("long", 2), "y", true)); + mth.setReturns(new Type("void")); + Type[] correctTypes = new Type[]{ + new Type("int"), + new Type("long", 2) + }; + + Type[] wrongTypes1 = new Type[]{ + new Type("int", 2), + new Type("long") + }; + + Type[] wrongTypes2 = new Type[]{ + new Type("int"), + new Type("long", 2), + new Type("double") + }; + + assertTrue(mth.signatureMatches("thing", correctTypes, true)); + assertFalse(mth.signatureMatches("thing", correctTypes, false)); + assertFalse(mth.signatureMatches("xxx", correctTypes, true)); + assertFalse(mth.signatureMatches("thing", wrongTypes1, true)); + assertFalse(mth.signatureMatches("thing", wrongTypes2, true)); + } + public void testParentClass() throws Exception { assertSame(clazz, mth.getParentClass()); }
To unsubscribe from this list please visit:
