Title: [572] trunk/qdox/src/grammar: starting support TypeVariable

Diff

Modified: trunk/qdox/src/grammar/parser.y (571 => 572)

--- trunk/qdox/src/grammar/parser.y	2009-02-25 21:51:55 UTC (rev 571)
+++ trunk/qdox/src/grammar/parser.y	2009-02-26 22:44:33 UTC (rev 572)
@@ -336,19 +336,25 @@
 
 opt_typeparams: | typeparams;
 
-typeparams: LESSTHAN typeparamlist GREATERTHAN;
+typeparams: LESSTHAN { mth.typeParams = new ArrayList(); } typeparamlist GREATERTHAN;
 
 typeparamlist:
     typeparam |
     typeparamlist COMMA typeparam;
 
 typeparam: 
-    IDENTIFIER |
-    IDENTIFIER EXTENDS typeboundlist;
+    IDENTIFIER { mth.typeParams.add(new TypeVariableDef($1)); } |
+    IDENTIFIER EXTENDS { 
+      typeVariable = new TypeVariableDef($1);
+      typeVariable.bounds = new ArrayList();
+    } typeboundlist {
+      mth.typeParams.add(typeVariable);
+      typeVariable = null;
+    };
 
 typeboundlist:
-    type | 
-    typeboundlist AMPERSAND type;
+    type { typeVariable.bounds.add($1); } | 
+    typeboundlist AMPERSAND type { typeVariable.bounds.add($3); };
 
 // ----- ENUM
 
@@ -537,6 +543,7 @@
 private FieldDef param = new FieldDef();
 private java.util.Set modifiers = new java.util.HashSet();
 private TypeDef fieldType;
+private TypeVariableDef typeVariable;
 private Stack typeStack = new Stack();
 private int line;
 private int column;

Modified: trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java (571 => 572)

--- trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java	2009-02-25 21:51:55 UTC (rev 571)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/model/JavaMethod.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -6,6 +6,7 @@
 
 public class JavaMethod extends AbstractInheritableJavaEntity implements Member {
 
+	private TypeVariable[] typeParameters = TypeVariable.EMPTY_ARRAY; 
     private Type returns = Type.VOID;
     private JavaParameter[] parameters = JavaParameter.EMPTY_ARRAY;
     private Type[] exceptions = Type.EMPTY_ARRAY;
@@ -315,4 +316,12 @@
     public void setSourceCode(String sourceCode){
     	this.sourceCode = sourceCode;
     }
+
+	public void setTypeParameters(TypeVariable[] typeParameters) {
+		this.typeParameters = typeParameters;
+	}
+	
+	public TypeVariable[] getTypeParameters() {
+		return typeParameters;
+	}
 }

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

--- trunk/qdox/src/java/com/thoughtworks/qdox/model/ModelBuilder.java	2009-02-25 21:51:55 UTC (rev 571)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/model/ModelBuilder.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -1,13 +1,13 @@
 package com.thoughtworks.qdox.model;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 import java.util.Set;
-import java.util.HashMap;
-import java.util.Map;
 
 import com.thoughtworks.qdox.model.annotation.AnnotationFieldRef;
 import com.thoughtworks.qdox.model.annotation.AnnotationVisitor;
@@ -19,6 +19,7 @@
 import com.thoughtworks.qdox.parser.structs.PackageDef;
 import com.thoughtworks.qdox.parser.structs.TagDef;
 import com.thoughtworks.qdox.parser.structs.TypeDef;
