improved APT configuration

This commit is contained in:
Timo Westkämper 2008-11-30 15:04:40 +00:00
parent 3dd2feef5b
commit 3d2e1cf273
10 changed files with 388 additions and 340 deletions

View File

@ -24,20 +24,20 @@ import com.sun.mirror.declaration.AnnotationTypeDeclaration;
* @version $Id$
*/
public class APTFactory implements AnnotationProcessorFactory {
static final String superClass = "javax.persistence.MappedSuperclass",
entity = "javax.persistence.Entity",
entity = "javax.persistence.Entity",
dto = "com.mysema.query.annotations.DTO";
static final Collection<String> supportedAnnotations = asList(superClass, entity, dto);
static final Collection<String> supportedOptions = asList(
"-AdestClass","destClass",
"-AdestPackage","destPackage",
"-AdtoClass","dtoClass",
"-AdtoPackage","dtoPackage",
"-Ainclude","include",
"-AnamePrefix","namePrefix");
"-AdestClass", "destClass",
"-AdestPackage", "destPackage",
"-AdtoClass", "dtoClass",
"-AdtoPackage", "dtoPackage",
"-Ainclude", "include",
"-AnamePrefix", "namePrefix");
public Collection<String> supportedAnnotationTypes() {
return supportedAnnotations;
@ -58,6 +58,4 @@ public class APTFactory implements AnnotationProcessorFactory {
}
}
}

View File

