[jbosstools-commits] JBoss Tools SVN: r43228 - in trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen: model and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Aug 24 16:21:29 EDT 2012


Author: scabanovich
Date: 2012-08-24 16:21:29 -0400 (Fri, 24 Aug 2012)
New Revision: 43228

Added:
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenImportsCollector.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenVariable.java
Modified:
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/CDIProjectGenerator.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenAnnotationReference.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenClass.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenField.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMember.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMethod.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenType.java
   trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/ui/GenProjectHandler.java
Log:
JBIDE-12446
https://issues.jboss.org/browse/JBIDE-12446
Initial implementation of plugin that allows to generate random CDI projects.
Added producers, initializers, method parameters.

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/CDIProjectGenerator.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/CDIProjectGenerator.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/CDIProjectGenerator.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -13,6 +13,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Random;
@@ -27,10 +28,12 @@
 import org.jboss.tools.cdi.gen.model.GenClass;
 import org.jboss.tools.cdi.gen.model.GenField;
 import org.jboss.tools.cdi.gen.model.GenInterface;
+import org.jboss.tools.cdi.gen.model.GenMember;
 import org.jboss.tools.cdi.gen.model.GenMethod;
 import org.jboss.tools.cdi.gen.model.GenProject;
 import org.jboss.tools.cdi.gen.model.GenQualifier;
 import org.jboss.tools.cdi.gen.model.GenType;
+import org.jboss.tools.cdi.gen.model.GenVariable;
 import org.jboss.tools.common.zip.UnzipOperation;
 import org.osgi.framework.Bundle;
 
@@ -48,8 +51,11 @@
 	int packageCount = 50;
 	int interfaceCount = 50;
 	int qualifierCount = 50;
-	int classCount = 300;
-	int injectionsPerClassCount = 20;
+	int classCount = 500;
+	int producersPerClass = 3;
+	int fieldInjectionsPerClassCount = 20;
+	int initMethodsPerClass = 3;
+	int paramsPerInitMethod = 2;
 	
 	public CDIProjectGenerator() {}
 
@@ -57,11 +63,10 @@
 		this.workspaceLocation = workspaceLocation;
 	}
 
-	public void generate() {
-		project.setName("GeneratedProject");
+	public void generate(String projectName) {
+		project.setName(projectName);
 		createPackages();
 		createTypes();
-		//TODO
 		project.flush(workspaceLocation);
 	}
 
@@ -87,6 +92,10 @@
 	}
 
 	void createTypes() {
+		// void type
+		GenClass voidType = new GenClass();
+		voidType.setFullyQualifiedName("void");
+		// String type
 		GenClass string = new GenClass();
 		string.setFullyQualifiedName("java.lang.String");
 
@@ -111,10 +120,13 @@
 		}
 
 		List<String> beanNames = new ArrayList<String>();
+
+		//@Named type
 		GenQualifier named = new GenQualifier();
 		named.setName("Named");
 		named.setPackageName("javax.inject");
-		
+
+		//Classes
 		GenClass[] classes = new GenClass[classCount];
 		for (int i = 0; i < classCount; i++) {
 			String name = "MyBean" + i;
@@ -131,12 +143,14 @@
 			classes[i] = type;
 			project.addType(type);
 		}
+		//Mix classes array
 		for (int i = classes.length - 1; i > 0; i--) {
 			int j = seed.nextInt(i);
 			GenClass c = classes[i];
 			classes[i] = classes[j];
 			classes[j] = c;
 		}
+		//Generate inheritance
 		for (int i = 0; i < classes.length; i++) {
 			int j = seed.nextInt(classes.length);
 			if(i != j && (classes[j].getExtendedType() != null || !classes[j].getImplementedTypes().isEmpty())) {
@@ -146,6 +160,44 @@
 				classes[i].addImplementedType(interfaces[j]);
 			}
 		}
