further improvements to Scala module

This commit is contained in:
Timo Westkämper 2011-11-17 17:08:50 +02:00
parent de3ccf8773
commit efde50e2ce
13 changed files with 27 additions and 42 deletions

View File

@ -113,10 +113,6 @@ public class ConstantImpl<T> extends ExpressionBase<T> implements Constant<T> {
public static <T> Constant<Class<T>> create(Class<T> constant) {
return new ConstantImpl<Class<T>>(constant);
}
// public static <C> Constant<C> create(C value){
// return new ConstantImpl<C>(value);
// }
private final T constant;

View File

@ -18,7 +18,7 @@ public abstract class ExpressionBase<T> implements Expression<T>{
private static final long serialVersionUID = -8862014178653364345L;
protected final Class<? extends T> type;
private final Class<? extends T> type;
@Nullable
private volatile String toString;

View File

@ -62,7 +62,7 @@ public class OperationImpl<T> extends ExpressionBase<T> implements Operation<T>{
Operation<?> op = (Operation<?>)o;
return op.getOperator().equals(operator)
&& op.getArgs().equals(args)
&& op.getType().equals(type);
&& op.getType().equals(getType());
} else {
return false;
}
@ -75,7 +75,7 @@ public class OperationImpl<T> extends ExpressionBase<T> implements Operation<T>{
@Override
public int hashCode(){
return type.hashCode();
return getType().hashCode();
}
}

View File

@ -76,10 +76,10 @@ public class PathImpl<T> extends ExpressionBase<T> implements Path<T> {
if (metadata.getPathType() == PathType.PROPERTY) {
Class<?> beanClass = metadata.getParent().getType();
String propertyName = metadata.getExpression().toString();
annotatedElement = ReflectionUtils.getAnnotatedElement(beanClass, propertyName, type);
annotatedElement = ReflectionUtils.getAnnotatedElement(beanClass, propertyName, getType());
} else {
annotatedElement = type;
annotatedElement = getType();
}
}
return annotatedElement;

View File

@ -144,7 +144,7 @@ public class QBean<T> extends ExpressionBase<T> implements FactoryExpression<T>{
private void initFields() {
for (String property : bindings.keySet()){
Class<?> beanType = type;
Class<?> beanType = getType();
while (!beanType.equals(Object.class)) {
try {
Field field = beanType.getDeclaredField(property);

View File

@ -42,7 +42,7 @@ public class SubQueryExpressionImpl<T> extends ExpressionBase<T> implements SubQ
}
public int hashCode(){
return type.hashCode();
return getType().hashCode();
}
@Override

View File

@ -83,7 +83,7 @@ public class TemplateExpressionImpl<T> extends ExpressionBase<T> implements Temp
} else if (o instanceof TemplateExpression) {
TemplateExpression<?> c = (TemplateExpression<?>)o;
return c.getTemplate().equals(template)
&& c.getType().equals(type);
&& c.getType().equals(getType());
} else {
return false;
}

View File

@ -150,7 +150,7 @@ public abstract class SimpleExpression<T> extends ExpressionBase<T> {
@Override
public int hashCode() {
return type.hashCode();
return getType().hashCode();
}
/**

Binary file not shown.

View File

@ -18,6 +18,7 @@ import java.io.IOException
import scala.reflect.BeanProperty
import scala.collection.JavaConversions._
import scala.collection.mutable.Set
import scala.collection.immutable.Map
import javax.inject.Inject;
@ -30,7 +31,11 @@ import javax.inject.Inject;
class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Serializer {
// val typeMappings = ScalaTypeMappings.typeMappings
private val methodNames = Map(ARRAY->"Array", BOOLEAN->"Boolean", COLLECTION->"Collection", COMPARABLE->"Comparable",
DATE->"Date", DATETIME->"DateTime", ENUM->"Enum", LIST->"List", MAP->"Map", NUMERIC->"Number", SET->"Set",
SIMPLE->"Simple", STRING->"String", TIME->"Time")
val classHeaderFormat = "%1$s(cl: Class[_ <: %2$s], md: PathMetadata[_]) extends EntityPathImpl[%2$s](cl, md)"
def serialize(model: EntityType, serializerConfig: SerializerConfig, writer: CodeWriter) {
@ -45,8 +50,8 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se
writer.staticimports(classOf[PathMetadataFactory])
var importedClasses = getAnnotationTypes(model)
if (model.hasLists()) importedClasses.add(classOf[List[_]].getName)
if (model.hasMaps()) importedClasses.add(classOf[Map[_, _]].getName)
if (model.hasLists()) importedClasses.add(classOf[java.util.List[_]].getName)
if (model.hasMaps()) importedClasses.add(classOf[java.util.Map[_, _]].getName)
writer.importClasses(importedClasses.toArray: _*)
writeHeader(model, scalaWriter)
@ -102,7 +107,7 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se
def writeAdditionalCompanionContent(model: EntityType, writer: ScalaWriter) = {}
private def serializeEntityProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) = {
private def getEntityProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) = {
for (property <- properties if property.getType.getCategory == ENTITY) yield {
val queryType = typeMappings.getPathType(property.getType, model, false)
val typeName = writer.getRawName(queryType)
@ -111,24 +116,9 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se
}
}
private def serializeOtherProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) = {
private def getOtherProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) = {
for (property <- properties if property.getType.getCategory != ENTITY) yield {
val methodName: String = property.getType.getCategory match {
case ARRAY => "createArray"
case BOOLEAN => "createBoolean"
case COLLECTION => "createCollection"
case COMPARABLE => "createComparable"
case DATE => "createDate"
case DATETIME => "createDateTime"
case ENUM => "createEnum"
case LIST => "createList"
case MAP => "createMap"
case NUMERIC => "createNumber"
case SET => "createSet"
case SIMPLE => "createSimple"
case STRING => "createString"
case TIME => "createTime"
}
val methodName: String = "create" + methodNames(property.getType.getCategory)
val value = property.getType.getCategory match {
case BOOLEAN | STRING => methodName + "(\"" + property.getName + "\")"
@ -151,20 +141,18 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se
def serializeProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) {
// entity properties
serializeEntityProperties(model, writer, properties) foreach (writer.line(_, "\n"))
getEntityProperties(model, writer, properties) foreach (writer.line(_, "\n"))
// other properties
serializeOtherProperties(model, writer, properties) foreach { case (propertyName, value) =>
getOtherProperties(model, writer, properties) foreach { case (propertyName, value) =>
writer.line("val ", escape(propertyName), " = ", value, "\n")
}
}
def escape(token: String): String = {
if (token == "type") "type$" // type is already available as a property in Expression
else if (ScalaSyntaxUtils.isReserved(token)) "`" + token + "`"
else token
if (ScalaSyntaxUtils.isReserved(token)) "`" + token + "`" else token
}
def getAnnotationTypes(model: EntityType): Set[String] = {
Set() ++ (model.getAnnotations.map(_.annotationType.getName))
}

View File

@ -62,6 +62,7 @@ class ScalaEntitySerializerTest extends CompileTestUtils {
val serializer = new ScalaEntitySerializer(typeMappings)
serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new ScalaWriter(writer))
val str = writer.toString()
System.err.println(str);
assertCompileSuccess(str)
}
}

View File

@ -405,7 +405,7 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>> extends
if (bindings.isEmpty()) {
throw new IllegalArgumentException("No bindings could be derived from " + expr);
}
return new QBean<RT>((Class)expr.getType(), bindings);
return new QBean<RT>((Class)expr.getType(), true, bindings);
}catch(IllegalAccessException e) {
throw new QueryException(e);
}

View File

@ -91,7 +91,7 @@ public class RelationalFunctionCall<T> extends SimpleExpression<T> implements Te
} else if (o instanceof TemplateExpression) {
TemplateExpression<?> c = (TemplateExpression<?>)o;
return c.getTemplate().equals(template)
&& c.getType().equals(type);
&& c.getType().equals(getType());
} else {
return false;
}