From 03d721ee6877bd58a2bf622c10c26550cda36fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Tue, 15 Nov 2011 20:54:41 +0200 Subject: [PATCH] #39 : added toString and full constructor support for BeanSerializer --- .../mysema/query/codegen/BeanSerializer.java | 67 +++++++++++++++++++ .../com/mysema/query/codegen/EntityType.java | 4 ++ .../query/codegen/BeanSerializerTest.java | 35 ++++++++++ 3 files changed, 106 insertions(+) diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/BeanSerializer.java b/querydsl-core/src/main/java/com/mysema/query/codegen/BeanSerializer.java index e14d7135c..967577473 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/BeanSerializer.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/BeanSerializer.java @@ -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 propertyToParameter = new Transformer() { + @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; + } + + } diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/EntityType.java b/querydsl-core/src/main/java/com/mysema/query/codegen/EntityType.java index fab063740..6819c7912 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/EntityType.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/EntityType.java @@ -157,6 +157,10 @@ public class EntityType extends TypeAdapter implements Comparable { return getFullName().hashCode(); } + public boolean hasArrays() { + return hasPropertyWithType(TypeCategory.ARRAY); + } + public boolean hasEntityFields() { return hasPropertyWithType(TypeCategory.ENTITY); } diff --git a/querydsl-core/src/test/java/com/mysema/query/codegen/BeanSerializerTest.java b/querydsl-core/src/test/java/com/mysema/query/codegen/BeanSerializerTest.java index 5faad1619..e2d860520 100644 --- a/querydsl-core/src/test/java/com/mysema/query/codegen/BeanSerializerTest.java +++ b/querydsl-core/src/test/java/com/mysema/query/codegen/BeanSerializerTest.java @@ -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