+import com.thoughtworks.qdox.parser.structs.TypeVariableDef;
 
 /**
  * @author <a href="" Walnes</a>
@@ -195,6 +196,17 @@
         currentMethod.setReturns(createType(def.returnType, def.dimensions));
         currentMethod.setConstructor(def.constructor);
 
+        // 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);
+        	}
+            currentMethod.setTypeParameters(typeParams);
+        }
+        
         // parameters
         {
             JavaParameter[] params = new JavaParameter[def.params.size()];
@@ -234,7 +246,22 @@
         currentClass.addMethod(currentMethod);
     }
 
-    public void addField(FieldDef def) {
+    public TypeVariable createTypeVariable(TypeVariableDef typeVariableDef) {
+    	if(typeVariableDef == null) {
+    		return null;
+    	}
+    	return TypeVariable.createUnresolved(typeVariableDef, currentClass == null ? currentParent : currentClass);
+
+	}
+
+	public TypeVariable createTypeVariable(String name, List typeParams) {
+    	if( name == null || name.equals( "" ) )
+            return null;
+    	
+        return createTypeVariable(new TypeVariableDef(name, typeParams));
+	}
+
+	public void addField(FieldDef def) {
         JavaField currentField = new JavaField();
         currentField.setParent(currentClass);
         currentField.setLineNumber(def.lineNumber);

Added: trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java (0 => 572)

--- trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java	                        (rev 0)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/model/TypeVariable.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -0,0 +1,53 @@
+package com.thoughtworks.qdox.model;
+
+import com.thoughtworks.qdox.parser.structs.TypeDef;
+import com.thoughtworks.qdox.parser.structs.TypeVariableDef;
+/**
+ * 
+ * 
+ * @author Robert Scholte
+ * @since 1.10
+ */
+public class TypeVariable extends Type {
+
+	public static final TypeVariable[] EMPTY_ARRAY = new TypeVariable[0];
+	
+	private Type[] bounds;
+
+	public TypeVariable(String fullName, TypeVariableDef def, JavaClassParent context) {
+		super(fullName, def.name, 0, context);
+		if(def.bounds != null && !def.bounds.isEmpty()) {
+			bounds = new Type[def.bounds.size()];
+        	for(int index = 0; index < def.bounds.size(); index++) {
+        		bounds[index] = createUnresolved((TypeDef) def.bounds.get(index), context);
+        	}
+        }
+	}
+
+	public static TypeVariable createUnresolved(TypeVariableDef def, JavaClassParent context) {
+		return new TypeVariable(null, def, context);
+	}
+	
+	
+	public String getValue() {
+		return ""; //a typical generic feature, asking value is weird
+	}
+	
+	public String getGenericValue() {
+		StringBuffer result = new StringBuffer("<");
+		result.append(super.getValue());
+		if(bounds != null && bounds.length > 0) {
+			result.append(" extends ");
+			for(int index = 0; index < bounds.length; index++) {
+				if(index > 0) {
+					result.append(",");
+				}
+				result.append(bounds[index].getGenericValue());
+			}
+		}
+		result.append(">");
+		return result.toString();
+	}
+	
+
+}

Modified: trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/MethodDef.java (571 => 572)

--- trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/MethodDef.java	2009-02-25 21:51:55 UTC (rev 571)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/MethodDef.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -7,6 +7,7 @@
 
 public class MethodDef extends LocatedDef {
     public String name = "";
+    public List typeParams; //<TypeVariableDef>
     public TypeDef returnType;
     public Set modifiers = new HashSet();
     public List params = new ArrayList();

Added: trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/TypeVariableDef.java (0 => 572)

--- trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/TypeVariableDef.java	                        (rev 0)
+++ trunk/qdox/src/java/com/thoughtworks/qdox/parser/structs/TypeVariableDef.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -0,0 +1,21 @@
+package com.thoughtworks.qdox.parser.structs;
+
+import java.util.List;
+
+public class TypeVariableDef {
+
+	public String name;
+	public List bounds;
+
+	public TypeVariableDef(String name) {
+		this.name = name;
+	}
+
+	public TypeVariableDef(String name, List bounds) {
+		super();
+		this.name = name;
+		this.bounds = bounds;
+	}
+	
+	
+}
\ No newline at end of file

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

--- trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java	2009-02-25 21:51:55 UTC (rev 571)
+++ trunk/qdox/src/test/com/thoughtworks/qdox/JSR14Test.java	2009-02-26 22:44:33 UTC (rev 572)
@@ -284,6 +284,18 @@
     	assertEquals("java.util.List<java.util.Set<java.lang.String>>", implementsClass.getGenericValue());
     }
     
+    public void testSimpleTypeVariable() throws Exception {
+    	String source = "public class Something {\n" +
+    			" public <T extends StringBuffer> void doStuff(T param) {}\n" +
+    			"}";
+    	
+    	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("<T extends java.lang.StringBuffer>", javaMethod.getTypeParameters()[0].getGenericValue());
+    	
+    }
     //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()


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to