+
+		GenAnnotation disposeType = new GenAnnotation();
+		disposeType.setFullyQualifiedName(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
+		GenAnnotationReference dispose = new GenAnnotationReference();
+		dispose.setAnnotation(disposeType);
+
+		//Producers
+		List<GenMethod> producers = new ArrayList<GenMethod>();
+		GenAnnotation producesType = new GenAnnotation();
+		producesType.setFullyQualifiedName(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME);
+		GenAnnotationReference produces = new GenAnnotationReference();
+		produces.setAnnotation(producesType);
+		for (int i = 0; i < classes.length; i++) {
+			for (int j = 0; j < producersPerClass; j++) {
+				GenMethod producer = new GenMethod();
+				producer.addAnnotation(produces);
+				GenClass c = classes[seed.nextInt(classes.length)];
+				producer.setReturnType(c);
+				producer.setName("produceC" + i + "M" + j);
+				GenQualifier q = qualifiers[seed.nextInt(qualifierCount)];
+				producer.addQualifierAnnotation(q, "qpvalue" + i + "_" + j);
+			
+				classes[i].addMethod(producer);
+				producers.add(producer);
+				
+				//Disposer
+				GenMethod disposer = new GenMethod();
+				disposer.setReturnType(voidType);
+				disposer.setName("disposeC" + i + "M" + j);
+				GenVariable v = new GenVariable();
+				v.setName("p0");
+				v.setType(getRandomSuperType(c, 0.6f));
+				v.addAnnotation(dispose);
+				v.addQualifierAnnotation(q, "qpvalue" + i + "_" + j);
+				disposer.addParameter(v);
+				classes[i].addMethod(disposer);
+			}
+		}
 		
 		//Injections
 		GenAnnotation injectType = new GenAnnotation();
@@ -154,17 +206,15 @@
 		inject.setAnnotation(injectType);
 
 		for (int i = 0; i < classes.length; i++) {
-			for (int j = 0; j < injectionsPerClassCount; j++) {
+			for (int j = 0; j < fieldInjectionsPerClassCount; j++) {
 				GenField f = new GenField();
 				f.setName("f" + j);
 				f.addAnnotation(inject);
-				GenClass c = classes[seed.nextInt(classes.length)];
-				for (GenAnnotationReference q: c.getQualifiers()) {
+				GenMember beanMember = getRandomBeanMember(classes, producers);
+				for (GenAnnotationReference q: beanMember.getQualifiers()) {
 					f.addAnnotation(q);
 				}
-				while(c.getExtendedType() != null) c = c.getExtendedType();
-				GenType type = c;
-				if(!c.getImplementedTypes().isEmpty() && seed.nextFloat() < 0.6f) type = c.getImplementedTypes().get(0);
+				GenType type = getRandomSuperType(beanMember.getType(), 0.6f);
 				f.setType(type);
 				classes[i].addField(f);
 			}
@@ -173,8 +223,29 @@
 			nameProperty.setReturnType(string);
 			nameProperty.setName("getName");
 			classes[i].addMethod(nameProperty);
+			
+			//initializers
+			for (int j = 0; j < initMethodsPerClass; j++) {
+				GenMethod m = new GenMethod();
+				m.addAnnotation(inject);
+				m.setReturnType(voidType);
+				m.setName("initC" + i + "M" + j);
+				for (int k = 0; k < paramsPerInitMethod; k++) {
+					GenVariable v = new GenVariable();
+					v.setName("p" + k);
+					GenMember beanMember = getRandomBeanMember(classes, producers);
+					for (GenAnnotationReference q: beanMember.getQualifiers()) {
+						v.addAnnotation(q);
+					}
+					GenType type = getRandomSuperType(beanMember.getType(), 0.6f);
+					v.setType(type);
+					m.addParameter(v);
+				}
+				
+				classes[i].addMethod(m);
+			}
 		}
-		
+	
 		//EL
 		for (int i = 0; i < classes.length; i++) {
 			GenField f = new GenField();
@@ -190,6 +261,29 @@
 		return project.getPackages().get(seed.nextInt(project.getPackages().size()));
 	}
 
+	private GenMember getRandomBeanMember(GenClass[] classes, List<GenMethod> producers) {
+		if(seed.nextFloat() < 0.6f) {
+			return classes[seed.nextInt(classes.length)];
+		} else {
+			return producers.get(seed.nextInt(producers.size()));
+		}
+	}
+
+	private GenType getRandomSuperType(GenType type, float level) {
+		if(type instanceof GenClass) {
+			GenClass c = (GenClass)type;
+			while(c.getExtendedType() != null) c = c.getExtendedType();
+			type = c;
+		}
+		if(seed.nextFloat() < level) {
+			Collection<GenInterface> is = type.getImplementedTypes();
+			if(!is.isEmpty()) {
+				type = is.iterator().next();
+			}
+		}		
+		return type;
+	}
+
 	private static File TEMPLATE_FOLDER;
 
 	public static File getTemplatesFolder() throws IOException {

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenAnnotationReference.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenAnnotationReference.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenAnnotationReference.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -19,24 +19,22 @@
  *
  */
 public class GenAnnotationReference {
-	String packageName;
-	String typeName;
-	Map<String, Object> values = new HashMap<String, Object>();
+	private GenAnnotation annotation;
+	private Map<String, Object> values = new HashMap<String, Object>();
 	
 	public GenAnnotationReference() {		
 	}
 
-	public void setAnnotation(GenAnnotation type) {
-		packageName = type.getPackageName();
-		typeName = type.getTypeName();
+	public void setAnnotation(GenAnnotation annotation) {
+		this.annotation = annotation;
 	}
 
 	public String getPackageName() {
-		return packageName;
+		return annotation.getPackageName();
 	}
 
 	public String getTypeName() {
-		return typeName;
+		return annotation.getTypeName();
 	}
 
 	public Map<String, Object> getValues() {
@@ -48,6 +46,6 @@
 	}
 
 	public String getFullyQualifiedName() {
-		return getPackageName() + "." + getTypeName();
+		return annotation.getFullyQualifiedName();
 	}
 }

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenClass.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenClass.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenClass.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -11,9 +11,7 @@
 package org.jboss.tools.cdi.gen.model;
 
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 
 /**
  * 
@@ -22,56 +20,23 @@
  */
 public class GenClass extends GenType {
 	GenClass extendedType;
-	List<GenInterface> implementedTypes = new ArrayList<GenInterface>();
-	Set<GenAnnotationReference> qualifierAnnotations = new HashSet<GenAnnotationReference>();
 	List<GenField> fields = new ArrayList<GenField>();
 	
 	public GenClass() {}
 	
 	public void setExtendedType(GenClass extendedType) {
 		this.extendedType = extendedType;
-		addImport(extendedType.getFullyQualifiedName());
+		getDeclaringType().addImport(extendedType.getFullyQualifiedName());
 	}
 
-	public void addImplementedType(GenInterface implementedType) {
-		if(!implementedTypes.contains(implementedType)) {
-			implementedTypes.add(implementedType);
-			addImport(implementedType.getFullyQualifiedName());
-		}
-	}
-
 	public GenClass getExtendedType() {
 		return extendedType;
 	}
 
-	public List<GenInterface> getImplementedTypes() {
-		return implementedTypes;
-	}
-
-	public void addQualifierAnnotation(GenQualifier q, String value) {
-		addImport(q.getFullyQualifiedName());
-		
-		GenAnnotationReference a = new GenAnnotationReference();
-		a.setAnnotation(q);
-		
-
-		if(value != null) {
-			a.getValues().put("value", "\"" + value + "\"");
-		}
-		
-		addAnnotation(a);
-		qualifierAnnotations.add(a);
-	}
-
-	public Set<GenAnnotationReference> getQualifiers() {
-		return qualifierAnnotations;
-	}
-
 	public void addField(GenField f) {
 		fields.add(f);
-		addImport(f.getType().getFullyQualifiedName());
-		for (GenAnnotationReference a: f.getAnnotations()) {
-			addImport(a.getFullyQualifiedName());
+		if(getDeclaringType() != null) {
+			new GenImportsCollector(getDeclaringType()).addImports(f);
 		}
 	}
 
@@ -79,7 +44,7 @@
 		sb.append("package ").append(getPackageName()).append(";").newLine().newLine();
 	
 		//imports
-		for (String i: imports) {
+		for (String i: getImports()) {
 			sb.append("import ").append(i).append(";").newLine();
 		}
 		sb.append("\n");
@@ -92,7 +57,7 @@
 			sb.append(" extends ").append(extendedType.getTypeName());
 		}
 		int imported = 0;
-		for (GenInterface in: implementedTypes) {
+		for (GenInterface in: getImplementedTypes()) {
 			if(imported == 0) {
 				sb.append(" implements ");
 			} else {

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenField.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenField.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenField.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -15,22 +15,13 @@
  * @author Viacheslav Kabanovich
  *
  */
-public class GenField extends GenMember {
-	GenType type;
-	String initValue;
+public class GenField extends GenVariable {
+	private String initValue;
 	
 	public GenField() {
 		setVisibility(GenVisibility.PROTECTED);
 	}
 
-	public void setType(GenType type) {
-		this.type = type;
-	}
-
-	public GenType getType() {
-		return type;
-	}
-
 	public void setInitValue(String s) {
 		initValue = s;
 	}
@@ -38,7 +29,7 @@
 	public void flush(BodyWriter sb) {
 		flushAnnotations(sb);
 		flushVisibility(sb);
-		sb.append(type.getTypeName()).append(" ").append(getName());
+		sb.append(getType().getTypeName()).append(" ").append(getName());
 		if(initValue != null) {
 			sb.append(" = ").append(initValue);
 		}

Added: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenImportsCollector.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenImportsCollector.java	                        (rev 0)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenImportsCollector.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -0,0 +1,30 @@
+package org.jboss.tools.cdi.gen.model;
+
+public class GenImportsCollector {
+	GenType type;
+
+	public GenImportsCollector(GenType type) {
+		this.type = type;
+	}
+
+	public void addImports(GenAnnotationReference a) {
+		type.addImport(a.getFullyQualifiedName());
+	}
+
+	public void addImports(GenVariable v) {
+		type.addImport(v.getType().getFullyQualifiedName());
+		for (GenAnnotationReference a: v.getAnnotations()) {
+			addImports(a);
+		}
+	}
+
+	public void addImports(GenMethod m) {
+		type.addImport(m.getType().getFullyQualifiedName());
+		for (GenAnnotationReference a: m.getAnnotations()) {
+			addImports(a);
+		}
+		for (GenVariable p: m.getParameters()) {
+			addImports(p);
+		}
+	}
+}


Property changes on: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenImportsCollector.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMember.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMember.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMember.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -10,30 +10,68 @@
  ******************************************************************************/
 package org.jboss.tools.cdi.gen.model;
 
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * 
  * @author Viacheslav Kabanovich
  *
  */
-public class GenMember {
-	protected Set<GenAnnotationReference> annotations = new HashSet<GenAnnotationReference>();
+public abstract class GenMember {
+	private GenMember parent;
+	private List<GenAnnotationReference> annotations = new ArrayList<GenAnnotationReference>();
+	private List<GenAnnotationReference> qualifierAnnotations = new ArrayList<GenAnnotationReference>();
 	String name;
 	GenVisibility visibility = GenVisibility.LOCAL;
 
 	public GenMember() {}
 
+	public GenMember getParent() {
+		return parent;
+	}
+
+	public void setParent(GenMember parent) {
+		this.parent = parent;
+	}
+
+	/**
+	 * May return null if this member is not a type and is not added to a type.
+	 * @return
+	 */
+	public GenType getDeclaringType() {
+		return parent != null ? parent.getDeclaringType() : null;
+	}
+
 	public void addAnnotation(GenAnnotationReference annotation) {
 		annotations.add(annotation);
+		if(getDeclaringType() != null) {
+			new GenImportsCollector(getDeclaringType()).addImports(annotation);
+		}
 	}
 	
-	public Set<GenAnnotationReference> getAnnotations() {
+	public Collection<GenAnnotationReference> getAnnotations() {
 		return annotations;
 	}
 
+	public void addQualifierAnnotation(GenQualifier q, String value) {
+		GenAnnotationReference a = new GenAnnotationReference();
+		a.setAnnotation(q);		
+
+		if(value != null) {
+			a.getValues().put("value", "\"" + value + "\"");
+		}
+		
+		addAnnotation(a);
+		qualifierAnnotations.add(a);
+	}
+
+	public Collection<GenAnnotationReference> getQualifiers() {
+		return qualifierAnnotations;
+	}
+
 	public void setName(String name) {
 		this.name = name;
 	}
@@ -56,7 +94,13 @@
 		}
 	}
 
+	public abstract GenType getType();
+
 	public void flushAnnotations(BodyWriter sb) {
+		flushAnnotations(sb, false);
+	}
+
+	public void flushAnnotations(BodyWriter sb, boolean separateBySpace) {
 		for (GenAnnotationReference a: getAnnotations()) {
 			sb.append("@").append(a.getTypeName());
 			Map<String, Object> vs = a.getValues();
@@ -69,7 +113,7 @@
 				}
 				sb.append(")");
 			}
-			sb.newLine();
+			if(separateBySpace) sb.append(" "); else sb.newLine();
 		}
 	}
 

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMethod.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMethod.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenMethod.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -10,14 +10,18 @@
  ******************************************************************************/
 package org.jboss.tools.cdi.gen.model;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * 
  * @author Viacheslav Kabanovich
  *
  */
 public class GenMethod extends GenMember {
-	GenType returnType;
-	boolean isAbstract = false;
+	private GenType returnType;
+	private boolean isAbstract = false;
+	private List<GenVariable> parameters = new ArrayList<GenVariable>();
 	
 	public GenMethod() {
 		setVisibility(GenVisibility.PUBLIC);
@@ -39,12 +43,35 @@
 		return returnType;
 	}
 
+	public GenType getType() {
+		return getReturnType();
+	}
+
+	public List<GenVariable> getParameters() {
+		return parameters;
+	}
+
+	public void addParameter(GenVariable p) {
+		parameters.add(p);
+		if(getDeclaringType() != null) {
+			new GenImportsCollector(getDeclaringType()).addImports(p);
+		}
+	}
+
 	public void flush(BodyWriter sb) {
 		flushAnnotations(sb);
 		flushVisibility(sb);
 		sb.append(returnType.getTypeName()).append(" ").append(getName());
 		sb.append("(");
-		//TODO parameters
+		boolean first = true;
+		for (GenVariable v: getParameters()) {
+			if(first) {
+				first = false;
+			} else {
+				sb.append(", ");
+			}
+			v.flush(sb);
+		}
 		sb.append(")");
 		if(isAbstract()) {
 			sb.append(";").newLine();
@@ -53,7 +80,7 @@
 			if(!"void".equals(returnType.getTypeName())) {
 				sb.append("return null;").newLine();
 			}		
-			sb.decreaseIndent().append("}");
+			sb.decreaseIndent().append("}").newLine();
 		}
 	}
 

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenType.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenType.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenType.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -11,6 +11,7 @@
 package org.jboss.tools.cdi.gen.model;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -23,8 +24,9 @@
  *
  */
 public class GenType extends GenMember implements CDIConstants {
-	Set<String> imports = new HashSet<String>();
-	String packageName;
+	private Set<String> imports = new HashSet<String>();
+	private String packageName;
+	private List<GenInterface> implementedTypes = new ArrayList<GenInterface>();
 	
 	List<GenMethod> methods = new ArrayList<GenMethod>();
 
@@ -48,6 +50,14 @@
 		return getName();
 	}
 
+	public GenType getType() {
+		return this;
+	}
+
+	public GenType getDeclaringType() {
+		return getParent() instanceof GenType ? getParent().getDeclaringType() : this;
+	}
+
 	public void setFullyQualifiedName(String qn) {
 		int dot = qn.lastIndexOf('.');
 		setTypeName(dot < 0 ? qn : qn.substring(dot + 1));
@@ -55,15 +65,33 @@
 	}
 
 	public String getFullyQualifiedName() {
-		return getPackageName() + "." + getName();
+		return getPackageName().length() == 0 ? getName() : getPackageName() + "." + getName();
 	}
 
+	public Collection<String> getImports() {
+		return imports;
+	}
+
 	public void addImport(String type) {
+		if(type.startsWith("java.lang.") && type.lastIndexOf(".") == 9) return;
+		if(type.indexOf('.') < 0) return;
 		imports.add(type);
 	}
 
+	public void addImplementedType(GenInterface implementedType) {
+		if(!implementedTypes.contains(implementedType)) {
+			implementedTypes.add(implementedType);
+			getDeclaringType().addImport(implementedType.getFullyQualifiedName());
+		}
+	}
+
+	public Collection<GenInterface> getImplementedTypes() {
+		return implementedTypes;
+	}
+
 	public void addMethod(GenMethod method) {
 		methods.add(method);
+		new GenImportsCollector(getDeclaringType()).addImports(method);
 	}
 
 	public void flushMethods(BodyWriter sb) {

Added: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenVariable.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenVariable.java	                        (rev 0)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenVariable.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -0,0 +1,19 @@
+package org.jboss.tools.cdi.gen.model;
+
+public class GenVariable extends GenMember {
+	private GenType type;
+
+	public void setType(GenType type) {
+		this.type = type;
+	}
+
+	public GenType getType() {
+		return type;
+	}
+
+	public void flush(BodyWriter sb) {
+		flushAnnotations(sb, true);
+		flushVisibility(sb);
+		sb.append(getType().getTypeName()).append(" ").append(getName());
+	}
+}


Property changes on: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/model/GenVariable.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/ui/GenProjectHandler.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/ui/GenProjectHandler.java	2012-08-24 20:19:30 UTC (rev 43227)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.gen/src/org/jboss/tools/cdi/gen/ui/GenProjectHandler.java	2012-08-24 20:21:29 UTC (rev 43228)
@@ -10,12 +10,17 @@
  ******************************************************************************/
 package org.jboss.tools.cdi.gen.ui;
 
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.ui.IObjectActionDelegate;
 import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.actions.RefreshAction;
 import org.jboss.tools.cdi.gen.CDIProjectGenerator;
+import org.jboss.tools.common.model.plugin.ModelPlugin;
 
 /**
  * 
@@ -23,16 +28,35 @@
  *
  */
 public class GenProjectHandler implements IObjectActionDelegate {
+	ISelection selection;
 
 	@Override
 	public void run(IAction action) {
 		CDIProjectGenerator g = new CDIProjectGenerator();
-		g.setWorkspaceLocation(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile());
-		g.generate();
+		IProject project = null;
+		if(selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
+			IStructuredSelection ss = (IStructuredSelection)selection;
+			Object o = ss.getFirstElement();
+			if(o instanceof IProject) {
+				project = (IProject)o;
+				if(!project.isAccessible()) project = null;
+			}
+		}
+		if(project != null) {
+			g.setWorkspaceLocation(project.getLocation().toFile().getParentFile());
+			g.generate(project.getName());
+			RefreshAction refreshAction = new RefreshAction(ModelPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow());
+			refreshAction.selectionChanged(new StructuredSelection(project));
+			refreshAction.run();
+		} else {
+			g.setWorkspaceLocation(ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile());
+			g.generate("GeneratedProject");
+		}
 	}
 
 	@Override
 	public void selectionChanged(IAction action, ISelection selection) {
+		this.selection = selection;
 	}
 
 	@Override



More information about the jbosstools-commits mailing list