mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
stripped Model suffix off from codegen class names
made references to external supertypes possible in APT based query type generation added Java compiler API based test
This commit is contained in:
parent
68aa9d2b94
commit
2efa88bc1f
@ -10,6 +10,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -30,14 +31,15 @@ import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.type.TypeVariable;
|
||||
import javax.lang.model.type.WildcardType;
|
||||
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.SimpleTypeModel;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.SimpleType;
|
||||
import com.mysema.query.codegen.Type;
|
||||
import com.mysema.query.codegen.TypeAdapter;
|
||||
import com.mysema.query.codegen.TypeCategory;
|
||||
import com.mysema.query.codegen.TypeExtendsModel;
|
||||
import com.mysema.query.codegen.TypeModel;
|
||||
import com.mysema.query.codegen.TypeModelFactory;
|
||||
import com.mysema.query.codegen.TypeModels;
|
||||
import com.mysema.query.codegen.TypeSuperModel;
|
||||
import com.mysema.query.codegen.TypeExtends;
|
||||
import com.mysema.query.codegen.TypeFactory;
|
||||
import com.mysema.query.codegen.TypeSuper;
|
||||
import com.mysema.query.codegen.Types;
|
||||
|
||||
/**
|
||||
* APTTypeModelFactory is a factory for APT inspection based TypeModel creation
|
||||
@ -45,26 +47,35 @@ import com.mysema.query.codegen.TypeSuperModel;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public final class APTTypeModelFactory {
|
||||
public final class APTTypeFactory {
|
||||
|
||||
private final Map<List<String>,TypeModel> cache = new HashMap<List<String>,TypeModel>();
|
||||
@Nullable
|
||||
private static Class<?> safeForName(String name){
|
||||
try {
|
||||
return Class.forName(name);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<List<String>,Type> cache = new HashMap<List<String>,Type>();
|
||||
|
||||
private final Configuration configuration;
|
||||
|
||||
private final TypeModel defaultValue;
|
||||
private final Type defaultValue;
|
||||
|
||||
private final List<Class<? extends Annotation>> entityAnnotations;
|
||||
|
||||
private final Map<List<String>,EntityModel> entityTypeCache = new HashMap<List<String>,EntityModel>();
|
||||
private final Map<List<String>,EntityType> entityTypeCache = new HashMap<List<String>,EntityType>();
|
||||
|
||||
private final ProcessingEnvironment env;
|
||||
|
||||
private final TypeModelFactory factory;
|
||||
private final TypeFactory factory;
|
||||
|
||||
private final TypeElement numberType, comparableType;
|
||||
|
||||
public APTTypeModelFactory(ProcessingEnvironment env, Configuration configuration,
|
||||
TypeModelFactory factory, List<Class<? extends Annotation>> annotations){
|
||||
public APTTypeFactory(ProcessingEnvironment env, Configuration configuration,
|
||||
TypeFactory factory, List<Class<? extends Annotation>> annotations){
|
||||
this.env = env;
|
||||
this.configuration = configuration;
|
||||
this.factory = factory;
|
||||
@ -74,67 +85,23 @@ public final class APTTypeModelFactory {
|
||||
this.comparableType = env.getElementUtils().getTypeElement(Comparable.class.getName());
|
||||
}
|
||||
|
||||
private EntityModel asEntityModel(TypeMirror type, TypeModel value) {
|
||||
if (type.getKind() == TypeKind.TYPEVAR){
|
||||
TypeVariable typeVar = (TypeVariable)type;
|
||||
if (typeVar.getUpperBound() != null){
|
||||
type = typeVar.getUpperBound();
|
||||
}
|
||||
}else if (type.getKind() == TypeKind.WILDCARD){
|
||||
WildcardType wildcard = (WildcardType)type;
|
||||
if (wildcard.getExtendsBound() != null){
|
||||
type = wildcard.getExtendsBound();
|
||||
}
|
||||
}
|
||||
|
||||
Collection<String> superTypes = Collections.emptySet();
|
||||
if (type.getKind() == TypeKind.DECLARED){
|
||||
DeclaredType declaredType = (DeclaredType)type;
|
||||
TypeElement e = (TypeElement)declaredType.asElement();
|
||||
if (e.getKind() == ElementKind.CLASS){
|
||||
superTypes = Collections.singleton(create(e.getSuperclass()).getFullName());
|
||||
}else{
|
||||
superTypes = new ArrayList<String>(e.getInterfaces().size());
|
||||
for (TypeMirror mirror : e.getInterfaces()){
|
||||
TypeModel iface = create(mirror);
|
||||
if (!iface.getFullName().startsWith("java")){
|
||||
superTypes.add(iface.getFullName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
throw new IllegalArgumentException("Unsupported type kind " + type.getKind());
|
||||
}
|
||||
|
||||
return new EntityModel(configuration.getNamePrefix(), value, superTypes);
|
||||
}
|
||||
|
||||
private TypeModel create(TypeElement typeElement, TypeCategory category,
|
||||
List<? extends TypeMirror> typeArgs) {
|
||||
private Type create(TypeElement typeElement, TypeCategory category, List<? extends TypeMirror> typeArgs) {
|
||||
String name = typeElement.getQualifiedName().toString();
|
||||
String simpleName = typeElement.getSimpleName().toString();
|
||||
String packageName = env.getElementUtils().getPackageOf(typeElement).getQualifiedName().toString();
|
||||
TypeModel[] params = new TypeModel[typeArgs.size()];
|
||||
Type[] params = new Type[typeArgs.size()];
|
||||
for (int i = 0; i < params.length; i++){
|
||||
params[i] = create(typeArgs.get(i));
|
||||
}
|
||||
return new SimpleTypeModel(category,
|
||||
return new SimpleType(category,
|
||||
name, packageName, simpleName,
|
||||
typeElement.getModifiers().contains(Modifier.FINAL),
|
||||
params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a TypeModel for the given TypeMirror
|
||||
*
|
||||
* @param type
|
||||
* @param el
|
||||
* @return
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
public TypeModel create(TypeMirror type){
|
||||
List<String> key = getKey(type, true);
|
||||
public Type create(TypeMirror type){
|
||||
List<String> key = createKey(type, true, true);
|
||||
if (entityTypeCache.containsKey(key)){
|
||||
return entityTypeCache.get(key);
|
||||
|
||||
@ -143,18 +110,19 @@ public final class APTTypeModelFactory {
|
||||
|
||||
}else{
|
||||
cache.put(key, null);
|
||||
TypeModel value = handle(type);
|
||||
if (value != null && value.getCategory() == TypeCategory.ENTITY){
|
||||
value = asEntityModel(type, value);
|
||||
entityTypeCache.put(key, (EntityModel)value);
|
||||
Type typeModel = handle(type);
|
||||
if (typeModel != null && typeModel.getCategory() == TypeCategory.ENTITY){
|
||||
EntityType entityType = createEntityType(type);
|
||||
cache.put(key, entityType);
|
||||
return entityType;
|
||||
}else{
|
||||
cache.put(key, value);
|
||||
}
|
||||
return value;
|
||||
cache.put(key, typeModel);
|
||||
return typeModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TypeModel createClassType(DeclaredType t, TypeElement typeElement) {
|
||||
private Type createClassType(DeclaredType t, TypeElement typeElement) {
|
||||
// entity type
|
||||
for (Class<? extends Annotation> entityAnn : entityAnnotations){
|
||||
if (typeElement.getAnnotation(entityAnn) != null){
|
||||
@ -178,33 +146,31 @@ public final class APTTypeModelFactory {
|
||||
return create(typeElement, typeCategory, t.getTypeArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an EntityModel for the given TypeMirror
|
||||
*
|
||||
* @param type
|
||||
* @param el
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
public EntityModel createEntityModel(TypeMirror type){
|
||||
List<String> key = getKey(type, true);
|
||||
public EntityType createEntityType(TypeMirror type){
|
||||
List<String> key = createKey(type, false, true);
|
||||
if (entityTypeCache.containsKey(key)){
|
||||
return entityTypeCache.get(key);
|
||||
|
||||
}else{
|
||||
}else{
|
||||
entityTypeCache.put(key, null);
|
||||
TypeModel value = handle(type);
|
||||
Type value = handle(type);
|
||||
if (value != null){
|
||||
EntityModel entityModel = asEntityModel(type, value);
|
||||
EntityType entityModel = new EntityType(configuration.getNamePrefix(), value);
|
||||
// entityTypeCache.put(Collections.singletonList(value.getFullName()), entityModel);
|
||||
entityTypeCache.put(key, entityModel);
|
||||
cache.put(createKey(type, true, true), entityModel);
|
||||
for (EntityType superType : getSupertypes(type, value)){
|
||||
entityModel.getSuperTypes().add(superType);
|
||||
}
|
||||
return entityModel;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TypeModel createEnumType(DeclaredType t, TypeElement typeElement) {
|
||||
|
||||
private Type createEnumType(DeclaredType t, TypeElement typeElement) {
|
||||
for (Class<? extends Annotation> entityAnn : entityAnnotations){
|
||||
if (typeElement.getAnnotation(entityAnn) != null){
|
||||
return create(typeElement, TypeCategory.ENTITY, t.getTypeArguments());
|
||||
@ -215,16 +181,7 @@ public final class APTTypeModelFactory {
|
||||
return create(typeElement, TypeCategory.SIMPLE, t.getTypeArguments());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Class<?> safeForName(String name){
|
||||
try {
|
||||
return Class.forName(name);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private TypeModel createInterfaceType(DeclaredType t, TypeElement typeElement) {
|
||||
private Type createInterfaceType(DeclaredType t, TypeElement typeElement) {
|
||||
// entity type
|
||||
for (Class<? extends Annotation> entityAnn : entityAnnotations){
|
||||
if (typeElement.getAnnotation(entityAnn) != null){
|
||||
@ -269,43 +226,94 @@ public final class APTTypeModelFactory {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a cache key for the given TypeMirror
|
||||
*
|
||||
* @param type
|
||||
* @param deep inspect type arguments
|
||||
* @return
|
||||
*/
|
||||
private List<String> getKey(TypeMirror type, boolean deep){
|
||||
private List<String> createKey(TypeMirror type, boolean useTypeArgs, boolean deep){
|
||||
List<String> key = new ArrayList<String>();
|
||||
key.add(type.toString());
|
||||
String name = type.toString();
|
||||
if (name.contains("<")){
|
||||
name = name.substring(0, name.indexOf('<'));
|
||||
}
|
||||
key.add(name);
|
||||
|
||||
if (type.getKind() == TypeKind.TYPEVAR){
|
||||
TypeVariable t = (TypeVariable)type;
|
||||
if (t.getUpperBound() != null){
|
||||
key.addAll(getKey(t.getUpperBound(), false));
|
||||
key.addAll(createKey(t.getUpperBound(), useTypeArgs, false));
|
||||
}
|
||||
if (t.getLowerBound() != null){
|
||||
key.addAll(getKey(t.getLowerBound(), false));
|
||||
key.addAll(createKey(t.getLowerBound(), useTypeArgs, false));
|
||||
}
|
||||
|
||||
}else if (type.getKind() == TypeKind.WILDCARD){
|
||||
WildcardType t = (WildcardType)type;
|
||||
if (t.getExtendsBound() != null){
|
||||
key.addAll(getKey(t.getExtendsBound(), false));
|
||||
key.addAll(createKey(t.getExtendsBound(), useTypeArgs, false));
|
||||
}
|
||||
if (t.getSuperBound() != null){
|
||||
key.addAll(getKey(t.getSuperBound(), false));
|
||||
key.addAll(createKey(t.getSuperBound(), useTypeArgs, false));
|
||||
}
|
||||
}else if (type.getKind() == TypeKind.DECLARED){
|
||||
|
||||
}else if (type.getKind() == TypeKind.DECLARED){
|
||||
DeclaredType t = (DeclaredType)type;
|
||||
for (TypeMirror arg : t.getTypeArguments()){
|
||||
key.addAll(deep ? getKey(arg, false) : Collections.singleton(arg.toString()));
|
||||
if (useTypeArgs){
|
||||
for (TypeMirror arg : t.getTypeArguments()){
|
||||
if (deep){
|
||||
key.addAll(createKey(arg, useTypeArgs, false));
|
||||
}else{
|
||||
key.add(arg.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
private Set<EntityType> getSupertypes(TypeMirror type, Type value) {
|
||||
type = normalize(type);
|
||||
Set<EntityType> superTypes = Collections.emptySet();
|
||||
if (type.getKind() == TypeKind.DECLARED){
|
||||
DeclaredType declaredType = (DeclaredType)type;
|
||||
TypeElement e = (TypeElement)declaredType.asElement();
|
||||
if (e.getKind() == ElementKind.CLASS){
|
||||
if (e.getSuperclass().getKind() != TypeKind.NONE){
|
||||
TypeMirror supertype = normalize(e.getSuperclass());
|
||||
Type superClass = create(supertype);
|
||||
if (!superClass.getFullName().startsWith("java")){
|
||||
superTypes = Collections.singleton(createEntityType(supertype));
|
||||
}
|
||||
}
|
||||
}else{
|
||||
superTypes = new HashSet<EntityType>(e.getInterfaces().size());
|
||||
for (TypeMirror mirror : e.getInterfaces()){
|
||||
EntityType iface = createEntityType(mirror);
|
||||
if (!iface.getFullName().startsWith("java")){
|
||||
superTypes.add(iface);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
throw new IllegalArgumentException("Unsupported type kind " + type.getKind());
|
||||
}
|
||||
return superTypes;
|
||||
}
|
||||
|
||||
private TypeMirror normalize(TypeMirror type) {
|
||||
if (type.getKind() == TypeKind.TYPEVAR){
|
||||
TypeVariable typeVar = (TypeVariable)type;
|
||||
if (typeVar.getUpperBound() != null){
|
||||
type = typeVar.getUpperBound();
|
||||
}
|
||||
}else if (type.getKind() == TypeKind.WILDCARD){
|
||||
WildcardType wildcard = (WildcardType)type;
|
||||
if (wildcard.getExtendsBound() != null){
|
||||
type = wildcard.getExtendsBound();
|
||||
}
|
||||
}
|
||||
return key;
|
||||
return type;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private TypeModel handle(TypeMirror type) {
|
||||
private Type handle(TypeMirror type) {
|
||||
if (type instanceof DeclaredType){
|
||||
DeclaredType t = (DeclaredType)type;
|
||||
if (t.asElement() instanceof TypeElement){
|
||||
@ -324,9 +332,9 @@ public final class APTTypeModelFactory {
|
||||
TypeVariable t = (TypeVariable)type;
|
||||
String varName = t.toString();
|
||||
if (t.getUpperBound() != null){
|
||||
return new TypeExtendsModel(varName, handle(t.getUpperBound()));
|
||||
return new TypeExtends(varName, handle(t.getUpperBound()));
|
||||
}else if (t.getLowerBound() != null && !(t.getLowerBound() instanceof NullType)){
|
||||
return new TypeSuperModel(varName, handle(t.getLowerBound()));
|
||||
return new TypeSuper(varName, handle(t.getLowerBound()));
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
@ -334,9 +342,9 @@ public final class APTTypeModelFactory {
|
||||
}else if (type instanceof WildcardType){
|
||||
WildcardType t = (WildcardType)type;
|
||||
if (t.getExtendsBound() != null){
|
||||
return new TypeExtendsModel(handle(t.getExtendsBound()));
|
||||
return new TypeExtends(handle(t.getExtendsBound()));
|
||||
}else if (t.getSuperBound() != null){
|
||||
return new TypeSuperModel(handle(t.getSuperBound()));
|
||||
return new TypeSuper(handle(t.getSuperBound()));
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
@ -348,14 +356,14 @@ public final class APTTypeModelFactory {
|
||||
}else if (type instanceof PrimitiveType){
|
||||
PrimitiveType t = (PrimitiveType)type;
|
||||
switch (t.getKind()) {
|
||||
case BOOLEAN: return TypeModels.BOOLEAN;
|
||||
case BYTE: return TypeModels.BYTE;
|
||||
case CHAR: return TypeModels.CHAR;
|
||||
case DOUBLE: return TypeModels.DOUBLE;
|
||||
case FLOAT: return TypeModels.FLOAT;
|
||||
case INT: return TypeModels.INT;
|
||||
case LONG: return TypeModels.LONG;
|
||||
case SHORT: return TypeModels.SHORT;
|
||||
case BOOLEAN: return Types.BOOLEAN;
|
||||
case BYTE: return Types.BYTE;
|
||||
case CHAR: return Types.CHAR;
|
||||
case DOUBLE: return Types.DOUBLE;
|
||||
case FLOAT: return Types.FLOAT;
|
||||
case INT: return Types.INT;
|
||||
case LONG: return Types.LONG;
|
||||
case SHORT: return Types.SHORT;
|
||||
}
|
||||
throw new IllegalArgumentException("Unsupported type " + t.getKind());
|
||||
|
||||
@ -14,7 +14,7 @@ import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.Serializer;
|
||||
import com.mysema.query.codegen.SerializerConfig;
|
||||
import com.mysema.query.codegen.TypeMappings;
|
||||
@ -74,7 +74,7 @@ public interface Configuration {
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
SerializerConfig getSerializerConfig(EntityModel model);
|
||||
SerializerConfig getSerializerConfig(EntityType model);
|
||||
|
||||
/**
|
||||
* @return
|
||||
|
||||
@ -25,7 +25,7 @@ import com.mysema.query.annotations.QueryType;
|
||||
import com.mysema.query.annotations.QuerydslConfig;
|
||||
import com.mysema.query.codegen.DTOSerializer;
|
||||
import com.mysema.query.codegen.EmbeddableSerializer;
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.EntitySerializer;
|
||||
import com.mysema.query.codegen.Serializer;
|
||||
import com.mysema.query.codegen.SerializerConfig;
|
||||
@ -134,7 +134,7 @@ public class DefaultConfiguration implements Configuration {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerializerConfig getSerializerConfig(EntityModel model) {
|
||||
public SerializerConfig getSerializerConfig(EntityType model) {
|
||||
if (typeToConfig.containsKey(model.getFullName())){
|
||||
return typeToConfig.get(model.getFullName());
|
||||
}else if (packageToConfig.containsKey(model.getPackageName())){
|
||||
|
||||
@ -26,13 +26,13 @@ import com.mysema.query.annotations.PropertyType;
|
||||
import com.mysema.query.annotations.QueryInit;
|
||||
import com.mysema.query.annotations.QueryMethod;
|
||||
import com.mysema.query.annotations.QueryType;
|
||||
import com.mysema.query.codegen.ConstructorModel;
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.MethodModel;
|
||||
import com.mysema.query.codegen.ParameterModel;
|
||||
import com.mysema.query.codegen.PropertyModel;
|
||||
import com.mysema.query.codegen.Constructor;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.Method;
|
||||
import com.mysema.query.codegen.Parameter;
|
||||
import com.mysema.query.codegen.Property;
|
||||
import com.mysema.query.codegen.TypeCategory;
|
||||
import com.mysema.query.codegen.TypeModel;
|
||||
import com.mysema.query.codegen.Type;
|
||||
|
||||
/**
|
||||
* EntityElementVisitor is a an APT visitor for entity types
|
||||
@ -45,15 +45,15 @@ public final class ElementHandler{
|
||||
|
||||
private final Configuration configuration;
|
||||
|
||||
private final APTTypeModelFactory typeFactory;
|
||||
private final APTTypeFactory typeFactory;
|
||||
|
||||
public ElementHandler(Configuration configuration, APTTypeModelFactory typeFactory){
|
||||
public ElementHandler(Configuration configuration, APTTypeFactory typeFactory){
|
||||
this.configuration = configuration;
|
||||
this.typeFactory = typeFactory;
|
||||
}
|
||||
|
||||
private TypeModel getType(VariableElement element){
|
||||
TypeModel rv = typeFactory.create(element.asType());
|
||||
private Type getType(VariableElement element){
|
||||
Type rv = typeFactory.create(element.asType());
|
||||
if (element.getAnnotation(QueryType.class) != null){
|
||||
QueryType qt = element.getAnnotation(QueryType.class);
|
||||
if (qt.value() != PropertyType.NONE){
|
||||
@ -64,22 +64,22 @@ public final class ElementHandler{
|
||||
return rv;
|
||||
}
|
||||
|
||||
public void handleConstructors(EntityModel entityModel, List<? extends Element> elements) {
|
||||
public void handleConstructors(EntityType entityModel, List<? extends Element> elements) {
|
||||
for (ExecutableElement constructor : ElementFilter.constructorsIn(elements)){
|
||||
if (configuration.isValidConstructor(constructor)){
|
||||
List<ParameterModel> parameters = transformParams(constructor.getParameters());
|
||||
entityModel.addConstructor(new ConstructorModel(parameters));
|
||||
List<Parameter> parameters = transformParams(constructor.getParameters());
|
||||
entityModel.addConstructor(new Constructor(parameters));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleFieldProperty(EntityModel entityModel, VariableElement field,
|
||||
Map<String, PropertyModel> properties,
|
||||
public void handleFieldProperty(EntityType entityModel, VariableElement field,
|
||||
Map<String, Property> properties,
|
||||
Set<String> blockedProperties,
|
||||
Map<String, TypeCategory> types) {
|
||||
String name = field.getSimpleName().toString();
|
||||
try{
|
||||
TypeModel fieldType = typeFactory.create(field.asType());
|
||||
Type fieldType = typeFactory.create(field.asType());
|
||||
if (field.getAnnotation(QueryType.class) != null){
|
||||
TypeCategory typeCategory = TypeCategory.get(field.getAnnotation(QueryType.class).value());
|
||||
if (typeCategory == null){
|
||||
@ -93,7 +93,7 @@ public final class ElementHandler{
|
||||
if (field.getAnnotation(QueryInit.class) != null){
|
||||
inits = field.getAnnotation(QueryInit.class).value();
|
||||
}
|
||||
properties.put(name, new PropertyModel(entityModel, name, fieldType, inits));
|
||||
properties.put(name, new Property(entityModel, name, fieldType, inits));
|
||||
}catch(IllegalArgumentException ex){
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Caught exception for field ");
|
||||
@ -102,12 +102,12 @@ public final class ElementHandler{
|
||||
}
|
||||
}
|
||||
|
||||
public void handleMethodProperty(EntityModel entityModel, String propertyName,
|
||||
public void handleMethodProperty(EntityType entityModel, String propertyName,
|
||||
ExecutableElement method,
|
||||
Map<String, PropertyModel> properties, Set<String> blockedProperties,
|
||||
Map<String, Property> properties, Set<String> blockedProperties,
|
||||
Map<String, TypeCategory> types) {
|
||||
try{
|
||||
TypeModel propertyType = typeFactory.create(method.getReturnType());
|
||||
Type propertyType = typeFactory.create(method.getReturnType());
|
||||
if (method.getAnnotation(QueryType.class) != null){
|
||||
TypeCategory typeCategory = TypeCategory.get(method.getAnnotation(QueryType.class).value());
|
||||
if (typeCategory == null){
|
||||
@ -124,7 +124,7 @@ public final class ElementHandler{
|
||||
if (method.getAnnotation(QueryInit.class) != null){
|
||||
inits = method.getAnnotation(QueryInit.class).value();
|
||||
}
|
||||
properties.put(propertyName, new PropertyModel(entityModel, propertyName, propertyType, inits));
|
||||
properties.put(propertyName, new Property(entityModel, propertyName, propertyType, inits));
|
||||
|
||||
}catch(IllegalArgumentException ex){
|
||||
StringBuilder builder = new StringBuilder();
|
||||
@ -134,21 +134,24 @@ public final class ElementHandler{
|
||||
}
|
||||
}
|
||||
|
||||
public EntityType handleNormalType(TypeElement e) {
|
||||
EntityType entityType = typeFactory.createEntityType(e.asType());
|
||||
return handleNormalType(entityType, e);
|
||||
}
|
||||
|
||||
public EntityModel handleNormalType(TypeElement e) {
|
||||
EntityModel entityModel = typeFactory.createEntityModel(e.asType());
|
||||
List<? extends Element> elements = e.getEnclosedElements();
|
||||
|
||||
public EntityType handleNormalType(EntityType entityType, TypeElement e) {
|
||||
List<? extends Element> elements = e.getEnclosedElements();
|
||||
VisitorConfig config = configuration.getConfig(e, elements);
|
||||
|
||||
Set<String> blockedProperties = new HashSet<String>();
|
||||
Map<String,PropertyModel> properties = new HashMap<String,PropertyModel>();
|
||||
Map<String,Property> properties = new HashMap<String,Property>();
|
||||
Map<String,TypeCategory> types = new HashMap<String,TypeCategory>();
|
||||
Set<MethodModel> queryMethods = new HashSet<MethodModel>();
|
||||
Set<Method> queryMethods = new HashSet<Method>();
|
||||
|
||||
// constructors
|
||||
if (config.visitConstructors()){
|
||||
handleConstructors(entityModel, elements);
|
||||
handleConstructors(entityType, elements);
|
||||
}
|
||||
|
||||
// fields
|
||||
@ -156,7 +159,7 @@ public final class ElementHandler{
|
||||
for (VariableElement field : ElementFilter.fieldsIn(elements)){
|
||||
String name = field.getSimpleName().toString();
|
||||
if (configuration.isValidField(field)){
|
||||
handleFieldProperty(entityModel, field, properties, blockedProperties, types);
|
||||
handleFieldProperty(entityType, field, properties, blockedProperties, types);
|
||||
}else if (configuration.isBlockedField(field)){
|
||||
blockedProperties.add(name);
|
||||
}
|
||||
@ -166,7 +169,7 @@ public final class ElementHandler{
|
||||
// methods
|
||||
for (ExecutableElement method : ElementFilter.methodsIn(elements)){
|
||||
if (method.getAnnotation(QueryMethod.class) != null){
|
||||
handleQueryMethod(entityModel, method, queryMethods);
|
||||
handleQueryMethod(entityType, method, queryMethods);
|
||||
|
||||
}else if (config.visitMethodProperties()){
|
||||
String name = method.getSimpleName().toString();
|
||||
@ -179,7 +182,7 @@ public final class ElementHandler{
|
||||
}
|
||||
|
||||
if (configuration.isValidGetter(method)){
|
||||
handleMethodProperty(entityModel, name, method, properties, blockedProperties, types);
|
||||
handleMethodProperty(entityType, name, method, properties, blockedProperties, types);
|
||||
}else if (configuration.isBlockedGetter(method)){
|
||||
blockedProperties.add(name);
|
||||
}
|
||||
@ -187,40 +190,40 @@ public final class ElementHandler{
|
||||
|
||||
}
|
||||
|
||||
for (MethodModel entry : queryMethods){
|
||||
entityModel.addMethod(entry);
|
||||
for (Method entry : queryMethods){
|
||||
entityType.addMethod(entry);
|
||||
}
|
||||
|
||||
for (Map.Entry<String,PropertyModel> entry : properties.entrySet()){
|
||||
for (Map.Entry<String,Property> entry : properties.entrySet()){
|
||||
if (!blockedProperties.contains(entry.getKey())){
|
||||
entityModel.addProperty(entry.getValue());
|
||||
entityType.addProperty(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return entityModel;
|
||||
return entityType;
|
||||
}
|
||||
|
||||
public EntityModel handleProjectionType(TypeElement e) {
|
||||
TypeModel c = typeFactory.create(e.asType());
|
||||
EntityModel entityModel = new EntityModel(configuration.getNamePrefix(), c.as(TypeCategory.ENTITY));
|
||||
public EntityType handleProjectionType(TypeElement e) {
|
||||
Type c = typeFactory.create(e.asType());
|
||||
EntityType entityModel = new EntityType(configuration.getNamePrefix(), c.as(TypeCategory.ENTITY));
|
||||
List<? extends Element> elements = e.getEnclosedElements();
|
||||
handleConstructors(entityModel, elements);
|
||||
return entityModel;
|
||||
}
|
||||
|
||||
public void handleQueryMethod(EntityModel entityModel, ExecutableElement method, Set<MethodModel> queryMethods) {
|
||||
public void handleQueryMethod(EntityType entityModel, ExecutableElement method, Set<Method> queryMethods) {
|
||||
String name = method.getSimpleName().toString();
|
||||
QueryMethod queryMethod = method.getAnnotation(QueryMethod.class);
|
||||
TypeModel returnType = typeFactory.create(method.getReturnType());
|
||||
MethodModel methodModel = new MethodModel(entityModel, name, queryMethod.value(), transformParams(method.getParameters()), returnType);
|
||||
Type returnType = typeFactory.create(method.getReturnType());
|
||||
Method methodModel = new Method(entityModel, name, queryMethod.value(), transformParams(method.getParameters()), returnType);
|
||||
queryMethods.add(methodModel);
|
||||
}
|
||||
|
||||
private List<ParameterModel> transformParams(List<? extends VariableElement> params){
|
||||
List<ParameterModel> parameters = new ArrayList<ParameterModel>(params.size());
|
||||
private List<Parameter> transformParams(List<? extends VariableElement> params){
|
||||
List<Parameter> parameters = new ArrayList<Parameter>(params.size());
|
||||
for (VariableElement param : params){
|
||||
TypeModel paramType = getType(param);
|
||||
parameters.add(new ParameterModel(param.getSimpleName().toString(), paramType));
|
||||
Type paramType = getType(param);
|
||||
parameters.add(new Parameter(param.getSimpleName().toString(), paramType));
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@ -16,7 +16,6 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
@ -36,13 +35,12 @@ import com.mysema.query.annotations.QueryExtensions;
|
||||
import com.mysema.query.annotations.QueryMethod;
|
||||
import com.mysema.query.annotations.QueryProjection;
|
||||
import com.mysema.query.annotations.QuerydslVariables;
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.EntityModelFactory;
|
||||
import com.mysema.query.codegen.MethodModel;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.Method;
|
||||
import com.mysema.query.codegen.Serializer;
|
||||
import com.mysema.query.codegen.SerializerConfig;
|
||||
import com.mysema.query.codegen.TypeFactory;
|
||||
import com.mysema.query.codegen.TypeMappings;
|
||||
import com.mysema.query.codegen.TypeModelFactory;
|
||||
import com.mysema.util.JavaWriter;
|
||||
|
||||
/**
|
||||
@ -53,29 +51,27 @@ import com.mysema.util.JavaWriter;
|
||||
*/
|
||||
public class Processor {
|
||||
|
||||
private final Map<String, EntityModel> actualSupertypes = new HashMap<String, EntityModel>();
|
||||
private final Map<String, EntityType> actualSupertypes = new HashMap<String, EntityType>();
|
||||
|
||||
private final Map<String, EntityModel> allSupertypes = new HashMap<String, EntityModel>();
|
||||
private final Map<String, EntityType> allSupertypes = new HashMap<String, EntityType>();
|
||||
|
||||
private final Configuration configuration;
|
||||
|
||||
private final Map<String, EntityModel> dtos = new HashMap<String, EntityModel>();
|
||||
private final Map<String, EntityType> dtos = new HashMap<String, EntityType>();
|
||||
|
||||
private final Map<String,EntityModel> extensionTypes = new HashMap<String,EntityModel>();
|
||||
private final Map<String,EntityType> extensionTypes = new HashMap<String,EntityType>();
|
||||
|
||||
private final Map<String,EntityModel> embeddables = new HashMap<String,EntityModel>();
|
||||
private final Map<String,EntityType> embeddables = new HashMap<String,EntityType>();
|
||||
|
||||
private final EntityModelFactory entityModelFactory;
|
||||
private final Map<String, EntityType> entityTypes = new HashMap<String, EntityType>();
|
||||
|
||||
private final Map<String, EntityModel> entityTypes = new HashMap<String, EntityModel>();
|
||||
|
||||
private final ElementHandler entityVisitor;
|
||||
private final ElementHandler elementHandler;
|
||||
|
||||
private final ProcessingEnvironment env;
|
||||
|
||||
private final RoundEnvironment roundEnv;
|
||||
|
||||
private final APTTypeModelFactory typeFactory;
|
||||
private final APTTypeFactory typeModelFactory;
|
||||
|
||||
public Processor(ProcessingEnvironment env, RoundEnvironment roundEnv, Configuration configuration) {
|
||||
this.env = Assert.notNull(env);
|
||||
@ -91,53 +87,21 @@ public class Processor {
|
||||
anns.add(configuration.getEmbeddableAnn());
|
||||
}
|
||||
|
||||
TypeModelFactory factory = new TypeModelFactory(anns);
|
||||
this.typeFactory = new APTTypeModelFactory(env, configuration, factory, anns);
|
||||
if (configuration.getSkipAnn() != null){
|
||||
this.entityModelFactory = new EntityModelFactory(factory, configuration.getSkipAnn());
|
||||
}else{
|
||||
this.entityModelFactory = new EntityModelFactory(factory);
|
||||
}
|
||||
|
||||
this.entityVisitor = new ElementHandler(configuration, typeFactory);
|
||||
TypeFactory factory = new TypeFactory(anns);
|
||||
this.typeModelFactory = new APTTypeFactory(env, configuration, factory, anns);
|
||||
this.elementHandler = new ElementHandler(configuration, typeModelFactory);
|
||||
|
||||
}
|
||||
|
||||
private void addSupertypeFields(EntityModel model, Map<String, EntityModel> superTypes) {
|
||||
boolean singleSuperType = model.getSuperTypes().size() == 1;
|
||||
for (String stype : model.getSuperTypes()) {
|
||||
// iterate over supertypes
|
||||
if (superTypes.containsKey(stype)) {
|
||||
Stack<String> stypeStack = new Stack<String>();
|
||||
stypeStack.push(stype);
|
||||
while (!stypeStack.isEmpty()) {
|
||||
String top = stypeStack.pop();
|
||||
if (superTypes.containsKey(top)) {
|
||||
EntityModel sdecl = superTypes.get(top);
|
||||
if (singleSuperType && model.getSuperTypes().contains(top)) {
|
||||
model.setSuperModel(sdecl);
|
||||
}
|
||||
model.include(sdecl);
|
||||
for (String type : sdecl.getSuperTypes()) {
|
||||
stypeStack.push(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create super class model via reflection
|
||||
if (model.getSuperModel() == null && singleSuperType) {
|
||||
String stype = model.getSuperTypes().iterator().next();
|
||||
Class<?> superClass = safeClassForName(stype);
|
||||
if (superClass != null && !superClass.equals(Object.class)) {
|
||||
// handle the supertype only, if it has the proper annotations
|
||||
if ((configuration.getSuperTypeAnn() == null
|
||||
|| superClass.getAnnotation(configuration.getSuperTypeAnn()) != null)
|
||||
|| superClass.getAnnotation(configuration.getEntityAnn()) != null) {
|
||||
EntityModel type = entityModelFactory.create(superClass, configuration.getNamePrefix());
|
||||
// include fields of supertype
|
||||
model.include(type);
|
||||
private void addSupertypeFields(EntityType model, Map<String, EntityType> superTypes) {
|
||||
Stack<EntityType> stypeStack = new Stack<EntityType>();
|
||||
for (EntityType stype : model.getSuperTypes()) {
|
||||
stypeStack.push(stype);
|
||||
while (!stypeStack.isEmpty()) {
|
||||
EntityType sdecl = stypeStack.pop();
|
||||
model.include(sdecl);
|
||||
for (EntityType type : sdecl.getSuperTypes()) {
|
||||
stypeStack.push(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -184,8 +148,8 @@ public class Processor {
|
||||
if (element instanceof PackageElement){
|
||||
QuerydslVariables vars = element.getAnnotation(QuerydslVariables.class);
|
||||
PackageElement packageElement = (PackageElement)element;
|
||||
List<EntityModel> models = new ArrayList<EntityModel>();
|
||||
for (EntityModel model : entityTypes.values()){
|
||||
List<EntityType> models = new ArrayList<EntityType>();
|
||||
for (EntityType model : entityTypes.values()){
|
||||
if (model.getPackageName().equals(packageElement.getQualifiedName().toString())){
|
||||
models.add(model);
|
||||
}
|
||||
@ -213,15 +177,15 @@ public class Processor {
|
||||
|
||||
|
||||
private void handleExtensionType(TypeMirror type, Element element) {
|
||||
EntityModel entityModel = typeFactory.createEntityModel(type);
|
||||
EntityType entityModel = typeModelFactory.createEntityType(type);
|
||||
// handle methods
|
||||
Set<MethodModel> queryMethods = new HashSet<MethodModel>();
|
||||
Set<Method> queryMethods = new HashSet<Method>();
|
||||
for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())){
|
||||
if (method.getAnnotation(QueryMethod.class) != null){
|
||||
entityVisitor.handleQueryMethod(entityModel, method, queryMethods);
|
||||
elementHandler.handleQueryMethod(entityModel, method, queryMethods);
|
||||
}
|
||||
}
|
||||
for (MethodModel method : queryMethods){
|
||||
for (Method method : queryMethods){
|
||||
entityModel.addMethod(method);
|
||||
}
|
||||
extensionTypes.put(entityModel.getFullName(), entityModel);
|
||||
@ -254,7 +218,7 @@ public class Processor {
|
||||
if (parent.getAnnotation(configuration.getEntityAnn()) == null
|
||||
&& parent.getAnnotation(configuration.getEmbeddableAnn()) == null
|
||||
&& !visitedDTOTypes.contains(parent)){
|
||||
EntityModel model = entityVisitor.handleProjectionType((TypeElement)parent);
|
||||
EntityType model = elementHandler.handleProjectionType((TypeElement)parent);
|
||||
dtos.put(model.getFullName(), model);
|
||||
visitedDTOTypes.add(parent);
|
||||
|
||||
@ -264,30 +228,44 @@ public class Processor {
|
||||
|
||||
private void processEmbeddables() {
|
||||
for (Element element : roundEnv.getElementsAnnotatedWith(configuration.getEmbeddableAnn())) {
|
||||
EntityModel model = entityVisitor.handleNormalType((TypeElement) element);
|
||||
EntityType model = elementHandler.handleNormalType((TypeElement) element);
|
||||
embeddables.put(model.getFullName(), model);
|
||||
}
|
||||
allSupertypes.putAll(embeddables);
|
||||
|
||||
// add super type fields
|
||||
for (EntityModel embeddable : embeddables.values()) {
|
||||
for (EntityType embeddable : embeddables.values()) {
|
||||
addSupertypeFields(embeddable, allSupertypes);
|
||||
}
|
||||
}
|
||||
|
||||
private void processEntities() {
|
||||
// populate entity type mappings
|
||||
|
||||
Set<EntityType> superTypes = new HashSet<EntityType>();
|
||||
|
||||
for (Element element : roundEnv.getElementsAnnotatedWith(configuration.getEntityAnn())) {
|
||||
if (configuration.getEmbeddableAnn() == null || element.getAnnotation(configuration.getEmbeddableAnn()) == null){
|
||||
EntityModel model = entityVisitor.handleNormalType((TypeElement) element);
|
||||
entityTypes.put(model.getFullName(), model);
|
||||
EntityType model = elementHandler.handleNormalType((TypeElement) element);
|
||||
entityTypes.put(model.getFullName(), model);
|
||||
if (model.getSuperType() != null){
|
||||
superTypes.add(model.getSuperType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (EntityType superType : superTypes){
|
||||
if (!allSupertypes.containsKey(superType.getFullName())
|
||||
&& !entityTypes.containsKey(superType.getFullName())){
|
||||
TypeElement typeElement = env.getElementUtils().getTypeElement(superType.getFullName());
|
||||
elementHandler.handleNormalType(superType, typeElement);
|
||||
entityTypes.put(superType.getFullName(), superType);
|
||||
}
|
||||
}
|
||||
|
||||
allSupertypes.putAll(entityTypes);
|
||||
|
||||
// add super type fields
|
||||
for (EntityModel entityType : entityTypes.values()) {
|
||||
for (EntityType entityType : entityTypes.values()) {
|
||||
addSupertypeFields(entityType, allSupertypes);
|
||||
}
|
||||
}
|
||||
@ -295,26 +273,17 @@ public class Processor {
|
||||
private void processSupertypes() {
|
||||
for (Element element : roundEnv.getElementsAnnotatedWith(configuration.getSuperTypeAnn())) {
|
||||
if (configuration.getEmbeddableAnn() == null || element.getAnnotation(configuration.getEmbeddableAnn()) == null){
|
||||
EntityModel model = entityVisitor.handleNormalType((TypeElement) element);
|
||||
EntityType model = elementHandler.handleNormalType((TypeElement) element);
|
||||
actualSupertypes.put(model.getFullName(), model);
|
||||
}
|
||||
}
|
||||
// add supertype fields
|
||||
for (EntityModel superType : actualSupertypes.values()) {
|
||||
for (EntityType superType : actualSupertypes.values()) {
|
||||
addSupertypeFields(superType, actualSupertypes);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Class<?> safeClassForName(String stype) {
|
||||
try {
|
||||
return Class.forName(stype);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void serializeVariableList(String packageName, QuerydslVariables vars, List<EntityModel> models){
|
||||
|
||||
private void serializeVariableList(String packageName, QuerydslVariables vars, List<EntityType> models){
|
||||
String className = packageName + "." + vars.value();
|
||||
TypeMappings typeMappings = configuration.getTypeMappings();
|
||||
try{
|
||||
@ -329,7 +298,7 @@ public class Processor {
|
||||
}else{
|
||||
writer.beginClass(vars.value(), null);
|
||||
}
|
||||
for (EntityModel model : models){
|
||||
for (EntityType model : models){
|
||||
String queryType = typeMappings.getPathType(model, model, true);
|
||||
String simpleName = model.getUncapSimpleName();
|
||||
writer.publicStaticFinal(queryType, simpleName, "new " + queryType + "(\"" + simpleName + "\")");
|
||||
@ -345,9 +314,9 @@ public class Processor {
|
||||
|
||||
}
|
||||
|
||||
private void serialize(Serializer serializer, Map<String, EntityModel> models) {
|
||||
private void serialize(Serializer serializer, Map<String, EntityType> models) {
|
||||
Messager msg = env.getMessager();
|
||||
for (EntityModel model : models.values()) {
|
||||
for (EntityType model : models.values()) {
|
||||
msg.printMessage(Kind.NOTE, model.getFullName() + " is processed");
|
||||
try {
|
||||
String packageName = model.getPackageName();
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package com.mysema.query.apt;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QuerydslAnnotationProcessorTest {
|
||||
|
||||
private static final String packagePath = "src/test/java/com/mysema/query/domain/";
|
||||
|
||||
private void process(List<String> classes) throws IOException{
|
||||
File out = new File("target/out");
|
||||
FileUtils.deleteDirectory(out);
|
||||
if (!out.mkdirs()){
|
||||
Assert.fail("Creation of " + out.getPath() + " failed");
|
||||
}
|
||||
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
System.out.println(compiler.getClass().getName());
|
||||
List<String> options = new ArrayList<String>(classes.size() + 3);
|
||||
options.add("-s");
|
||||
options.add("target/out");
|
||||
options.add("-proc:only");
|
||||
options.add("-processor");
|
||||
options.add(QuerydslAnnotationProcessor.class.getName());
|
||||
options.addAll(classes);
|
||||
int compilationResult = compiler.run(null, null, null, options.toArray(new String[options.size()]));
|
||||
if(compilationResult == 0){
|
||||
System.out.println("Compilation is successful");
|
||||
}else{
|
||||
Assert.fail("Compilation Failed");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void processAll() throws IOException{
|
||||
// works only in Eclipse for the moment
|
||||
List<String> classes = new ArrayList<String>();
|
||||
for (File file : new File(packagePath).listFiles()){
|
||||
if (file.getName().endsWith(".java")){
|
||||
classes.add(file.getPath());
|
||||
}
|
||||
}
|
||||
process(classes);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void inheritance2Test() {
|
||||
// process(Collections.singletonList(packagePath + "Inheritance2Test.java"));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// @Ignore
|
||||
// public void inheritanceTest() {
|
||||
// process(Collections.singletonList(packagePath + "InheritanceTest.java"));
|
||||
// }
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -16,12 +16,12 @@ public class Inheritance2Test {
|
||||
Base2 base;
|
||||
Base2<?,?> base2;
|
||||
}
|
||||
|
||||
|
||||
@QueryEntity
|
||||
public abstract class Base2<T extends Base2<T,U>,U extends IFace>{
|
||||
|
||||
}
|
||||
|
||||
|
||||
@QueryEntity
|
||||
public abstract class BaseSub extends Base<BaseSub>{
|
||||
|
||||
|
||||
@ -65,6 +65,7 @@ public class InheritanceTest {
|
||||
|
||||
}
|
||||
|
||||
@QueryEntity
|
||||
public abstract class Storable extends Merchandise{
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
package com.mysema.query.domain;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.annotations.QueryEntity;
|
||||
|
||||
public class Superclass3Test {
|
||||
|
||||
@QueryEntity
|
||||
public static class Subtype extends DefaultQueryMetadata{
|
||||
|
||||
private static final long serialVersionUID = -218949941713252847L;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
Assert.assertNotNull(QSuperclass3Test_Subtype.subtype.distinct);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public interface SimpleProjectable<T> {
|
||||
|
||||
List<T> list();
|
||||
|
||||
List<T> listDistinct();
|
||||
|
||||
T uniqueResult();
|
||||
|
||||
SearchResults<T> listResults();
|
||||
|
||||
SearchResults<T> listDistinctResults();
|
||||
|
||||
long count();
|
||||
|
||||
long countDistinct();
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <Q>
|
||||
*/
|
||||
public interface SimpleQuery<Q extends SimpleQuery<Q>> {
|
||||
|
||||
Q where(EBoolean... e);
|
||||
|
||||
Q limit(long limit);
|
||||
|
||||
Q offset(long offset);
|
||||
|
||||
Q restrict(QueryModifiers modifiers);
|
||||
|
||||
}
|
||||
@ -8,15 +8,15 @@ package com.mysema.query.codegen;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* AbstractTypeModel is an abstract base class for TypeModel implementations
|
||||
* AbstractType is an abstract base class for Type implementations
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractTypeModel implements TypeModel{
|
||||
public abstract class AbstractType implements Type{
|
||||
|
||||
@Override
|
||||
public String getLocalGenericName(TypeModel context, boolean asArgType){
|
||||
public String getLocalGenericName(Type context, boolean asArgType){
|
||||
try {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendLocalGenericName(context, builder, asArgType);
|
||||
@ -27,7 +27,7 @@ public abstract class AbstractTypeModel implements TypeModel{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalRawName(TypeModel context){
|
||||
public String getLocalRawName(Type context){
|
||||
try {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendLocalRawName(context, builder);
|
||||
@ -20,17 +20,17 @@ import org.apache.commons.lang.ClassUtils;
|
||||
import com.mysema.commons.lang.Assert;
|
||||
|
||||
/**
|
||||
* ClassTypeModel is a minimal implementation of the TypeModel interface
|
||||
* ClassType is a minimal implementation of the Type interface
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@Immutable
|
||||
public final class ClassTypeModel extends AbstractTypeModel{
|
||||
public final class ClassType extends AbstractType{
|
||||
|
||||
private final Class<?> clazz;
|
||||
|
||||
private final List<TypeModel> parameters;
|
||||
private final List<Type> parameters;
|
||||
|
||||
@Nullable
|
||||
private final Class<?> primitiveClass;
|
||||
@ -38,12 +38,12 @@ public final class ClassTypeModel extends AbstractTypeModel{
|
||||
private final TypeCategory typeCategory;
|
||||
|
||||
private final boolean visible;
|
||||
|
||||
public ClassTypeModel(TypeCategory typeCategory, Class<?> clazz, TypeModel... params){
|
||||
|
||||
public ClassType(TypeCategory typeCategory, Class<?> clazz, Type... params){
|
||||
this(typeCategory, clazz, ClassUtils.wrapperToPrimitive(clazz), params);
|
||||
}
|
||||
|
||||
public ClassTypeModel(TypeCategory typeCategory, Class<?> clazz, @Nullable Class<?> primitiveClass, TypeModel... params){
|
||||
|
||||
public ClassType(TypeCategory typeCategory, Class<?> clazz, @Nullable Class<?> primitiveClass, Type... params){
|
||||
this.typeCategory = Assert.notNull(typeCategory);
|
||||
this.clazz = Assert.notNull(clazz);
|
||||
this.primitiveClass = primitiveClass;
|
||||
@ -56,23 +56,23 @@ public final class ClassTypeModel extends AbstractTypeModel{
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel asArrayType() {
|
||||
return new ClassTypeModel(TypeCategory.ARRAY, Array.newInstance(clazz, 0).getClass(), this);
|
||||
public Type asArrayType() {
|
||||
return new ClassType(TypeCategory.ARRAY, Array.newInstance(clazz, 0).getClass(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel as(TypeCategory category) {
|
||||
public Type as(TypeCategory category) {
|
||||
if (typeCategory == category){
|
||||
return this;
|
||||
}else{
|
||||
return new ClassTypeModel(category, clazz);
|
||||
return new ClassType(category, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if (o instanceof TypeModel){
|
||||
TypeModel t = (TypeModel)o;
|
||||
if (o instanceof Type){
|
||||
Type t = (Type)o;
|
||||
return clazz.getName().equals(t.getFullName());
|
||||
}else{
|
||||
return false;
|
||||
@ -90,12 +90,12 @@ public final class ClassTypeModel extends AbstractTypeModel{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException {
|
||||
public void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException {
|
||||
appendLocalRawName(context, builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalRawName(TypeModel context, Appendable builder) throws IOException {
|
||||
public void appendLocalRawName(Type context, Appendable builder) throws IOException {
|
||||
String packageName;
|
||||
String name;
|
||||
if (clazz.isArray()){
|
||||
@ -122,7 +122,7 @@ public final class ClassTypeModel extends AbstractTypeModel{
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getParameter(int i) {
|
||||
public Type getParameter(int i) {
|
||||
return parameters.get(i);
|
||||
}
|
||||
|
||||
@ -137,7 +137,7 @@ public final class ClassTypeModel extends AbstractTypeModel{
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getSelfOrValueType() {
|
||||
public Type getSelfOrValueType() {
|
||||
if (typeCategory.isSubCategoryOf(TypeCategory.COLLECTION)
|
||||
|| typeCategory.isSubCategoryOf(TypeCategory.MAP)){
|
||||
return parameters.get(parameters.size()-1);
|
||||
@ -12,17 +12,17 @@ import net.jcip.annotations.Immutable;
|
||||
import com.mysema.commons.lang.Assert;
|
||||
|
||||
/**
|
||||
* ConstructorModel represents a constructor for a DTO query type.
|
||||
* Constructor represents a constructor for a DTO query type.
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
@Immutable
|
||||
public final class ConstructorModel {
|
||||
public final class Constructor {
|
||||
|
||||
private final Collection<ParameterModel> parameters;
|
||||
private final Collection<Parameter> parameters;
|
||||
|
||||
public ConstructorModel(Collection<ParameterModel> params) {
|
||||
public Constructor(Collection<Parameter> params) {
|
||||
parameters = Assert.notNull(params,"params was null");
|
||||
}
|
||||
|
||||
@ -30,14 +30,14 @@ public final class ConstructorModel {
|
||||
public boolean equals(Object o){
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof ConstructorModel){
|
||||
return ((ConstructorModel)o).parameters.equals(parameters);
|
||||
}else if (o instanceof Constructor){
|
||||
return ((Constructor)o).parameters.equals(parameters);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ParameterModel> getParameters() {
|
||||
public Collection<Parameter> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@ -25,12 +25,12 @@ import com.mysema.util.CodeWriter;
|
||||
public final class DTOSerializer implements Serializer{
|
||||
|
||||
private final TypeMappings typeMappings;
|
||||
|
||||
|
||||
public DTOSerializer(TypeMappings typeMappings){
|
||||
this.typeMappings = Assert.notNull(typeMappings);
|
||||
}
|
||||
|
||||
protected void intro(EntityModel model, CodeWriter writer) throws IOException {
|
||||
protected void intro(EntityType model, CodeWriter writer) throws IOException {
|
||||
String simpleName = model.getSimpleName();
|
||||
String queryType = typeMappings.getPathType(model, model, false);
|
||||
String localName = model.getLocalRawName();
|
||||
@ -50,22 +50,22 @@ public final class DTOSerializer implements Serializer{
|
||||
writer.beginClass(queryType, "EConstructor<" + localName + ">");
|
||||
}
|
||||
|
||||
protected void outro(EntityModel model, CodeWriter writer) throws IOException {
|
||||
protected void outro(EntityType model, CodeWriter writer) throws IOException {
|
||||
writer.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final EntityModel model, SerializerConfig serializerConfig, CodeWriter writer) throws IOException{
|
||||
public void serialize(final EntityType model, SerializerConfig serializerConfig, CodeWriter writer) throws IOException{
|
||||
// intro
|
||||
intro(model, writer);
|
||||
|
||||
final String localName = model.getLocalRawName();
|
||||
|
||||
for (ConstructorModel c : model.getConstructors()){
|
||||
for (Constructor c : model.getConstructors()){
|
||||
// begin
|
||||
writer.beginConstructor(c.getParameters(), new Transformer<ParameterModel,String>(){
|
||||
writer.beginConstructor(c.getParameters(), new Transformer<Parameter,String>(){
|
||||
@Override
|
||||
public String transform(ParameterModel p) {
|
||||
public String transform(Parameter p) {
|
||||
return typeMappings.getExprType(p.getType(), model, false, false, true) + " " + p.getName();
|
||||
}
|
||||
});
|
||||
@ -74,7 +74,7 @@ public final class DTOSerializer implements Serializer{
|
||||
writer.beginLine("super(" + localName + ".class");
|
||||
writer.append(", new Class[]{");
|
||||
boolean first = true;
|
||||
for (ParameterModel p : c.getParameters()){
|
||||
for (Parameter p : c.getParameters()){
|
||||
if (!first){
|
||||
writer.append(", ");
|
||||
}
|
||||
@ -88,7 +88,7 @@ public final class DTOSerializer implements Serializer{
|
||||
}
|
||||
writer.append("}");
|
||||
|
||||
for (ParameterModel p : c.getParameters()){
|
||||
for (Parameter p : c.getParameters()){
|
||||
writer.append(", " + p.getName());
|
||||
}
|
||||
|
||||
|
||||
@ -20,28 +20,28 @@ import com.mysema.util.CodeWriter;
|
||||
*
|
||||
*/
|
||||
public final class EmbeddableSerializer extends EntitySerializer{
|
||||
|
||||
|
||||
public EmbeddableSerializer(TypeMappings typeMappings) {
|
||||
super(typeMappings);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityModel model) {
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityType model) {
|
||||
// no root constructors
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityModel model) {
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityType model) {
|
||||
// no default instance
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introFactoryMethods(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introFactoryMethods(CodeWriter writer, EntityType model) throws IOException {
|
||||
// no factory methods
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityModel model) throws IOException {
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
|
||||
writer.imports(Path.class.getPackage());
|
||||
if ((model.hasLists() && config.useListAccessors())
|
||||
|| !model.getMethods().isEmpty()
|
||||
|
||||
@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import net.jcip.annotations.Immutable;
|
||||
|
||||
import com.mysema.query.annotations.QueryInit;
|
||||
import com.mysema.query.annotations.QueryTransient;
|
||||
import com.mysema.query.annotations.QueryType;
|
||||
|
||||
/**
|
||||
* A Reflection based Factory implementation for EntityModel instances
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@Immutable
|
||||
public class EntityModelFactory {
|
||||
|
||||
private final Class<? extends Annotation> skipAnn;
|
||||
|
||||
private final TypeModelFactory typeModelFactory;
|
||||
|
||||
public EntityModelFactory(TypeModelFactory typeModelFactory){
|
||||
this(typeModelFactory, QueryTransient.class);
|
||||
}
|
||||
|
||||
public EntityModelFactory(TypeModelFactory typeModelFactory, Class<? extends Annotation> skipAnn){
|
||||
this.typeModelFactory = typeModelFactory;
|
||||
this.skipAnn = skipAnn;
|
||||
}
|
||||
|
||||
public EntityModel create(Class<?> key, String prefix){
|
||||
Collection<String> superTypes;
|
||||
if (key.isInterface()){
|
||||
superTypes = new ArrayList<String>();
|
||||
for (Class<?> iface : key.getInterfaces()){
|
||||
if (!iface.getName().startsWith("java")){
|
||||
superTypes.add(iface.getName());
|
||||
}
|
||||
}
|
||||
}else{
|
||||
superTypes = Collections.singleton(key.getSuperclass().getName());
|
||||
}
|
||||
EntityModel entityModel = new EntityModel(prefix,
|
||||
new ClassTypeModel(TypeCategory.ENTITY, key),
|
||||
superTypes);
|
||||
for (Field f : key.getDeclaredFields()) {
|
||||
if (isValidField(f)){
|
||||
TypeModel typeModel = typeModelFactory.create(f.getType(), f.getGenericType());
|
||||
if (f.getAnnotation(QueryType.class) != null){
|
||||
TypeCategory typeCategory = TypeCategory.get(f.getAnnotation(QueryType.class).value());
|
||||
if (typeCategory == null){
|
||||
continue;
|
||||
}
|
||||
typeModel = typeModel.as(typeCategory);
|
||||
}
|
||||
String[] inits = new String[0];
|
||||
if (f.getAnnotation(QueryInit.class) != null){
|
||||
inits = f.getAnnotation(QueryInit.class).value();
|
||||
}
|
||||
entityModel.addProperty(new PropertyModel(entityModel, f.getName(), typeModel, inits));
|
||||
}
|
||||
}
|
||||
return entityModel;
|
||||
}
|
||||
|
||||
protected boolean isValidField(Field field) {
|
||||
return field.getAnnotation(skipAnn) == null
|
||||
&& !Modifier.isTransient(field.getModifiers())
|
||||
&& !Modifier.isStatic(field.getModifiers());
|
||||
}
|
||||
|
||||
}
|
||||
@ -42,12 +42,12 @@ public class EntitySerializer implements Serializer{
|
||||
private static final String PATH_METADATA = "PathMetadata<?> metadata";
|
||||
|
||||
private final TypeMappings typeMappings;
|
||||
|
||||
|
||||
public EntitySerializer(TypeMappings mappings){
|
||||
this.typeMappings = Assert.notNull(mappings);
|
||||
}
|
||||
|
||||
protected void constructors(EntityModel model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
protected void constructors(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
String localName = model.getLocalRawName();
|
||||
String genericName = model.getLocalGenericName();
|
||||
|
||||
@ -99,7 +99,7 @@ public class EntitySerializer implements Serializer{
|
||||
|
||||
}
|
||||
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityType model) throws IOException {
|
||||
String localName = model.getLocalRawName();
|
||||
String genericName = model.getLocalGenericName();
|
||||
|
||||
@ -115,7 +115,7 @@ public class EntitySerializer implements Serializer{
|
||||
writer.end();
|
||||
}
|
||||
|
||||
protected void entityAccessor(PropertyModel field, CodeWriter writer) throws IOException {
|
||||
protected void entityAccessor(Property field, CodeWriter writer) throws IOException {
|
||||
String queryType = typeMappings.getPathType(field.getType(), field.getContext(), false);
|
||||
writer.beginMethod(queryType, field.getEscapedName());
|
||||
writer.line("if (", field.getEscapedName(), " == null){");
|
||||
@ -126,7 +126,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
|
||||
protected void entityField(PropertyModel field, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
protected void entityField(Property field, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
String queryType = typeMappings.getPathType(field.getType(), field.getContext(), false);
|
||||
if (field.isInherited()){
|
||||
writer.line("// inherited");
|
||||
@ -139,9 +139,9 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
|
||||
protected boolean hasOwnEntityProperties(EntityModel model){
|
||||
protected boolean hasOwnEntityProperties(EntityType model){
|
||||
if (model.hasEntityFields()){
|
||||
for (PropertyModel property : model.getProperties()){
|
||||
for (Property property : model.getProperties()){
|
||||
if (!property.isInherited() && property.getType().getCategory() == TypeCategory.ENTITY){
|
||||
return true;
|
||||
}
|
||||
@ -150,14 +150,14 @@ public class EntitySerializer implements Serializer{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void initEntityFields(CodeWriter writer, SerializerConfig config, EntityModel model) throws IOException {
|
||||
EntityModel superModel = model.getSuperModel();
|
||||
if (superModel != null && superModel.hasEntityFields()){
|
||||
String superQueryType = typeMappings.getPathType(superModel, model, false);
|
||||
protected void initEntityFields(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
|
||||
EntityType superType = model.getSuperType();
|
||||
if (superType != null && superType.hasEntityFields()){
|
||||
String superQueryType = typeMappings.getPathType(superType, model, false);
|
||||
writer.line("this._super = new " + superQueryType + "(type, metadata, inits);");
|
||||
}
|
||||
|
||||
for (PropertyModel field : model.getProperties()){
|
||||
for (Property field : model.getProperties()){
|
||||
if (field.getType().getCategory() == TypeCategory.ENTITY){
|
||||
String queryType = typeMappings.getPathType(field.getType(), model, false);
|
||||
if (!field.isInherited()){
|
||||
@ -170,13 +170,13 @@ public class EntitySerializer implements Serializer{
|
||||
writer.line("this.", field.getEscapedName(), ASSIGN, "_super.", field.getEscapedName(), SEMICOLON);
|
||||
}
|
||||
|
||||
}else if (field.isInherited() && superModel != null && superModel.hasEntityFields()){
|
||||
}else if (field.isInherited() && superType != null && superType.hasEntityFields()){
|
||||
writer.line("this.", field.getEscapedName(), " = _super.", field.getEscapedName(), SEMICOLON);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void intro(EntityModel model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
protected void intro(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
introPackage(writer, model);
|
||||
introImports(writer, config, model);
|
||||
writer.nl();
|
||||
@ -189,13 +189,13 @@ public class EntitySerializer implements Serializer{
|
||||
if (config.createDefaultVariable()){
|
||||
introDefaultInstance(writer, model);
|
||||
}
|
||||
if (model.getSuperModel() != null){
|
||||
if (model.getSuperType() != null){
|
||||
introSuper(writer, model);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings(UNCHECKED)
|
||||
protected void introClassHeader(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introClassHeader(CodeWriter writer, EntityType model) throws IOException {
|
||||
String queryType = typeMappings.getPathType(model, model, true);
|
||||
String localName = model.getLocalGenericName();
|
||||
|
||||
@ -216,25 +216,25 @@ public class EntitySerializer implements Serializer{
|
||||
writer.beginClass(queryType, pathType.getSimpleName() + "<" + localName + ">");
|
||||
}
|
||||
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityType model) throws IOException {
|
||||
String simpleName = model.getUncapSimpleName();
|
||||
String queryType = typeMappings.getPathType(model, model, true);
|
||||
|
||||
writer.publicStaticFinal(queryType, simpleName, NEW + queryType + "(\"" + simpleName + "\")");
|
||||
}
|
||||
|
||||
protected void introFactoryMethods(CodeWriter writer, final EntityModel model) throws IOException {
|
||||
protected void introFactoryMethods(CodeWriter writer, final EntityType model) throws IOException {
|
||||
String localName = model.getLocalRawName();
|
||||
String genericName = model.getLocalGenericName();
|
||||
|
||||
for (ConstructorModel c : model.getConstructors()){
|
||||
for (Constructor c : model.getConstructors()){
|
||||
// begin
|
||||
if (!localName.equals(genericName)){
|
||||
writer.suppressWarnings(UNCHECKED);
|
||||
}
|
||||
writer.beginStaticMethod("EConstructor<" + genericName + ">", "create", c.getParameters(), new Transformer<ParameterModel, String>(){
|
||||
writer.beginStaticMethod("EConstructor<" + genericName + ">", "create", c.getParameters(), new Transformer<Parameter, String>(){
|
||||
@Override
|
||||
public String transform(ParameterModel p) {
|
||||
public String transform(Parameter p) {
|
||||
return typeMappings.getExprType(p.getType(), model, false, false, true) + SPACE + p.getName();
|
||||
}
|
||||
});
|
||||
@ -247,7 +247,7 @@ public class EntitySerializer implements Serializer{
|
||||
writer.append(localName + DOT_CLASS);
|
||||
writer.append(", new Class[]{");
|
||||
boolean first = true;
|
||||
for (ParameterModel p : c.getParameters()){
|
||||
for (Parameter p : c.getParameters()){
|
||||
if (!first){
|
||||
writer.append(COMMA);
|
||||
}
|
||||
@ -261,7 +261,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
writer.append("}");
|
||||
|
||||
for (ParameterModel p : c.getParameters()){
|
||||
for (Parameter p : c.getParameters()){
|
||||
writer.append(COMMA + p.getName());
|
||||
}
|
||||
|
||||
@ -271,7 +271,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
}
|
||||
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityModel model) throws IOException {
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
|
||||
writer.imports(Path.class.getPackage());
|
||||
writer.staticimports(PathMetadataFactory.class);
|
||||
|
||||
@ -287,10 +287,10 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
}
|
||||
|
||||
protected void introInits(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introInits(CodeWriter writer, EntityType model) throws IOException {
|
||||
if (model.hasEntityFields()){
|
||||
List<String> inits = new ArrayList<String>();
|
||||
for (PropertyModel property : model.getProperties()){
|
||||
for (Property property : model.getProperties()){
|
||||
if (property.getType().getCategory() == TypeCategory.ENTITY){
|
||||
for (String init : property.getInits()){
|
||||
inits.add(property.getEscapedName() + DOT + init);
|
||||
@ -307,29 +307,29 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
}
|
||||
|
||||
protected void introJavadoc(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introJavadoc(CodeWriter writer, EntityType model) throws IOException {
|
||||
String simpleName = model.getSimpleName();
|
||||
String queryType = model.getPrefix() + simpleName;
|
||||
writer.javadoc(queryType + " is a Querydsl query type for " + simpleName);
|
||||
}
|
||||
|
||||
protected void introPackage(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introPackage(CodeWriter writer, EntityType model) throws IOException {
|
||||
writer.packageDecl(model.getPackageName());
|
||||
writer.nl();
|
||||
}
|
||||
|
||||
protected void introSuper(CodeWriter writer, EntityModel model) throws IOException {
|
||||
EntityModel superModel = model.getSuperModel();
|
||||
String superQueryType = typeMappings.getPathType(superModel, model, false);
|
||||
protected void introSuper(CodeWriter writer, EntityType model) throws IOException {
|
||||
EntityType superType = model.getSuperType();
|
||||
String superQueryType = typeMappings.getPathType(superType, model, false);
|
||||
|
||||
if (!superModel.hasEntityFields()){
|
||||
if (!superType.hasEntityFields()){
|
||||
writer.publicFinal(superQueryType, "_super", NEW + superQueryType + "(this)");
|
||||
}else{
|
||||
writer.publicFinal(superQueryType, "_super");
|
||||
}
|
||||
}
|
||||
|
||||
protected void listAccessor(PropertyModel field, CodeWriter writer) throws IOException {
|
||||
protected void listAccessor(Property field, CodeWriter writer) throws IOException {
|
||||
String escapedName = field.getEscapedName();
|
||||
String queryType = typeMappings.getPathType(field.getParameter(0), field.getContext(), false);
|
||||
|
||||
@ -340,7 +340,7 @@ public class EntitySerializer implements Serializer{
|
||||
writer.line(RETURN + escapedName +".get(index);").end();
|
||||
}
|
||||
|
||||
protected void mapAccessor(PropertyModel field, CodeWriter writer) throws IOException {
|
||||
protected void mapAccessor(Property field, CodeWriter writer) throws IOException {
|
||||
String escapedName = field.getEscapedName();
|
||||
String queryType = typeMappings.getPathType(field.getParameter(1), field.getContext(), false);
|
||||
String keyType = field.getParameter(0).getLocalGenericName(field.getContext(), false);
|
||||
@ -353,12 +353,12 @@ public class EntitySerializer implements Serializer{
|
||||
writer.line(RETURN + escapedName + ".get(key);").end();
|
||||
}
|
||||
|
||||
protected void method(final EntityModel model, MethodModel method, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
protected void method(final EntityType model, Method method, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
// header
|
||||
String type = typeMappings.getExprType(method.getReturnType(), model, false, true, false);
|
||||
writer.beginMethod(type, method.getName(), method.getParameters(), new Transformer<ParameterModel,String>(){
|
||||
writer.beginMethod(type, method.getName(), method.getParameters(), new Transformer<Parameter,String>(){
|
||||
@Override
|
||||
public String transform(ParameterModel p) {
|
||||
public String transform(Parameter p) {
|
||||
return typeMappings.getExprType(p.getType(), model, false, false, true) + SPACE + p.getName();
|
||||
}
|
||||
});
|
||||
@ -373,7 +373,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
writer.append(QUOTE + StringEscapeUtils.escapeJava(method.getTemplate()) + QUOTE);
|
||||
writer.append(", this");
|
||||
for (ParameterModel p : method.getParameters()){
|
||||
for (Parameter p : method.getParameters()){
|
||||
writer.append(COMMA + p.getName());
|
||||
}
|
||||
writer.append(");\n");
|
||||
@ -382,11 +382,11 @@ public class EntitySerializer implements Serializer{
|
||||
writer.end();
|
||||
}
|
||||
|
||||
protected void outro(EntityModel model, CodeWriter writer) throws IOException {
|
||||
protected void outro(EntityType model, CodeWriter writer) throws IOException {
|
||||
writer.end();
|
||||
}
|
||||
|
||||
public void serialize(EntityModel model, SerializerConfig config, CodeWriter writer) throws IOException{
|
||||
public void serialize(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException{
|
||||
intro(model, config, writer);
|
||||
|
||||
// properties
|
||||
@ -396,12 +396,12 @@ public class EntitySerializer implements Serializer{
|
||||
constructors(model, config, writer);
|
||||
|
||||
// methods
|
||||
for (MethodModel method : model.getMethods()){
|
||||
for (Method method : model.getMethods()){
|
||||
method(model, method, config, writer);
|
||||
}
|
||||
|
||||
// property accessors
|
||||
for (PropertyModel property : model.getProperties()){
|
||||
for (Property property : model.getProperties()){
|
||||
TypeCategory category = property.getType().getCategory();
|
||||
if (category == TypeCategory.MAP && config.useMapAccessors()){
|
||||
mapAccessor(property, writer);
|
||||
@ -414,12 +414,12 @@ public class EntitySerializer implements Serializer{
|
||||
outro(model, writer);
|
||||
}
|
||||
|
||||
protected void serialize(PropertyModel field, String type, CodeWriter writer, String factoryMethod, String... args) throws IOException{
|
||||
EntityModel superModel = field.getContext().getSuperModel();
|
||||
protected void serialize(Property field, String type, CodeWriter writer, String factoryMethod, String... args) throws IOException{
|
||||
EntityType superType = field.getContext().getSuperType();
|
||||
// construct value
|
||||
StringBuilder value = new StringBuilder();
|
||||
if (field.isInherited() && superModel != null){
|
||||
if (!superModel.hasEntityFields()){
|
||||
if (field.isInherited() && superType != null){
|
||||
if (!superType.hasEntityFields()){
|
||||
value.append("_super." + field.getEscapedName());
|
||||
}
|
||||
}else{
|
||||
@ -441,8 +441,8 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
}
|
||||
|
||||
private void serializeProperties(EntityModel model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
for (PropertyModel property : model.getProperties()){
|
||||
private void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
for (Property property : model.getProperties()){
|
||||
String queryType = typeMappings.getPathType(property.getType(), model, false);
|
||||
String localGenericName = property.getType().getLocalGenericName(model, true);
|
||||
String localRawName = property.getType().getLocalRawName(model);
|
||||
|
||||
@ -8,76 +8,68 @@ package com.mysema.query.codegen;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
|
||||
/**
|
||||
* EntityModel represents a model of a query domain type with properties
|
||||
* EntityType represents a model of a query domain type with properties
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
// TODO : rename this
|
||||
public final class EntityModel extends TypeModelAdapter implements Comparable<EntityModel> {
|
||||
public final class EntityType extends TypeAdapter implements Comparable<EntityType> {
|
||||
|
||||
private final Set<ConstructorModel> constructors = new HashSet<ConstructorModel>();
|
||||
private final Set<Annotation> annotations = new HashSet<Annotation>();
|
||||
|
||||
private final Set<Constructor> constructors = new HashSet<Constructor>();
|
||||
|
||||
// mutable
|
||||
private int escapeSuffix = 1;
|
||||
|
||||
// mutable
|
||||
private boolean hasLists, hasMaps, hasEntityFields;
|
||||
|
||||
private final Set<Annotation> annotations = new HashSet<Annotation>();
|
||||
|
||||
private final Set<MethodModel> methods = new HashSet<MethodModel>();
|
||||
private final Set<Method> methods = new HashSet<Method>();
|
||||
|
||||
private final String prefix;
|
||||
|
||||
private final Set<PropertyModel> properties = new TreeSet<PropertyModel>();
|
||||
private final Set<Property> properties = new TreeSet<Property>();
|
||||
|
||||
// mutable
|
||||
@Nullable
|
||||
private EntityModel superModel;
|
||||
|
||||
private final Collection<String> superTypes;
|
||||
private final Collection<EntityType> superTypes;
|
||||
|
||||
// mutable
|
||||
private String uncapSimpleName;
|
||||
|
||||
public EntityModel(String prefix, TypeModel typeModel) {
|
||||
this(prefix, typeModel, Collections.<String>emptyList());
|
||||
|
||||
public EntityType(String prefix, Type type) {
|
||||
this(prefix, type, new HashSet<EntityType>());
|
||||
}
|
||||
|
||||
public EntityModel(String prefix, TypeModel typeModel, Collection<String> superTypes) {
|
||||
super(typeModel);
|
||||
|
||||
public EntityType(String prefix, Type type, Set<EntityType> superTypes) {
|
||||
super(type);
|
||||
this.prefix = Assert.notNull(prefix);
|
||||
this.uncapSimpleName = StringUtils.uncapitalize(typeModel.getSimpleName());
|
||||
this.uncapSimpleName = StringUtils.uncapitalize(type.getSimpleName());
|
||||
this.superTypes = superTypes;
|
||||
}
|
||||
|
||||
public void addConstructor(ConstructorModel co) {
|
||||
constructors.add(co);
|
||||
}
|
||||
|
||||
public void addAnnotation(Annotation annotation){
|
||||
annotations.add(annotation);
|
||||
}
|
||||
|
||||
public void addMethod(MethodModel method){
|
||||
public void addConstructor(Constructor co) {
|
||||
constructors.add(co);
|
||||
}
|
||||
|
||||
public void addMethod(Method method){
|
||||
methods.add(method);
|
||||
}
|
||||
|
||||
public void addProperty(PropertyModel field) {
|
||||
properties.add(validateField(field));
|
||||
public void addProperty(Property field) {
|
||||
properties.add(validateField(field));
|
||||
switch(field.getType().getCategory()){
|
||||
case MAP:
|
||||
hasMaps = true;
|
||||
@ -90,23 +82,31 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(EntityModel o) {
|
||||
return getTypeModel().getSimpleName().compareTo(o.getTypeModel().getSimpleName());
|
||||
public void addSuperType(EntityType entityType){
|
||||
superTypes.add(entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(EntityType o) {
|
||||
return getType().getSimpleName().compareTo(o.getType().getSimpleName());
|
||||
}
|
||||
|
||||
public Set<Annotation> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public TypeCategory getCategory() {
|
||||
return TypeCategory.ENTITY;
|
||||
}
|
||||
|
||||
public Set<ConstructorModel> getConstructors() {
|
||||
public Set<Constructor> getConstructors() {
|
||||
return constructors;
|
||||
}
|
||||
|
||||
public String getLocalGenericName(){
|
||||
try {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
getTypeModel().appendLocalGenericName(this, builder, false);
|
||||
getType().appendLocalGenericName(this, builder, false);
|
||||
return builder.toString();
|
||||
} catch (IOException e) {
|
||||
throw new CodeGenerationException(e.getMessage(), e);
|
||||
@ -116,35 +116,34 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
public String getLocalRawName() {
|
||||
try {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
getTypeModel().appendLocalRawName(this, builder);
|
||||
getType().appendLocalRawName(this, builder);
|
||||
return builder.toString();
|
||||
} catch (IOException e) {
|
||||
throw new CodeGenerationException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<MethodModel> getMethods(){
|
||||
|
||||
public Set<Method> getMethods(){
|
||||
return methods;
|
||||
}
|
||||
|
||||
|
||||
public TypeCategory getOriginalCategory(){
|
||||
return super.getCategory();
|
||||
}
|
||||
|
||||
|
||||
public String getPrefix(){
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public Set<PropertyModel> getProperties() {
|
||||
public Set<Property> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public EntityModel getSuperModel() {
|
||||
return superModel;
|
||||
|
||||
public EntityType getSuperType(){
|
||||
return superTypes.size() == 1 ? superTypes.iterator().next() : null;
|
||||
}
|
||||
|
||||
public Collection<String> getSuperTypes() {
|
||||
public Collection<EntityType> getSuperTypes() {
|
||||
return superTypes;
|
||||
}
|
||||
|
||||
@ -155,7 +154,7 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
public boolean hasEntityFields() {
|
||||
return hasEntityFields;
|
||||
}
|
||||
|
||||
|
||||
public boolean hasLists() {
|
||||
return hasLists;
|
||||
}
|
||||
@ -163,30 +162,22 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
public boolean hasMaps() {
|
||||
return hasMaps;
|
||||
}
|
||||
|
||||
public Set<Annotation> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public void include(EntityModel clazz) {
|
||||
for (MethodModel method : clazz.methods){
|
||||
public void include(EntityType clazz) {
|
||||
for (Method method : clazz.methods){
|
||||
addMethod(method);
|
||||
}
|
||||
|
||||
for (PropertyModel property : clazz.properties){
|
||||
for (Property property : clazz.properties){
|
||||
if (!property.isInherited()){
|
||||
addProperty(property.createCopy(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setSuperModel(EntityModel superModel) {
|
||||
this.superModel = superModel;
|
||||
}
|
||||
|
||||
private PropertyModel validateField(PropertyModel field) {
|
||||
private Property validateField(Property field) {
|
||||
if (field.getName().equals(this.uncapSimpleName)) {
|
||||
uncapSimpleName = StringUtils.uncapitalize(getTypeModel().getSimpleName())+ (escapeSuffix++);
|
||||
uncapSimpleName = StringUtils.uncapitalize(getType().getSimpleName())+ (escapeSuffix++);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
@ -16,19 +16,19 @@ import com.mysema.commons.lang.Assert;
|
||||
*
|
||||
*/
|
||||
@Immutable
|
||||
public final class MethodModel {
|
||||
public final class Method {
|
||||
|
||||
private final EntityModel context;
|
||||
private final EntityType context;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final List<ParameterModel> parameters;
|
||||
private final List<Parameter> parameters;
|
||||
|
||||
private final TypeModel returnType;
|
||||
private final Type returnType;
|
||||
|
||||
private final String template;
|
||||
|
||||
public MethodModel(EntityModel context, String name, String template, List<ParameterModel> params, TypeModel returnType) {
|
||||
public Method(EntityType context, String name, String template, List<Parameter> params, Type returnType) {
|
||||
this.context = Assert.notNull(context);
|
||||
this.name = Assert.notNull(name);
|
||||
this.template = Assert.notNull(template);
|
||||
@ -40,15 +40,15 @@ public final class MethodModel {
|
||||
public boolean equals(Object o) {
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof MethodModel){
|
||||
MethodModel m = (MethodModel)o;
|
||||
}else if (o instanceof Method){
|
||||
Method m = (Method)o;
|
||||
return m.name.equals(name) && m.parameters.equals(parameters);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public EntityModel getContext() {
|
||||
public EntityType getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
@ -56,11 +56,11 @@ public final class MethodModel {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<ParameterModel> getParameters() {
|
||||
public List<Parameter> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public TypeModel getReturnType() {
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@ -8,19 +8,19 @@ package com.mysema.query.codegen;
|
||||
import net.jcip.annotations.Immutable;
|
||||
|
||||
/**
|
||||
* ParameterModel represents a parameter in a Constructor
|
||||
* Parameter represents a parameter in a Constructor
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
@Immutable
|
||||
public final class ParameterModel {
|
||||
public final class Parameter {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final TypeModel type;
|
||||
private final Type type;
|
||||
|
||||
public ParameterModel(String name, TypeModel type) {
|
||||
public Parameter(String name, Type type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
@ -29,8 +29,8 @@ public final class ParameterModel {
|
||||
public boolean equals(Object o) {
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof ParameterModel){
|
||||
return type.equals(((ParameterModel) o).type);
|
||||
}else if (o instanceof Parameter){
|
||||
return type.equals(((Parameter) o).type);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
@ -40,7 +40,7 @@ public final class ParameterModel {
|
||||
return name;
|
||||
}
|
||||
|
||||
public TypeModel getType(){
|
||||
public Type getType(){
|
||||
return type;
|
||||
}
|
||||
|
||||
@ -13,15 +13,15 @@ import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.util.JavaSyntaxUtils;
|
||||
|
||||
/**
|
||||
* PropertyModel represents a property in a query domain type.
|
||||
* Property represents a property in a query domain type.
|
||||
*
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
@Immutable
|
||||
public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
public final class Property implements Comparable<Property> {
|
||||
|
||||
private final EntityModel context;
|
||||
private final EntityType context;
|
||||
|
||||
private final boolean inherited;
|
||||
|
||||
@ -29,17 +29,23 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
|
||||
private final String name, escapedName;
|
||||
|
||||
private final TypeModel type;
|
||||
private final Type type;
|
||||
|
||||
public PropertyModel(EntityModel context, String name, TypeModel type, String[] inits) {
|
||||
/**
|
||||
* @param context
|
||||
* @param name
|
||||
* @param type
|
||||
* @param inits
|
||||
*/
|
||||
public Property(EntityType context, String name, Type type, String[] inits) {
|
||||
this(context, name, type, inits, false);
|
||||
}
|
||||
|
||||
public PropertyModel(EntityModel context, String name, TypeModel type, String[] inits, boolean inherited) {
|
||||
public Property(EntityType context, String name, Type type, String[] inits, boolean inherited) {
|
||||
this(context, name, JavaSyntaxUtils.isReserved(name) ? (name + "_") : name, type, inits, inherited);
|
||||
}
|
||||
|
||||
public PropertyModel(EntityModel context, String name, String escapedName, TypeModel type, String[] inits, boolean inherited) {
|
||||
|
||||
public Property(EntityType context, String name, String escapedName, Type type, String[] inits, boolean inherited) {
|
||||
this.context = context;
|
||||
this.name = Assert.notNull(name);
|
||||
this.escapedName = escapedName;
|
||||
@ -48,25 +54,25 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
this.inherited = inherited;
|
||||
}
|
||||
|
||||
public int compareTo(PropertyModel o) {
|
||||
public int compareTo(Property o) {
|
||||
return name.compareToIgnoreCase(o.getName());
|
||||
}
|
||||
|
||||
public PropertyModel createCopy(EntityModel model) {
|
||||
return new PropertyModel(model, name, type, inits, model.getSuperModel() != null);
|
||||
public Property createCopy(EntityType model) {
|
||||
return new Property(model, name, type, inits, model.getSuperType() != null);
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof PropertyModel){
|
||||
return name.equals(((PropertyModel) o).name);
|
||||
}else if (o instanceof Property){
|
||||
return name.equals(((Property) o).name);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public EntityModel getContext() {
|
||||
public EntityType getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
@ -82,11 +88,11 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
return name;
|
||||
}
|
||||
|
||||
public TypeModel getParameter(int i) {
|
||||
public Type getParameter(int i) {
|
||||
return type.getParameter(i);
|
||||
}
|
||||
|
||||
public TypeModel getType() {
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import java.io.IOException;
|
||||
import com.mysema.util.CodeWriter;
|
||||
|
||||
/**
|
||||
* Serializer defines a common interface for EntityModel serializers
|
||||
* Serializer defines a common interface for EntityType serializers
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
@ -18,14 +18,14 @@ import com.mysema.util.CodeWriter;
|
||||
public interface Serializer {
|
||||
|
||||
/**
|
||||
* Serialize the given ClassModel
|
||||
* Serialize the given EntityType
|
||||
*
|
||||
* @param type ClassModel to serialize
|
||||
* @param type EntityType to serialize
|
||||
* @param serializerConfig TODO
|
||||
* @param writer serialization target
|
||||
* @throws IOException
|
||||
*/
|
||||
void serialize(EntityModel type, SerializerConfig serializerConfig, CodeWriter writer) throws IOException;
|
||||
void serialize(EntityType type, SerializerConfig serializerConfig, CodeWriter writer) throws IOException;
|
||||
|
||||
|
||||
}
|
||||
@ -13,29 +13,29 @@ import com.mysema.commons.lang.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* SimpleTypeModel represents a java type
|
||||
* SimpleType represents a java type
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@Immutable
|
||||
public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
public final class SimpleType extends AbstractType {
|
||||
|
||||
private final String fullName, packageName, simpleName, localName;
|
||||
|
||||
private final TypeModel[] parameters;
|
||||
private final Type[] parameters;
|
||||
|
||||
private final TypeCategory typeCategory;
|
||||
|
||||
private final boolean visible, finalClass;
|
||||
|
||||
public SimpleTypeModel(
|
||||
public SimpleType(
|
||||
TypeCategory typeCategory,
|
||||
String name,
|
||||
String packageName,
|
||||
String simpleName,
|
||||
boolean finalClass,
|
||||
TypeModel... parameters) {
|
||||
Type... parameters) {
|
||||
this.typeCategory = Assert.notNull(typeCategory,"typeCategory is null");
|
||||
this.fullName = Assert.notNull(name,"name is null");
|
||||
this.packageName = Assert.notNull(packageName,"packageName is null");
|
||||
@ -46,25 +46,25 @@ public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
this.finalClass = finalClass;
|
||||
}
|
||||
|
||||
public SimpleTypeModel as(TypeCategory category) {
|
||||
public SimpleType as(TypeCategory category) {
|
||||
if (typeCategory == category){
|
||||
return this;
|
||||
}else{
|
||||
return new SimpleTypeModel(category, fullName, packageName, simpleName, finalClass, parameters);
|
||||
return new SimpleType(category, fullName, packageName, simpleName, finalClass, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel asArrayType() {
|
||||
return new SimpleTypeModel(TypeCategory.ARRAY, fullName+"[]", packageName, simpleName+"[]", finalClass, this);
|
||||
public Type asArrayType() {
|
||||
return new SimpleType(TypeCategory.ARRAY, fullName+"[]", packageName, simpleName+"[]", finalClass, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if (o == this){
|
||||
return true;
|
||||
}else if (o instanceof TypeModel){
|
||||
TypeModel t = (TypeModel)o;
|
||||
}else if (o instanceof Type){
|
||||
Type t = (Type)o;
|
||||
return fullName.equals(t.getFullName());
|
||||
}else{
|
||||
return false;
|
||||
@ -83,7 +83,7 @@ public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
|
||||
// NOTE: Java serialization aspects mixed into model
|
||||
@Override
|
||||
public void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException {
|
||||
public void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException {
|
||||
appendLocalRawName(context, builder);
|
||||
if (parameters.length > 0){
|
||||
builder.append("<");
|
||||
@ -103,7 +103,7 @@ public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalRawName(TypeModel context, Appendable builder) throws IOException{
|
||||
public void appendLocalRawName(Type context, Appendable builder) throws IOException{
|
||||
if (visible || context.getPackageName().equals(packageName)){
|
||||
builder.append(localName);
|
||||
}else{
|
||||
@ -117,7 +117,7 @@ public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getParameter(int i) {
|
||||
public Type getParameter(int i) {
|
||||
return parameters[i];
|
||||
}
|
||||
|
||||
@ -132,7 +132,7 @@ public final class SimpleTypeModel extends AbstractTypeModel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getSelfOrValueType() {
|
||||
public Type getSelfOrValueType() {
|
||||
if (typeCategory.isSubCategoryOf(TypeCategory.COLLECTION)
|
||||
|| typeCategory.isSubCategoryOf(TypeCategory.MAP)){
|
||||
return parameters[parameters.length - 1];
|
||||
@ -28,22 +28,22 @@ public final class SupertypeSerializer extends EntitySerializer{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityModel model) {
|
||||
protected void constructorsForVariables(CodeWriter writer, EntityType model) {
|
||||
// no constructors for variables
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityModel model) {
|
||||
protected void introDefaultInstance(CodeWriter writer, EntityType model) {
|
||||
// no default instance
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introFactoryMethods(CodeWriter writer, EntityModel model) throws IOException {
|
||||
protected void introFactoryMethods(CodeWriter writer, EntityType model) throws IOException {
|
||||
// no factory methods
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityModel model) throws IOException {
|
||||
protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
|
||||
writer.imports(Path.class.getPackage());
|
||||
if ((model.hasLists() && config.useListAccessors())
|
||||
|| !model.getMethods().isEmpty()
|
||||
|
||||
@ -10,12 +10,12 @@ import java.io.IOException;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* TypeModel represents Java types
|
||||
* Type represents Java types
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public interface TypeModel {
|
||||
public interface Type {
|
||||
|
||||
/**
|
||||
* @param context
|
||||
@ -24,7 +24,7 @@ public interface TypeModel {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException;
|
||||
void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException;
|
||||
|
||||
/**
|
||||
* @param context
|
||||
@ -32,18 +32,18 @@ public interface TypeModel {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
void appendLocalRawName(TypeModel context, Appendable builder) throws IOException;
|
||||
void appendLocalRawName(Type context, Appendable builder) throws IOException;
|
||||
|
||||
/**
|
||||
* @param category
|
||||
* @return
|
||||
*/
|
||||
TypeModel as(TypeCategory category);
|
||||
Type as(TypeCategory category);
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
TypeModel asArrayType();
|
||||
Type asArrayType();
|
||||
|
||||
/**
|
||||
* @return
|
||||
@ -60,13 +60,13 @@ public interface TypeModel {
|
||||
* @param asArgType
|
||||
* @return
|
||||
*/
|
||||
String getLocalGenericName(TypeModel context, boolean asArgType);
|
||||
String getLocalGenericName(Type context, boolean asArgType);
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
String getLocalRawName(TypeModel context);
|
||||
String getLocalRawName(Type context);
|
||||
|
||||
/**
|
||||
* @return
|
||||
@ -78,7 +78,7 @@ public interface TypeModel {
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
TypeModel getParameter(int i);
|
||||
Type getParameter(int i);
|
||||
|
||||
/**
|
||||
* @return
|
||||
@ -94,7 +94,7 @@ public interface TypeModel {
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
TypeModel getSelfOrValueType();
|
||||
Type getSelfOrValueType();
|
||||
|
||||
/**
|
||||
* @return
|
||||
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
|
||||
/**
|
||||
* TypeAdapter is a basic adapter implementation for the Type interface
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class TypeAdapter implements Type{
|
||||
|
||||
private final Type type;
|
||||
|
||||
public TypeAdapter(Type type){
|
||||
this.type = Assert.notNull(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException {
|
||||
type.appendLocalGenericName(context, builder, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalRawName(Type context, Appendable builder) throws IOException {
|
||||
type.appendLocalRawName(context, builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type as(TypeCategory category) {
|
||||
return type.as(category);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type asArrayType() {
|
||||
return type.asArrayType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
return type.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCategory getCategory() {
|
||||
return type.getCategory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return type.getFullName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalGenericName(Type context, boolean asArgType) {
|
||||
return type.getLocalGenericName(context, asArgType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalRawName(Type context) {
|
||||
return type.getLocalRawName(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
return type.getPackageName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getParameter(int i) {
|
||||
return type.getParameter(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterCount() {
|
||||
return type.getParameterCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrimitiveName() {
|
||||
return type.getPrimitiveName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getSelfOrValueType() {
|
||||
return type.getSelfOrValueType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleName() {
|
||||
return type.getSimpleName();
|
||||
}
|
||||
|
||||
protected Type getType(){
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEntityFields() {
|
||||
return type.hasEntityFields();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return type.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinal() {
|
||||
return type.isFinal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitive() {
|
||||
return type.isPrimitive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return type.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -10,33 +10,33 @@ import java.io.IOException;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* TypeExtendsModel is a TypeModel for type variables and wildcard types
|
||||
* TypeExtends is a Type implementation for type variables and wildcard types
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
// TODO : take varName into account
|
||||
public class TypeExtendsModel extends TypeModelAdapter{
|
||||
public class TypeExtends extends TypeAdapter{
|
||||
|
||||
@Nullable
|
||||
private final String varName;
|
||||
|
||||
public TypeExtendsModel(String varName, TypeModel typeModel) {
|
||||
super(typeModel);
|
||||
public TypeExtends(String varName, Type type) {
|
||||
super(type);
|
||||
this.varName = varName;
|
||||
}
|
||||
|
||||
public TypeExtendsModel(TypeModel typeModel) {
|
||||
super(typeModel);
|
||||
public TypeExtends(Type type) {
|
||||
super(type);
|
||||
varName = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException {
|
||||
public void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException {
|
||||
if (!asArgType){
|
||||
builder.append("? extends ");
|
||||
}
|
||||
getTypeModel().appendLocalGenericName(context, builder, true);
|
||||
getType().appendLocalGenericName(context, builder, true);
|
||||
}
|
||||
|
||||
public String getVarName(){
|
||||
@ -7,7 +7,6 @@ package com.mysema.query.codegen;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@ -20,39 +19,39 @@ import org.apache.commons.lang.ClassUtils;
|
||||
import com.mysema.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* TypeModelFactory is a factory class for TypeModel instances
|
||||
* TypeFactory is a factory class for Type instances
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public final class TypeModelFactory {
|
||||
public final class TypeFactory {
|
||||
|
||||
private final Map<List<Type>, TypeModel> cache = new HashMap<List<Type>, TypeModel>();
|
||||
private final Map<List<java.lang.reflect.Type>, Type> cache = new HashMap<List<java.lang.reflect.Type>, Type>();
|
||||
|
||||
private final Collection<Class<? extends Annotation>> entityAnnotations;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public TypeModelFactory(Class<?>... entityAnnotations){
|
||||
public TypeFactory(Class<?>... entityAnnotations){
|
||||
this.entityAnnotations = (List)Arrays.asList(entityAnnotations);
|
||||
}
|
||||
|
||||
public TypeModelFactory(List<Class<? extends Annotation>> entityAnnotations){
|
||||
public TypeFactory(List<Class<? extends Annotation>> entityAnnotations){
|
||||
this.entityAnnotations = entityAnnotations;
|
||||
}
|
||||
|
||||
public TypeModel create(Class<?> cl){
|
||||
public Type create(Class<?> cl){
|
||||
return create(cl, cl);
|
||||
}
|
||||
|
||||
public TypeModel create(Class<?> cl, Type genericType) {
|
||||
List<Type> key = Arrays.<Type> asList(cl, genericType);
|
||||
public Type create(Class<?> cl, java.lang.reflect.Type genericType) {
|
||||
List<java.lang.reflect.Type> key = Arrays.<java.lang.reflect.Type> asList(cl, genericType);
|
||||
if (cache.containsKey(key)) {
|
||||
return cache.get(key);
|
||||
}else{
|
||||
if (cl.isPrimitive()) {
|
||||
cl = ClassUtils.primitiveToWrapper(cl);
|
||||
}
|
||||
TypeModel value;
|
||||
Type value;
|
||||
boolean entity= false;
|
||||
for (Class<? extends Annotation> clazz : entityAnnotations){
|
||||
if (cl.getAnnotation(clazz) != null){
|
||||
@ -61,40 +60,40 @@ public final class TypeModelFactory {
|
||||
}
|
||||
}
|
||||
if (entity){
|
||||
value = new ClassTypeModel(TypeCategory.ENTITY, cl);
|
||||
value = new ClassType(TypeCategory.ENTITY, cl);
|
||||
|
||||
}else if (cl.isArray()) {
|
||||
value = create(cl.getComponentType()).asArrayType();
|
||||
|
||||
} else if (cl.isEnum()) {
|
||||
value = new ClassTypeModel(TypeCategory.SIMPLE, cl);
|
||||
value = new ClassType(TypeCategory.SIMPLE, cl);
|
||||
|
||||
} else if (Map.class.isAssignableFrom(cl)) {
|
||||
TypeModel keyInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
TypeModel valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 1));
|
||||
Type keyInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
Type valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 1));
|
||||
value = createMapType(keyInfo, valueInfo);
|
||||
|
||||
} else if (List.class.isAssignableFrom(cl)) {
|
||||
TypeModel valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
Type valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
value = createListType(valueInfo);
|
||||
|
||||
} else if (Set.class.isAssignableFrom(cl)) {
|
||||
TypeModel valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
Type valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
value = createSetType(valueInfo);
|
||||
|
||||
} else if (Collection.class.isAssignableFrom(cl)) {
|
||||
TypeModel valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
Type valueInfo = create(ReflectionUtils.getTypeParameter(genericType, 0));
|
||||
value = createCollectionType(valueInfo);
|
||||
|
||||
}else if (Number.class.isAssignableFrom(cl) && Comparable.class.isAssignableFrom(cl)){
|
||||
value = new ClassTypeModel(TypeCategory.NUMERIC, cl);
|
||||
value = new ClassType(TypeCategory.NUMERIC, cl);
|
||||
|
||||
} else {
|
||||
TypeCategory typeCategory = TypeCategory.get(cl.getName());
|
||||
if (!typeCategory.isSubCategoryOf(TypeCategory.COMPARABLE) && Comparable.class.isAssignableFrom(cl)){
|
||||
typeCategory = TypeCategory.COMPARABLE;
|
||||
}
|
||||
value = new ClassTypeModel(typeCategory, cl);
|
||||
value = new ClassType(typeCategory, cl);
|
||||
}
|
||||
cache.put(key, value);
|
||||
return value;
|
||||
@ -102,12 +101,12 @@ public final class TypeModelFactory {
|
||||
|
||||
}
|
||||
|
||||
public TypeModel createCollectionType(TypeModel valueType) {
|
||||
public Type createCollectionType(Type valueType) {
|
||||
return createComposite(TypeCategory.COLLECTION, Collection.class, valueType);
|
||||
}
|
||||
|
||||
private TypeModel createComposite(TypeCategory container, Class<?> containerType, TypeModel... parameters) {
|
||||
return new SimpleTypeModel(container,
|
||||
private Type createComposite(TypeCategory container, Class<?> containerType, Type... parameters) {
|
||||
return new SimpleType(container,
|
||||
containerType.getName(),
|
||||
containerType.getPackage().getName(),
|
||||
containerType.getSimpleName(),
|
||||
@ -116,15 +115,15 @@ public final class TypeModelFactory {
|
||||
|
||||
}
|
||||
|
||||
public TypeModel createListType(TypeModel valueType) {
|
||||
public Type createListType(Type valueType) {
|
||||
return createComposite(TypeCategory.LIST, List.class, valueType);
|
||||
}
|
||||
|
||||
public TypeModel createMapType(TypeModel keyType, TypeModel valueType) {
|
||||
public Type createMapType(Type keyType, Type valueType) {
|
||||
return createComposite(TypeCategory.MAP, Map.class, keyType, valueType);
|
||||
}
|
||||
|
||||
public TypeModel createSetType(TypeModel valueType) {
|
||||
public Type createSetType(Type valueType) {
|
||||
return createComposite(TypeCategory.SET, Collection.class, valueType);
|
||||
}
|
||||
|
||||
@ -70,36 +70,36 @@ public class TypeMappings {
|
||||
}
|
||||
|
||||
|
||||
public String getCustomType(TypeModel type, EntityModel model, boolean raw){
|
||||
public String getCustomType(Type type, EntityType model, boolean raw){
|
||||
return getCustomType(type, model, raw, false, false);
|
||||
}
|
||||
|
||||
public String getCustomType(TypeModel type, EntityModel model, boolean raw, boolean rawParameters, boolean extend){
|
||||
public String getCustomType(Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend){
|
||||
return getQueryType(customTypes, type, model, raw, rawParameters, extend);
|
||||
}
|
||||
|
||||
public String getExprType(TypeModel type, EntityModel model, boolean raw){
|
||||
public String getExprType(Type type, EntityType model, boolean raw){
|
||||
return getExprType(type, model, raw, false, false);
|
||||
}
|
||||
|
||||
public String getExprType(TypeModel type, EntityModel model, boolean raw, boolean rawParameters, boolean extend){
|
||||
public String getExprType(Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend){
|
||||
return getQueryType(exprTypes, type, model, raw, rawParameters, extend);
|
||||
}
|
||||
|
||||
public String getPathType(TypeModel type, EntityModel model, boolean raw){
|
||||
public String getPathType(Type type, EntityType model, boolean raw){
|
||||
return getPathType(type, model, raw, false, false);
|
||||
}
|
||||
|
||||
public String getPathType(TypeModel type, EntityModel model, boolean raw, boolean rawParameters, boolean extend){
|
||||
public String getPathType(Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend){
|
||||
return getQueryType(pathTypes, type, model, raw, rawParameters, extend);
|
||||
}
|
||||
|
||||
private String getQueryType(Map<TypeCategory, ? extends Class<?>> types, TypeModel type, EntityModel model, boolean raw, boolean rawParameters, boolean extend){
|
||||
private String getQueryType(Map<TypeCategory, ? extends Class<?>> types, Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend){
|
||||
String typeName = types.get(type.getCategory()).getSimpleName();
|
||||
return getQueryType(type, model, typeName, raw, rawParameters, extend);
|
||||
}
|
||||
|
||||
public String getQueryType(TypeModel type, EntityModel model, String typeName, boolean raw, boolean rawParameters, boolean extend){
|
||||
public String getQueryType(Type type, EntityType model, String typeName, boolean raw, boolean rawParameters, boolean extend){
|
||||
String localName = null;
|
||||
TypeCategory category = type.getCategory();
|
||||
|
||||
|
||||
@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
|
||||
/**
|
||||
* TypeModelAdapter is a basic adapter implementation for the TypeModel interface
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class TypeModelAdapter implements TypeModel{
|
||||
|
||||
private final TypeModel typeModel;
|
||||
|
||||
public TypeModelAdapter(TypeModel typeModel){
|
||||
this.typeModel = Assert.notNull(typeModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException {
|
||||
typeModel.appendLocalGenericName(context, builder, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalRawName(TypeModel context, Appendable builder) throws IOException {
|
||||
typeModel.appendLocalRawName(context, builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel as(TypeCategory category) {
|
||||
return typeModel.as(category);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel asArrayType() {
|
||||
return typeModel.asArrayType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
return typeModel.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCategory getCategory() {
|
||||
return typeModel.getCategory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return typeModel.getFullName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalGenericName(TypeModel context, boolean asArgType) {
|
||||
return typeModel.getLocalGenericName(context, asArgType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalRawName(TypeModel context) {
|
||||
return typeModel.getLocalRawName(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
return typeModel.getPackageName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getParameter(int i) {
|
||||
return typeModel.getParameter(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterCount() {
|
||||
return typeModel.getParameterCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrimitiveName() {
|
||||
return typeModel.getPrimitiveName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getSelfOrValueType() {
|
||||
return typeModel.getSelfOrValueType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleName() {
|
||||
return typeModel.getSimpleName();
|
||||
}
|
||||
|
||||
protected TypeModel getTypeModel(){
|
||||
return typeModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEntityFields() {
|
||||
return typeModel.hasEntityFields();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return typeModel.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinal() {
|
||||
return typeModel.isFinal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitive() {
|
||||
return typeModel.isPrimitive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return typeModel.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public final class TypeModels {
|
||||
|
||||
private TypeModels(){}
|
||||
|
||||
public static final TypeModel OBJECTS = new ClassTypeModel(TypeCategory.SIMPLE, Object[].class);
|
||||
|
||||
public static final TypeModel OBJECT = new ClassTypeModel(TypeCategory.SIMPLE, Object.class);
|
||||
|
||||
public static final TypeModel BOOLEAN = new ClassTypeModel(TypeCategory.BOOLEAN, Boolean.class, boolean.class);
|
||||
|
||||
public static final TypeModel BYTE = new ClassTypeModel(TypeCategory.NUMERIC, Byte.class, byte.class);
|
||||
|
||||
public static final TypeModel CHAR = new ClassTypeModel(TypeCategory.COMPARABLE, Character.class, char.class);
|
||||
|
||||
public static final TypeModel DOUBLE = new ClassTypeModel(TypeCategory.NUMERIC, Double.class, double.class);
|
||||
|
||||
public static final TypeModel FLOAT = new ClassTypeModel(TypeCategory.NUMERIC, Float.class, float.class);
|
||||
|
||||
public static final TypeModel INT = new ClassTypeModel(TypeCategory.NUMERIC, Integer.class, int.class);
|
||||
|
||||
public static final TypeModel LONG = new ClassTypeModel(TypeCategory.NUMERIC, Long.class, long.class);
|
||||
|
||||
public static final TypeModel SHORT = new ClassTypeModel(TypeCategory.NUMERIC, Short.class, short.class);
|
||||
|
||||
public static final TypeModel STRING = new ClassTypeModel(TypeCategory.STRING, String.class);
|
||||
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* TypeSuper is a Type for type variables and wildcard types
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
// TODO : take varName into account
|
||||
public class TypeSuper extends TypeAdapter{
|
||||
|
||||
private final Type superType;
|
||||
|
||||
@Nullable
|
||||
private final String varName;
|
||||
|
||||
public TypeSuper(String varName, Type type) {
|
||||
super(Types.OBJECT);
|
||||
this.superType = type;
|
||||
this.varName = varName;
|
||||
}
|
||||
|
||||
public TypeSuper(Type type) {
|
||||
super(Types.OBJECT);
|
||||
this.superType = type;
|
||||
this.varName = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(Type context, Appendable builder, boolean asArgType) throws IOException {
|
||||
if (!asArgType){
|
||||
builder.append("? super ");
|
||||
superType.appendLocalGenericName(context, builder, true);
|
||||
}else{
|
||||
super.appendLocalGenericName(context, builder, asArgType);
|
||||
}
|
||||
}
|
||||
|
||||
public String getVarName(){
|
||||
return varName;
|
||||
}
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* TypeSuperModel is a TypeModel for type variables and wildcard types
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
// TODO : take varName into account
|
||||
public class TypeSuperModel extends TypeModelAdapter{
|
||||
|
||||
private static final TypeModel objectModel = new ClassTypeModel(TypeCategory.SIMPLE, Object.class);
|
||||
|
||||
private final TypeModel superModel;
|
||||
|
||||
@Nullable
|
||||
private final String varName;
|
||||
|
||||
public TypeSuperModel(String varName, TypeModel typeModel) {
|
||||
super(objectModel);
|
||||
this.superModel = typeModel;
|
||||
this.varName = varName;
|
||||
}
|
||||
|
||||
public TypeSuperModel(TypeModel typeModel) {
|
||||
super(objectModel);
|
||||
this.superModel = typeModel;
|
||||
this.varName = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLocalGenericName(TypeModel context, Appendable builder, boolean asArgType) throws IOException {
|
||||
if (!asArgType){
|
||||
builder.append("? super ");
|
||||
superModel.appendLocalGenericName(context, builder, true);
|
||||
}else{
|
||||
super.appendLocalGenericName(context, builder, asArgType);
|
||||
}
|
||||
}
|
||||
|
||||
public String getVarName(){
|
||||
return varName;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public final class Types {
|
||||
|
||||
private Types(){}
|
||||
|
||||
public static final Type OBJECTS = new ClassType(TypeCategory.SIMPLE, Object[].class);
|
||||
|
||||
public static final Type OBJECT = new ClassType(TypeCategory.SIMPLE, Object.class);
|
||||
|
||||
public static final Type BOOLEAN = new ClassType(TypeCategory.BOOLEAN, Boolean.class, boolean.class);
|
||||
|
||||
public static final Type BYTE = new ClassType(TypeCategory.NUMERIC, Byte.class, byte.class);
|
||||
|
||||
public static final Type CHAR = new ClassType(TypeCategory.COMPARABLE, Character.class, char.class);
|
||||
|
||||
public static final Type DOUBLE = new ClassType(TypeCategory.NUMERIC, Double.class, double.class);
|
||||
|
||||
public static final Type FLOAT = new ClassType(TypeCategory.NUMERIC, Float.class, float.class);
|
||||
|
||||
public static final Type INT = new ClassType(TypeCategory.NUMERIC, Integer.class, int.class);
|
||||
|
||||
public static final Type LONG = new ClassType(TypeCategory.NUMERIC, Long.class, long.class);
|
||||
|
||||
public static final Type SHORT = new ClassType(TypeCategory.NUMERIC, Short.class, short.class);
|
||||
|
||||
public static final Type STRING = new ClassType(TypeCategory.STRING, String.class);
|
||||
|
||||
}
|
||||
@ -14,7 +14,7 @@ import org.junit.Test;
|
||||
import com.mysema.query.annotations.QueryExtensions;
|
||||
import com.mysema.util.JavaWriter;
|
||||
|
||||
public class EntityModelTest {
|
||||
public class EntityTypeTest {
|
||||
|
||||
public static class QueryExt implements QueryExtensions{
|
||||
|
||||
@ -37,8 +37,8 @@ public class EntityModelTest {
|
||||
@Test
|
||||
public void annotation() throws IOException{
|
||||
Annotation annotation = new QueryExt(Object.class);
|
||||
ClassTypeModel typeModel = new ClassTypeModel(TypeCategory.ENTITY, Object.class);
|
||||
EntityModel entityModel = new EntityModel("Q", typeModel);
|
||||
ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class);
|
||||
EntityType entityModel = new EntityType("Q", typeModel);
|
||||
entityModel.addAnnotation(annotation);
|
||||
|
||||
TypeMappings typeMappings = new TypeMappings();
|
||||
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ParameterModelTest {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
ParameterModel param1 = new ParameterModel("test", new ClassTypeModel(TypeCategory.STRING, String.class));
|
||||
ParameterModel param2 = new ParameterModel("test2", new ClassTypeModel(TypeCategory.STRING, String.class));
|
||||
ParameterModel param3 = new ParameterModel("test2", new ClassTypeModel(TypeCategory.NUMERIC, Integer.class));
|
||||
|
||||
assertTrue(param1.equals(param2));
|
||||
assertFalse(param1.equals(param3));
|
||||
assertFalse(param2.equals(param3));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ParameterTest {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
Parameter param1 = new Parameter("test", new ClassType(TypeCategory.STRING, String.class));
|
||||
Parameter param2 = new Parameter("test2", new ClassType(TypeCategory.STRING, String.class));
|
||||
Parameter param3 = new Parameter("test2", new ClassType(TypeCategory.NUMERIC, Integer.class));
|
||||
|
||||
assertTrue(param1.equals(param2));
|
||||
assertFalse(param1.equals(param3));
|
||||
assertFalse(param2.equals(param3));
|
||||
}
|
||||
}
|
||||
@ -21,19 +21,19 @@ import com.mysema.util.JavaWriter;
|
||||
*/
|
||||
public class SerializerTest {
|
||||
|
||||
private EntityModel type;
|
||||
private EntityType type;
|
||||
|
||||
private Writer writer = new StringWriter();
|
||||
|
||||
public SerializerTest() {
|
||||
TypeModelFactory typeFactory = new TypeModelFactory();
|
||||
TypeModel typeModel = new SimpleTypeModel(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false);
|
||||
type = new EntityModel("Q", typeModel, Collections.singleton("com.mysema.query.DomainSuperClass"));
|
||||
TypeFactory typeFactory = new TypeFactory();
|
||||
Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false);
|
||||
type = new EntityType("Q", typeModel);
|
||||
|
||||
PropertyModel field = new PropertyModel(type, "field", typeFactory.create(String.class), new String[0]);
|
||||
Property field = new Property(type, "field", typeFactory.create(String.class), new String[0]);
|
||||
type.addProperty(field);
|
||||
ParameterModel param = new ParameterModel("name", new ClassTypeModel(TypeCategory.STRING, String.class));
|
||||
type.addConstructor(new ConstructorModel(Collections.singleton(param)));
|
||||
Parameter param = new Parameter("name", new ClassType(TypeCategory.STRING, String.class));
|
||||
type.addConstructor(new Constructor(Collections.singleton(param)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -17,19 +17,19 @@ import org.junit.Test;
|
||||
|
||||
|
||||
|
||||
public class TypeModelFactoryTest {
|
||||
public class TypeFactoryTest {
|
||||
|
||||
private TypeModelFactory factory = new TypeModelFactory();
|
||||
private TypeFactory factory = new TypeFactory();
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
TypeModel blob = factory.create(Blob.class);
|
||||
Type blob = factory.create(Blob.class);
|
||||
// assertEquals("Blob", blob.getLocalName());
|
||||
assertEquals("Blob", blob.getSimpleName());
|
||||
assertEquals("java.sql.Blob", blob.getFullName());
|
||||
assertEquals("java.sql", blob.getPackageName());
|
||||
|
||||
TypeModel bo = factory.create(boolean.class);
|
||||
Type bo = factory.create(boolean.class);
|
||||
// assertEquals("Boolean", bo.getLocalName());
|
||||
assertEquals("Boolean", bo.getSimpleName());
|
||||
assertEquals("java.lang.Boolean", bo.getFullName());
|
||||
@ -9,11 +9,11 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TypeModelTest {
|
||||
public class TypeTest {
|
||||
|
||||
@Test
|
||||
public void arrayType(){
|
||||
assertEquals("Object[]",TypeModels.OBJECTS.getLocalRawName(TypeModels.OBJECT));
|
||||
assertEquals("Object[]",Types.OBJECTS.getLocalRawName(Types.OBJECT));
|
||||
}
|
||||
|
||||
}
|
||||
@ -21,20 +21,20 @@ import javax.annotation.Nullable;
|
||||
import net.jcip.annotations.Immutable;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.query.codegen.ClassTypeModel;
|
||||
import com.mysema.query.codegen.EntityModel;
|
||||
import com.mysema.query.codegen.ClassType;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.EntitySerializer;
|
||||
import com.mysema.query.codegen.MethodModel;
|
||||
import com.mysema.query.codegen.ParameterModel;
|
||||
import com.mysema.query.codegen.PropertyModel;
|
||||
import com.mysema.query.codegen.Method;
|
||||
import com.mysema.query.codegen.Parameter;
|
||||
import com.mysema.query.codegen.Property;
|
||||
import com.mysema.query.codegen.Serializer;
|
||||
import com.mysema.query.codegen.SerializerConfig;
|
||||
import com.mysema.query.codegen.SimpleSerializerConfig;
|
||||
import com.mysema.query.codegen.SimpleTypeModel;
|
||||
import com.mysema.query.codegen.SimpleType;
|
||||
import com.mysema.query.codegen.TypeCategory;
|
||||
import com.mysema.query.codegen.TypeMappings;
|
||||
import com.mysema.query.codegen.TypeModel;
|
||||
import com.mysema.query.codegen.TypeModels;
|
||||
import com.mysema.query.codegen.Type;
|
||||
import com.mysema.query.codegen.Types;
|
||||
import com.mysema.util.JavaWriter;
|
||||
|
||||
/**
|
||||
@ -87,15 +87,15 @@ public class MetaDataExporter {
|
||||
while (tables.next()) {
|
||||
String tableName = tables.getString(3);
|
||||
String simpleClassName = toClassName(tableName);
|
||||
TypeModel classTypeModel = new SimpleTypeModel(
|
||||
Type classTypeModel = new SimpleType(
|
||||
TypeCategory.ENTITY,
|
||||
packageName + "." + namePrefix + simpleClassName,
|
||||
packageName,
|
||||
namePrefix + simpleClassName,
|
||||
false);
|
||||
EntityModel classModel = new EntityModel("", classTypeModel);
|
||||
MethodModel wildcard = new MethodModel(classModel, "all", "{0}.*",
|
||||
Collections.<ParameterModel>emptyList(), TypeModels.OBJECTS);
|
||||
EntityType classModel = new EntityType("", classTypeModel);
|
||||
Method wildcard = new Method(classModel, "all", "{0}.*",
|
||||
Collections.<Parameter>emptyList(), Types.OBJECTS);
|
||||
classModel.addMethod(wildcard);
|
||||
classModel.addAnnotation(new TableImpl(tableName));
|
||||
ResultSet columns = md.getColumns(null, schemaPattern, tables.getString(3), null);
|
||||
@ -118,8 +118,8 @@ public class MetaDataExporter {
|
||||
fieldType = TypeCategory.COMPARABLE;
|
||||
}
|
||||
|
||||
TypeModel typeModel = new ClassTypeModel(fieldType, clazz);
|
||||
classModel.addProperty(new PropertyModel(
|
||||
Type typeModel = new ClassType(fieldType, clazz);
|
||||
classModel.addProperty(new Property(
|
||||
classModel,
|
||||
columnName,
|
||||
propertyName,
|
||||
@ -158,7 +158,7 @@ public class MetaDataExporter {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void serialize(EntityModel type) {
|
||||
private void serialize(EntityType type) {
|
||||
String path = packageName.replace('.', '/') + "/" + type.getSimpleName() + ".java";
|
||||
try {
|
||||
Writer writer = writerFor(new File(targetFolder, path));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user