@ -5,23 +5,49 @@
*/
package com.mysema.query.apt;
import java.io.*;
import java.util.Map;
import org.apache.commons.io.FileUtils;
/**
* APUtils provides
*
*
* @author tiwe
* @version $Id$
*/
public class APTUtils {
public static Writer writerFor(File file) {
if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
System.err.println("Folder " + file.getParent()
+ " could not be created");
}
try {
return new OutputStreamWriter(new FileOutputStream(file));
} catch (FileNotFoundException e) {
String error = "Caught " + e.getClass().getName();
throw new RuntimeException(error, e);
}
}
public static String getFileContent(Map<String, String> options,
String key, String defaultValue) throws IOException {
String path = getString(options, key, null);
if (path != null) {
return FileUtils.readFileToString(new File(path), "UTF-8");
} else {
return "";
}
}
public static String getString(Map<String, String> options, String key,
String defaultValue) {
String prefix = "-A" + key + "=";
String prefix = "-A" + key + "=";
for (Map.Entry<String, String> entry : options.entrySet()) {
if (entry.getKey().startsWith(prefix)) {
return entry.getKey().substring(prefix.length());
}else if (entry.getKey().equals(key)){
} else if (entry.getKey().equals(key)) {
return entry.getValue();
}
}

View File

@ -14,27 +14,17 @@ import com.sun.mirror.declaration.FieldDeclaration;
* @author tiwe
* @version $Id$
*/
public class Field implements Comparable<Field>{
public class Field implements Comparable<Field> {
/**
* The Enum Type.
*/
public enum Type {
BOOLEAN,
COMPARABLE,
ENTITY,
ENTITYLIST,
ENTITYCOLLECTION,
ENTITYMAP,
SIMPLE,
SIMPLELIST,
SIMPLECOLLECTION,
SIMPLEMAP,
STRING
BOOLEAN, COMPARABLE, ENTITY, ENTITYLIST, ENTITYCOLLECTION, ENTITYMAP, SIMPLE, SIMPLELIST, SIMPLECOLLECTION, SIMPLEMAP, STRING
}
private final Type fieldType;
private String name, keyTypeName, typeName, simpleTypeName;
public Field(FieldDeclaration field) {
@ -43,48 +33,48 @@ public class Field implements Comparable<Field>{
this.keyTypeName = typeInfo.getKeyTypeName();
this.typeName = typeInfo.getFullName();
this.simpleTypeName = typeInfo.getSimpleName();
this.fieldType = typeInfo.getFieldType();
this.fieldType = typeInfo.getFieldType();
}
public Field(String name, String keyTypeName, String typeName,
String simpleTypeName, Type fieldType){
public Field(String name, String keyTypeName, String typeName,
String simpleTypeName, Type fieldType) {
this.name = name;
this.keyTypeName = keyTypeName;
this.typeName = typeName;
this.simpleTypeName = simpleTypeName;
this.fieldType = fieldType;
}
public int compareTo(Field o) {
return name.compareTo(o.name);
}
public boolean equals(Object o){
return o instanceof Field && name.equals(((Field)o).name);
public boolean equals(Object o) {
return o instanceof Field && name.equals(((Field) o).name);
}
public Type getFieldType(){
public Type getFieldType() {
return fieldType;
}
public String getKeyTypeName(){
public String getKeyTypeName() {
return keyTypeName;
}
public String getName(){
public String getName() {
return name;
}
public String getSimpleTypeName(){
public String getSimpleTypeName() {
return simpleTypeName;
}
public String getTypeName(){
public String getTypeName() {
return typeName;
}
public int hashCode(){
public int hashCode() {
return name.hashCode();
}
}

View File

@ -15,40 +15,44 @@ import freemarker.template.TemplateException;
/**
* Serializer provides
*
*
* @author tiwe
* @version $Id$
*/
public interface Serializer {
/**
*
* @param model
* @param writer
* @throws Exception
*/
public void serialize(Map<String,Object> model, Writer writer) throws Exception;
public final class FreeMarker implements Serializer{
public void serialize(Map<String, Object> model, Writer writer)
throws Exception;
public final class FreeMarker implements Serializer {
private static final Configuration cfg;
static {
cfg = new Configuration();
cfg.setClassForTemplateLoading(Serializer.class, "/");
cfg.setObjectWrapper(new DefaultObjectWrapper());
}
private final String templateLocation;
public FreeMarker(String template){
if (template == null) throw new IllegalArgumentException("template was null");
public FreeMarker(String template) {
if (template == null)
throw new IllegalArgumentException("template was null");
templateLocation = template;
}
public void serialize(Map<String,Object> model, Writer writer) throws IOException, TemplateException{
public void serialize(Map<String, Object> model, Writer writer)
throws IOException, TemplateException {
cfg.getTemplate(templateLocation).process(model, writer);
writer.flush(); }
writer.flush();
}
}

View File

@ -5,7 +5,10 @@
*/
package com.mysema.query.apt;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.ConstructorDeclaration;
@ -17,109 +20,130 @@ import com.sun.mirror.declaration.FieldDeclaration;
* @author tiwe
* @version $Id$
*/
public class Type implements Comparable<Type>{
private final Set<Field> booleanFields = new TreeSet<Field>();
// not sorted
private final Set<Constructor> constructors = new HashSet<Constructor>();
private final Set<Field> entityCollections = new TreeSet<Field>();
private final Set<Field> entityLists = new TreeSet<Field>();
private final Set<Field> entityMaps = new TreeSet<Field>();
private final Set<Field> entityFields = new TreeSet<Field>();
public class Type implements Comparable<Type> {
private final Set<Field> simpleCollections = new TreeSet<Field>();
private final Set<Field> simpleLists = new TreeSet<Field>();
private final Set<Field> simpleMaps = new TreeSet<Field>();
private final Set<Field> simpleFields = new TreeSet<Field>();
private final Set<Field> comparableFields = new TreeSet<Field>();
private final String simpleName, name;
Set<Field> booleanFields = new TreeSet<Field>();
Set<Constructor> constructors = new HashSet<Constructor>();
Set<Field> entityCollections = new TreeSet<Field>();
Set<Field> entityLists = new TreeSet<Field>();
Set<Field> entityMaps = new TreeSet<Field>();
Set<Field> entityFields = new TreeSet<Field>();
Set<Field> simpleCollections = new TreeSet<Field>();
Set<Field> simpleLists = new TreeSet<Field>();
Set<Field> simpleMaps = new TreeSet<Field>();
Set<Field> simpleFields = new TreeSet<Field>();
Set<Field> comparableFields = new TreeSet<Field>();
String simpleName, name;
Set<Field> stringFields = new TreeSet<Field>();
String superType;
private final Set<Field> stringFields = new TreeSet<Field>();
private final String superType;
public Type(ClassDeclaration d) {
this.simpleName = d.getSimpleName();
this.name = d.getQualifiedName();
this.superType = d.getSuperclass().getDeclaration().getQualifiedName();
}
public Type(String superType, String name, String simpleName){
public Type(String superType, String name, String simpleName) {
this.superType = superType;
this.name = name;
this.simpleName = simpleName;
}
public void addConstructor(ConstructorDeclaration co) {
addConstructor(new Constructor(co));
}
public void addConstructor(Constructor co){
constructors.add(co);
public void addConstructor(Constructor co) {
constructors.add(co);
}
public void addField(FieldDeclaration field){
public void addField(FieldDeclaration field) {
addField(new Field(field));
}
public void addField(Field fieldDecl){
switch(fieldDecl.getFieldType()){
case BOOLEAN : booleanFields.add(fieldDecl); break;
case STRING : stringFields.add(fieldDecl); break;
case SIMPLE : simpleFields.add(fieldDecl); break;
case COMPARABLE: comparableFields.add(fieldDecl); break;
case ENTITY : entityFields.add(fieldDecl); break;
case ENTITYCOLLECTION : entityCollections.add(fieldDecl); break;
case SIMPLECOLLECTION : simpleCollections.add(fieldDecl); break;
case ENTITYLIST : entityLists.add(fieldDecl); break;
case SIMPLELIST : simpleLists.add(fieldDecl); break;
case ENTITYMAP : entityMaps.add(fieldDecl); break;
case SIMPLEMAP : simpleMaps.add(fieldDecl); break;
}
public void addField(Field fieldDecl) {
switch (fieldDecl.getFieldType()) {
case BOOLEAN:
booleanFields.add(fieldDecl);
break;
case STRING:
stringFields.add(fieldDecl);
break;
case SIMPLE:
simpleFields.add(fieldDecl);
break;
case COMPARABLE:
comparableFields.add(fieldDecl);
break;
case ENTITY:
entityFields.add(fieldDecl);
break;
case ENTITYCOLLECTION:
entityCollections.add(fieldDecl);
break;
case SIMPLECOLLECTION:
simpleCollections.add(fieldDecl);
break;
case ENTITYLIST:
entityLists.add(fieldDecl);
break;
case SIMPLELIST:
simpleLists.add(fieldDecl);
break;
case ENTITYMAP:
entityMaps.add(fieldDecl);
break;
case SIMPLEMAP:
simpleMaps.add(fieldDecl);
break;
}
}
public int compareTo(Type o) {
return simpleName.compareTo(o.simpleName);
}
public boolean equals(Object o){
return o instanceof Type && simpleName.equals(((Type)o).simpleName);
public boolean equals(Object o) {
return o instanceof Type && simpleName.equals(((Type) o).simpleName);
}
public Collection<Field> getBooleanFields() {
return booleanFields;
}
public Collection<Constructor> getConstructors(){
public Collection<Constructor> getConstructors() {
return constructors;
}
public Collection<Field> getEntityCollections() {
return entityCollections;
}
public Collection<Field> getEntityLists() {
return entityLists;
}
public Collection<Field> getEntityMaps() {
return entityMaps;
}
public Collection<Field> getEntityFields() {
return entityFields;
}
public String getName() {
return name;
}
@ -127,11 +151,11 @@ public class Type implements Comparable<Type>{
public Collection<Field> getSimpleCollections() {
return simpleCollections;
}
public Collection<Field> getSimpleLists() {
return simpleLists;
}
public Collection<Field> getSimpleMaps() {
return simpleMaps;
}
@ -139,27 +163,27 @@ public class Type implements Comparable<Type>{
public Collection<Field> getSimpleFields() {
return simpleFields;
}
public Collection<Field> getComparableFields(){
public Collection<Field> getComparableFields() {
return comparableFields;
}
public String getSimpleName() {
return simpleName;
}
public Collection<Field> getStringFields() {
return stringFields;
}
public String getSupertypeName(){
public String getSupertypeName() {
return superType;
}
public int hashCode(){
public int hashCode() {
return name.hashCode();
}
public void include(Type decl) {
booleanFields.addAll(decl.booleanFields);
entityCollections.addAll(decl.entityCollections);
@ -167,11 +191,11 @@ public class Type implements Comparable<Type>{
entityLists.addAll(decl.entityLists);
entityMaps.addAll(decl.entityMaps);
comparableFields.addAll(decl.comparableFields);
simpleCollections.addAll(decl.simpleCollections);
simpleCollections.addAll(decl.simpleCollections);
simpleFields.addAll(decl.simpleFields);
simpleLists.addAll(decl.simpleLists);
simpleLists.addAll(decl.simpleLists);
simpleMaps.addAll(decl.simpleMaps);
stringFields.addAll(decl.stringFields);
stringFields.addAll(decl.stringFields);
}
}

View File

@ -29,9 +29,10 @@ public class DTOVisitor extends SimpleDeclarationVisitor {
last = new Type(d);
types.add(last);
}
@Override
public void visitConstructorDeclaration(ConstructorDeclaration d){
public void visitConstructorDeclaration(ConstructorDeclaration d) {
last.addConstructor(d);
}
}

View File

@ -21,7 +21,7 @@ import com.sun.mirror.util.SimpleDeclarationVisitor;
* @version $Id$
*/
public class EntityVisitor extends SimpleDeclarationVisitor {
public final Map<String,Type> types = new HashMap<String,Type>();
public final Map<String, Type> types = new HashMap<String, Type>();
private Type last;
@ -33,9 +33,9 @@ public class EntityVisitor extends SimpleDeclarationVisitor {
@Override
public void visitFieldDeclaration(FieldDeclaration d) {
if (!d.getModifiers().contains(Modifier.STATIC)){
last.addField(d);
}
if (!d.getModifiers().contains(Modifier.STATIC)) {
last.addField(d);
}
}
}

View File

@ -5,7 +5,7 @@
*/
package com.mysema.query.apt.general;
import static com.mysema.query.apt.APTUtils.getString;
import static com.mysema.query.apt.APTUtils.*;
import static com.sun.mirror.util.DeclarationVisitors.NO_OP;
import static com.sun.mirror.util.DeclarationVisitors.getDeclarationScanner;
@ -15,8 +15,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import org.apache.commons.io.FileUtils;
import com.mysema.query.apt.Serializer;
import com.mysema.query.apt.Type;
import com.sun.mirror.apt.AnnotationProcessor;
@ -26,11 +24,11 @@ import com.sun.mirror.declaration.Declaration;
/**
* GeneralProcessor provides
*
*
* @author tiwe
* @version $Id$
*/
public class GeneralProcessor implements AnnotationProcessor{
public class GeneralProcessor implements AnnotationProcessor {
static final Serializer
DOMAIN_INNER_TMPL = new Serializer.FreeMarker("/domain-as-inner-classes.ftl"),
@ -39,130 +37,120 @@ public class GeneralProcessor implements AnnotationProcessor{
DTO_OUTER_TMPL = new Serializer.FreeMarker("/dto-as-outer-classes.ftl");
private final String destClass, destPackage, dtoClass, dtoPackage;
private final AnnotationProcessorEnvironment env;
private final String include, namePrefix, targetFolder;
private final String superClassAnnotation, domainAnnotation, dtoAnnotation;
public GeneralProcessor(AnnotationProcessorEnvironment env,
String superClassAnnotation,
String domainAnnotation, String dtoAnnotation) throws IOException {
public GeneralProcessor(AnnotationProcessorEnvironment env,
String superClassAnnotation, String domainAnnotation,
String dtoAnnotation) throws IOException {
this.env = env;
this.targetFolder = env.getOptions().get("-s");
this.destClass = getString(env.getOptions(), "destClass", null);
this.destPackage = getString(env.getOptions(), "destPackage", null);
this.dtoClass = getString(env.getOptions(), "dtoClass", null);
this.dtoPackage = getString(env.getOptions(), "dtoPackage", null);
this.dtoPackage = getString(env.getOptions(), "dtoPackage", null);
this.include = getFileContent(env.getOptions(), "include", "");
this.namePrefix = getString(env.getOptions(), "namePrefix", "");
this.superClassAnnotation = superClassAnnotation;
this.domainAnnotation = domainAnnotation;
this.dtoAnnotation = dtoAnnotation;
}
private void addSupertypeFields(Type typeDecl,
Map<String, Type> entityTypes,
Map<String, Type> mappedSupertypes) {
Map<String, Type> entityTypes, Map<String, Type> mappedSupertypes) {
String stype = typeDecl.getSupertypeName();
while (true){
while (true) {
Type sdecl;
if (entityTypes.containsKey(stype)){
if (entityTypes.containsKey(stype)) {
sdecl = entityTypes.get(stype);
}else if (mappedSupertypes.containsKey(stype)){
} else if (mappedSupertypes.containsKey(stype)) {
sdecl = mappedSupertypes.get(stype);
}else{
} else {
return;
}
typeDecl.include(sdecl);
stype = sdecl.getSupertypeName();
}
}
private void createDomainClasses() {
EntityVisitor visitor1 = new EntityVisitor();
// mapped superclass
AnnotationTypeDeclaration a = (AnnotationTypeDeclaration) env.getTypeDeclaration(superClassAnnotation);
for (Declaration typeDecl : env.getDeclarationsAnnotatedWith(a)) {
typeDecl.accept(getDeclarationScanner(visitor1, NO_OP));
}
Map<String,Type> mappedSupertypes = visitor1.types;
}
private void createDomainClasses() {
EntityVisitor superclassVisitor = new EntityVisitor();
// mapped superclass
AnnotationTypeDeclaration a = (AnnotationTypeDeclaration) env
.getTypeDeclaration(superClassAnnotation);
for (Declaration typeDecl : env.getDeclarationsAnnotatedWith(a)) {
typeDecl.accept(getDeclarationScanner(superclassVisitor, NO_OP));
}
Map<String, Type> mappedSupertypes = superclassVisitor.types;
// TODO : embeddable types
// domain types
visitor1 = new EntityVisitor();
a = (AnnotationTypeDeclaration) env.getTypeDeclaration(domainAnnotation);
EntityVisitor entityVisitor = new EntityVisitor();
a = (AnnotationTypeDeclaration) env
.getTypeDeclaration(domainAnnotation);
for (Declaration typeDecl : env.getDeclarationsAnnotatedWith(a)) {
typeDecl.accept(getDeclarationScanner(visitor1, NO_OP));
typeDecl.accept(getDeclarationScanner(entityVisitor, NO_OP));
}
Map<String,Type> entityTypes = visitor1.types;
for (Type typeDecl : entityTypes.values()){
Map<String, Type> entityTypes = entityVisitor.types;
for (Type typeDecl : entityTypes.values()) {
addSupertypeFields(typeDecl, entityTypes, mappedSupertypes);
}
if (entityTypes.isEmpty()){
if (entityTypes.isEmpty()) {
String error = "No class generation for domain types";
System.err.print(error);
env.getMessager().printError(error);
}else{
if (destClass != null){
} else {
if (destClass != null) {
serializeAsInnerClasses(entityTypes.values());
}else if (destPackage != null){
} else if (destPackage != null) {
serializeAsOuterClasses(entityTypes.values());
}else{
} else {
String error = "No class generation for domain types";
System.err.print(error);
env.getMessager().printError(error);
}
}
}
}
private void createDTOClasses() {
private void createDTOClasses() {
AnnotationTypeDeclaration a = (AnnotationTypeDeclaration) env
.getTypeDeclaration(dtoAnnotation);
DTOVisitor visitor2 = new DTOVisitor();
DTOVisitor dtoVisitor = new DTOVisitor();
for (Declaration typeDecl : env.getDeclarationsAnnotatedWith(a)) {
typeDecl.accept(getDeclarationScanner(visitor2, NO_OP));
}
if (visitor2.types.isEmpty()){
typeDecl.accept(getDeclarationScanner(dtoVisitor, NO_OP));
}
if (dtoVisitor.types.isEmpty()) {
String error = "No class generation for DTO types";
System.err.print(error);
env.getMessager().printError(error);
}else{
if (dtoClass != null){
serializeDTOsAsInnerClasses(visitor2.types);
}else if (dtoPackage != null){
serializeDTOsAsOuterClasses(visitor2.types);
}else{
} else {
if (dtoClass != null) {
serializeDTOsAsInnerClasses(dtoVisitor.types);
} else if (dtoPackage != null) {
serializeDTOsAsOuterClasses(dtoVisitor.types);
} else {
String error = "No class generation for DTO types";
System.err.print(error);
env.getMessager().printError(error);
}
}
}
private String getFileContent(Map<String, String> options, String prefix,
String defaultValue) throws IOException {
for (Map.Entry<String, String> entry : options.entrySet()) {
if (entry.getKey().startsWith(prefix)) {
String fileName = entry.getKey().substring(prefix.length());
return FileUtils.readFileToString(new File(fileName), "UTF-8");
}
}
return defaultValue;
}
public void process() {
createDomainClasses();
createDTOClasses();
createDTOClasses();
}
private void serializeAsInnerClasses(Collection<Type> entityTypes) {
@ -171,18 +159,21 @@ public class GeneralProcessor implements AnnotationProcessor{
model.put("domainTypes", new TreeSet<Type>(entityTypes));
model.put("pre", namePrefix);
model.put("include", include);
model.put("package", destClass.substring(0, destClass.lastIndexOf('.')));
model.put("classSimpleName", destClass.substring(destClass.lastIndexOf('.') + 1));
model
.put("package", destClass.substring(0, destClass
.lastIndexOf('.')));
model.put("classSimpleName", destClass.substring(destClass
.lastIndexOf('.') + 1));
// serialize it
try {
String path = destClass.replace('.', '/') + ".java";
DOMAIN_INNER_TMPL.serialize(model, writerFor(new File(targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception",e);
DOMAIN_INNER_TMPL.serialize(model, writerFor(new File(targetFolder,
path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception", e);
}
}
private void serializeAsOuterClasses(Collection<Type> entityTypes) {
// populate model
@ -190,70 +181,66 @@ public class GeneralProcessor implements AnnotationProcessor{
model.put("pre", namePrefix);
model.put("include", include);
model.put("package", destPackage);
for (Type type : entityTypes){
for (Type type : entityTypes) {
model.put("type", type);
model.put("classSimpleName", type.getSimpleName());
// serialize it
try {
String path = destPackage.replace('.', '/') + "/" + namePrefix + type.getSimpleName() + ".java";
DOMAIN_OUTER_TMPL.serialize(model, writerFor(new File(targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception",e);
String path = destPackage.replace('.', '/') + "/" + namePrefix
+ type.getSimpleName() + ".java";
DOMAIN_OUTER_TMPL.serialize(model, writerFor(new File(
targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception", e);
}
}
}
}
private void serializeDTOsAsInnerClasses(Collection<Type> types) {
// populate model
Map<String, Object> model = new HashMap<String, Object>();
model.put("dtoTypes", types);
model.put("pre", namePrefix);
model.put("package", dtoClass.substring(0, dtoClass.lastIndexOf('.')));
model.put("classSimpleName", dtoClass.substring(dtoClass.lastIndexOf('.') + 1));
// serialize it
model.put("classSimpleName", dtoClass.substring(dtoClass
.lastIndexOf('.') + 1));
// serialize it
try {
String path = dtoClass.replace('.', '/') + ".java";
DTO_INNER_TMPL.serialize(model, writerFor(new File(targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception",e);
}
DTO_INNER_TMPL.serialize(model, writerFor(new File(targetFolder,
path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception", e);
}
}
private void serializeDTOsAsOuterClasses(Collection<Type> types){
private void serializeDTOsAsOuterClasses(Collection<Type> types) {
// populate model
Map<String, Object> model = new HashMap<String, Object>();
model.put("pre", namePrefix);
model.put("include", include);
model.put("package", dtoPackage);
for (Type type : types){
model.put("type", type);
model.put("classSimpleName", type.getSimpleName());
// serialize it
try {
String path = dtoPackage.replace('.', '/') + "/" + namePrefix + type.getSimpleName() + ".java";
DTO_OUTER_TMPL.serialize(model, writerFor(new File(targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception",e);
}
}
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("pre", namePrefix);
model.put("include", include);
model.put("package", dtoPackage);
private Writer writerFor(File file){
if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()){
System.err.println("Folder " + file.getParent() + " could not be created");
}
try {
return new OutputStreamWriter(new FileOutputStream(file));
} catch (FileNotFoundException e) {
String error = "Caught " + e.getClass().getName();
throw new RuntimeException(error, e);
for (Type type : types) {
model.put("type", type);
model.put("classSimpleName", type.getSimpleName());
// serialize it
try {
String path = dtoPackage.replace('.', '/') + "/" + namePrefix
+ type.getSimpleName() + ".java";
DTO_OUTER_TMPL.serialize(model, writerFor(new File(
targetFolder, path)));
} catch (Exception e) {
throw new RuntimeException("Caught exception", e);
}
}
}
}

View File

@ -21,24 +21,24 @@ import com.sun.mirror.util.SimpleTypeVisitor;
public class TypeInfo {
private Field.Type fieldType;
private String simpleName, fullName, keyTypeName;
private final TypeInfoVisitor visitor = new TypeInfoVisitor();
public TypeInfo(TypeMirror type){
public TypeInfo(TypeMirror type) {
type.accept(visitor);
if (fieldType == null){
if (fieldType == null) {
fieldType = Field.Type.ENTITY;
}
if (fullName == null){
if (fullName == null) {
fullName = type.toString();
}
if (simpleName == null){
simpleName = fullName.substring(fullName.lastIndexOf('.')+1);
if (simpleName == null) {
simpleName = fullName.substring(fullName.lastIndexOf('.') + 1);
}
}
public Field.Type getFieldType() {
return fieldType;
}
@ -59,34 +59,34 @@ public class TypeInfo {
return fullName;
}
public String toString(){
public String toString() {
return fullName;
}
private class TypeInfoVisitor extends SimpleTypeVisitor{
private class TypeInfoVisitor extends SimpleTypeVisitor {
public void visitAnnotationType(AnnotationType arg0) {
//
}
public void visitArrayType(ArrayType arg0) {
public void visitArrayType(ArrayType arg0) {
TypeInfo valueInfo = new TypeInfo(arg0.getComponentType());
fullName = valueInfo.getFullName();
if (valueInfo.fieldType == Field.Type.ENTITY){
if (valueInfo.fieldType == Field.Type.ENTITY) {
fieldType = Field.Type.ENTITYCOLLECTION;
}else{
} else {
fieldType = Field.Type.SIMPLECOLLECTION;
}
}
public void visitClassType(ClassType arg0) {
fullName = arg0.toString();
if (fullName.equals(String.class.getName())){
if (fullName.equals(String.class.getName())) {
fieldType = Field.Type.STRING;
}else if (fullName.equals(Boolean.class.getName())){
} else if (fullName.equals(Boolean.class.getName())) {
fieldType = Field.Type.BOOLEAN;
}else if (fullName.equals(Locale.class.getName())){
} else if (fullName.equals(Locale.class.getName())) {
fieldType = Field.Type.SIMPLE;
}else if (fullName.startsWith("java")){
} else if (fullName.startsWith("java")) {
fieldType = Field.Type.COMPARABLE;
}
}
@ -98,36 +98,37 @@ public class TypeInfo {
public void visitInterfaceType(InterfaceType arg0) {
Iterator<TypeMirror> i = arg0.getActualTypeArguments().iterator();
String typeName = arg0.toString();
if (arg0.getActualTypeArguments().size() > 0){
if (arg0.getActualTypeArguments().size() > 0) {
typeName = typeName.substring(0, typeName.indexOf('<'));
}
if (typeName.equals(java.util.Map.class.getName())){
if (typeName.equals(java.util.Map.class.getName())) {
TypeInfo keyInfo = new TypeInfo(i.next());
keyTypeName = keyInfo.getFullName();
TypeInfo valueInfo = new TypeInfo(i.next());
fullName = valueInfo.getFullName();
if (valueInfo.fieldType == Field.Type.ENTITY){
if (valueInfo.fieldType == Field.Type.ENTITY) {
fieldType = Field.Type.ENTITYMAP;
}else{
} else {
fieldType = Field.Type.SIMPLEMAP;
}
}else if (typeName.equals(java.util.Collection.class.getName()) || typeName.equals(java.util.Set.class.getName())){
} else if (typeName.equals(java.util.Collection.class.getName())
|| typeName.equals(java.util.Set.class.getName())) {
TypeInfo valueInfo = new TypeInfo(i.next());
fullName = valueInfo.getFullName();
if (valueInfo.fieldType == Field.Type.ENTITY){
if (valueInfo.fieldType == Field.Type.ENTITY) {
fieldType = Field.Type.ENTITYCOLLECTION;
}else{
} else {
fieldType = Field.Type.SIMPLECOLLECTION;
}
}else if (typeName.equals(java.util.List.class.getName())){
} else if (typeName.equals(java.util.List.class.getName())) {
TypeInfo valueInfo = new TypeInfo(i.next());
fullName = valueInfo.getFullName();
if (valueInfo.fieldType == Field.Type.ENTITY){
if (valueInfo.fieldType == Field.Type.ENTITY) {
fieldType = Field.Type.ENTITYLIST;
}else{
} else {
fieldType = Field.Type.SIMPLELIST;
}
}
@ -135,26 +136,42 @@ public class TypeInfo {
public void visitPrimitiveType(PrimitiveType arg0) {
Class<?> cl = null;
switch (arg0.getKind()){
case BOOLEAN: cl = Boolean.class; break;
case BYTE: cl = Byte.class; break;
case CHAR: cl = Character.class; break;
case DOUBLE: cl = Double.class; break;
case FLOAT: cl = Float.class; break;
case INT: cl = Integer.class; break;
case LONG: cl = Long.class; break;
case SHORT: cl = Short.class; break;
switch (arg0.getKind()) {
case BOOLEAN:
cl = Boolean.class;
break;
case BYTE:
cl = Byte.class;
break;
case CHAR:
cl = Character.class;
break;
case DOUBLE:
cl = Double.class;
break;
case FLOAT:
cl = Float.class;
break;
case INT:
cl = Integer.class;
break;
case LONG:
cl = Long.class;
break;
case SHORT:
cl = Short.class;
break;
}
if (cl.equals(Boolean.class)){
if (cl.equals(Boolean.class)) {
fieldType = Field.Type.BOOLEAN;
}else {
} else {
fieldType = Field.Type.COMPARABLE;
}
fullName = cl.getName();
simpleName = cl.getSimpleName();
}
}
}

View File

@ -17,7 +17,6 @@ import com.mysema.query.apt.Constructor;
import com.mysema.query.apt.Field;
import com.mysema.query.apt.Parameter;
import com.mysema.query.apt.Type;
import com.mysema.query.apt.general.GeneralProcessor;
/**
* HibernateProcessorTest provides.
@ -25,55 +24,57 @@ import com.mysema.query.apt.general.GeneralProcessor;
* @author tiwe
* @version $Id$
*/
public class GeneralProcessorTest {
public class GeneralProcessorTest {
private Type type;
private Writer writer = new StringWriter();
private Map<String, Object> model = new HashMap<String, Object>();
public GeneralProcessorTest(){
type = new Type("com.mysema.query.DomainSuperClass","com.mysema.query.DomainClass","DomainClass");
Field field = new Field("field",null,"java.lang.String","java.lang.String",Field.Type.STRING);
public GeneralProcessorTest() {
type = new Type("com.mysema.query.DomainSuperClass",
"com.mysema.query.DomainClass", "DomainClass");
Field field = new Field("field", null, "java.lang.String",
"java.lang.String", Field.Type.STRING);
type.addField(field);
Parameter param = new Parameter("name","java.lang.String");
type.addConstructor( new Constructor(Collections.singleton(param)));
Parameter param = new Parameter("name", "java.lang.String");
type.addConstructor(new Constructor(Collections.singleton(param)));
}
@Test
public void testDomainTypesAsInnerClass() throws Exception{
public void testDomainTypesAsInnerClass() throws Exception {
model.put("domainTypes", Collections.singleton(type));
model.put("pre", "");
model.put("include", "");
model.put("package", "com.mysema.query");
model.put("package", "com.mysema.query");
model.put("classSimpleName", "Test");
// as inner classes
// as inner classes
GeneralProcessor.DOMAIN_INNER_TMPL.serialize(model, writer);
}
@Test
public void testDomainTypesAsOuterClasses() throws Exception{
public void testDomainTypesAsOuterClasses() throws Exception {
model.put("type", type);
model.put("pre", "");
model.put("include", "");
model.put("package", "com.mysema.query");
model.put("package", "com.mysema.query");
model.put("classSimpleName", "Test");
// as outer classes
// as outer classes
GeneralProcessor.DOMAIN_OUTER_TMPL.serialize(model, writer);
}
@Test
public void testDTOTypes() throws Exception{
public void testDTOTypes() throws Exception {
model.put("dtoTypes", Collections.singleton(type));
model.put("pre", "");
model.put("package", "com.mysema.query");
model.put("classSimpleName", "Test");
// as inner classes
// as inner classes
GeneralProcessor.DTO_INNER_TMPL.serialize(model, writer);
}
}