mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
#39 : added toString and full constructor support for BeanSerializer
This commit is contained in:
parent
6e7146ea95
commit
03d721ee68
@ -7,13 +7,17 @@ package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections15.Transformer;
|
||||
|
||||
import com.mysema.codegen.CodeWriter;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.TypeCategory;
|
||||
import com.mysema.codegen.model.Types;
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
@ -24,10 +28,19 @@ import com.mysema.util.BeanUtils;
|
||||
*
|
||||
*/
|
||||
public class BeanSerializer implements Serializer{
|
||||
|
||||
private static final Transformer<Property, Parameter> propertyToParameter = new Transformer<Property, Parameter>() {
|
||||
@Override
|
||||
public Parameter transform(Property input) {
|
||||
return new Parameter(input.getName(), input.getType());
|
||||
}
|
||||
};
|
||||
|
||||
private final boolean propertyAnnotations;
|
||||
|
||||
private final String javadocSuffix;
|
||||
|
||||
private boolean addToString, addFullConstructor;
|
||||
|
||||
public BeanSerializer() {
|
||||
this(true, " is a Querydsl bean type");
|
||||
@ -63,6 +76,9 @@ public class BeanSerializer implements Serializer{
|
||||
if (model.hasMaps()) {
|
||||
importedClasses.add(Map.class.getName());
|
||||
}
|
||||
if (addToString && model.hasArrays()) {
|
||||
importedClasses.add(Arrays.class.getName());
|
||||
}
|
||||
writer.importClasses(importedClasses.toArray(new String[importedClasses.size()]));
|
||||
|
||||
// javadoc
|
||||
@ -76,6 +92,10 @@ public class BeanSerializer implements Serializer{
|
||||
|
||||
bodyStart(model, writer);
|
||||
|
||||
if (addFullConstructor) {
|
||||
addFullConstructor(model, writer);
|
||||
}
|
||||
|
||||
// fields
|
||||
for (Property property : model.getProperties()) {
|
||||
if (propertyAnnotations) {
|
||||
@ -99,12 +119,50 @@ public class BeanSerializer implements Serializer{
|
||||
writer.line("this.", propertyName, " = ", propertyName, ";");
|
||||
writer.end();
|
||||
}
|
||||
|
||||
if (addToString) {
|
||||
addToString(model, writer);
|
||||
}
|
||||
|
||||
bodyEnd(model, writer);
|
||||
|
||||
writer.end();
|
||||
}
|
||||
|
||||
protected void addFullConstructor(EntityType model, CodeWriter writer) throws IOException {
|
||||
// public empty constructor
|
||||
writer.beginConstructor();
|
||||
writer.end();
|
||||
|
||||
// full constructor
|
||||
writer.beginConstructor(model.getProperties(), propertyToParameter);
|
||||
for (Property property : model.getProperties()) {
|
||||
writer.line("this.", property.getEscapedName(), " = ", property.getEscapedName(), ";");
|
||||
}
|
||||
writer.end();
|
||||
}
|
||||
|
||||
protected void addToString(EntityType model, CodeWriter writer) throws IOException {
|
||||
writer.beginPublicMethod(Types.STRING, "toString");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Property property : model.getProperties()) {
|
||||
String propertyName = property.getEscapedName();
|
||||
if (builder.length() > 0) {
|
||||
builder.append(" + \", ");
|
||||
} else {
|
||||
builder.append("\"");
|
||||
}
|
||||
builder.append(propertyName + " = \" + ");
|
||||
if (property.getType().getCategory() == TypeCategory.ARRAY) {
|
||||
builder.append("Arrays.toString(" + propertyName + ")");
|
||||
} else {
|
||||
builder.append(propertyName);
|
||||
}
|
||||
}
|
||||
writer.line(" return ", builder.toString(), ";");
|
||||
writer.end();
|
||||
}
|
||||
|
||||
protected void bodyStart(EntityType model, CodeWriter writer) throws IOException {
|
||||
// template method
|
||||
}
|
||||
@ -128,4 +186,13 @@ public class BeanSerializer implements Serializer{
|
||||
return imports;
|
||||
}
|
||||
|
||||
public void setAddToString(boolean addToString) {
|
||||
this.addToString = addToString;
|
||||
}
|
||||
|
||||
public void setAddFullConstructor(boolean addFullConstructor) {
|
||||
this.addFullConstructor = addFullConstructor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -157,6 +157,10 @@ public class EntityType extends TypeAdapter implements Comparable<EntityType> {
|
||||
return getFullName().hashCode();
|
||||
}
|
||||
|
||||
public boolean hasArrays() {
|
||||
return hasPropertyWithType(TypeCategory.ARRAY);
|
||||
}
|
||||
|
||||
public boolean hasEntityFields() {
|
||||
return hasPropertyWithType(TypeCategory.ENTITY);
|
||||
}
|
||||
|
||||
@ -91,6 +91,41 @@ public class BeanSerializerTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ToString() throws IOException{
|
||||
// property
|
||||
type.addProperty(new Property(type, "entityField", type));
|
||||
type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
|
||||
type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
|
||||
type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
|
||||
type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
|
||||
type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
|
||||
|
||||
BeanSerializer serializer = new BeanSerializer();
|
||||
serializer.setAddToString(true);
|
||||
serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
|
||||
System.out.println(writer.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void FullConstructor() throws IOException{
|
||||
// property
|
||||
type.addProperty(new Property(type, "entityField", type));
|
||||
type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
|
||||
type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
|
||||
type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
|
||||
type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
|
||||
type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
|
||||
|
||||
BeanSerializer serializer = new BeanSerializer();
|
||||
serializer.setAddFullConstructor(true);
|
||||
serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
|
||||
System.out.println(writer.toString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Properties() throws IOException{
|
||||
// property
|
||||
|
||||
Loading…
Reference in New Issue
Block a user