From 3d2e1cf273a07c6b98079c2f6af7984bc11a6ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sun, 30 Nov 2008 15:04:40 +0000 Subject: [PATCH] improved APT configuration --- .../java/com/mysema/query/apt/APTFactory.java | 20 +- .../java/com/mysema/query/apt/APTUtils.java | 32 ++- .../main/java/com/mysema/query/apt/Field.java | 62 ++--- .../java/com/mysema/query/apt/Serializer.java | 32 +-- .../main/java/com/mysema/query/apt/Type.java | 178 +++++++------ .../mysema/query/apt/general/DTOVisitor.java | 5 +- .../query/apt/general/EntityVisitor.java | 8 +- .../query/apt/general/GeneralProcessor.java | 233 +++++++++--------- .../com/mysema/query/apt/util/TypeInfo.java | 107 ++++---- .../apt/general/GeneralProcessorTest.java | 51 ++-- 10 files changed, 388 insertions(+), 340 deletions(-) diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/APTFactory.java b/querydsl-apt/src/main/java/com/mysema/query/apt/APTFactory.java index b312259f3..6d5e3e254 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/APTFactory.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/APTFactory.java @@ -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 supportedAnnotations = asList(superClass, entity, dto); static final Collection 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 supportedAnnotationTypes() { return supportedAnnotations; @@ -58,6 +58,4 @@ public class APTFactory implements AnnotationProcessorFactory { } } - - } \ No newline at end of file diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/APTUtils.java b/querydsl-apt/src/main/java/com/mysema/query/apt/APTUtils.java index 19425f050..847eb3d6c 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/APTUtils.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/APTUtils.java @@ -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 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 options, String key, String defaultValue) { - String prefix = "-A" + key + "="; + String prefix = "-A" + key + "="; for (Map.Entry 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(); } } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/Field.java b/querydsl-apt/src/main/java/com/mysema/query/apt/Field.java index 9839b9642..fe820e9b1 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/Field.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/Field.java @@ -14,27 +14,17 @@ import com.sun.mirror.declaration.FieldDeclaration; * @author tiwe * @version $Id$ */ -public class Field implements Comparable{ - +public class Field implements Comparable { + /** * 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{ 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(); } - + } \ No newline at end of file diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/Serializer.java b/querydsl-apt/src/main/java/com/mysema/query/apt/Serializer.java index e64b96a41..6f24d9175 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/Serializer.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/Serializer.java @@ -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 model, Writer writer) throws Exception; - - public final class FreeMarker implements Serializer{ - + public void serialize(Map 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 model, Writer writer) throws IOException, TemplateException{ + + public void serialize(Map model, Writer writer) + throws IOException, TemplateException { cfg.getTemplate(templateLocation).process(model, writer); - writer.flush(); } + writer.flush(); + } } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/Type.java b/querydsl-apt/src/main/java/com/mysema/query/apt/Type.java index 61e5a8717..4283027e4 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/Type.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/Type.java @@ -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{ - - private final Set booleanFields = new TreeSet(); - - // not sorted - private final Set constructors = new HashSet(); - - private final Set entityCollections = new TreeSet(); - - private final Set entityLists = new TreeSet(); - - private final Set entityMaps = new TreeSet(); - - private final Set entityFields = new TreeSet(); +public class Type implements Comparable { - private final Set simpleCollections = new TreeSet(); - - private final Set simpleLists = new TreeSet(); - - private final Set simpleMaps = new TreeSet(); - - private final Set simpleFields = new TreeSet(); - - private final Set comparableFields = new TreeSet(); - - private final String simpleName, name; + Set booleanFields = new TreeSet(); + + Set constructors = new HashSet(); + + Set entityCollections = new TreeSet(); + + Set entityLists = new TreeSet(); + + Set entityMaps = new TreeSet(); + + Set entityFields = new TreeSet(); + + Set simpleCollections = new TreeSet(); + + Set simpleLists = new TreeSet(); + + Set simpleMaps = new TreeSet(); + + Set simpleFields = new TreeSet(); + + Set comparableFields = new TreeSet(); + + String simpleName, name; + + Set stringFields = new TreeSet(); + + String superType; - private final Set stringFields = new TreeSet(); - - 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 getBooleanFields() { return booleanFields; } - public Collection getConstructors(){ + public Collection getConstructors() { return constructors; } - + public Collection getEntityCollections() { return entityCollections; } - + public Collection getEntityLists() { return entityLists; } - + public Collection getEntityMaps() { return entityMaps; } - + public Collection getEntityFields() { return entityFields; } - + public String getName() { return name; } @@ -127,11 +151,11 @@ public class Type implements Comparable{ public Collection getSimpleCollections() { return simpleCollections; } - + public Collection getSimpleLists() { return simpleLists; } - + public Collection getSimpleMaps() { return simpleMaps; } @@ -139,27 +163,27 @@ public class Type implements Comparable{ public Collection getSimpleFields() { return simpleFields; } - - public Collection getComparableFields(){ + + public Collection getComparableFields() { return comparableFields; } - + public String getSimpleName() { return simpleName; } - + public Collection 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{ 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); } - + } \ No newline at end of file diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/general/DTOVisitor.java b/querydsl-apt/src/main/java/com/mysema/query/apt/general/DTOVisitor.java index 5a216a14e..b6e37cef8 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/general/DTOVisitor.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/general/DTOVisitor.java @@ -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); } - + } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/general/EntityVisitor.java b/querydsl-apt/src/main/java/com/mysema/query/apt/general/EntityVisitor.java index bf7512fa0..8475f5cb3 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/general/EntityVisitor.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/general/EntityVisitor.java @@ -21,7 +21,7 @@ import com.sun.mirror.util.SimpleDeclarationVisitor; * @version $Id$ */ public class EntityVisitor extends SimpleDeclarationVisitor { - public final Map types = new HashMap(); + public final Map types = new HashMap(); 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); + } } } \ No newline at end of file diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/general/GeneralProcessor.java b/querydsl-apt/src/main/java/com/mysema/query/apt/general/GeneralProcessor.java index 4e9de9c92..948697e4f 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/general/GeneralProcessor.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/general/GeneralProcessor.java @@ -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 entityTypes, - Map mappedSupertypes) { + Map entityTypes, Map 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 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 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 entityTypes = visitor1.types; - - for (Type typeDecl : entityTypes.values()){ + Map 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 options, String prefix, - String defaultValue) throws IOException { - for (Map.Entry 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 entityTypes) { @@ -171,18 +159,21 @@ public class GeneralProcessor implements AnnotationProcessor{ model.put("domainTypes", new TreeSet(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 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 types) { // populate model Map model = new HashMap(); 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 types){ + private void serializeDTOsAsOuterClasses(Collection types) { // populate model - Map model = new HashMap(); - 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 model = new HashMap(); + 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); + } } + } - + + + } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/util/TypeInfo.java b/querydsl-apt/src/main/java/com/mysema/query/apt/util/TypeInfo.java index 13872d25e..39768c369 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/util/TypeInfo.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/util/TypeInfo.java @@ -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 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(); - + } } - + } diff --git a/querydsl-apt/src/test/java/com/mysema/query/apt/general/GeneralProcessorTest.java b/querydsl-apt/src/test/java/com/mysema/query/apt/general/GeneralProcessorTest.java index 74476ae0e..dcd37ba42 100644 --- a/querydsl-apt/src/test/java/com/mysema/query/apt/general/GeneralProcessorTest.java +++ b/querydsl-apt/src/test/java/com/mysema/query/apt/general/GeneralProcessorTest.java @@ -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 model = new HashMap(); - - 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); } - + }