Title: [712] trunk/qdox/src/grammar: Fix for QDOX-205: implement JavaClass.typeParameters

Diff

Modified: trunk/qdox/src/grammar/parser.y (711 => 712)

--- trunk/qdox/src/grammar/parser.y	2010-03-09 10:52:28 UTC (rev 711)
+++ trunk/qdox/src/grammar/parser.y	2010-03-09 17:02:58 UTC (rev 712)
@@ -338,19 +338,19 @@
 
 opt_typeparams: | typeparams;
 
-typeparams: LESSTHAN { mth.typeParams = new ArrayList(); } typeparamlist GREATERTHAN;
+typeparams: LESSTHAN { typeParams = new ArrayList(); } typeparamlist GREATERTHAN;
 
 typeparamlist:
     typeparam |
     typeparamlist COMMA typeparam;
 
 typeparam: 
-    IDENTIFIER { mth.typeParams.add(new TypeVariableDef($1)); } |
+    IDENTIFIER { typeParams.add(new TypeVariableDef($1)); } |
     IDENTIFIER EXTENDS { 
       typeVariable = new TypeVariableDef($1);
       typeVariable.bounds = new ArrayList();
     } typeboundlist {
-      mth.typeParams.add(typeVariable);
+      typeParams.add(typeVariable);
       typeVariable = null;
     };
 
@@ -403,6 +403,7 @@
         cls.lineNumber = line;
         cls.modifiers.addAll(modifiers); modifiers.clear(); 
         cls.name = $3;
+        cls.typeParams = typeParams;
         builder.beginClass(cls); 
         cls = new ClassDef(); 
     };
