- Revision
- 585
- Author
- rfscholte
- Date
- 2009-03-19 16:46:58 -0500 (Thu, 19 Mar 2009)
Log Message
Improved TypeVariable handling and toString() implementations
Modified Paths
- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java
- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaParameter.java
- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaSource.java
- trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java
- trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java
- trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java
Diff
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java (584 => 585)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java 2009-03-19 21:46:58 UTC (rev 585) @@ -206,7 +206,7 @@ } public boolean isPublic() { - return super.isPublic() || getParentClass().isInterface(); + return super.isPublic() || (getParentClass() != null ? getParentClass().isInterface() : false); } /** @@ -324,4 +324,58 @@ public TypeVariable[] getTypeParameters() { return typeParameters; } + + public String toString() { + StringBuffer result = new StringBuffer(); + if(isPrivate()) { + result.append("private "); + } + else if(isProtected()) { + result.append("protected "); + } + else if(isPublic()) { + result.append("public "); + } + if(isAbstract()) { + result.append("abstract "); + } + if(isStatic()) { + result.append("static "); + } + if(isFinal()) { + result.append("final "); + } + if(isSynchronized()) { + result.append("synchronized "); + } + if(isNative()) { + result.append("native "); + } + result.append(getReturns().getValue() + " "); + if(getParentClass() != null) { + result.append(getParentClass().getFullyQualifiedName() + "."); + } + result.append(getName()); + result.append("("); + for(int paramIndex=0;paramIndex<getParameters().length;paramIndex++) { + if(paramIndex>1) { + result.append(","); + } + String typeValue = getParameters()[paramIndex].getType().getValue(); + for(int typeIndex=0;typeIndex<typeParameters.length; typeIndex++) { + if(typeParameters[typeIndex].getName().equals(getParameters()[paramIndex].getType().getValue())) { + typeValue = typeParameters[typeIndex].getValue() + " "+ getParameters()[paramIndex].getName(); + + break; + } + } + result.append(typeValue); + } + result.append(")"); + for(int i = 0; i < exceptions.length; i++) { + result.append(i==0 ? " throws " : ","); + result.append(exceptions[i].getValue()); + } + return result.toString(); + } }
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaParameter.java (584 => 585)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaParameter.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaParameter.java 2009-03-19 21:46:58 UTC (rev 585) @@ -55,5 +55,34 @@ public boolean isVarArgs() { return varArgs; } + + public String toString() { + return getResolvedValue() + " "+ name; + } + + /** + * + * @return the resolved value if the method has typeParameters, otherwise type's value + * @since 1.10 + */ + public String getResolvedValue() { + return getResolvedType().getValue(); + } + public String getResolvedGenericValue() { + return getResolvedType().getGenericValue(); + } + + private Type getResolvedType() { + Type result = type; + if(getParentMethod().getTypeParameters() != null) { + for(int typeIndex=0;typeIndex<getParentMethod().getTypeParameters().length; typeIndex++) { + if(getParentMethod().getTypeParameters()[typeIndex].getName().equals(type.getValue())) { + result = getParentMethod().getTypeParameters()[typeIndex]; + } + } + } + return result; + } + }
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaSource.java (584 => 585)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaSource.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaSource.java 2009-03-19 21:46:58 UTC (rev 585) @@ -112,7 +112,7 @@ this.classLibrary = classLibrary; } - public String toString() { + public String getCodeBlock() { IndentBuffer result = new IndentBuffer(); // package statement @@ -145,6 +145,10 @@ return result.toString(); } + + public String toString() { + return getCodeBlock(); + } public String resolveType(String typeName) { if (resolvedTypeCache.containsKey(typeName)) {
Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java (584 => 585)
--- trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java 2009-03-19 21:46:58 UTC (rev 585) @@ -30,7 +30,7 @@ public String getValue() { - return ""; //a typical generic feature, asking value is weird + return bounds[0].getValue(); } public String getGenericValue() { @@ -49,5 +49,8 @@ return result.toString(); } + public String getName() { + return super.getValue(); + } }
Modified: trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java (584 => 585)
--- trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java 2009-03-19 21:46:58 UTC (rev 585) @@ -1,14 +1,18 @@ package com.thoughtworks.qdox; import java.io.StringReader; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; +import junit.framework.TestCase; + import com.thoughtworks.qdox.model.JavaClass; import com.thoughtworks.qdox.model.JavaField; import com.thoughtworks.qdox.model.JavaMethod; +import com.thoughtworks.qdox.model.JavaParameter; import com.thoughtworks.qdox.model.JavaSource; import com.thoughtworks.qdox.model.Type; - -import junit.framework.TestCase; /** * QDOX-54 Support for retrieval of generic type information (JSR 14) * @@ -292,17 +296,37 @@ JavaSource javaSource = builder.addSource(new StringReader(source)); JavaMethod javaMethod = javaSource.getClasses()[0].getMethods()[0]; assertEquals(1, javaMethod.getTypeParameters().length); - assertEquals("", javaMethod.getTypeParameters()[0].getValue()); + assertEquals("java.lang.StringBuffer", javaMethod.getTypeParameters()[0].getValue()); assertEquals("<T extends java.lang.StringBuffer>", javaMethod.getTypeParameters()[0].getGenericValue()); } + + public void testComplexTypeVariable() throws Exception { + String source = "class Collections {\n" + + "public static <T, S extends T> void copy(List<T> dest, List<S> src){}\n" + + "}"; + JavaSource javaSource = builder.addSource(new StringReader(source)); + JavaMethod javaMethod = javaSource.getClasses()[0].getMethods()[0]; + assertEquals("T", javaMethod.getTypeParameters()[0].getName()); + assertEquals("S", javaMethod.getTypeParameters()[1].getName()); + assertEquals("T", javaMethod.getTypeParameters()[1].getValue()); + } + + public void testComplexTypeVariableMultipleBounds() throws Exception { + String source = "class Collections\n" + + "public static <T extends Object & Comparable<? super T>>\n" + + "T max(Collection<? extends T> coll) {\n" + + "return null;}\n"; + + } + //for qdox-150 // second assert is based on java's Method.toString() // http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html#toString() // 3rd and 4th are resolved Types, based on <T extends StringBuffer> in method - // maybe wrong methodcalls here... - public void todo_testGenericMethodDeclaration() throws Exception { + public void testGenericMethodDeclaration() throws Exception { String source = "package com.thoughtworks.qdox;" + + "import java.util.*;\n" + "public class TestQDOX150 {\n" + " public <T extends StringBuffer> List<StringBuffer> myMethod( T request ) throws Exception {\n" + " return null;\n" + @@ -311,12 +335,12 @@ JavaSource javaSource = builder.addSource(new StringReader(source)); JavaClass javaClass = javaSource.getClasses()[0]; JavaMethod javaMethod = javaClass.getMethods()[0]; - Type paramType = javaMethod.getParameters()[0].getType(); + JavaParameter paramType = javaMethod.getParameters()[0]; Type returnType = javaMethod.getReturns(); assertEquals("myMethod(request)", javaMethod.getCallSignature()); assertEquals("public java.util.List com.thoughtworks.qdox.TestQDOX150.myMethod(java.lang.StringBuffer request) throws java.lang.Exception", javaMethod.toString()); - assertEquals("java.lang.StringBuffer", paramType.getValue()); - assertEquals("<T extends java.lang.StringBuffer>", paramType.getGenericValue()); + assertEquals("java.lang.StringBuffer", paramType.getResolvedValue()); + assertEquals("<T extends java.lang.StringBuffer>", paramType.getResolvedGenericValue()); assertEquals("java.util.List", returnType.getValue()); assertEquals("java.util.List<java.lang.StringBuffer>", returnType.getGenericValue());
Modified: trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java (584 => 585)
--- trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java 2009-03-19 21:00:37 UTC (rev 584) +++ trunk/qdox/src/test/com/thoughtworks/qdox/model/JavaMethodTest.java 2009-03-19 21:46:58 UTC (rev 585) @@ -344,8 +344,20 @@ assertEquals(null, mth.getParameterByName("z")); } + public void testToString() throws Exception { + JavaClass cls = new JavaClass("java.lang.Object"); + JavaMethod mthd = new JavaMethod(new Type("boolean"),"equals"); + cls.addMethod(mthd); + mthd.setModifiers(new String[]{"public"}); + JavaParameter prmtr = new JavaParameter(new Type("java.lang.Object"), null); + mthd.setParameters(new JavaParameter[] {prmtr}); + assertEquals("public boolean java.lang.Object.equals(java.lang.Object)", mthd.toString()); + + } + private void assertNotEquals(Object o1, Object o2) { assertTrue(o1.toString() + " should not equals " + o2.toString(), !o1.equals(o2)); } + }
To unsubscribe from this list please visit:
