This commit is contained in:
Timo Westkämper 2009-05-28 14:09:20 +00:00
parent f5ded0408e
commit 2e92db39d5
8 changed files with 39 additions and 209 deletions

View File

@ -10,9 +10,9 @@ import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import com.mysema.query.codegen.Constructor;
import com.mysema.query.codegen.ConstructorModel;
import com.mysema.query.codegen.Parameter;
import com.mysema.query.codegen.Type;
import com.mysema.query.codegen.ClassModel;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.ConstructorDeclaration;
import com.sun.mirror.declaration.ParameterDeclaration;
@ -25,9 +25,9 @@ import com.sun.mirror.util.SimpleDeclarationVisitor;
* @version $Id$
*/
public class DefaultDTOVisitor extends SimpleDeclarationVisitor {
final Set<Type> types = new TreeSet<Type>();
final Set<ClassModel> types = new TreeSet<ClassModel>();
private Type last;
private ClassModel last;
@Override
public void visitClassDeclaration(ClassDeclaration d) {
@ -36,7 +36,7 @@ public class DefaultDTOVisitor extends SimpleDeclarationVisitor {
String packageName = d.getPackage().getQualifiedName();
String superType = d.getSuperclass().getDeclaration()
.getQualifiedName();
last = new Type(superType, packageName, name, simpleName);
last = new ClassModel(superType, packageName, name, simpleName);
types.add(last);
}
@ -49,7 +49,7 @@ public class DefaultDTOVisitor extends SimpleDeclarationVisitor {
String typeName = new TypeHelper(pa.getType()).getFullName();
parameters.add(new Parameter(name, typeName));
}
last.addConstructor(new Constructor(parameters));
last.addConstructor(new ConstructorModel(parameters));
}
}

View File

@ -10,9 +10,9 @@ import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.mysema.query.codegen.Field;
import com.mysema.query.codegen.FieldModel;
import com.mysema.query.codegen.FieldType;
import com.mysema.query.codegen.Type;
import com.mysema.query.codegen.ClassModel;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.FieldDeclaration;
import com.sun.mirror.declaration.InterfaceDeclaration;
@ -27,9 +27,9 @@ import com.sun.mirror.util.SimpleDeclarationVisitor;
* @version $Id$
*/
public class DefaultEntityVisitor extends SimpleDeclarationVisitor {
private Type last;
private ClassModel last;
public final Map<String, Type> types = new HashMap<String, Type>();
public final Map<String, ClassModel> types = new HashMap<String, ClassModel>();
private void addField(String name, TypeHelper typeInfo) {
String keyTypeName = typeInfo.getKeyTypeName();
@ -37,7 +37,7 @@ public class DefaultEntityVisitor extends SimpleDeclarationVisitor {
String typePackage = typeInfo.getPackageName();
String simpleTypeName = typeInfo.getSimpleName();
FieldType fieldType = typeInfo.getFieldType();
last.addField(new Field(name, keyTypeName, typePackage,
last.addField(new FieldModel(name, keyTypeName, typePackage,
typeName, simpleTypeName, fieldType));
}
@ -48,7 +48,7 @@ public class DefaultEntityVisitor extends SimpleDeclarationVisitor {
String packageName = d.getPackage().getQualifiedName();
String superType = d.getSuperclass().getDeclaration()
.getQualifiedName();
last = new Type(superType, packageName, name, simpleName);
last = new ClassModel(superType, packageName, name, simpleName);
types.put(d.getQualifiedName(), last);
}
@ -70,7 +70,7 @@ public class DefaultEntityVisitor extends SimpleDeclarationVisitor {
superType = d.getSuperinterfaces().iterator().next()
.getDeclaration().getQualifiedName();
}
last = new Type(superType, packageName, name, simpleName);
last = new ClassModel(superType, packageName, name, simpleName);
types.put(d.getQualifiedName(), last);
}

View File

@ -14,9 +14,10 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.mysema.query.codegen.ClassModelFactory;
import com.mysema.query.codegen.ClassModel;
import com.mysema.query.codegen.Serializer;
import com.mysema.query.codegen.Serializers;
import com.mysema.query.codegen.Type;
import com.mysema.query.util.FileUtils;
import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
@ -50,14 +51,14 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
this.dtoAnnotation = dtoAnnotation;
}
private void addSupertypeFields(Type typeDecl,
Map<String, Type> entityTypes, Map<String, Type> mappedSupertypes) {
private void addSupertypeFields(ClassModel typeDecl,
Map<String, ClassModel> entityTypes, Map<String, ClassModel> mappedSupertypes) {
String stype = typeDecl.getSupertypeName();
Class<?> superClass = safeClassForName(stype);
if (entityTypes.containsKey(stype)
|| mappedSupertypes.containsKey(stype)) {
while (true) {
Type sdecl;
ClassModel sdecl;
if (entityTypes.containsKey(stype)) {
sdecl = entityTypes.get(stype);
} else if (mappedSupertypes.containsKey(stype)) {
@ -71,7 +72,7 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
} else if (superClass != null && !superClass.equals(Object.class)) {
// TODO : recursively up ?
Type type = TypeFactory.createType(superClass);
ClassModel type = ClassModelFactory.createType(superClass);
// include fields of supertype
typeDecl.include(type);
}
@ -98,7 +99,7 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
// mapped superclass
AnnotationTypeDeclaration a;
Map<String, Type> mappedSupertypes;
Map<String, ClassModel> mappedSupertypes;
if (superClassAnnotation != null) {
a = (AnnotationTypeDeclaration) env
.getTypeDeclaration(superClassAnnotation);
@ -108,7 +109,7 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
}
mappedSupertypes = superclassVisitor.types;
} else {
mappedSupertypes = new HashMap<String, Type>();
mappedSupertypes = new HashMap<String, ClassModel>();
}
// domain types
@ -118,9 +119,9 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
for (Declaration typeDecl : env.getDeclarationsAnnotatedWith(a)) {
typeDecl.accept(getDeclarationScanner(entityVisitor, NO_OP));
}
Map<String, Type> entityTypes = entityVisitor.types;
Map<String, ClassModel> entityTypes = entityVisitor.types;
for (Type typeDecl : entityTypes.values()) {
for (ClassModel typeDecl : entityTypes.values()) {
addSupertypeFields(typeDecl, entityTypes, mappedSupertypes);
}
@ -157,12 +158,12 @@ public abstract class GeneralProcessor implements AnnotationProcessor {
}
}
protected void serializeAsOuterClasses(Collection<Type> entityTypes, Serializer serializer) {
protected void serializeAsOuterClasses(Collection<ClassModel> entityTypes, Serializer serializer) {
// populate model
Map<String, Object> model = new HashMap<String, Object>();
model.put("pre", namePrefix);
for (Type type : entityTypes) {
for (ClassModel type : entityTypes) {
String packageName = type.getPackageName();
model.put("package", packageName);
model.put("type", type);

View File

@ -1,64 +0,0 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.apt.general;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import com.mysema.query.codegen.Field;
import com.mysema.query.codegen.Type;
/**
* TypeFactory provides
*
* @author tiwe
* @version $Id$
*/
public class TypeFactory {
public static Type createType(Class<?> clazz) {
Type type = new Type(clazz.getSuperclass().getName(), clazz
.getPackage().getName(), clazz.getName(), clazz.getSimpleName());
for (java.lang.reflect.Field f : clazz.getDeclaredFields()) {
TypeHelper typeHelper = new TypeHelper(f.getType(), f
.getGenericType());
Field field = new Field(
f.getName(),
typeHelper.getKeyTypeName(), typeHelper.getPackageName(),
typeHelper.getFullName(), typeHelper.getSimpleName(),
typeHelper.getFieldType());
type.addField(field);
}
return type;
}
// TODO : move this to common place
public static Class<?> getTypeParameter(java.lang.reflect.Type type,
int index) {
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
java.lang.reflect.Type[] targs = ptype.getActualTypeArguments();
if (targs[index] instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) targs[index];
return (Class<?>) wildcardType.getUpperBounds()[0];
} else if (targs[index] instanceof TypeVariable) {
return (Class<?>) ((TypeVariable) targs[index])
.getGenericDeclaration();
} else if (targs[index] instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) targs[index])
.getRawType();
} else {
try {
return (Class<?>) targs[index];
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
}

View File

@ -5,12 +5,9 @@
*/
package com.mysema.query.apt.general;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Locale;
import org.apache.commons.lang.ClassUtils;
import com.mysema.query.annotations.Literal;
import com.mysema.query.codegen.FieldType;
import com.sun.mirror.type.AnnotationType;
@ -30,38 +27,12 @@ import com.sun.mirror.util.SimpleTypeVisitor;
* @author tiwe
* @version $Id$
*/
// TODO : clean this up
public class TypeHelper extends SimpleTypeVisitor {
private FieldType fieldType;
private String simpleName, fullName, packageName = "", keyTypeName;
public TypeHelper(Class<?> cl) {
this(cl, cl);
}
public TypeHelper(Class<?> cl, Type genericType) {
if (cl == null) {
throw new IllegalArgumentException("cl was null");
} else if (cl.isArray()) {
visitArrayType(cl);
} else if (cl.isEnum()) {
visitEnumType(cl);
} else if (cl.isPrimitive()) {
visitPrimitiveType(cl);
} else if (cl.isInterface()) {
visitInterfaceType(cl, genericType);
} else {
visitClassType(cl);
}
if (fullName == null) {
fullName = cl.getName();
}
setDefaults();
}
public TypeHelper(TypeMirror type) {
type.accept(this);
if (fullName == null) {
@ -94,12 +65,6 @@ public class TypeHelper extends SimpleTypeVisitor {
return fullName;
}
private void handleCollectionInterface(Class<?> type, Type genericType) {
TypeHelper valueInfo = new TypeHelper(TypeFactory.getTypeParameter(
genericType, 0));
handleCollection(valueInfo);
}
private void handleCollectionInterface(Iterator<TypeMirror> i) {
TypeHelper valueInfo = new TypeHelper(i.next());
handleCollection(valueInfo);
@ -130,20 +95,6 @@ public class TypeHelper extends SimpleTypeVisitor {
}
}
private void handleListInterface(Class<?> type, Type genericType) {
TypeHelper valueInfo = new TypeHelper(TypeFactory.getTypeParameter(
genericType, 0));
handleList(valueInfo);
}
private void handleMapInterface(Class<?> type, Type genericType) {
TypeHelper keyInfo = new TypeHelper(TypeFactory.getTypeParameter(
genericType, 0));
TypeHelper valueInfo = new TypeHelper(TypeFactory.getTypeParameter(
genericType, 1));
handleMapInterface(keyInfo, valueInfo);
}
private void handleMapInterface(Iterator<TypeMirror> i) {
TypeHelper keyInfo = new TypeHelper(i.next());
TypeHelper valueInfo = new TypeHelper(i.next());
@ -195,46 +146,6 @@ public class TypeHelper extends SimpleTypeVisitor {
visitArrayComponentType(valueInfo);
}
public void visitArrayType(Class<?> clazz) {
TypeHelper valueInfo = new TypeHelper(clazz.getComponentType());
visitArrayComponentType(valueInfo);
}
public void visitClassType(Class<?> type) {
fullName = type.getName();
packageName = type.getPackage().getName();
if (type.equals(String.class)) {
fieldType = FieldType.STRING;
} else if (type.equals(Boolean.class)) {
fieldType = FieldType.BOOLEAN;
} else if (type.equals(Locale.class) || type.equals(Class.class)
|| type.equals(Object.class)) {
fieldType = FieldType.SIMPLE;
} else if (isNumericSupported(fullName)
&& Number.class.isAssignableFrom(type)) {
fieldType = FieldType.NUMERIC;
} else if (type.getAnnotation(Literal.class) != null) {
if (Comparable.class.isAssignableFrom(type)) {
fieldType = FieldType.COMPARABLE;
} else {
fieldType = FieldType.SIMPLE;
}
} else if (isComparableSupported(fullName)
&& Comparable.class.isAssignableFrom(type)) {
fieldType = FieldType.COMPARABLE;
} else if (asSimpleType(fullName)) {
fieldType = FieldType.SIMPLE;
}
}
@Override
public void visitClassType(ClassType arg0) {
try {
@ -282,8 +193,7 @@ public class TypeHelper extends SimpleTypeVisitor {
}
private boolean isComparableSupported(String fullName) {
return fullName.startsWith("java.") || fullName.startsWith("javax.")
|| fullName.startsWith("org.joda.time");
return fullName.startsWith("java.") || fullName.startsWith("javax.") || fullName.startsWith("org.joda.time");
}
private boolean asSimpleType(String fullName) {
@ -299,19 +209,6 @@ public class TypeHelper extends SimpleTypeVisitor {
fieldType = FieldType.SIMPLE;
}
public void visitInterfaceType(Class<?> type, Type genericType) {
if (java.util.Map.class.isAssignableFrom(type)) {
handleMapInterface(type, genericType);
} else if (java.util.List.class.isAssignableFrom(type)) {
handleListInterface(type, genericType);
} else if (java.util.Collection.class.isAssignableFrom(type)) {
handleCollectionInterface(type, genericType);
}
}
@Override
public void visitInterfaceType(InterfaceType arg0) {
Iterator<TypeMirror> i = arg0.getActualTypeArguments().iterator();
@ -330,10 +227,6 @@ public class TypeHelper extends SimpleTypeVisitor {
}
}
public void visitPrimitiveType(Class<?> cl) {
visitPrimitiveWrapperType(ClassUtils.primitiveToWrapper(cl));
}
@Override
public void visitPrimitiveType(PrimitiveType arg0) {
Class<?> cl = null;
@ -396,8 +289,7 @@ public class TypeHelper extends SimpleTypeVisitor {
@Override
public void visitWildcardType(WildcardType arg0) {
if (!arg0.getUpperBounds().isEmpty()) {
TypeHelper lb = new TypeHelper(arg0.getUpperBounds().iterator()
.next());
TypeHelper lb = new TypeHelper(arg0.getUpperBounds().iterator().next());
fullName = lb.getFullName();
packageName = lb.getPackageName();
simpleName = lb.getSimpleName();

View File

@ -17,7 +17,7 @@ import java.util.Map;
import com.mysema.query.apt.general.DefaultEntityVisitor;
import com.mysema.query.apt.general.GeneralProcessor;
import com.mysema.query.codegen.Serializers;
import com.mysema.query.codegen.Type;
import com.mysema.query.codegen.ClassModel;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import com.sun.mirror.declaration.Declaration;
@ -43,7 +43,7 @@ public class JPAProcessor extends GeneralProcessor {
typeDecl.accept(getDeclarationScanner(entityVisitor, NO_OP));
}
Map<String, Type> entityTypes = entityVisitor.types;
Map<String, ClassModel> entityTypes = entityVisitor.types;
if (entityTypes.isEmpty()) {
env.getMessager().printNotice(
"No class generation for embeddable types");

View File

@ -13,12 +13,12 @@ import java.util.Map;
import org.junit.Test;
import com.mysema.query.codegen.Constructor;
import com.mysema.query.codegen.Field;
import com.mysema.query.codegen.ConstructorModel;
import com.mysema.query.codegen.FieldModel;
import com.mysema.query.codegen.FieldType;
import com.mysema.query.codegen.Parameter;
import com.mysema.query.codegen.Serializers;
import com.mysema.query.codegen.Type;
import com.mysema.query.codegen.ClassModel;
/**
* HibernateProcessorTest provides.
@ -28,22 +28,22 @@ import com.mysema.query.codegen.Type;
*/
public class GeneralProcessorTest {
private Type type;
private ClassModel type;
private Writer writer = new StringWriter();
private Map<String, Object> model = new HashMap<String, Object>();
public GeneralProcessorTest() {
type = new Type("com.mysema.query.DomainSuperClass",
type = new ClassModel("com.mysema.query.DomainSuperClass",
"com.mysema.query", "com.mysema.query.DomainClass",
"DomainClass");
Field field = new Field("field", null, "java.lang",
FieldModel field = new FieldModel("field", null, "java.lang",
"java.lang.String", "String", FieldType.STRING);
type.addField(field);
Parameter param = new Parameter("name", "java.lang.String");
type.addConstructor(new Constructor(Collections.singleton(param)));
type.addConstructor(new ConstructorModel(Collections.singleton(param)));
}
@Test

View File

@ -15,7 +15,8 @@ import java.util.Set;
import org.junit.Test;
import com.mysema.query.codegen.Type;
import com.mysema.query.codegen.ClassModel;
import com.mysema.query.codegen.ClassModelFactory;
/**
* TypeHelperTest provides
@ -27,7 +28,7 @@ public class TypeHelperTest {
@Test
public void test() {
Type type = TypeFactory.createType(TestType.class);
ClassModel type = ClassModelFactory.createType(TestType.class);
assertEquals(1, type.getEntityMaps().size());
assertEquals(1, type.getSimpleMaps().size());
assertEquals(2, type.getEntityCollections().size());