Title: [1267] trunk/qdox/src/site/apt: Fix for QDOX-199: Add array information when using FQN

Diff

Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/Type.java (1266 => 1267)

--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/Type.java	2011-06-18 18:04:34 UTC (rev 1266)
+++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/Type.java	2011-06-19 19:52:52 UTC (rev 1267)
@@ -53,10 +53,11 @@
     /**
      * Should only be used by primitives, since they don't have a classloader.
      * 
-     * @param fullName
+     * @param fullName the name of the primitive
      */
-    public Type(String fullName) {
-        this(fullName, 0);
+    public Type( String fullName ) 
+    {
+        this( fullName, 0 );
     }
     
 	public static Type createUnresolved(String name, int dimensions, JavaClassParent context) {
@@ -68,9 +69,10 @@
     }
 
     /**
-     * Returns the FQN of an Object or the handler of a Type
-     * If the name of the can't be resolved based on the imports and the classes on the classpath the name will be returned
-     * InnerClasses will use the $ sign
+     * Returns the FQN of an Object or the handler of a Type.
+     * If the name of the can't be resolved based on the imports and the classes on the classpath the name will be returned.
+     * InnerClasses will use the $ sign.
+     * If the type is an array, the brackets will be included. The get only the name, use {@link #getComponentType()}.
      * 
      * Some examples how names will be translated 
      * <pre>
@@ -79,16 +81,32 @@
      * ?  > ?
      * T  > T
      * anypackage.Outer.Inner > anypackage.Outer$Inner
+     * String[][] > java.lang.String[][]
      * </pre>
      * 
-     * @return
+     * @return the fully qualified name, never <code>null</code>
+     * @see #getComponentType()
      */
     public String getFullyQualifiedName() {
-        
-        return isResolved() ? fullName : name;
+        StringBuffer result = new StringBuffer( isResolved() ? fullName : name );
+        for (int i = 0; i < dimensions; i++) 
+        {
+            result.append("[]");
+        }
+        return result.toString();
     }
 
     /**
+     * Equivalent of {@link Class#getComponentType()}
+     * If this type is an array, return its component type
+     * 
+     * @return the type of array if it's one, otherwise <code>null</code>
+     */
+    public JavaClass getComponentType() {
+      return isArray() ? getJavaClass() : null;
+    }
+    
+    /**
      * The FQN representation of an Object for code usage
      * This implementation ignores generics
      *
@@ -135,7 +153,10 @@
     		}
     		result.append(">");
     	}
-    	for (int i = 0; i < dimensions; i++) result.append("[]");
+    	for (int i = 0; i < dimensions; i++) 
+    	{
+    	    result.append("[]");
+    	}
         return result.toString();
     }
     
@@ -193,7 +214,7 @@
     /**
      * Returns true if this Type is an array
      * 
-     * @return
+     * @return true if this type is an array, otherwise <code>null</code>
      */
     public boolean isArray() {
         return dimensions > 0;
@@ -202,7 +223,7 @@
     /**
      * Returns the depth of this array, 0 if it's not an array
      * 
-     * @return The depth of this array
+     * @return The depth of this array, at least <code>0</code>
      */
     public int getDimensions() {
         return dimensions;
@@ -223,22 +244,15 @@
     }
     
     /**
-     * Returns getValue() extended with the array information 
+     * Equivalent of {@link Class#toString()}. 
+     * Converts the object to a string.
      * 
-     * @return
+     * @return a string representation of this type.
+     * @see Class#toString()
      */
     public String toString()
     {
-        if ( dimensions == 0 )
-        {
-            return getValue();
-        }
-        StringBuffer buff = new StringBuffer( getValue() );
-        for ( int i = 0; i < dimensions; i++ )
-        {
-            buff.append( "[]" );
-        }
-        return buff.toString();
+        return getFullyQualifiedName();
     }
 
     /**
@@ -251,15 +265,10 @@
      * Outer.Inner > Outer.Inner 
      * Outer.Inner<Object>[][] > Outer.Inner<java.lang.Object>[][] 
      * </pre>
-     * @return 
+     * @return a generic string representation of this type.
      */
     public String toGenericString() {
-        StringBuffer buff = new StringBuffer(getGenericFullyQualifiedName());
-        for (int i = 0; i < dimensions; i++) 
-        {
-            buff.append("[]");
-        }
-        return buff.toString();
+        return getGenericFullyQualifiedName();
     }
 
     @Override
@@ -269,7 +278,7 @@
         {
             return true;
         }
-        if ( obj == null || !( obj instanceof Type ) )
+        if ( !( obj instanceof Type ) )
         {
             return false;
         }
@@ -279,18 +288,27 @@
 
     @Override
     public int hashCode() {
-        return getValue().hashCode();
+        return getFullyQualifiedName().hashCode();
     }
 
-    public JavaClass getJavaClass() {
-    	JavaClass result;
-    	
-        JavaClassParent javaClassParent = getJavaClassParent();
-    	result = javaClassParent.getNestedClassByName(getFullyQualifiedName());
-        if(result == null) 
+    public JavaClass getJavaClass()
+    {
+        JavaClass result;
+        String qualifiedName = isResolved() ? fullName : name;
+        if ( isPrimitive( qualifiedName ) )
         {
-            result = javaClassParent.getJavaClassLibrary().getJavaClass( getFullyQualifiedName(), true );
+            result = new DefaultJavaClass( qualifiedName );
         }
+        else
+        {
+            JavaClassParent javaClassParent = getJavaClassParent();
+            result = javaClassParent.getNestedClassByName( qualifiedName );
+            if ( result == null )
+            {
+                result = javaClassParent.getJavaClassLibrary().getJavaClass( qualifiedName, true );
+            }
+        }
+
         return result;
     }
 
@@ -315,8 +333,12 @@
      * @since 1.6
      */
     public boolean isPrimitive() {
-       String value = getValue();
-       return "void".equals(value)           
+       return isPrimitive( getValue() );
+    }
+    
+    private static boolean isPrimitive( String value )
+    {
+        return "void".equals(value)           
         || "boolean".equals(value)
         || "byte".equals(value)
         || "char".equals(value)
@@ -325,6 +347,7 @@
         || "long".equals(value)
         || "float".equals(value)
         || "double".equals(value);
+        
     }
 
     /**
@@ -386,9 +409,13 @@
         return result;
     }
 
+    /**
+     * 
+     * @return a generic string representation of this type with fully qualified names.
+     */
     public String getGenericFullyQualifiedName()
     {
-        StringBuffer result = new StringBuffer( getFullyQualifiedName() );
+        StringBuffer result = new StringBuffer( isResolved() ? fullName : name );
         if ( actualArgumentTypes != null && actualArgumentTypes.size() > 0 )
         {
             result.append( "<" );

Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/WildcardType.java (1266 => 1267)

--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/WildcardType.java	2011-06-18 18:04:34 UTC (rev 1266)
+++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/WildcardType.java	2011-06-19 19:52:52 UTC (rev 1267)
@@ -19,7 +19,6 @@
  * under the License.
  */
 
-import com.thoughtworks.qdox.parser.structs.WildcardTypeDef;
 
 /**
  * This class supports both the 'super' and 'extends' wildcards.

Modified: trunk/qdox/src/site/apt/migrationplan.apt.vm (1266 => 1267)

--- trunk/qdox/src/site/apt/migrationplan.apt.vm	2011-06-18 18:04:34 UTC (rev 1266)
+++ trunk/qdox/src/site/apt/migrationplan.apt.vm	2011-06-19 19:52:52 UTC (rev 1267)
@@ -11,4 +11,7 @@
   * Refactor every javamodel array to generic <<List>> objects, for example: <<JavaClass[]>> will become <<List\<JavaClass\>>>
   
   * Redefined getValue(). This would normally return the FQN, but now it'll return the value as used in the code(when available).
+  
+  * Redefined getFullyQualifiedName(). This used to return the classname, but according to the specs it should also return the array dimensions.
+    To get the classname, use <<getComponentType()>>.
   
\ No newline at end of file

Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/model/JavaMethodTest.java (1266 => 1267)

--- trunk/qdox/src/test/java/com/thoughtworks/qdox/model/JavaMethodTest.java	2011-06-18 18:04:34 UTC (rev 1266)
+++ trunk/qdox/src/test/java/com/thoughtworks/qdox/model/JavaMethodTest.java	2011-06-19 19:52:52 UTC (rev 1267)
@@ -120,14 +120,14 @@
     public void testGetCodeBlockTwoException() throws Exception {
         setName(mth, "blah");
         setReturns(mth, newType("void"));
-        setExceptions(mth, Arrays.asList( new Type[]{newType("RuntimeException"), newType("java.lang.SheepException", 1)}));
+        setExceptions(mth, Arrays.asList( new Type[]{newType("RuntimeException"), newType("java.lang.SheepException")}));
         assertEquals("void blah() throws RuntimeException, java.lang.SheepException;\n", mth.getCodeBlock());
     }
 
     public void testGetCodeBlockThreeException() throws Exception {
         setName(mth, "blah");
         setReturns(mth, newType("void"));
-        setExceptions(mth, Arrays.asList( new Type[]{newType("RuntimeException"), newType("java.lang.SheepException", 1), newType("CowException", 1)}));
+        setExceptions(mth, Arrays.asList( new Type[]{newType("RuntimeException"), newType("java.lang.SheepException"), newType("CowException")}));
         assertEquals("void blah() throws RuntimeException, java.lang.SheepException, CowException;\n", mth.getCodeBlock());
     }
 

Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/model/TypeTest.java (1266 => 1267)

--- trunk/qdox/src/test/java/com/thoughtworks/qdox/model/TypeTest.java	2011-06-18 18:04:34 UTC (rev 1266)
+++ trunk/qdox/src/test/java/com/thoughtworks/qdox/model/TypeTest.java	2011-06-19 19:52:52 UTC (rev 1267)
@@ -43,7 +43,19 @@
         assertEquals("int[]", newType("int", 1).toString());
         assertEquals("long[][][]", newType("long", 3).toString());
     }
+    
+    public void testFullyQualifiedName() throws Exception {
+        assertEquals("int", newType("int").getFullyQualifiedName());
+        assertEquals("int[]", newType("int", 1).getFullyQualifiedName());
+        assertEquals("long[][][]", newType("long", 3).getFullyQualifiedName());
+    }
 
+    public void testComponentType() throws Exception {
+        assertNull( newType("int").getComponentType());
+        assertEquals("int", newType("int", 1).getComponentType().getFullyQualifiedName());
+        assertEquals("long", newType("long", 3).getComponentType().getFullyQualifiedName());
+    }
+
     public void testEquals() throws Exception {
         assertEquals(newType("string"),
                 newType("string"));


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to