Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1962 1c488680-804c-0410-94cd-c6b725194a0e
- Loading branch information
Showing
9 changed files
with
370 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
impl/src/main/java/org/jboss/webbeans/xsd/AnnotationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
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; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.annotation.processing.SupportedSourceVersion; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.DeclaredType; | ||
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.util.ElementFilter; | ||
|
||
import org.dom4j.Document; | ||
import org.dom4j.DocumentHelper; | ||
import org.dom4j.Namespace; | ||
import org.dom4j.QName; | ||
import org.jboss.webbeans.xsd.model.ClassModel; | ||
|
||
@SupportedSourceVersion(SourceVersion.RELEASE_6) | ||
@SupportedAnnotationTypes("*") | ||
public class AnnotationProcessor extends AbstractProcessor | ||
{ | ||
private Map<String, ClassModel> classModelCache = new HashMap<String, ClassModel>(); | ||
private Map<String, Document> packageXSDs = new HashMap<String, Document>(); | ||
private List<ClassModel> workingSet = new ArrayList<ClassModel>(); | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) | ||
{ | ||
for (Element element : roundEnv.getRootElements()) | ||
{ | ||
workingSet.add(inspectClass(element)); | ||
} | ||
if (roundEnv.processingOver()) | ||
{ | ||
writeXSD(); | ||
} | ||
return true; | ||
} | ||
|
||
private Document getPackageXSD(String packageName) | ||
{ | ||
Document packageXSD = packageXSDs.get(packageName); | ||
if (packageXSD == null) | ||
{ | ||
packageXSD = initPackageXSD(packageName); | ||
packageXSDs.put(packageName, packageXSD); | ||
} | ||
return packageXSD; | ||
} | ||
|
||
private String getShortName(String packageName) | ||
{ | ||
int lastDot = packageName.lastIndexOf("."); | ||
return lastDot < 0 ? packageName : packageName.substring(lastDot + 1); | ||
} | ||
|
||
private Document initPackageXSD(String packageName) | ||
{ | ||
Document packageXSD = DocumentHelper.createDocument(); | ||
|
||
packageXSD.addElement(new QName("Package", new Namespace(getShortName(packageName), "urn:java:" + packageName))); | ||
return packageXSD; | ||
} | ||
|
||
private void writeXSD() | ||
{ | ||
for (ClassModel classModel : workingSet) | ||
{ | ||
Document packageXSD = getPackageXSD(classModel.getPackage()); | ||
addClass(packageXSD, classModel); | ||
} | ||
for (Document document : packageXSDs.values()) | ||
{ | ||
System.out.println(document.asXML()); | ||
} | ||
} | ||
|
||
private void addClass(Document packageXSD, ClassModel classModel) | ||
{ | ||
packageXSD.getRootElement().addElement(classModel.getSimpleName()); | ||
} | ||
|
||
private ClassModel inspectClass(Element element) | ||
{ | ||
TypeElement typeElement = (TypeElement) element; | ||
ClassModel classModel = new ClassModel(); | ||
|
||
if (typeElement.getSuperclass().getKind() != TypeKind.NONE) | ||
{ | ||
inspectClass(((DeclaredType) typeElement.getSuperclass()).asElement()); | ||
} | ||
|
||
ClassModel parent = classModelCache.get(typeElement.getSuperclass().toString()); | ||
DataSetter.populateClassModel(classModel, element, parent); | ||
for (Element field : ElementFilter.fieldsIn(element.getEnclosedElements())) | ||
{ | ||
DataSetter.populateFieldModel(classModel, field); | ||
} | ||
for (Element method : ElementFilter.methodsIn(element.getEnclosedElements())) | ||
{ | ||
DataSetter.populateMethodModel(classModel, method); | ||
} | ||
for (Element constructor : ElementFilter.constructorsIn(element.getEnclosedElements())) | ||
{ | ||
DataSetter.populateMethodModel(classModel, constructor); | ||
} | ||
classModelCache.put(classModel.getName(), classModel); | ||
return classModel; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
impl/src/main/java/org/jboss/webbeans/xsd/AnnotationTest2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.jboss.webbeans.xsd; | ||
|
||
public class AnnotationTest2 extends AnnotationTest | ||
{ | ||
public String tar; | ||
private String xar; | ||
|
||
private void hidden() { | ||
} | ||
|
||
public AnnotationTest2(String foo) | ||
{ | ||
} | ||
|
||
public AnnotationTest2() { | ||
} | ||
|
||
public void ping() | ||
{ | ||
} | ||
|
||
public void ping(String foo) | ||
{ | ||
} | ||
|
||
public String foo(String bar) | ||
{ | ||
return bar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.jboss.webbeans.xsd; | ||
|
||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.element.VariableElement; | ||
|
||
import org.jboss.webbeans.xsd.model.ClassModel; | ||
import org.jboss.webbeans.xsd.model.MethodModel; | ||
import org.jboss.webbeans.xsd.model.ParameterFieldModel; | ||
|
||
public class DataSetter | ||
{ | ||
|
||
private static boolean isPublic(Element element) | ||
{ | ||
return element.getModifiers().contains(Modifier.PUBLIC); | ||
} | ||
|
||
public static void populateClassModel(ClassModel classModel, Element element, ClassModel parent) | ||
{ | ||
TypeElement typeElement = (TypeElement) element; | ||
classModel.setName(typeElement.getQualifiedName().toString()); | ||
classModel.setParent(parent); | ||
} | ||
|
||
public static void populateFieldModel(ClassModel classModel, Element element) | ||
{ | ||
if (!isPublic(element)) | ||
{ | ||
return; | ||
} | ||
String name = element.getSimpleName().toString(); | ||
String type = element.asType().toString(); | ||
classModel.addField(new ParameterFieldModel(name, type)); | ||
} | ||
|
||
public static void populateMethodModel(ClassModel classModel, Element element) | ||
{ | ||
if (!isPublic(element)) | ||
{ | ||
return; | ||
} | ||
ExecutableElement executableElement = (ExecutableElement) element; | ||
|
||
String name = element.getSimpleName().toString(); | ||
String returnType = executableElement.getReturnType().toString(); | ||
MethodModel method = new MethodModel(name, returnType); | ||
|
||
for (VariableElement parameterElement : executableElement.getParameters()) | ||
{ | ||
String paramName = parameterElement.getSimpleName().toString(); | ||
String paramType = parameterElement.asType().toString(); | ||
ParameterFieldModel parameter = new ParameterFieldModel(paramName, paramType); | ||
method.addParameter(parameter); | ||
} | ||
if ("<init>".equals(name)) | ||
{ | ||
classModel.addConstructor(method); | ||
} | ||
else | ||
{ | ||
classModel.addMethod(method); | ||
} | ||
} | ||
|
||
public static String getSimpleNameFromQualifiedName(String qualifiedName) | ||
{ | ||
int lastDot = qualifiedName.lastIndexOf("."); | ||
return lastDot < 0 ? qualifiedName : qualifiedName.substring(lastDot); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
impl/src/main/java/org/jboss/webbeans/xsd/model/ClassModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jboss.webbeans.xsd.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ClassModel extends NamedModel | ||
{ | ||
private ClassModel parent; | ||
|
||
private List<ParameterFieldModel> fields = new ArrayList<ParameterFieldModel>(); | ||
private List<MethodModel> methods = new ArrayList<MethodModel>(); | ||
private List<MethodModel> constructors = new ArrayList<MethodModel>(); | ||
|
||
public ClassModel() | ||
{ | ||
} | ||
|
||
public void addField(ParameterFieldModel fieldModel) | ||
{ | ||
fields.add(fieldModel); | ||
} | ||
|
||
public void addConstructor(MethodModel constructorModel) | ||
{ | ||
constructors.add(constructorModel); | ||
} | ||
|
||
public void addMethod(MethodModel methodModel) | ||
{ | ||
methods.add(methodModel); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
StringBuilder buffer = new StringBuilder(); | ||
buffer.append("Name: " + name + "\n"); | ||
buffer.append("Constructors: " + constructors + "\n"); | ||
buffer.append("Methods: " + methods + "\n"); | ||
buffer.append("Fields: " + fields + "\n"); | ||
return buffer.toString(); | ||
} | ||
|
||
public ClassModel getParent() | ||
{ | ||
return parent; | ||
} | ||
|
||
public void setParent(ClassModel parent) | ||
{ | ||
this.parent = parent; | ||
} | ||
|
||
public String getPackage() | ||
{ | ||
int lastDot = name.lastIndexOf("."); | ||
return lastDot < 0 ? name : name.substring(0, lastDot); | ||
} | ||
|
||
public String getSimpleName() | ||
{ | ||
int lastDot = name.lastIndexOf("."); | ||
return lastDot < 0 ? name : name.substring(lastDot + 1); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
impl/src/main/java/org/jboss/webbeans/xsd/model/MethodModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.jboss.webbeans.xsd.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MethodModel extends NamedModel | ||
{ | ||
private String returnType; | ||
private List<ParameterFieldModel> parameters = new ArrayList<ParameterFieldModel>(); | ||
|
||
public MethodModel(String name, String returnType) | ||
{ | ||
super(name); | ||
this.returnType = returnType; | ||
} | ||
|
||
public void addParameter(ParameterFieldModel parameter) | ||
{ | ||
parameters.add(parameter); | ||
} | ||
|
||
public List<ParameterFieldModel> getParameters() | ||
{ | ||
return parameters; | ||
} | ||
|
||
public String getReturnType() | ||
{ | ||
return returnType; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
impl/src/main/java/org/jboss/webbeans/xsd/model/NamedModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.jboss.webbeans.xsd.model; | ||
|
||
public class NamedModel | ||
{ | ||
protected String name; | ||
|
||
public NamedModel() | ||
{ | ||
} | ||
|
||
public NamedModel(String name) | ||
{ | ||
this.name = name; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
public void setName(String name) | ||
{ | ||
this.name = name; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
impl/src/main/java/org/jboss/webbeans/xsd/model/ParameterFieldModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.jboss.webbeans.xsd.model; | ||
|
||
public class ParameterFieldModel extends NamedModel | ||
{ | ||
private String type; | ||
|
||
public ParameterFieldModel(String name, String type) | ||
{ | ||
super(name); | ||
this.type = type; | ||
} | ||
|
||
public String getType() | ||
{ | ||
return type; | ||
} | ||
|
||
} |