mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
added better support for generic signature of domain types
This commit is contained in:
parent
8a8d9acbf9
commit
fb413b106c
@ -66,6 +66,7 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
if (cache.containsKey(key)){
|
||||
return cache.get(key);
|
||||
}else{
|
||||
cache.put(key, null);
|
||||
TypeModel value = type.accept(this, el);
|
||||
cache.put(key, value);
|
||||
return value;
|
||||
@ -92,9 +93,9 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
if (t.asElement() != null && t.asElement() instanceof TypeElement){
|
||||
TypeElement typeElement = (TypeElement)t.asElement();
|
||||
switch(typeElement.getKind()){
|
||||
case CLASS: return createClassType(typeElement, p);
|
||||
case CLASS: return createClassType(t, typeElement, p);
|
||||
case INTERFACE: return createInterfaceType(t, typeElement, p);
|
||||
case ENUM: return create(typeElement, TypeCategory.SIMPLE, p);
|
||||
case ENUM: return create(typeElement, TypeCategory.SIMPLE, p, t.getTypeArguments());
|
||||
}
|
||||
}else{
|
||||
throw new IllegalArgumentException("Unsupported element type " + t.asElement());
|
||||
@ -106,7 +107,7 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
// entity type
|
||||
for (Class<? extends Annotation> entityAnn : entityAnnotations){
|
||||
if (typeElement.getAnnotation(entityAnn) != null){
|
||||
return create(typeElement, TypeCategory.ENTITY, p);
|
||||
return create(typeElement, TypeCategory.ENTITY, p, t.getTypeArguments());
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,7 +116,7 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
Iterator<? extends TypeMirror> i = t.getTypeArguments().iterator();
|
||||
Class<?> cl = TypeUtil.safeForName(name);
|
||||
if (cl == null) { // class not available
|
||||
return create(typeElement, TypeCategory.get(name), p);
|
||||
return create(typeElement, TypeCategory.get(name), p, t.getTypeArguments());
|
||||
|
||||
}else if (Map.class.isAssignableFrom(cl)){
|
||||
if (!i.hasNext()){
|
||||
@ -136,15 +137,15 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
return factory.createCollectionType(create(i.next(), p));
|
||||
|
||||
}else{
|
||||
return create(typeElement, TypeCategory.get(name), p);
|
||||
return create(typeElement, TypeCategory.get(name), p, t.getTypeArguments());
|
||||
}
|
||||
}
|
||||
|
||||
private TypeModel createClassType(TypeElement typeElement, Elements p) {
|
||||
private TypeModel createClassType(DeclaredType t, TypeElement typeElement, Elements p) {
|
||||
// entity type
|
||||
for (Class<? extends Annotation> entityAnn : entityAnnotations){
|
||||
if (typeElement.getAnnotation(entityAnn) != null){
|
||||
return create(typeElement, TypeCategory.ENTITY, p);
|
||||
return create(typeElement, TypeCategory.ENTITY, p, t.getTypeArguments());
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,7 +162,7 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
&& isAssignable(typeElement, comparableType)){
|
||||
typeCategory = TypeCategory.COMPARABLE;
|
||||
}
|
||||
return create(typeElement, typeCategory, p);
|
||||
return create(typeElement, typeCategory, p, t.getTypeArguments());
|
||||
}
|
||||
|
||||
private boolean isSubType(TypeElement type1, TypeElement type2) {
|
||||
@ -175,11 +176,15 @@ public class APTModelFactory implements TypeVisitor<TypeModel,Elements> {
|
||||
}
|
||||
|
||||
|
||||
private TypeModel create(TypeElement typeElement, TypeCategory category, Elements p) {
|
||||
private TypeModel create(TypeElement typeElement, TypeCategory category, Elements p, List<? extends TypeMirror> typeArgs) {
|
||||
String name = typeElement.getQualifiedName().toString();
|
||||
String simpleName = typeElement.getSimpleName().toString();
|
||||
String packageName = p.getPackageOf(typeElement).getQualifiedName().toString();
|
||||
return new SimpleTypeModel(category, name, packageName, simpleName, null, null);
|
||||
TypeModel[] params = new TypeModel[typeArgs.size()];
|
||||
for (int i = 0; i < params.length; i++){
|
||||
params[i] = create(typeArgs.get(i), p);
|
||||
}
|
||||
return new SimpleTypeModel(category, name, packageName, simpleName, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
package com.mysema.query.apt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
@ -48,9 +47,7 @@ public final class DTOElementVisitor extends SimpleElementVisitor6<BeanModel, Vo
|
||||
public BeanModel visitType(TypeElement e, Void p) {
|
||||
Elements elementUtils = env.getElementUtils();
|
||||
TypeModel c = typeFactory.create(e.asType(), elementUtils);
|
||||
BeanModel classModel = new BeanModel(
|
||||
configuration.getNamePrefix(),
|
||||
c.getPackageName(), c.getName(), c.getSimpleName(), Collections.<String>emptySet());
|
||||
BeanModel classModel = new BeanModel(configuration.getNamePrefix(), c);
|
||||
List<? extends Element> elements = e.getEnclosedElements();
|
||||
|
||||
// CONSTRUCTOR
|
||||
|
||||
@ -72,9 +72,7 @@ public final class EntityElementVisitor extends SimpleElementVisitor6<BeanModel,
|
||||
}
|
||||
}
|
||||
TypeModel c = typeFactory.create(e.asType(), elementUtils);
|
||||
BeanModel classModel = new BeanModel(configuration.getNamePrefix(),
|
||||
c.getPackageName(), c.getName(), c.getSimpleName(),
|
||||
superTypes);
|
||||
BeanModel classModel = new BeanModel(configuration.getNamePrefix(), c, superTypes);
|
||||
List<? extends Element> elements = e.getEnclosedElements();
|
||||
|
||||
// CONSTRUCTORS
|
||||
|
||||
@ -7,21 +7,21 @@ import org.junit.Test;
|
||||
import com.mysema.query.annotations.QueryEntity;
|
||||
import com.mysema.query.annotations.QuerySupertype;
|
||||
|
||||
public class EntityTest {
|
||||
public class EntityTest extends AbstractTest{
|
||||
|
||||
@QueryEntity
|
||||
public static class Entity1 {
|
||||
String entity1Field;
|
||||
public String entity1Field;
|
||||
}
|
||||
|
||||
@QueryEntity
|
||||
public static class Entity2 extends Supertype{
|
||||
String entity2Field;
|
||||
public String entity2Field;
|
||||
}
|
||||
|
||||
@QueryEntity
|
||||
public static class Entity3 extends Entity2{
|
||||
String entity3Field;
|
||||
public String entity3Field;
|
||||
}
|
||||
|
||||
@QueryEntity
|
||||
@ -31,7 +31,7 @@ public class EntityTest {
|
||||
|
||||
@QuerySupertype
|
||||
public static class Supertype {
|
||||
String supertypeField;
|
||||
public String supertypeField;
|
||||
}
|
||||
|
||||
@QuerySupertype
|
||||
@ -39,10 +39,9 @@ public class EntityTest {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void inheritance(){
|
||||
assertTrue(QEntity2.entity2 instanceof QSupertype);
|
||||
assertTrue(QEntity3.entity3 instanceof QSupertype);
|
||||
assertTrue(QSupertype.class.isAssignableFrom(QEntity2.class));
|
||||
assertTrue(QSupertype.class.isAssignableFrom(QEntity3.class));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.mysema.query.domain;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.joda.time.DateMidnight;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Instant;
|
||||
@ -17,7 +15,7 @@ import com.mysema.query.types.path.PDate;
|
||||
import com.mysema.query.types.path.PDateTime;
|
||||
import com.mysema.query.types.path.PTime;
|
||||
|
||||
public class JodaTimeSupportTest {
|
||||
public class JodaTimeSupportTest extends AbstractTest{
|
||||
|
||||
@QueryEntity
|
||||
public static class JodaTimeSupport {
|
||||
@ -38,16 +36,15 @@ public class JodaTimeSupportTest {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test() {
|
||||
QJodaTimeSupport i = QJodaTimeSupport.jodaTimeSupport;
|
||||
assertTrue(i.dateMidnight instanceof PDateTime);
|
||||
assertTrue(i.dateTime instanceof PDateTime);
|
||||
assertTrue(i.instant instanceof PDateTime);
|
||||
assertTrue(i.localDate instanceof PDate);
|
||||
assertTrue(i.localDateTime instanceof PDateTime);
|
||||
assertTrue(i.localTime instanceof PTime);
|
||||
assertTrue(i.partial instanceof PComparable);
|
||||
public void test() throws SecurityException, NoSuchFieldException {
|
||||
cl = QJodaTimeSupport.class;
|
||||
match(PDateTime.class, "dateMidnight");
|
||||
match(PDateTime.class, "dateTime");
|
||||
match(PDateTime.class, "instant");
|
||||
match(PDate.class, "localDate");
|
||||
match(PDateTime.class, "localDateTime");
|
||||
match(PTime.class, "localTime");
|
||||
match(PComparable.class, "partial");
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,8 +10,15 @@ import org.junit.Test;
|
||||
|
||||
import com.mysema.query.annotations.QueryEntity;
|
||||
import com.mysema.query.domain.rel.RelationType2;
|
||||
import com.mysema.query.types.path.PComponentCollection;
|
||||
import com.mysema.query.types.path.PComponentList;
|
||||
import com.mysema.query.types.path.PComponentMap;
|
||||
import com.mysema.query.types.path.PEntityCollection;
|
||||
import com.mysema.query.types.path.PEntityList;
|
||||
import com.mysema.query.types.path.PEntityMap;
|
||||
import com.mysema.query.types.path.PSimple;
|
||||
|
||||
public class RelationTest {
|
||||
public class RelationTest extends AbstractTest{
|
||||
|
||||
public enum MyEnum {
|
||||
VAR1, VAR2
|
||||
@ -70,8 +77,43 @@ public class RelationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
// TODO
|
||||
public void test() throws SecurityException, NoSuchFieldException{
|
||||
cl = QRelationType.class;
|
||||
match(PSimple.class, "enumProperty");
|
||||
match(PComponentList.class, "enumList");
|
||||
match(PComponentMap.class, "enumMap1");
|
||||
match(PComponentMap.class, "enumMap");
|
||||
|
||||
match(PEntityList.class, "list");
|
||||
match(PEntityList.class, "list2");
|
||||
match(PComponentList.class, "list3");
|
||||
match(PEntityList.class, "list4");
|
||||
match(PEntityList.class, "list5");
|
||||
|
||||
match(PEntityCollection.class, "set");
|
||||
match(PEntityCollection.class, "sortedSet");
|
||||
match(PComponentCollection.class, "set2");
|
||||
match(PEntityCollection.class, "set3");
|
||||
match(PEntityCollection.class, "set4");
|
||||
|
||||
match(PComponentList.class, "listOfObjects");
|
||||
match(PComponentCollection.class, "setOfObjects");
|
||||
match(PEntityCollection.class, "setOfObjects2");
|
||||
|
||||
match(PEntityCollection.class, "collection");
|
||||
match(PEntityCollection.class, "collection2");
|
||||
match(PComponentCollection.class, "collection3");
|
||||
match(PEntityCollection.class, "collection4");
|
||||
|
||||
match(PEntityMap.class, "map");
|
||||
match(PEntityMap.class, "map2");
|
||||
match(PComponentMap.class, "map3");
|
||||
match(PEntityMap.class, "map4");
|
||||
match(PEntityMap.class, "map5");
|
||||
match(PComponentMap.class, "map6");
|
||||
match(PEntityMap.class, "map7");
|
||||
match(PEntityMap.class, "map8");
|
||||
match(PComponentMap.class, "map9");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package com.mysema.query.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -69,6 +70,7 @@ public class SimpleTypesTest extends AbstractTest{
|
||||
@QueryEntity
|
||||
public static class SimpleTypes {
|
||||
transient int test;
|
||||
Calendar calendar;
|
||||
long id;
|
||||
BigDecimal bigDecimal;
|
||||
Byte bbyte;
|
||||
@ -124,6 +126,7 @@ public class SimpleTypesTest extends AbstractTest{
|
||||
match(PString.class, "sstring");
|
||||
|
||||
match(PDateTime.class, "date");
|
||||
match(PDateTime.class, "calendar");
|
||||
match(PDateTime.class, "timestamp");
|
||||
|
||||
match(PTime.class, "time");
|
||||
|
||||
@ -5,6 +5,6 @@ import com.mysema.query.annotations.QuerySupertype;
|
||||
@QuerySupertype
|
||||
public class SSupertype {
|
||||
|
||||
String supertypeField;
|
||||
public String supertypeField;
|
||||
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
@ -28,19 +29,19 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
|
||||
private final Collection<ConstructorModel> constructors = new HashSet<ConstructorModel>();
|
||||
|
||||
private BeanModel superModel;
|
||||
private boolean entityModel = true;
|
||||
|
||||
// mutable
|
||||
private int escapeSuffix = 1;
|
||||
|
||||
private boolean entityModel = true;
|
||||
|
||||
private final String simpleName, name, packageName, localName;
|
||||
|
||||
private final String prefix;
|
||||
|
||||
private BeanModel superModel;
|
||||
|
||||
private final Collection<String> superTypes;
|
||||
|
||||
private final TypeModel typeModel;
|
||||
|
||||
private final Map<TypeCategory,Collection<PropertyModel>> typeToProperties = MapUtils.lazyMap(
|
||||
new HashMap<TypeCategory,Collection<PropertyModel>>(),
|
||||
new Factory<Collection<PropertyModel>>(){
|
||||
@ -52,13 +53,14 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
|
||||
private String uncapSimpleName;
|
||||
|
||||
public BeanModel(String prefix, String packageName, String name, String simpleName, Collection<String> superTypes) {
|
||||
public BeanModel(String prefix, TypeModel typeModel) {
|
||||
this(prefix, typeModel, Collections.<String>emptyList());
|
||||
}
|
||||
|
||||
public BeanModel(String prefix, TypeModel typeModel, Collection<String> superTypes) {
|
||||
this.prefix = Assert.notNull(prefix);
|
||||
this.packageName = Assert.notNull(packageName);
|
||||
this.name = Assert.notNull(name);
|
||||
this.simpleName = Assert.notNull(simpleName);
|
||||
this.uncapSimpleName = StringUtils.uncapitalize(simpleName);
|
||||
this.localName = name.substring(packageName.length()+1);
|
||||
this.typeModel = typeModel;
|
||||
this.uncapSimpleName = StringUtils.uncapitalize(typeModel.getSimpleName());
|
||||
this.superTypes = superTypes;
|
||||
}
|
||||
|
||||
@ -74,11 +76,11 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
}
|
||||
|
||||
public int compareTo(BeanModel o) {
|
||||
return simpleName.compareTo(o.simpleName);
|
||||
return typeModel.getSimpleName().compareTo(o.typeModel.getSimpleName());
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof BeanModel && simpleName.equals(((BeanModel) o).simpleName);
|
||||
return o instanceof BeanModel && typeModel.getName().equals(((BeanModel) o).typeModel.getName());
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getBooleanProperties() {
|
||||
@ -105,10 +107,6 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
return typeToProperties.get(TypeCategory.ENTITYCOLLECTION);
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getEntityProperties() {
|
||||
return typeToProperties.get(TypeCategory.ENTITY);
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getEntityLists() {
|
||||
return typeToProperties.get(TypeCategory.ENTITYLIST);
|
||||
}
|
||||
@ -117,8 +115,29 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
return typeToProperties.get(TypeCategory.ENTITYMAP);
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getEntityProperties() {
|
||||
return typeToProperties.get(TypeCategory.ENTITY);
|
||||
}
|
||||
|
||||
public String getLocalName() {
|
||||
return typeModel.getLocalName();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return typeModel.getName();
|
||||
}
|
||||
|
||||
public String getGenericName(){
|
||||
if (typeModel.getParameterCount() == 0){
|
||||
return typeModel.getLocalName();
|
||||
}else{
|
||||
StringBuilder builder = new StringBuilder(typeModel.getLocalName()).append("<");
|
||||
for (int i = 0; i < typeModel.getParameterCount(); i++){
|
||||
if (i > 0) builder.append(",");
|
||||
builder.append("?");
|
||||
}
|
||||
return builder.append(">").toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getNumericProperties() {
|
||||
@ -126,15 +145,15 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getSimpleCollections() {
|
||||
return typeToProperties.get(TypeCategory.SIMPLECOLLECTION);
|
||||
return typeModel.getPackageName();
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getSimpleProperties() {
|
||||
return typeToProperties.get(TypeCategory.SIMPLE);
|
||||
public String getPrefix(){
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getSimpleCollections() {
|
||||
return typeToProperties.get(TypeCategory.SIMPLECOLLECTION);
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getSimpleLists() {
|
||||
@ -145,18 +164,22 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
return typeToProperties.get(TypeCategory.SIMPLEMAP);
|
||||
}
|
||||
|
||||
public String getLocalName(){
|
||||
return localName;
|
||||
}
|
||||
|
||||
public String getSimpleName() {
|
||||
return simpleName;
|
||||
return typeModel.getSimpleName();
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getSimpleProperties() {
|
||||
return typeToProperties.get(TypeCategory.SIMPLE);
|
||||
}
|
||||
|
||||
public Collection<PropertyModel> getStringProperties() {
|
||||
return typeToProperties.get(TypeCategory.STRING);
|
||||
}
|
||||
|
||||
|
||||
public BeanModel getSuperModel() {
|
||||
return superModel;
|
||||
}
|
||||
|
||||
public Collection<String> getSuperTypes() {
|
||||
return superTypes;
|
||||
}
|
||||
@ -170,7 +193,7 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
return typeModel.getName().hashCode();
|
||||
}
|
||||
|
||||
public void include(BeanModel clazz) {
|
||||
@ -184,32 +207,28 @@ public final class BeanModel implements Comparable<BeanModel> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PropertyModel validateField(PropertyModel field) {
|
||||
if (field.getName().equals(this.uncapSimpleName)) {
|
||||
uncapSimpleName = StringUtils.uncapitalize(simpleName)+ (escapeSuffix++);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
public String getPrefix(){
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public BeanModel getSuperModel() {
|
||||
return superModel;
|
||||
}
|
||||
|
||||
public void setSuperModel(BeanModel superModel) {
|
||||
this.superModel = superModel;
|
||||
}
|
||||
|
||||
public boolean isEntityModel() {
|
||||
return entityModel;
|
||||
}
|
||||
|
||||
|
||||
public void setEntityModel(boolean entityModel) {
|
||||
this.entityModel = entityModel;
|
||||
}
|
||||
|
||||
|
||||
public void setSuperModel(BeanModel superModel) {
|
||||
this.superModel = superModel;
|
||||
}
|
||||
|
||||
|
||||
private PropertyModel validateField(PropertyModel field) {
|
||||
if (field.getName().equals(this.uncapSimpleName)) {
|
||||
uncapSimpleName = StringUtils.uncapitalize(typeModel.getSimpleName())+ (escapeSuffix++);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
}
|
||||
@ -51,11 +51,8 @@ public class BeanModelFactory {
|
||||
}else{
|
||||
superTypes = Collections.singleton(key.getSuperclass().getName());
|
||||
}
|
||||
BeanModel beanModel = new BeanModel(
|
||||
prefix,
|
||||
key.getPackage().getName(),
|
||||
key.getName(),
|
||||
key.getSimpleName(),
|
||||
BeanModel beanModel = new BeanModel(prefix,
|
||||
new ClassTypeModel(TypeCategory.ENTITY, key),
|
||||
superTypes);
|
||||
for (Field f : key.getDeclaredFields()) {
|
||||
if (isValidField(f)){
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.ClassUtils;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
@ -16,6 +19,8 @@ public class ClassTypeModel implements TypeModel{
|
||||
|
||||
private final Class<?> primitiveClass;
|
||||
|
||||
private final List<TypeModel> parameters;
|
||||
|
||||
public ClassTypeModel(TypeCategory typeCategory, Class<?> clazz){
|
||||
this(typeCategory, clazz, ClassUtils.wrapperToPrimitive(clazz));
|
||||
}
|
||||
@ -24,6 +29,8 @@ public class ClassTypeModel implements TypeModel{
|
||||
this.typeCategory = Assert.notNull(typeCategory);
|
||||
this.clazz = Assert.notNull(clazz);
|
||||
this.primitiveClass = primitiveClass;
|
||||
// TODO
|
||||
this.parameters = Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -35,11 +42,6 @@ public class ClassTypeModel implements TypeModel{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getKeyType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalName() {
|
||||
return clazz.getName().substring(clazz.getPackage().getName().length()+1);
|
||||
@ -70,14 +72,19 @@ public class ClassTypeModel implements TypeModel{
|
||||
return typeCategory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getValueType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitive() {
|
||||
return primitiveClass != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getParameter(int i) {
|
||||
return parameters.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterCount() {
|
||||
return parameters.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -134,27 +134,28 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void collectionOfEntity(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PEntityCollection<" + field.getTypeName()+">", writer, "createEntityCollection", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\"");
|
||||
serialize(field, "PEntityCollection<" + field.getGenericTypeName()+">", writer, "createEntityCollection", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\"");
|
||||
}
|
||||
|
||||
protected void collectionOfSimple(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PComponentCollection<" + field.getTypeName()+">", writer, "createSimpleCollection", field.getTypeName()+".class");
|
||||
serialize(field, "PComponentCollection<" + field.getGenericTypeName()+">", writer, "createSimpleCollection", field.getTypeName()+".class");
|
||||
}
|
||||
|
||||
|
||||
protected void comparableField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PComparable<" + field.getTypeName() + ">", writer, "createComparable", field.getTypeName() + ".class");
|
||||
serialize(field, "PComparable<" + field.getGenericTypeName() + ">", writer, "createComparable", field.getTypeName() + ".class");
|
||||
}
|
||||
|
||||
protected void constructors(BeanModel model, Writer writer) throws IOException {
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
final String localName = model.getLocalName();
|
||||
final String genericName = model.getGenericName();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
constructorsForVariables(builder, model);
|
||||
|
||||
builder.append(" public " + queryType + "(PEntity<? extends "+localName+"> entity) {\n");
|
||||
builder.append(" public " + queryType + "(PEntity<? extends "+genericName+"> entity) {\n");
|
||||
builder.append(" super(entity.getType(), entity.getEntityName(), entity.getMetadata());\n");
|
||||
if (!model.getEntityProperties().isEmpty()){
|
||||
builder.append(" if (entity.getMetadata().getParent() == null){\n");
|
||||
@ -166,8 +167,15 @@ public class EntitySerializer implements Serializer{
|
||||
|
||||
|
||||
builder.append(" }\n\n");
|
||||
if (!localName.equals(genericName)){
|
||||
builder.append(" @SuppressWarnings(\"unchecked\")\n");
|
||||
}
|
||||
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
|
||||
builder.append(" super("+ localName + ".class, \"" + simpleName + "\", metadata);\n");
|
||||
builder.append(" super(");
|
||||
if (!localName.equals(genericName)){
|
||||
builder.append("(Class)");
|
||||
}
|
||||
builder.append(localName + ".class, \"" + simpleName + "\", metadata);\n");
|
||||
if (!model.getEntityProperties().isEmpty()){
|
||||
builder.append(" if (metadata.getParent() == null){\n");
|
||||
for (PropertyModel entityField : model.getEntityProperties()){
|
||||
@ -183,8 +191,17 @@ public class EntitySerializer implements Serializer{
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
final String localName = model.getLocalName();
|
||||
final String genericName = model.getGenericName();
|
||||
|
||||
if (!localName.equals(genericName)){
|
||||
builder.append(" @SuppressWarnings(\"unchecked\")\n");
|
||||
}
|
||||
builder.append(" public " + queryType + "(@NotEmpty String variable) {\n");
|
||||
builder.append(" super(" + localName + ".class, \""+simpleName+"\", PathMetadata.forVariable(variable));\n");
|
||||
builder.append(" super(");
|
||||
if (!localName.equals(genericName)){
|
||||
builder.append("(Class)");
|
||||
}
|
||||
builder.append(localName + ".class, \""+simpleName+"\", PathMetadata.forVariable(variable));\n");
|
||||
for (PropertyModel entityField : model.getEntityProperties()){
|
||||
builder.append(" _" + entityField.getName()+"();\n");
|
||||
}
|
||||
@ -192,11 +209,11 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void dateField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PDate<" + field.getTypeName() + ">", writer, "createDate", field.getTypeName()+".class");
|
||||
serialize(field, "PDate<" + field.getGenericTypeName() + ">", writer, "createDate", field.getTypeName()+".class");
|
||||
}
|
||||
|
||||
protected void dateTimeField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PDateTime<" + field.getTypeName() + ">", writer, "createDateTime", field.getTypeName()+".class");
|
||||
serialize(field, "PDateTime<" + field.getGenericTypeName() + ">", writer, "createDateTime", field.getTypeName()+".class");
|
||||
}
|
||||
|
||||
protected void entityField(PropertyModel field, Writer writer) throws IOException {
|
||||
@ -246,7 +263,8 @@ public class EntitySerializer implements Serializer{
|
||||
|
||||
protected void introClassHeader(StringBuilder builder, BeanModel model) {
|
||||
final String queryType = model.getPrefix() + model.getSimpleName();
|
||||
final String localName = model.getLocalName();
|
||||
final String localName = model.getGenericName();
|
||||
|
||||
builder.append("@SuppressWarnings(\"serial\")\n");
|
||||
BeanModel superModel = model.getSuperModel();
|
||||
while (superModel != null && superModel.isEntityModel()){
|
||||
@ -267,6 +285,7 @@ public class EntitySerializer implements Serializer{
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String unscapSimpleName = model.getUncapSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
|
||||
builder.append(" public static final " + queryType + " " + unscapSimpleName + " = new " + queryType + "(\"" + unscapSimpleName + "\");\n\n");
|
||||
}
|
||||
|
||||
@ -281,6 +300,7 @@ public class EntitySerializer implements Serializer{
|
||||
protected void introJavadoc(StringBuilder builder, BeanModel model) {
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
|
||||
builder.append("/**\n");
|
||||
builder.append(" * " + queryType + " is a Querydsl query type for " + simpleName + "\n");
|
||||
builder.append(" * \n");
|
||||
@ -292,14 +312,14 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void listOfEntity(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PEntityList<" + field.getTypeName()+ ">", writer, "createEntityList", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\"");
|
||||
serialize(field, "PEntityList<" + field.getGenericTypeName()+ ">", writer, "createEntityList", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\"");
|
||||
}
|
||||
|
||||
protected void listOfEntityAccessor(PropertyModel field, Writer writer) throws IOException {
|
||||
final String escapedName = field.getEscapedName();
|
||||
final String queryType = field.getQueryTypeName();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(" public " + queryType + " " + escapedName + "(int index) {\n");
|
||||
builder.append(" return new " + queryType + "(PathMetadata.forListAccess(" + escapedName+", index));\n");
|
||||
builder.append(" }\n\n");
|
||||
@ -311,9 +331,9 @@ public class EntitySerializer implements Serializer{
|
||||
|
||||
protected void listOfSimpleAccessor(PropertyModel field, Writer writer) throws IOException {
|
||||
final String escapedName = field.getEscapedName();
|
||||
final String valueType = field.getValueTypeName();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
final String valueType = field.getParameterName(0);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(int index) {\n");
|
||||
builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forListAccess(" + escapedName+", index));\n");
|
||||
builder.append(" }\n\n");
|
||||
@ -329,10 +349,13 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void mapOfEntity(PropertyModel field, Writer writer) throws IOException{
|
||||
final String keyType = field.getKeyTypeName();
|
||||
final String valueType = field.getValueTypeName();
|
||||
final String keyType = field.getParameterName(0);
|
||||
final String valueType = field.getParameterName(1);
|
||||
final String simpleName = field.getSimpleTypeName();
|
||||
serialize(field, "PEntityMap<"+keyType+","+valueType+">",
|
||||
final String genericKey = field.getGenericParameterName(0);
|
||||
final String genericValue = field.getGenericParameterName(1);
|
||||
|
||||
serialize(field, "PEntityMap<"+genericKey+","+genericValue+">",
|
||||
writer, "createEntityMap", keyType+".class", valueType+".class", "\""+simpleName+"\"");
|
||||
|
||||
}
|
||||
@ -340,13 +363,14 @@ public class EntitySerializer implements Serializer{
|
||||
protected void mapOfEntityAccessor(PropertyModel field, Writer writer) throws IOException {
|
||||
final String escapedName = field.getEscapedName();
|
||||
final String queryType = field.getQueryTypeName();
|
||||
final String keyType = field.getKeyTypeName();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
final String keyType = field.getGenericParameterName(0);
|
||||
final String genericKey = field.getGenericParameterName(0);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(" public " + queryType + " " + escapedName + "(" + keyType+ " key) {\n");
|
||||
builder.append(" return new " + queryType + "(PathMetadata.forMapAccess(" + escapedName+", key));\n");
|
||||
builder.append(" }\n\n");
|
||||
builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr<"+keyType+"> key) {\n");
|
||||
builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n");
|
||||
builder.append(" return new " + queryType + "(PathMetadata.forMapAccess(" + escapedName+", key));\n");
|
||||
builder.append(" }\n\n");
|
||||
writer.append(builder.toString());
|
||||
@ -354,24 +378,29 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void mapOfSimple(PropertyModel field, Writer writer) throws IOException {
|
||||
final String keyType = field.getKeyTypeName();
|
||||
final String valueType = field.getValueTypeName();
|
||||
final String keyType = field.getParameterName(0);
|
||||
final String valueType = field.getParameterName(1);
|
||||
final String genericKey = field.getGenericParameterName(0);
|
||||
final String genericValue = field.getGenericParameterName(1);
|
||||
|
||||
serialize(field, "PComponentMap<"+keyType+","+valueType+">", writer, "createSimpleMap", keyType+".class", valueType+".class");
|
||||
serialize(field, "PComponentMap<"+genericKey+","+genericValue+">",
|
||||
writer, "createSimpleMap", keyType+".class", valueType+".class");
|
||||
|
||||
}
|
||||
|
||||
protected void mapOfSimpleAccessor(PropertyModel field, Writer writer) throws IOException {
|
||||
// final String fieldName = field.getName();
|
||||
final String escapedName = field.getEscapedName();
|
||||
final String keyType = field.getKeyTypeName();
|
||||
final String valueType = field.getValueTypeName();
|
||||
// final String keyType = field.getParameterName(0);
|
||||
final String valueType = field.getParameterName(1);
|
||||
final String genericKey = field.getGenericParameterName(0);
|
||||
final String genericValue = field.getGenericParameterName(1);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(" + keyType + " key) {\n");
|
||||
builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forMapAccess(" + escapedName+", key));\n");
|
||||
builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(" + genericKey + " key) {\n");
|
||||
builder.append(" return new PSimple<" + genericValue + ">("+valueType+".class, PathMetadata.forMapAccess(" + escapedName+", key));\n");
|
||||
builder.append(" }\n\n");
|
||||
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(com.mysema.query.types.expr.Expr<"+keyType+"> key) {\n");
|
||||
builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n");
|
||||
builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forMapAccess(" + escapedName+", key));\n");
|
||||
builder.append(" }\n\n");
|
||||
writer.append(builder.toString());
|
||||
@ -379,7 +408,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void numericField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PNumber<" + field.getTypeName() + ">", writer, "createNumber", field.getTypeName() +".class");
|
||||
serialize(field, "PNumber<" + field.getGenericTypeName() + ">", writer, "createNumber", field.getTypeName() +".class");
|
||||
}
|
||||
|
||||
protected void outro(BeanModel model, Writer writer) throws IOException {
|
||||
@ -414,7 +443,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void simpleField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PSimple<" + field.getTypeName()+">", writer, "createSimple", field.getTypeName()+".class");
|
||||
serialize(field, "PSimple<" + field.getGenericTypeName()+">", writer, "createSimple", field.getTypeName()+".class");
|
||||
}
|
||||
|
||||
protected void stringField(PropertyModel field, Writer writer) throws IOException {
|
||||
@ -422,7 +451,7 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
|
||||
protected void timeField(PropertyModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PTime<" + field.getTypeName() + ">", writer, "createTime", field.getTypeName()+".class");
|
||||
serialize(field, "PTime<" + field.getGenericTypeName() + ">", writer, "createTime", field.getTypeName()+".class");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
private final String name, escapedName, typeName;
|
||||
|
||||
@Nullable
|
||||
private final String keyTypeName, valueTypeName, queryTypeName;
|
||||
private final String queryTypeName;
|
||||
|
||||
private final TypeModel type;
|
||||
|
||||
@ -35,9 +35,7 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
this.name = Assert.notNull(name);
|
||||
this.escapedName = JavaSyntaxUtils.isReserved(name) ? (name + "_") : name;
|
||||
this.type = Assert.notNull(type);
|
||||
this.typeName = getLocalName(type);
|
||||
this.keyTypeName = type.getKeyType() != null ? getLocalName(type.getKeyType()) : null;
|
||||
this.valueTypeName = type.getValueType() != null ? getLocalName(type.getValueType()) : null;
|
||||
this.typeName = getLocalName(type);
|
||||
if (type.getTypeCategory().isSubCategoryOf(TypeCategory.SIMPLE)){
|
||||
this.queryTypeName = null;
|
||||
}else if (isVisible(type)){
|
||||
@ -71,14 +69,28 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
return type.getTypeCategory();
|
||||
}
|
||||
|
||||
public String getKeyTypeName() {
|
||||
return keyTypeName;
|
||||
public String getGenericParameterName(int i){
|
||||
if (i < type.getParameterCount()){
|
||||
TypeModel typeModel = type.getParameter(i);
|
||||
if (typeModel.getParameterCount() > 0){
|
||||
return getGenericName(typeModel);
|
||||
}else{
|
||||
return getLocalName(typeModel);
|
||||
}
|
||||
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getValueTypeName() {
|
||||
return valueTypeName;
|
||||
public String getParameterName(int i){
|
||||
if (i < type.getParameterCount()){
|
||||
return getLocalName(type.getParameter(i));
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@ -99,6 +111,29 @@ public final class PropertyModel implements Comparable<PropertyModel> {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public String getGenericTypeName(){
|
||||
TypeModel base = type;
|
||||
if (type.getTypeCategory().isSubCategoryOf(TypeCategory.COLLECTION)){
|
||||
base = type.getParameter(0);
|
||||
}else if (type.getTypeCategory().isSubCategoryOf(TypeCategory.MAP)){
|
||||
base = type.getParameter(1);
|
||||
}
|
||||
if (base.getParameterCount() > 0){
|
||||
return getGenericName(base);
|
||||
}else{
|
||||
return typeName;
|
||||
}
|
||||
}
|
||||
|
||||
private String getGenericName(TypeModel typeModel){
|
||||
StringBuilder builder = new StringBuilder(getLocalName(typeModel)).append("<");
|
||||
for (int i = 0; i < typeModel.getParameterCount(); i++){
|
||||
if (i > 0) builder.append(",");
|
||||
builder.append("?");
|
||||
}
|
||||
return builder.append(">").toString();
|
||||
}
|
||||
|
||||
public String getTypePackage() {
|
||||
return type.getPackageName();
|
||||
}
|
||||
|
||||
@ -5,8 +5,6 @@
|
||||
*/
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.jcip.annotations.Immutable;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
@ -21,8 +19,7 @@ import com.mysema.commons.lang.Assert;
|
||||
@Immutable
|
||||
public final class SimpleTypeModel implements TypeModel {
|
||||
|
||||
@Nullable
|
||||
private final TypeModel keyType, valueType;
|
||||
private final TypeModel[] parameters;
|
||||
|
||||
private final String name, packageName, simpleName, localName;
|
||||
|
||||
@ -33,64 +30,72 @@ public final class SimpleTypeModel implements TypeModel {
|
||||
String name,
|
||||
String packageName,
|
||||
String simpleName,
|
||||
@Nullable TypeModel keyType,
|
||||
@Nullable TypeModel valueType) {
|
||||
TypeModel... parameters) {
|
||||
this.typeCategory = Assert.notNull(typeCategory,"typeCategory is null");
|
||||
this.name = Assert.notNull(name,"name is null");
|
||||
this.packageName = Assert.notNull(packageName,"packageName is null");
|
||||
this.simpleName = Assert.notNull(simpleName,"simpleName is null");
|
||||
this.localName = name.substring(packageName.length()+1);
|
||||
this.keyType = keyType;
|
||||
this.valueType = valueType;
|
||||
this.parameters = Assert.notNull(parameters);
|
||||
}
|
||||
|
||||
public SimpleTypeModel as(TypeCategory category) {
|
||||
if (typeCategory == category){
|
||||
return this;
|
||||
}else{
|
||||
return new SimpleTypeModel(category, name, packageName, simpleName, keyType, valueType);
|
||||
return new SimpleTypeModel(category, name, packageName, simpleName, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
public TypeModel getKeyType() {
|
||||
return keyType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalName(){
|
||||
return localName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrimitiveName(){
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSimpleName() {
|
||||
return simpleName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCategory getTypeCategory() {
|
||||
return typeCategory;
|
||||
}
|
||||
|
||||
public TypeModel getValueType() {
|
||||
return valueType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeModel getParameter(int i) {
|
||||
return parameters[i];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterCount() {
|
||||
return parameters.length;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -23,6 +23,14 @@ import com.mysema.query.annotations.PropertyType;
|
||||
*/
|
||||
@Immutable
|
||||
public enum TypeCategory {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
MAP(null),
|
||||
/**
|
||||
*
|
||||
*/
|
||||
COLLECTION(null),
|
||||
/**
|
||||
* Simple non-entity fields
|
||||
*/
|
||||
@ -43,6 +51,7 @@ public enum TypeCategory {
|
||||
* Date/Time fields
|
||||
*/
|
||||
DATETIME(COMPARABLE,
|
||||
java.util.Calendar.class.getName(),
|
||||
java.util.Date.class.getName(),
|
||||
java.sql.Timestamp.class.getName(),
|
||||
"org.joda.time.LocalDateTime",
|
||||
@ -56,7 +65,7 @@ public enum TypeCategory {
|
||||
/**
|
||||
* Entity collection fields
|
||||
*/
|
||||
ENTITYCOLLECTION(null),
|
||||
ENTITYCOLLECTION(COLLECTION),
|
||||
/**
|
||||
* Entity list fields
|
||||
*/
|
||||
@ -64,7 +73,8 @@ public enum TypeCategory {
|
||||
/**
|
||||
* Entity map fields
|
||||
*/
|
||||
ENTITYMAP(null),
|
||||
ENTITYMAP(MAP),
|
||||
|
||||
/**
|
||||
* Numeric fields (? extends Number & Comparable)
|
||||
*/
|
||||
@ -72,7 +82,7 @@ public enum TypeCategory {
|
||||
/**
|
||||
* Simple collection fields
|
||||
*/
|
||||
SIMPLECOLLECTION(null),
|
||||
SIMPLECOLLECTION(COLLECTION),
|
||||
/**
|
||||
* Simple list fields
|
||||
*/
|
||||
@ -80,7 +90,7 @@ public enum TypeCategory {
|
||||
/**
|
||||
* Simple map fields
|
||||
*/
|
||||
SIMPLEMAP(null),
|
||||
SIMPLEMAP(MAP),
|
||||
/**
|
||||
* String fields
|
||||
*/
|
||||
|
||||
@ -11,8 +11,10 @@ public interface TypeModel {
|
||||
TypeModel as(TypeCategory category);
|
||||
|
||||
@Nullable
|
||||
TypeModel getKeyType();
|
||||
TypeModel getParameter(int i);
|
||||
|
||||
int getParameterCount();
|
||||
|
||||
String getLocalName();
|
||||
|
||||
String getName();
|
||||
@ -26,9 +28,6 @@ public interface TypeModel {
|
||||
|
||||
TypeCategory getTypeCategory();
|
||||
|
||||
@Nullable
|
||||
TypeModel getValueType();
|
||||
|
||||
boolean isPrimitive();
|
||||
|
||||
String toString();
|
||||
|
||||
@ -13,8 +13,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang.ClassUtils;
|
||||
|
||||
import com.mysema.query.util.TypeUtil;
|
||||
@ -98,30 +96,31 @@ public class TypeModelFactory {
|
||||
}
|
||||
|
||||
public TypeModel createArrayType(TypeModel valueType) {
|
||||
return createComposite(null, valueType, TypeCategory.ENTITYCOLLECTION, TypeCategory.SIMPLECOLLECTION);
|
||||
return createComposite(TypeCategory.ENTITYCOLLECTION, TypeCategory.SIMPLECOLLECTION, valueType);
|
||||
}
|
||||
|
||||
public TypeModel createCollectionType(TypeModel valueType) {
|
||||
return createComposite(null, valueType, TypeCategory.ENTITYCOLLECTION, TypeCategory.SIMPLECOLLECTION);
|
||||
return createComposite(TypeCategory.ENTITYCOLLECTION, TypeCategory.SIMPLECOLLECTION, valueType);
|
||||
}
|
||||
|
||||
private TypeModel createComposite(@Nullable TypeModel key, TypeModel value, TypeCategory entity, TypeCategory simple) {
|
||||
private TypeModel createComposite(TypeCategory entity, TypeCategory simple, TypeModel... parameters) {
|
||||
TypeCategory category;
|
||||
TypeModel value = parameters[parameters.length -1];
|
||||
if (value.getTypeCategory() == TypeCategory.ENTITY) {
|
||||
category = entity;
|
||||
} else {
|
||||
category = simple;
|
||||
}
|
||||
return new SimpleTypeModel(category, value.getName(), value.getPackageName(), value.getSimpleName(), key, value);
|
||||
return new SimpleTypeModel(category, value.getName(), value.getPackageName(), value.getSimpleName(), parameters);
|
||||
|
||||
}
|
||||
|
||||
public TypeModel createListType(TypeModel valueType) {
|
||||
return createComposite(null, valueType, TypeCategory.ENTITYLIST, TypeCategory.SIMPLELIST);
|
||||
return createComposite(TypeCategory.ENTITYLIST, TypeCategory.SIMPLELIST, valueType);
|
||||
}
|
||||
|
||||
public TypeModel createMapType(TypeModel keyType, TypeModel valueType) {
|
||||
return createComposite(keyType, valueType, TypeCategory.ENTITYMAP, TypeCategory.SIMPLEMAP);
|
||||
return createComposite(TypeCategory.ENTITYMAP, TypeCategory.SIMPLEMAP, keyType, valueType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,6 +23,10 @@ public abstract class EBoolean extends EComparable<Boolean> {
|
||||
|
||||
public static final EBoolean TRUE = new EBooleanConst(Boolean.TRUE);
|
||||
|
||||
public static final EBoolean create(Boolean b){
|
||||
return b.booleanValue() ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
private volatile EBoolean not;
|
||||
|
||||
public EBoolean() {
|
||||
@ -50,7 +54,7 @@ public abstract class EBoolean extends EComparable<Boolean> {
|
||||
}
|
||||
return not;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a union of this and the given expression
|
||||
*
|
||||
@ -60,8 +64,4 @@ public abstract class EBoolean extends EComparable<Boolean> {
|
||||
public final EBoolean or(EBoolean right) {
|
||||
return OBoolean.create(Ops.OR, this, right);
|
||||
}
|
||||
|
||||
public static final EBoolean create(Boolean b){
|
||||
return b.booleanValue() ? TRUE : FALSE;
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,12 @@
|
||||
*/
|
||||
package com.mysema.query.types.expr;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.query.types.operation.OBoolean;
|
||||
import com.mysema.query.types.operation.OComparable;
|
||||
@ -23,9 +29,21 @@ import com.mysema.query.types.operation.Ops;
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class EString extends EComparable<String> {
|
||||
|
||||
public static final EString emptyString = new EStringConst("");
|
||||
private static final Map<String,EString> cache;
|
||||
|
||||
static{
|
||||
List<String> strs = new ArrayList<String>();
|
||||
strs.addAll(Arrays.asList("", ".", ".*", "%", "id", "name"));
|
||||
for (int i = 0; i < 256; i++){
|
||||
strs.add(String.valueOf(i));
|
||||
}
|
||||
|
||||
cache = new HashMap<String,EString>(strs.size());
|
||||
for (String str : strs){
|
||||
cache.put(str, new EStringConst(str));
|
||||
}
|
||||
}
|
||||
|
||||
public static final EString percentString = new EStringConst("%");
|
||||
|
||||
/**
|
||||
* Factory method for constants
|
||||
@ -34,12 +52,10 @@ public abstract class EString extends EComparable<String> {
|
||||
* @return
|
||||
*/
|
||||
public static final EString create(String str){
|
||||
if (str.equals("")){
|
||||
return emptyString;
|
||||
}else if (str.equals("%")){
|
||||
return percentString;
|
||||
if (cache.containsKey(str)){
|
||||
return cache.get(str);
|
||||
}else{
|
||||
return new EStringConst(Assert.notNull(str));
|
||||
return new EStringConst(Assert.notNull(str));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -37,20 +37,20 @@ public class PComponentMap<K, V> extends EMapBase<K, V> implements PMap<K, V> {
|
||||
private volatile EBoolean isnull, isnotnull;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public PComponentMap(Class<K> keyType, Class<V> valueType,
|
||||
public PComponentMap(Class<? super K> keyType, Class<? super V> valueType,
|
||||
PathMetadata<?> metadata) {
|
||||
super((Class)Map.class);
|
||||
this.keyType = keyType;
|
||||
this.valueType = valueType;
|
||||
this.keyType = (Class<K>)keyType;
|
||||
this.valueType = (Class<V>)valueType;
|
||||
this.metadata = metadata;
|
||||
this.root = metadata.getRoot() != null ? metadata.getRoot() : this;
|
||||
}
|
||||
|
||||
public PComponentMap(Class<K> keyType, Class<V> valueType, @NotEmpty String var) {
|
||||
public PComponentMap(Class<? super K> keyType, Class<? super V> valueType, @NotEmpty String var) {
|
||||
this(keyType, valueType, PathMetadata.forVariable(var));
|
||||
}
|
||||
|
||||
public PComponentMap(Class<K> keyType, Class<V> valueType, Path<?> parent, @NotEmpty String property) {
|
||||
public PComponentMap(Class<? super K> keyType, Class<? super V> valueType, Path<?> parent, @NotEmpty String property) {
|
||||
this(keyType, valueType, PathMetadata.forProperty(parent, property));
|
||||
}
|
||||
|
||||
|
||||
@ -58,15 +58,15 @@ public class PEntity<D> extends EEntity<D> implements Path<D> {
|
||||
return new PEntity<A>(type, entityName, PathMetadata.forProperty(this, property));
|
||||
}
|
||||
|
||||
protected <A> PEntityCollection<A> createEntityCollection(@NotEmpty String property, Class<A> type, @NotEmpty String entityName) {
|
||||
protected <A> PEntityCollection<A> createEntityCollection(@NotEmpty String property, Class<? super A> type, @NotEmpty String entityName) {
|
||||
return new PEntityCollection<A>(type, entityName, this, property);
|
||||
}
|
||||
|
||||
protected <A> PEntityList<A> createEntityList(@NotEmpty String property, Class<A> type, @NotEmpty String entityName) {
|
||||
protected <A> PEntityList<A> createEntityList(@NotEmpty String property, Class<? super A> type, @NotEmpty String entityName) {
|
||||
return new PEntityList<A>(type, entityName, this, property);
|
||||
}
|
||||
|
||||
protected <K, V> PEntityMap<K, V> createEntityMap(@NotEmpty String property, Class<K> key, Class<V> value, @NotEmpty String entityName) {
|
||||
protected <K, V> PEntityMap<K, V> createEntityMap(@NotEmpty String property, Class<? super K> key, Class<? super V> value, @NotEmpty String entityName) {
|
||||
return new PEntityMap<K, V>(key, value, entityName, this, property);
|
||||
}
|
||||
|
||||
@ -74,8 +74,9 @@ public class PEntity<D> extends EEntity<D> implements Path<D> {
|
||||
return new PNumber<A>(type, this, property);
|
||||
}
|
||||
|
||||
protected <A> PSimple<A> createSimple(@NotEmpty String path, Class<A> type) {
|
||||
return new PSimple<A>(type, this, path);
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <A> PSimple<A> createSimple(@NotEmpty String path, Class<? super A> type) {
|
||||
return new PSimple<A>((Class<A>)type, this, path);
|
||||
}
|
||||
|
||||
protected <A> PComponentCollection<A> createSimpleCollection(@NotEmpty String path, Class<A> type) {
|
||||
@ -86,7 +87,7 @@ public class PEntity<D> extends EEntity<D> implements Path<D> {
|
||||
return new PComponentList<A>(type, this, path);
|
||||
}
|
||||
|
||||
protected <K, V> PComponentMap<K, V> createSimpleMap(@NotEmpty String path, Class<K> key, Class<V> value) {
|
||||
protected <K, V> PComponentMap<K, V> createSimpleMap(@NotEmpty String path, Class<? super K> key, Class<? super V> value) {
|
||||
return new PComponentMap<K, V>(key, value, this, path);
|
||||
}
|
||||
|
||||
|
||||
@ -42,19 +42,19 @@ public class PEntityCollection<D> extends EEntity<java.util.Collection<D>> imple
|
||||
private final Path<?> root;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public PEntityCollection(Class<D> type, @NotEmpty String entityName, PathMetadata<?> metadata) {
|
||||
public PEntityCollection(Class<? super D> type, @NotEmpty String entityName, PathMetadata<?> metadata) {
|
||||
super((Class)Collection.class);
|
||||
this.elementType = Assert.notNull(type,"type is null");
|
||||
this.elementType = (Class<D>) Assert.notNull(type,"type is null");
|
||||
this.metadata = Assert.notNull(metadata,"metadata is null");
|
||||
this.entityName = Assert.notNull(entityName,"entityName is null");
|
||||
this.root = metadata.getRoot() != null ? metadata.getRoot() : this;
|
||||
}
|
||||
|
||||
public PEntityCollection(Class<D> type, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
public PEntityCollection(Class<? super D> type, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
this(type, entityName, PathMetadata.forVariable(var));
|
||||
}
|
||||
|
||||
public PEntityCollection(Class<D> type, @NotEmpty String entityName, Path<?> parent, @NotEmpty String property) {
|
||||
public PEntityCollection(Class<? super D> type, @NotEmpty String entityName, Path<?> parent, @NotEmpty String property) {
|
||||
this(type, entityName, PathMetadata.forProperty(parent, property));
|
||||
}
|
||||
|
||||
|
||||
@ -18,15 +18,15 @@ import com.mysema.query.util.NotEmpty;
|
||||
@SuppressWarnings("serial")
|
||||
public class PEntityList<D> extends PEntityCollection<D> implements PList<D> {
|
||||
|
||||
public PEntityList(Class<D> elementType, @NotEmpty String entityName, PathMetadata<?> metadata) {
|
||||
public PEntityList(Class<? super D> elementType, @NotEmpty String entityName, PathMetadata<?> metadata) {
|
||||
super(elementType, entityName, metadata);
|
||||
}
|
||||
|
||||
public PEntityList(Class<D> elementType, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
public PEntityList(Class<? super D> elementType, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
super(elementType, entityName, PathMetadata.forVariable(var));
|
||||
}
|
||||
|
||||
public PEntityList(Class<D> elementType, @NotEmpty String entityName, Path<?> parent, @NotEmpty String property) {
|
||||
public PEntityList(Class<? super D> elementType, @NotEmpty String entityName, Path<?> parent, @NotEmpty String property) {
|
||||
super(elementType, entityName, PathMetadata.forProperty(parent, property));
|
||||
}
|
||||
|
||||
|
||||
@ -40,21 +40,21 @@ public class PEntityMap<K, V> extends EMapBase<K, V> implements PMap<K, V> {
|
||||
private final Path<?> root;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public PEntityMap(Class<K> keyType, Class<V> valueType, @NotEmpty String entityName,
|
||||
public PEntityMap(Class<? super K> keyType, Class<? super V> valueType, @NotEmpty String entityName,
|
||||
PathMetadata<?> metadata) {
|
||||
super((Class)Map.class);
|
||||
this.keyType = keyType;
|
||||
this.valueType = valueType;
|
||||
this.keyType = (Class<K>) keyType;
|
||||
this.valueType = (Class<V>) valueType;
|
||||
this.entityName = entityName;
|
||||
this.metadata = metadata;
|
||||
this.root = metadata.getRoot() != null ? metadata.getRoot() : this;
|
||||
}
|
||||
|
||||
public PEntityMap(Class<K> keyType, Class<V> valueType, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
public PEntityMap(Class<? super K> keyType, Class<? super V> valueType, @NotEmpty String entityName, @NotEmpty String var) {
|
||||
this(keyType, valueType, entityName, PathMetadata.forVariable(var));
|
||||
}
|
||||
|
||||
public PEntityMap(Class<K> keyType, Class<V> valueType, @NotEmpty String entityName, Path<?> parent, @NotEmpty String var) {
|
||||
public PEntityMap(Class<? super K> keyType, Class<? super V> valueType, @NotEmpty String entityName, Path<?> parent, @NotEmpty String var) {
|
||||
this(keyType, valueType, entityName, PathMetadata.forProperty(parent, var));
|
||||
}
|
||||
|
||||
|
||||
@ -28,12 +28,8 @@ public class SerializerTest {
|
||||
*/
|
||||
public SerializerTest() {
|
||||
TypeModelFactory typeFactory = new TypeModelFactory();
|
||||
type = new BeanModel(
|
||||
"Q",
|
||||
"com.mysema.query",
|
||||
"com.mysema.query.DomainClass",
|
||||
"DomainClass",
|
||||
Collections.singleton("com.mysema.query.DomainSuperClass"));
|
||||
TypeModel typeModel = new SimpleTypeModel(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass");
|
||||
type = new BeanModel("Q", typeModel, Collections.singleton("com.mysema.query.DomainSuperClass"));
|
||||
|
||||
PropertyModel field = new PropertyModel(type, "field", typeFactory.create(String.class));
|
||||
type.addProperty(field);
|
||||
|
||||
@ -21,6 +21,7 @@ import com.mysema.query.codegen.ClassTypeModel;
|
||||
import com.mysema.query.codegen.PropertyModel;
|
||||
import com.mysema.query.codegen.Serializer;
|
||||
import com.mysema.query.codegen.Serializers;
|
||||
import com.mysema.query.codegen.SimpleTypeModel;
|
||||
import com.mysema.query.codegen.TypeCategory;
|
||||
import com.mysema.query.codegen.TypeModel;
|
||||
import com.mysema.query.util.FileUtils;
|
||||
@ -98,7 +99,8 @@ public class MetaDataExporter {
|
||||
// ClassModelFactory factory = new ClassModelFactory(new TypeModelFactory());
|
||||
while (tables.next()) {
|
||||
String tableName = tables.getString(3);
|
||||
BeanModel classModel = new BeanModel(namePrefix, "java.lang", "java.lang.Object", tableName, Collections.<String>emptySet());
|
||||
TypeModel classTypeModel = new SimpleTypeModel(TypeCategory.ENTITY, "java.lang.Object", "java.lang", tableName);
|
||||
BeanModel classModel = new BeanModel(namePrefix, classTypeModel);
|
||||
ResultSet columns = md.getColumns(null, schemaPattern, tables.getString(3), null);
|
||||
while (columns.next()) {
|
||||
String name = columns.getString(4);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user