diff --git a/querydsl-root/pom.xml b/querydsl-root/pom.xml index 5f5c32a1d..0fb6bf296 100644 --- a/querydsl-root/pom.xml +++ b/querydsl-root/pom.xml @@ -31,7 +31,7 @@ 4.01 3.0.1 2.1 - 0.4.4 + 0.4.5 0.2.2 2.2 1.3.2 diff --git a/querydsl-scala/.cache b/querydsl-scala/.cache deleted file mode 100644 index f89dfe1c8..000000000 Binary files a/querydsl-scala/.cache and /dev/null differ diff --git a/querydsl-scala/src/main/scala/com/mysema/query/scala/BeanSerializer.scala b/querydsl-scala/src/main/scala/com/mysema/query/scala/BeanSerializer.scala index 128db8c58..1e7030792 100644 --- a/querydsl-scala/src/main/scala/com/mysema/query/scala/BeanSerializer.scala +++ b/querydsl-scala/src/main/scala/com/mysema/query/scala/BeanSerializer.scala @@ -2,7 +2,7 @@ package com.mysema.query.scala import com.mysema.query.codegen._ import com.mysema.codegen.{ CodeWriter, ScalaWriter } -import com.mysema.codegen.model.Type +import com.mysema.codegen.model.{ Parameter, Type } import com.mysema.query import javax.inject.Inject @@ -14,19 +14,50 @@ import scala.reflect.BeanProperty import scala.collection.JavaConversions._ import scala.collection.mutable.Set -/** - * BeanSerializer for Scala - * - * @author tiwe - * - */ -class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends Serializer { +class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends BeanSerializer(typeMappings) { + + var javadocSuffix = " is a Querydsl bean type" + + var javaBeanSupport = true + + def writeClass(model: EntityType, writer: ScalaWriter) = { + // javadoc + writer.javadoc(model.getSimpleName + javadocSuffix) + + // header + model.getAnnotations foreach (writer.annotation(_)) + writer.beginClass(model) + + // properties + model.getProperties foreach { property => + property.getAnnotations.foreach(writer.annotation(_)) + if (javaBeanSupport) writer.line("@BeanProperty") + writer.publicField(property.getType(), property.getEscapedName, "_") + } + + writer.end() + } + +} + +class CaseClassSerializer @Inject() (typeMappings: TypeMappings) extends BeanSerializer(typeMappings) { + + def javaBeanSupport = false + + def writeClass(model: EntityType, writer: ScalaWriter) = { + model.getAnnotations foreach (writer.annotation(_)) + val parameters = model.getProperties.map(p => new Parameter(p.getEscapedName, p.getType)).toArray + writer.caseClass(model.getSimpleName, parameters:_*) + } + +} + + +abstract class BeanSerializer(typeMappings: TypeMappings) extends Serializer { var createCompanionObject = true - var javaBeanSupport = true - - var javadocSuffix = " is a Querydsl bean type" + def javaBeanSupport: Boolean def serialize(model: EntityType, serializerConfig: SerializerConfig, writer: CodeWriter) { val simpleName = model.getSimpleName @@ -47,24 +78,12 @@ class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends Seriali writeCompanionObject(model, queryType, writer.asInstanceOf[ScalaWriter]) } - // javadoc - writer.javadoc(simpleName + javadocSuffix) - - // header - model.getAnnotations foreach (writer.annotation(_)) - writer.beginClass(model) - - // properties - model.getProperties foreach { property => - property.getAnnotations.foreach(writer.annotation(_)) - if (javaBeanSupport) writer.line("@BeanProperty") - writer.publicField(property.getType(), property.getEscapedName, "_") - } - - writer.end() + writeClass(model, writer.asInstanceOf[ScalaWriter]) } - private def writeCompanionObject(model: EntityType, queryType: Type, writer: ScalaWriter) = { + def writeClass(model: EntityType, writer: ScalaWriter) + + def writeCompanionObject(model: EntityType, queryType: Type, writer: ScalaWriter) = { val modelName = writer.getRawName(model) val queryTypeName = writer.getRawName(queryType) val variable = model.getUncapSimpleName @@ -75,7 +94,6 @@ class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends Seriali writer.end() } - private def getAnnotationTypes(model: EntityType): Set[String] = { val imports = Set() ++ model.getAnnotations.map(_.annotationType.getName); // flatMap flattens the results of the map-function. @@ -83,4 +101,6 @@ class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends Seriali imports ++ model.getProperties.flatMap(_.getAnnotations.map(_.annotationType.getName)); } -} \ No newline at end of file +} + + diff --git a/querydsl-scala/src/test/scala/com/mysema/query/scala/CaseClassSerializerTest.scala b/querydsl-scala/src/test/scala/com/mysema/query/scala/CaseClassSerializerTest.scala new file mode 100644 index 000000000..041087287 --- /dev/null +++ b/querydsl-scala/src/test/scala/com/mysema/query/scala/CaseClassSerializerTest.scala @@ -0,0 +1,58 @@ +package com.mysema.query.scala + +import org.apache.commons.lang3.StringUtils +import com.mysema.codegen._; +import com.mysema.codegen.model._; +import com.mysema.query.codegen._; + +import java.io.StringWriter; + +import org.junit._ +import org.junit.Assert._ + +import scala.collection.JavaConversions._ + +class CaseClassSerializerTest extends CompileTestUtils { + + val typeMappings = ScalaTypeMappings.create + + var entityType: EntityType = null + + var writer = new StringWriter() + + @Before + def setUp() { + // type + val typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false, false) + entityType = new EntityType(typeModel) + + // property + entityType.addProperty(new Property(entityType, "entityField", entityType, new Array[String](0))) + entityType.addProperty(new Property(entityType, "collection", new SimpleType(Types.COLLECTION, typeModel), new Array[String](0))) + entityType.addProperty(new Property(entityType, "listField", new SimpleType(Types.LIST, typeModel), new Array[String](0))) + entityType.addProperty(new Property(entityType, "setField", new SimpleType(Types.SET, typeModel), new Array[String](0))) + entityType.addProperty(new Property(entityType, "arrayField", new ClassType(TypeCategory.ARRAY, classOf[Array[String]]), new Array[String](0))) + entityType.addProperty(new Property(entityType, "mapField", new SimpleType(Types.MAP, typeModel, typeModel), new Array[String](0))) + } + + @Test + def Print { + val serializer = new CaseClassSerializer(typeMappings) + typeMappings.register(entityType, new QueryTypeFactoryImpl("Q", "", "").create(entityType)) + serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new ScalaWriter(writer)) + + println(writer.toString) + } + + @Test + def Compile { + val serializer = new CaseClassSerializer(typeMappings) + serializer.createCompanionObject = false + typeMappings.register(entityType, new QueryTypeFactoryImpl("Q", "", "").create(entityType)) + serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new ScalaWriter(writer)) + val str = writer.toString() + assertCompileSuccess(str) + } + +} + \ No newline at end of file