@@ -471,7 +472,8 @@
 
 method:
     modifiers typeparams type IDENTIFIER {
-      builder.beginMethod();
+        builder.beginMethod();
+        mth.typeParams = typeParams;
     } methoddef dimensions opt_exceptions memberend {
         mth.lineNumber = line;
         mth.modifiers.addAll(modifiers); modifiers.clear(); 
@@ -497,7 +499,7 @@
 
 constructor:
     modifiers IDENTIFIER {
-      builder.beginMethod();
+        builder.beginMethod();
     } methoddef opt_exceptions memberend {
         mth.lineNumber = line;
         mth.modifiers.addAll(modifiers); modifiers.clear(); 
@@ -507,7 +509,8 @@
         mth = new MethodDef(); 
     } |
     modifiers typeparams IDENTIFIER {
-      builder.beginMethod();
+        builder.beginMethod();
+        mth.typeParams = typeParams;
     } methoddef opt_exceptions memberend {
         mth.lineNumber = line;
         mth.modifiers.addAll(modifiers); modifiers.clear(); 
@@ -558,6 +561,7 @@
 private StringBuffer textBuffer = new StringBuffer();
 private ClassDef cls = new ClassDef();
 private MethodDef mth = new MethodDef();
+private List typeParams = new ArrayList(); //for both JavaClass and JavaMethod
 private List annotationStack = new ArrayList(); // Use ArrayList intead of Stack because it is unsynchronized 
 private Annotation annotation = null;
 private List annoValueListStack = new ArrayList(); // Use ArrayList intead of Stack because it is unsynchronized

Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java (711 => 712)

--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java	2010-03-09 10:52:28 UTC (rev 711)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaClass.java	2010-03-09 17:02:58 UTC (rev 712)
@@ -35,6 +35,7 @@
     private Type type;
     private Type superClass;
     private Type[] implementz = new Type[0];
+    private TypeVariable[] typeParameters = TypeVariable.EMPTY_ARRAY; 
     private JavaClassContext context;
     
     //sourceless class can use this property
@@ -199,6 +200,16 @@
     public void setImplementz(Type[] implementz) {
         this.implementz = implementz;
     }
+    
+    public TypeVariable[] getTypeParameters()
+    {
+        return typeParameters;
+    }
+    
+    public void setTypeParameters( TypeVariable[] typeParameters )
+    {
+        this.typeParameters = typeParameters;
+    }
 
     public void addField(JavaField javaField) {
         javaField.setParentClass( this );

Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/ModelBuilder.java (711 => 712)

--- trunk/qdox/src/java/com/thoughtworks/qdox/model/ModelBuilder.java	2010-03-09 10:52:28 UTC (rev 711)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/model/ModelBuilder.java	2010-03-09 17:02:58 UTC (rev 712)
@@ -108,6 +108,17 @@
             def.modifiers.toArray(modifiers);
             currentClass.setModifiers(modifiers);
         }
+        
+        // typeParameters
+        if (def.typeParams != null) {
+            TypeVariable[] typeParams = new TypeVariable[def.typeParams.size()];
+            int index = 0;
+            for(Iterator iterator = def.typeParams.iterator(); iterator.hasNext();) {
+                TypeVariableDef typeVariableDef = (TypeVariableDef) iterator.next();
+                typeParams[index++] = createTypeVariable(typeVariableDef);
+            }
+            currentClass.setTypeParameters(typeParams);
+        }
 
         // javadoc
         addJavaDoc(currentClass);

Modified: trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/ClassDef.java (711 => 712)

--- trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/ClassDef.java	2010-03-09 10:52:28 UTC (rev 711)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/ClassDef.java	2010-03-09 17:02:58 UTC (rev 712)
@@ -1,6 +1,8 @@
 package com.thoughtworks.qdox.parser.structs;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 public class ClassDef extends LocatedDef {
@@ -12,6 +14,7 @@
     
     public String name = "";
     public Set modifiers = new HashSet();
+    public List typeParams = new ArrayList(); //<TypeVariableDef>
     public Set extendz = new HashSet();
     public Set implementz = new HashSet();
     public String type = CLASS;
@@ -20,13 +23,14 @@
         ClassDef classDef = (ClassDef) obj;
         return classDef.name.equals(name)
                 && classDef.type == type
+                && classDef.typeParams.equals( typeParams )
                 && classDef.modifiers.equals(modifiers)
                 && classDef.extendz.equals(extendz)
                 && classDef.implementz.equals(implementz);
     }
 
     public int hashCode() {
-        return name.hashCode() + type.hashCode() +
+        return name.hashCode() + type.hashCode() + typeParams.hashCode()+
                 modifiers.hashCode() + extendz.hashCode() +
                 implementz.hashCode();
     }
@@ -38,6 +42,7 @@
         result.append(type);
         result.append(" ");
         result.append(name);
+        //typeParams
         result.append(" extends ");
         result.append(extendz);
         result.append(" implements ");

Modified: trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java (711 => 712)

--- trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java	2010-03-09 10:52:28 UTC (rev 711)
+++ trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java	2010-03-09 17:02:58 UTC (rev 712)
@@ -378,4 +378,21 @@
         builder.addSource(new StringReader(source));
     }
     
+    // For QDox-205
+    public void testClassTypeParameters() throws Exception {
+        String source1 = "class GenericControllerImpl<T, K, D extends GenericDAO<T, K>>\n" + 
+        		"    implements GenericController<T, K>\n {}";
+        String source2 = "class GroupControllerImpl extends\n" + 
+        		"    GenericControllerImpl<Group, Long, GroupDAO>\n {}";
+        String source3 = "interface GenericController<T, K> {}";
+        builder.addSource(new StringReader(source1));
+        builder.addSource(new StringReader(source2));
+        builder.addSource(new StringReader(source3));
+        JavaClass genericControllerImpl = builder.getSources()[0].getClasses()[0];
+        assertEquals( 3, genericControllerImpl.getTypeParameters().length );
+        JavaClass groupControllerImpl = builder.getSources()[1].getClasses()[0];
+        assertEquals( 0, groupControllerImpl.getTypeParameters().length );
+        JavaClass genericController = builder.getSources()[2].getClasses()[0];
+        assertEquals( 2, genericController.getTypeParameters().length );
+    }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to