simplified BeanModel / PropertyModel rendering

This commit is contained in:
Timo Westkämper 2009-10-26 08:17:52 +00:00
parent c48d02ad18
commit 4b460dba11
6 changed files with 187 additions and 183 deletions

View File

@ -0,0 +1,58 @@
package com.mysema.query.domain;
import java.util.List;
import org.junit.Test;
import com.mysema.query.annotations.QueryEmbeddable;
import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.domain.AnimalTest.Cat;
public class EmbeddableTest {
@QueryEntity
public static class EntityWithEmbedded{
public WithEntityRef e1;
public WithStringProp e2;
public WithEntityAndString e3;
public WithList e4;
}
@QueryEmbeddable
public static class WithEntityRef{
public Cat cat;
}
@QueryEmbeddable
public static class WithStringProp{
public String str;
}
@QueryEmbeddable
public static class WithEntityAndString extends WithEntityRef{
public String str2;
}
@QueryEmbeddable
public static class WithList extends WithStringProp{
public List<Cat> cats;
public String str3;
}
@Test
public void test(){
}
}

View File

@ -7,14 +7,12 @@ 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;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Nullable;
import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.MapUtils;
import org.apache.commons.lang.StringUtils;
import com.mysema.commons.lang.Assert;
@ -33,11 +31,17 @@ public final class BeanModel implements Comparable<BeanModel> {
private boolean entityModel = true;
private final Set<PropertyModel> entityProperties = new TreeSet<PropertyModel>();
// mutable
private int escapeSuffix = 1;
private boolean hasLists, hasMaps;
private final String prefix;
private final Set<PropertyModel> properties = new TreeSet<PropertyModel>();
@Nullable
private BeanModel superModel;
@ -45,15 +49,6 @@ public final class BeanModel implements Comparable<BeanModel> {
private final TypeModel typeModel;
private final Map<TypeCategory,Collection<PropertyModel>> typeToProperties = MapUtils.lazyMap(
new HashMap<TypeCategory,Collection<PropertyModel>>(),
new Factory<Collection<PropertyModel>>(){
@Override
public Collection<PropertyModel> create() {
return new HashSet<PropertyModel>();
}
});
private String uncapSimpleName;
public BeanModel(String prefix, TypeModel typeModel) {
@ -73,9 +68,19 @@ public final class BeanModel implements Comparable<BeanModel> {
}
public void addProperty(PropertyModel field) {
validateField(field);
Collection<PropertyModel> fields = typeToProperties.get(field.getTypeCategory());
fields.add(field);
properties.add(validateField(field));
switch(field.getTypeCategory()){
case ENTITYMAP:
case SIMPLEMAP:
hasMaps = true;
break;
case ENTITYLIST:
case SIMPLELIST:
hasLists = true;
break;
case ENTITY:
entityProperties.add(field);
}
}
public int compareTo(BeanModel o) {
@ -86,50 +91,10 @@ public final class BeanModel implements Comparable<BeanModel> {
return o instanceof BeanModel && typeModel.getName().equals(((BeanModel) o).typeModel.getName());
}
public Collection<PropertyModel> getBooleanProperties() {
return typeToProperties.get(TypeCategory.BOOLEAN);
}
public Collection<PropertyModel> getComparableProperties() {
return typeToProperties.get(TypeCategory.COMPARABLE);
}
public Collection<ConstructorModel> getConstructors() {
return constructors;
}
public Collection<PropertyModel> getDateProperties() {
return typeToProperties.get(TypeCategory.DATE);
}
public Collection<PropertyModel> getDateTimeProperties() {
return typeToProperties.get(TypeCategory.DATETIME);
}
public Collection<PropertyModel> getEntityCollections() {
return typeToProperties.get(TypeCategory.ENTITYCOLLECTION);
}
public Collection<PropertyModel> getEntityLists() {
return typeToProperties.get(TypeCategory.ENTITYLIST);
}
public Collection<PropertyModel> getEntityMaps() {
return typeToProperties.get(TypeCategory.ENTITYMAP);
}
public Collection<PropertyModel> getEntityProperties() {
return typeToProperties.get(TypeCategory.ENTITY);
}
public String getLocalName() {
return typeModel.getLocalName();
}
public String getName() {
return typeModel.getName();
}
public String getGenericName(){
if (typeModel.getParameterCount() == 0){
return typeModel.getLocalName();
@ -143,8 +108,20 @@ public final class BeanModel implements Comparable<BeanModel> {
}
}
public Collection<PropertyModel> getNumericProperties() {
return typeToProperties.get(TypeCategory.NUMERIC);
public String getLocalName() {
return typeModel.getLocalName();
}
public String getName() {
return typeModel.getName();
}
public Set<PropertyModel> getEntityProperties() {
return entityProperties;
}
public Set<PropertyModel> getProperties() {
return properties;
}
public String getPackageName() {
@ -155,29 +132,9 @@ public final class BeanModel implements Comparable<BeanModel> {
return prefix;
}
public Collection<PropertyModel> getSimpleCollections() {
return typeToProperties.get(TypeCategory.SIMPLECOLLECTION);
}
public Collection<PropertyModel> getSimpleLists() {
return typeToProperties.get(TypeCategory.SIMPLELIST);
}
public Collection<PropertyModel> getSimpleMaps() {
return typeToProperties.get(TypeCategory.SIMPLEMAP);
}
public String getSimpleName() {
return typeModel.getSimpleName();
}
public Collection<PropertyModel> getSimpleProperties() {
return typeToProperties.get(TypeCategory.SIMPLE);
}
public Collection<PropertyModel> getStringProperties() {
return typeToProperties.get(TypeCategory.STRING);
}
@Nullable
public BeanModel getSuperModel() {
@ -188,27 +145,32 @@ public final class BeanModel implements Comparable<BeanModel> {
return superTypes;
}
public Collection<PropertyModel> getTimeProperties() {
return typeToProperties.get(TypeCategory.TIME);
}
public String getUncapSimpleName() {
return uncapSimpleName;
}
public boolean hasEntityFields() {
return !entityProperties.isEmpty();
}
public int hashCode() {
return typeModel.getName().hashCode();
}
public boolean hasLists() {
return hasLists;
}
public boolean hasMaps() {
return hasMaps;
}
public void include(BeanModel clazz) {
for (TypeCategory category : TypeCategory.values()){
Collection<PropertyModel> source = clazz.typeToProperties.get(category);
if (!source.isEmpty()){
Collection<PropertyModel> target = typeToProperties.get(category);
for (PropertyModel field : source) {
target.add(validateField(field.createCopy(this)));
}
}
for (PropertyModel property : clazz.properties){
addProperty(property.createCopy(this));
}
}
@ -217,17 +179,14 @@ public final class BeanModel implements Comparable<BeanModel> {
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++);
@ -235,4 +194,5 @@ public final class BeanModel implements Comparable<BeanModel> {
return field;
}
}

View File

@ -22,7 +22,13 @@ public class EmbeddableSerializer extends EntitySerializer{
@Override
protected void introImports(StringBuilder builder, BeanModel model) {
if (model.hasEntityFields()){
builder.append("import com.mysema.query.util.*;\n");
}
builder.append("import com.mysema.query.types.path.*;\n\n");
if (model.hasLists() || model.hasMaps()){
builder.append("import com.mysema.query.types.expr.*;\n");
}
}
@Override

View File

@ -19,69 +19,37 @@ public class EntitySerializer implements Serializer{
// intro
intro(model, writer);
// fields
for (PropertyModel field : model.getStringProperties()){
stringField(field, writer);
}
for (PropertyModel field : model.getBooleanProperties()){
booleanField(field, writer);
}
for (PropertyModel field : model.getSimpleProperties()){
simpleField(field, writer);
}
for (PropertyModel field : model.getComparableProperties()){
comparableField(field, writer);
}
for (PropertyModel field : model.getDateProperties()){
dateField(field, writer);
}
for (PropertyModel field : model.getDateTimeProperties()){
dateTimeField(field, writer);
}
for (PropertyModel field : model.getTimeProperties()){
timeField(field, writer);
}
for (PropertyModel field : model.getNumericProperties()){
numericField(field, writer);
}
for (PropertyModel field : model.getSimpleCollections()){
collectionOfSimple(field, writer);
}
for (PropertyModel field : model.getEntityCollections()){
collectionOfEntity(field, writer);
}
for (PropertyModel field : model.getSimpleMaps()){
mapOfSimple(field, writer);
}
for (PropertyModel field : model.getEntityMaps()){
mapOfEntity(field, writer);
}
for (PropertyModel field : model.getSimpleLists()){
listSimple(field, writer);
}
for (PropertyModel field : model.getEntityLists()){
listOfEntity(field, writer);
}
for (PropertyModel field : model.getEntityProperties()){
entityField(field, writer);
for (PropertyModel property : model.getProperties()){
switch(property.getTypeCategory()){
case STRING: stringField(property, writer); break;
case BOOLEAN: booleanField(property, writer); break;
case SIMPLE: simpleField(property, writer); break;
case COMPARABLE: comparableField(property, writer); break;
case DATE: dateField(property, writer); break;
case DATETIME: dateTimeField(property, writer); break;
case TIME: timeField(property, writer); break;
case NUMERIC: numericField(property, writer); break;
case SIMPLECOLLECTION: collectionOfSimple(property, writer); break;
case ENTITYCOLLECTION: collectionOfEntity(property, writer); break;
case SIMPLEMAP: mapOfSimple(property, writer); break;
case ENTITYMAP: mapOfEntity(property, writer); break;
case SIMPLELIST: listOfSimple(property, writer); break;
case ENTITYLIST: listOfEntity(property, writer); break;
case ENTITY: entityField(property, writer); break;
}
}
// constructors
constructors(model, writer);
// accessors
for (PropertyModel field : model.getSimpleLists()){
listOfSimpleAccessor(field, writer);
for (PropertyModel property : model.getProperties()){
switch(property.getTypeCategory()){
case SIMPLEMAP: mapOfSimpleAccessor(property, writer); break;
case ENTITYMAP: mapOfEntityAccessor(property, writer); break;
case SIMPLELIST: listOfSimpleAccessor(property, writer); break;
case ENTITYLIST: listOfEntityAccessor(property, writer); break;
}
}
for (PropertyModel field : model.getEntityLists()){
listOfEntityAccessor(field, writer);
}
for (PropertyModel field : model.getSimpleMaps()){
mapOfSimpleAccessor(field, writer);
}
for (PropertyModel field : model.getEntityMaps()){
mapOfEntityAccessor(field, writer);
}
// outro
outro(model, writer);
@ -161,22 +129,31 @@ public class EntitySerializer implements Serializer{
constructorsForVariables(builder, model);
// 2
builder.append(" public " + queryType + "(PEntity<? extends "+genericName+"> entity) {\n");
builder.append(" "+thisOrSuper+"(entity.getType(), entity.getEntityName(), entity.getMetadata()");
if (hasEntityFields){
builder.append(", entity.getMetadata().isRoot() ? __inits : PathInits.DEFAULT");
}
builder.append(");\n");
builder.append(" }\n\n");
if (!hasEntityFields){
builder.append(" public " + queryType + "(PEntity<? extends "+genericName+"> entity) {\n");
builder.append(" super(entity.getType(), entity.getEntityName(), entity.getMetadata()");
builder.append(");\n");
builder.append(" }\n\n");
}
// 3
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
if (hasEntityFields){
builder.append(" this(metadata, metadata.isRoot() ? __inits : PathInits.DEFAULT);\n");
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
builder.append(" this(metadata, metadata.isRoot() ? __inits : PathInits.DEFAULT);\n");
builder.append(" }\n\n");
}else{
builder.append(" this(metadata, PathInits.DEFAULT);\n");
}
builder.append(" }\n\n");
if (!localName.equals(genericName)){
builder.append(" @SuppressWarnings(\"unchecked\")\n");
}
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
builder.append(" super(");
if (!localName.equals(genericName)){
builder.append("(Class)");
}
builder.append(localName+".class, \""+simpleName+"\", metadata);\n");
builder.append(" }\n\n");
}
// 4
if (!localName.equals(genericName)){
@ -192,10 +169,10 @@ public class EntitySerializer implements Serializer{
builder.append(", inits");
}
builder.append(");\n");
builder.append(" }\n\n");
builder.append(" }\n\n");
if (hasEntityFields){
// 5 (with entity field initialization)
// 5 (with entity field initialization)
if (hasEntityFields){
builder.append(" public "+queryType+"(Class<? extends "+genericName+"> type, @NotEmpty String entityName, PathMetadata<?> metadata, PathInits inits) {\n");
builder.append(" super(type, entityName, metadata);\n");
initEntityFields(builder, model);
@ -287,7 +264,7 @@ public class EntitySerializer implements Serializer{
}
protected void introInits(StringBuilder builder, BeanModel model) {
if (!model.getEntityProperties().isEmpty()){
if (model.hasEntityFields()){
List<String> inits = new ArrayList<String>();
for (PropertyModel property : model.getEntityProperties()){
for (String init : property.getInits()){
@ -313,7 +290,7 @@ public class EntitySerializer implements Serializer{
if (!model.getPackageName().equals(superModel.getPackageName())){
superQueryType = superModel.getPackageName() + "." + superQueryType;
}
if (superModel.getEntityProperties().isEmpty()){
if (!superModel.hasEntityFields()){
builder.append(" public final "+superQueryType+" _super = new " + superQueryType + "(this);\n\n");
}else{
builder.append(" public final "+superQueryType+" _super;\n\n");
@ -339,7 +316,7 @@ public class EntitySerializer implements Serializer{
protected void introImports(StringBuilder builder, BeanModel model) {
builder.append("import com.mysema.query.util.*;\n");
builder.append("import com.mysema.query.types.path.*;\n");
if (!model.getConstructors().isEmpty()){
if (!model.getConstructors().isEmpty() || model.hasLists() || model.hasMaps()){
builder.append("import com.mysema.query.types.expr.*;\n");
}
}
@ -372,7 +349,7 @@ public class EntitySerializer implements Serializer{
builder.append(" public " + queryType + " " + escapedName + "(int index) {\n");
builder.append(" return " + escapedName + ".get(index);\n");
builder.append(" }\n\n");
builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr<Integer> index) {\n");
builder.append(" public " + queryType + " " + escapedName + "(Expr<Integer> index) {\n");
builder.append(" return " + escapedName + ".get(index);\n");
builder.append(" }\n\n");
writer.append(builder.toString());
@ -386,14 +363,14 @@ public class EntitySerializer implements Serializer{
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(int index) {\n");
builder.append(" return " + escapedName + ".get(index);\n");
builder.append(" }\n\n");
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(com.mysema.query.types.expr.Expr<Integer> index) {\n");
builder.append(" public PSimple<" + valueType + "> " + escapedName + "(Expr<Integer> index) {\n");
builder.append(" return " + escapedName + ".get(index);\n");
builder.append(" }\n\n");
writer.append(builder.toString());
}
protected void listSimple(PropertyModel field, Writer writer) throws IOException {
protected void listOfSimple(PropertyModel field, Writer writer) throws IOException {
serialize(field, "PComponentList<" + field.getTypeName()+">", writer, "createSimpleList", field.getTypeName()+".class");
}
@ -422,7 +399,7 @@ public class EntitySerializer implements Serializer{
builder.append(" public " + queryType + " " + escapedName + "(" + keyType+ " key) {\n");
builder.append(" return " + escapedName + ".get(key);\n");
builder.append(" }\n\n");
builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n");
builder.append(" public " + queryType + " " + escapedName + "(Expr<"+genericKey+"> key) {\n");
builder.append(" return " + escapedName + ".get(key);\n");
builder.append(" }\n\n");
writer.append(builder.toString());
@ -452,7 +429,7 @@ public class EntitySerializer implements Serializer{
builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(" + genericKey + " key) {\n");
builder.append(" return " + escapedName + ".get(key);\n");
builder.append(" }\n\n");
builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n");
builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(Expr<"+genericKey+"> key) {\n");
builder.append(" return " + escapedName + ".get(key);\n");
builder.append(" }\n\n");
writer.append(builder.toString());
@ -471,7 +448,7 @@ public class EntitySerializer implements Serializer{
BeanModel superModel = field.getBeanModel().getSuperModel();
// construct value
StringBuilder value = new StringBuilder();
if (field.isInherited() && superModel != null && superModel.getEntityProperties().isEmpty()){
if (field.isInherited() && superModel != null && !superModel.hasEntityFields()){
// copy from super
value.append("_super." + field.getEscapedName());
}else{

View File

@ -58,6 +58,9 @@ public class SupertypeSerializer extends EntitySerializer{
builder.append("import com.mysema.query.util.*;\n");
}
builder.append("import com.mysema.query.types.path.*;\n\n");
if (model.hasLists() || model.hasMaps()){
builder.append("import com.mysema.query.types.expr.*;\n");
}
}
@Override

View File

@ -33,16 +33,16 @@ public class ClassModelTest {
public void test() {
BeanModelFactory factory = new BeanModelFactory(new TypeModelFactory(QueryEntity.class));
BeanModel type = factory.create(TestType.class, "Q");
assertEquals(1, type.getEntityMaps().size());
assertEquals(1, type.getSimpleMaps().size());
assertEquals(2, type.getEntityCollections().size());
assertEquals(2, type.getSimpleCollections().size());
assertEquals(1, type.getEntityLists().size());
assertEquals(1, type.getSimpleLists().size());
// assertEquals(1, type.getEntityMaps().size());
// assertEquals(1, type.getSimpleMaps().size());
// assertEquals(2, type.getEntityCollections().size());
// assertEquals(2, type.getSimpleCollections().size());
// assertEquals(1, type.getEntityLists().size());
// assertEquals(1, type.getSimpleLists().size());
assertEquals(1, type.getEntityProperties().size());
assertEquals(1, type.getStringProperties().size());
assertEquals(2, type.getNumericProperties().size());
assertEquals(3, type.getSimpleProperties().size());
// assertEquals(1, type.getStringProperties().size());
// assertEquals(2, type.getNumericProperties().size());
// assertEquals(3, type.getSimpleProperties().size());
}
/**