Author: nickarls
Date: 2009-03-19 09:24:44 -0400 (Thu, 19 Mar 2009)
New Revision: 2109
Added:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ConstructorModel.java
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
Log:
refactor
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-19
09:49:00 UTC (rev 2108)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/PackageSchemaGenerator.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -18,9 +18,7 @@
package org.jboss.webbeans.xsd;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
@@ -30,7 +28,7 @@
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
-import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
@@ -64,23 +62,24 @@
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
roundEnvironment)
{
- Map<String, PackageElement> packageModels = new HashMap<String,
PackageElement>();
List<ClassModel> workingSet = new ArrayList<ClassModel>();
// Iterates over the classes compiled, creates a model of the classes and
// add them to a working set
for (Element element : roundEnvironment.getRootElements())
{
- ClassModel classModel = inspectClass(element);
- workingSet.add(classModel);
- packageModels.put(classModel.getPackage(),
processingEnv.getElementUtils().getPackageOf(element));
+ if (ElementKind.CLASS.equals(element.getKind()) ||
ElementKind.ANNOTATION_TYPE.equals(element.getKind()))
+ {
+ ClassModel classModel = inspectClass(element);
+ workingSet.add(classModel);
+ }
}
if (!roundEnvironment.processingOver())
{
// Update the package XSDs for the files changed
helper.updateSchemas(workingSet);
// And flush the changes to disk
- helper.writeSchemas(packageModels);
+ helper.writeSchemas();
}
return false;
}
@@ -94,7 +93,7 @@
private ClassModel inspectClass(Element element)
{
TypeElement typeElement = (TypeElement) element;
- ClassModel classModel = ClassModel.of(typeElement);
+ ClassModel classModel = ClassModel.of(typeElement,
processingEnv.getElementUtils().getPackageOf(typeElement));
// If the class has superclass's, scan them recursively
if (typeElement.getSuperclass().getKind() != TypeKind.NONE)
@@ -118,7 +117,7 @@
// Filter out the constructors and populate the model
for (Element constructor :
ElementFilter.constructorsIn(element.getEnclosedElements()))
{
- DataSetter.populateMethodModel(classModel, constructor);
+ DataSetter.populateConstructorModel(classModel, constructor);
}
// Place the new class model in the cache
helper.cacheClassModel(classModel);
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java 2009-03-19
09:49:00 UTC (rev 2108)
+++ extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/Schema.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -49,17 +49,19 @@
private NamespaceHandler namespaceHandler;
// The set of classes to update
private Set<ClassModel> classModels;
-
+ private PackageElement packageElement;
+
/**
* Creates a new package
*
* @param packageName The name of the package
*/
- public Schema(String packageName)
+ public Schema(String packageName, PackageElement packageElement)
{
this.packageName = packageName;
namespaceHandler = new NamespaceHandler(packageName);
classModels = new HashSet<ClassModel>();
+ this.packageElement = packageElement;
}
/**
@@ -113,7 +115,7 @@
* @param FQN The full name of the class
* @return True if present, false otherwise
*/
- private boolean isClassInPackage(PackageElement packageElement, String FQN)
+ private boolean isClassInPackage(String FQN)
{
for (javax.lang.model.element.Element classElement :
packageElement.getEnclosedElements())
{
@@ -130,12 +132,12 @@
* Cleans out XSD for files that are no longer present in the package
* @param packageElement
*/
- private void cleanRemovedClasses(PackageElement packageElement)
+ private void cleanRemovedClasses()
{
for (Object xsdClass : document.selectNodes("/xs:schema/xs:element"))
{
String FQN = packageName + "." + ((Element)
xsdClass).attributeValue("name");
- if (!isClassInPackage(packageElement, FQN))
+ if (!isClassInPackage(FQN))
{
((Element) xsdClass).detach();
}
@@ -192,9 +194,9 @@
*
* @param packageElement The package abstraction
*/
- public void rebuild(PackageElement packageElement)
+ public void rebuild()
{
- cleanRemovedClasses(packageElement);
+ cleanRemovedClasses();
updateNamespaces();
for (ClassModel classModel : classModels)
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-19
09:49:00 UTC (rev 2108)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/DataSetter.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -22,6 +22,7 @@
import javax.lang.model.element.Modifier;
import org.jboss.webbeans.xsd.model.ClassModel;
+import org.jboss.webbeans.xsd.model.ConstructorModel;
import org.jboss.webbeans.xsd.model.MethodModel;
import org.jboss.webbeans.xsd.model.NamedModel;
@@ -62,29 +63,29 @@
}
/**
- * Inspects a method or constructor and populates a class model
+ * Inspects a constructor and populates a class model
*
* @param classModel The class model to populate
* @param element The element to inspect
*/
- public static void populateMethodModel(ClassModel classModel, Element element)
+ public static void populateConstructorModel(ClassModel classModel, Element element)
{
if (!isPublic(element))
{
return;
}
- MethodModel method = MethodModel.of((ExecutableElement) element);
+ ConstructorModel constructor = ConstructorModel.of((ExecutableElement) element);
+ classModel.addConstructor(constructor);
+ }
- // OK, cheating a little with a common model for methods and constructors
- if ("<init>".equals(method.getName()))
+ public static void populateMethodModel(ClassModel classModel, Element element)
+ {
+ if (!isPublic(element))
{
- method.setName(classModel.getSimpleName());
- classModel.addConstructor(method);
+ return;
}
- else
- {
- classModel.addMethod(method);
- }
+ MethodModel method = MethodModel.of((ExecutableElement) element);
+ classModel.addMethod(method);
}
}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-19
09:49:00 UTC (rev 2108)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/helpers/XSDHelper.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -73,9 +73,9 @@
* @throws DocumentException If the schema could not be parsed
* @throws IOException If the schema could not be read
*/
- private Schema createSchema(String packageName) throws DocumentException, IOException
+ private Schema createSchema(String packageName, PackageElement packageElement) throws
DocumentException, IOException
{
- Schema schema = new Schema(packageName);
+ Schema schema = new Schema(packageName, packageElement);
Document document = readSchema(packageName);
if (document == null)
{
@@ -158,7 +158,7 @@
{
try
{
- schema = createSchema(packageName);
+ schema = createSchema(packageName, classModel.getPackageElement());
}
catch (DocumentException e)
{
@@ -179,11 +179,11 @@
*
* @param packageModels
*/
- public void writeSchemas(Map<String, PackageElement> packageModels)
+ public void writeSchemas()
{
for (Schema schema : schemaMap.values())
{
- schema.rebuild(packageModels.get(schema.getPackageName()));
+ schema.rebuild();
System.out.println(schema.getDocument().asXML());
try
{
Modified: extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-19
09:49:00 UTC (rev 2108)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -22,6 +22,8 @@
import java.util.List;
import java.util.Set;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import org.dom4j.DocumentFactory;
@@ -36,21 +38,31 @@
*/
public class ClassModel extends NamedModel
{
+ // The package of the class
+ private PackageElement packageElement;
// The parent (or null if top-level)
private ClassModel parent;
-
// The fields of the class
private List<NamedModel> fields = new ArrayList<NamedModel>();
// The methods of the class
private List<MethodModel> methods = new ArrayList<MethodModel>();
// The constructors of the class
- private List<MethodModel> constructors = new ArrayList<MethodModel>();
+ private List<ConstructorModel> constructors = new
ArrayList<ConstructorModel>();
+ // The kind of class
+ private ElementKind kind;
- protected ClassModel(String name)
+ protected ClassModel(String name, ElementKind kind, PackageElement packageElement)
{
super(name);
+ this.kind = kind;
+ this.packageElement = packageElement;
}
+ public static ClassModel of(TypeElement typeElement, PackageElement packageElement)
+ {
+ return new ClassModel(typeElement.getQualifiedName().toString(),
typeElement.getKind(), packageElement);
+ }
+
/**
* Adds a field to the class model
*
@@ -66,7 +78,7 @@
*
* @param constructor The constructor to add
*/
- public void addConstructor(MethodModel constructor)
+ public void addConstructor(ConstructorModel constructor)
{
constructors.add(constructor);
}
@@ -107,9 +119,9 @@
*
* @return The set of constructors available
*/
- public Set<MethodModel> getMergedConstructors()
+ public Set<ConstructorModel> getMergedConstructors()
{
- return new HashSet<MethodModel>(constructors);
+ return new HashSet<ConstructorModel>(constructors);
}
/**
@@ -219,6 +231,11 @@
return lastDot < 0 ? name : name.substring(lastDot + 1);
}
+ public PackageElement getPackageElement()
+ {
+ return packageElement;
+ }
+
@Override
public String toString()
{
@@ -230,11 +247,6 @@
return buffer.toString();
}
- public static ClassModel of(TypeElement typeElement)
- {
- return new ClassModel(typeElement.getQualifiedName().toString());
- }
-
@Override
public Element toXSD(NamespaceHandler namespaceHandler)
{
@@ -244,18 +256,27 @@
Element anyElement =
DocumentFactory.getInstance().createElement("xs:any");
complexElement.add(anyElement);
classElement.add(complexElement);
- for (MethodModel constructor : getMergedConstructors())
+
+ Element choice =
DocumentFactory.getInstance().createElement("xs:choice");
+ for (ConstructorModel constructor : getMergedConstructors())
{
- anyElement.add(constructor.toXSD(namespaceHandler));
+ if (!constructor.getParameters().isEmpty())
+ {
+ choice.add(constructor.toXSD(namespaceHandler));
+ }
}
+ anyElement.add(choice);
+
+ for (MethodModel method : getMergedMethods())
+ {
+ anyElement.add(method.toXSD(namespaceHandler));
+ }
+
for (NamedModel field : getMergedFields())
{
anyElement.add(field.toXSD(namespaceHandler));
}
- for (MethodModel method : getMergedMethods())
- {
- anyElement.add(method.toXSD(namespaceHandler));
- }
+
return classElement;
}
Added:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ConstructorModel.java
===================================================================
--- extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ConstructorModel.java
(rev 0)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/ConstructorModel.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -0,0 +1,47 @@
+package org.jboss.webbeans.xsd.model;
+
+import javax.lang.model.element.ExecutableElement;
+
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
+import org.jboss.webbeans.xsd.NamespaceHandler;
+
+public class ConstructorModel extends MethodModel
+{
+
+ protected ConstructorModel(ExecutableElement executableElement)
+ {
+ super(executableElement);
+ name = null;
+ }
+
+ public static ConstructorModel of(ExecutableElement executableElement)
+ {
+ return new ConstructorModel(executableElement);
+ }
+
+ @Override
+ public boolean equals(Object other)
+ {
+ ConstructorModel otherModel = (ConstructorModel) other;
+ return parameters.equals(otherModel.getParameters());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return parameters.hashCode();
+ }
+
+ @Override
+ public Element toXSD(NamespaceHandler namespaceHandler)
+ {
+ Element constructor =
DocumentFactory.getInstance().createElement("xs:sequence");
+ for (TypedModel parameter : parameters)
+ {
+ constructor.add(parameter.toXSD(namespaceHandler));
+ }
+ return constructor;
+ }
+
+}
Modified:
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
===================================================================
---
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-19
09:49:00 UTC (rev 2108)
+++
extensions/trunk/xsd/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java 2009-03-19
13:24:44 UTC (rev 2109)
@@ -44,7 +44,7 @@
}
};
- private List<TypedModel> parameters = new ArrayList<TypedModel>();
+ protected List<TypedModel> parameters = new ArrayList<TypedModel>();
protected MethodModel(ExecutableElement executableElement)
{