mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
#595972 : improved serialization of custom types
This commit is contained in:
parent
9eb73e61ec
commit
2d2f5a3cfd
@ -26,8 +26,8 @@ public class QueryExtensions2Test {
|
||||
@QueryMethod("geo_distance({0}, {1})")
|
||||
int geoDistance(Point otherPoint);
|
||||
|
||||
@QueryMethod("")
|
||||
Point getPoint();
|
||||
// @QueryMethod("")
|
||||
// Point getPoint();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.mysema.query.domain;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
@ -9,6 +9,7 @@ import org.junit.Test;
|
||||
import com.mysema.commons.lang.Pair;
|
||||
import com.mysema.query.annotations.QueryDelegate;
|
||||
import com.mysema.query.annotations.QueryEntity;
|
||||
import com.mysema.query.annotations.QuerydslConfig;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.path.PBoolean;
|
||||
import com.mysema.query.types.path.PDate;
|
||||
@ -48,8 +49,11 @@ public class QueryExtensions8Test {
|
||||
|
||||
|
||||
@QueryEntity
|
||||
@QuerydslConfig(entityAccessors=true)
|
||||
public static class Entity {
|
||||
|
||||
Entity superior;
|
||||
|
||||
Date dateVal;
|
||||
|
||||
Boolean booleanVal;
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
<dependency>
|
||||
<groupId>com.mysema.codegen</groupId>
|
||||
<artifactId>codegen</artifactId>
|
||||
<version>0.1.5</version>
|
||||
<version>0.1.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@ -579,7 +579,19 @@ public class EntitySerializer implements Serializer{
|
||||
writer.publicFinal(type, field.getEscapedName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void customField(EntityType model, Property field, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
String queryType = typeMappings.getPathType(field.getType(), model, false);
|
||||
writer.line("// custom");
|
||||
if (field.isInherited()){
|
||||
writer.line("// inherited");
|
||||
writer.publicFinal(queryType, field.getEscapedName(),"_super." + field.getEscapedName());
|
||||
}else{
|
||||
String value = NEW + queryType + "(forProperty(\"" + field.getName() + "\"))";
|
||||
writer.publicFinal(queryType, field.getEscapedName(), value);
|
||||
}
|
||||
}
|
||||
|
||||
protected void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
|
||||
for (Property property : model.getProperties()){
|
||||
String queryType = typeMappings.getPathType(property.getType(), model, false);
|
||||
@ -611,6 +623,9 @@ public class EntitySerializer implements Serializer{
|
||||
case NUMERIC:
|
||||
serialize(model, property, queryType, writer, "createNumber", localRawName +DOT_CLASS);
|
||||
break;
|
||||
case CUSTOM:
|
||||
customField(model, property, config, writer);
|
||||
break;
|
||||
case ARRAY:
|
||||
localGenericName = property.getParameter(0).getLocalGenericName(model, true);
|
||||
serialize(model, property, "PArray<" + localGenericName + ">", writer, "createArray",localRawName+DOT_CLASS);
|
||||
@ -646,7 +661,7 @@ public class EntitySerializer implements Serializer{
|
||||
queryType = typeMappings.getPathType(property.getParameter(0), model, true);
|
||||
|
||||
serialize(model, property, "PList<" + localGenericName+ COMMA + genericQueryType + ">", writer, "createList", localRawName+DOT_CLASS, queryType +DOT_CLASS);
|
||||
break;
|
||||
break;
|
||||
case ENTITY:
|
||||
entityField(model, property, config, writer);
|
||||
break;
|
||||
@ -654,4 +669,6 @@ public class EntitySerializer implements Serializer{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -104,8 +104,12 @@ public final class EntityType extends TypeAdapter implements Comparable<EntityTy
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public TypeCategory getCategory() {
|
||||
return TypeCategory.ENTITY;
|
||||
public TypeCategory getCategory() {
|
||||
if (getType().getCategory() == TypeCategory.ENTITY || !properties.isEmpty()){
|
||||
return TypeCategory.ENTITY;
|
||||
}else{
|
||||
return TypeCategory.CUSTOM;
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Constructor> getConstructors() {
|
||||
|
||||
@ -69,6 +69,11 @@ public enum TypeCategory {
|
||||
"org.joda.time.Instant",
|
||||
"org.joda.time.DateTime",
|
||||
"org.joda.time.DateMidnight"),
|
||||
/**
|
||||
*
|
||||
*/
|
||||
CUSTOM(null),
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
@ -66,6 +66,7 @@ public class TypeMappings {
|
||||
register(TypeCategory.MAP, Expr.class, PSimple.class, CSimple.class);
|
||||
register(TypeCategory.SIMPLE, Expr.class, PSimple.class, CSimple.class);
|
||||
|
||||
register(TypeCategory.CUSTOM, Expr.class, Path.class, CSimple.class);
|
||||
register(TypeCategory.ENTITY, Expr.class, Path.class, CSimple.class);
|
||||
}
|
||||
|
||||
@ -102,7 +103,7 @@ public class TypeMappings {
|
||||
String localName = null;
|
||||
TypeCategory category = type.getCategory();
|
||||
|
||||
if (raw && category != TypeCategory.ENTITY){
|
||||
if (raw && category != TypeCategory.ENTITY && category != TypeCategory.CUSTOM){
|
||||
return typeName;
|
||||
}
|
||||
|
||||
@ -118,7 +119,7 @@ public class TypeMappings {
|
||||
if (category == TypeCategory.STRING || category == TypeCategory.BOOLEAN){
|
||||
return typeName;
|
||||
|
||||
}else if (category == TypeCategory.ENTITY){
|
||||
}else if (category == TypeCategory.ENTITY || category == TypeCategory.CUSTOM){
|
||||
String suffix;
|
||||
if (!type.getPackageName().isEmpty()){
|
||||
suffix = type.getFullName().substring(type.getPackageName().length()+1).replace('.', '_');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user