From 00358cc65244f76288645149e0bfe9d6b84ca917 Mon Sep 17 00:00:00 2001 From: Marcello Romano Date: Sat, 25 Jul 2015 23:25:05 +0100 Subject: [PATCH] Default variable name customization --- .../java/com/mysema/query/apt/APTOptions.java | 2 + .../query/apt/AbstractQuerydslProcessor.java | 2 +- .../com/mysema/query/apt/Configuration.java | 12 +++-- .../query/apt/DefaultConfiguration.java | 23 ++++++++ .../mysema/query/apt/ExtendedTypeFactory.java | 13 +++-- .../mysema/query/apt/TypeElementHandler.java | 2 +- .../mysema/query/codegen/CodegenModule.java | 3 ++ .../codegen/DefaultVariableNameFunction.java | 23 ++++++++ .../query/codegen/EntitySerializer.java | 2 +- .../com/mysema/query/codegen/EntityType.java | 54 ++++++++++++++----- .../mysema/query/codegen/GenericExporter.java | 3 +- .../com/mysema/query/codegen/TypeFactory.java | 26 ++++----- .../mysema/query/codegen/EntityTypeTest.java | 10 ++-- .../jpa/codegen/AbstractDomainExporter.java | 19 ++++--- .../query/scala/ScalaBeanSerializer.scala | 6 +-- .../query/scala/ScalaEntitySerializer.scala | 6 +-- .../query/sql/codegen/MetaDataExporter.java | 5 +- 17 files changed, 155 insertions(+), 56 deletions(-) create mode 100644 querydsl-codegen/src/main/java/com/mysema/query/codegen/DefaultVariableNameFunction.java diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/APTOptions.java b/querydsl-apt/src/main/java/com/mysema/query/apt/APTOptions.java index d5e4723c5..ab300cb9d 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/APTOptions.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/APTOptions.java @@ -49,6 +49,8 @@ public final class APTOptions { public static final String QUERYDSL_UNKNOWN_AS_EMBEDDABLE = "querydsl.unknownAsEmbeddable"; + public static final String QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS = "querydsl.variableNameFunctionClass"; + private APTOptions() {} } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/AbstractQuerydslProcessor.java b/querydsl-apt/src/main/java/com/mysema/query/apt/AbstractQuerydslProcessor.java index c34d0281d..118f05b70 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/AbstractQuerydslProcessor.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/AbstractQuerydslProcessor.java @@ -90,7 +90,7 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor { Set> entityAnnotations = conf.getEntityAnnotations(); TypeMappings typeMappings = conf.getTypeMappings(); QueryTypeFactory queryTypeFactory = conf.getQueryTypeFactory(); - this.typeFactory = new ExtendedTypeFactory(processingEnv, conf, entityAnnotations, typeMappings, queryTypeFactory); + this.typeFactory = new ExtendedTypeFactory(processingEnv, conf, entityAnnotations, typeMappings, queryTypeFactory, conf.getVariableNameFunction()); elementHandler = new TypeElementHandler(conf, typeFactory, typeMappings, queryTypeFactory); this.roundEnv = roundEnv; diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/Configuration.java b/querydsl-apt/src/main/java/com/mysema/query/apt/Configuration.java index ae53f88af..3189445d4 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/Configuration.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/Configuration.java @@ -13,17 +13,19 @@ */ package com.mysema.query.apt; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.List; +import java.util.Set; + import javax.annotation.Nullable; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; -import java.lang.annotation.Annotation; -import java.util.Collection; -import java.util.List; -import java.util.Set; +import com.google.common.base.Function; import com.mysema.query.codegen.*; import com.mysema.util.Annotations; @@ -229,4 +231,6 @@ public interface Configuration { */ boolean isStrictMode(); + Function getVariableNameFunction(); + } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/DefaultConfiguration.java b/querydsl-apt/src/main/java/com/mysema/query/apt/DefaultConfiguration.java index 4b1ae5f3f..2430f9c06 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/DefaultConfiguration.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/DefaultConfiguration.java @@ -24,6 +24,7 @@ import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.*; import javax.lang.model.type.TypeMirror; +import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.mysema.codegen.model.ClassType; @@ -74,6 +75,8 @@ public class DefaultConfiguration implements Configuration { private boolean strictMode; + private Function variableNameFunction; + public DefaultConfiguration( RoundEnvironment roundEnv, Map options, @@ -185,6 +188,21 @@ public class DefaultConfiguration implements Configuration { useGetters = Boolean.valueOf(options.get(QUERYDSL_USE_GETTERS)); } + if (options.containsKey(QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS)) { + try { + @SuppressWarnings("unchecked") + Class> variableNameFunctionClass = (Class>) Class.forName(options.get(QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS)); + variableNameFunction = variableNameFunctionClass.newInstance(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + variableNameFunction = DefaultVariableNameFunction.INSTANCE; + } + } else { + variableNameFunction = DefaultVariableNameFunction.INSTANCE; + } + module.bind(CodegenModule.VARIABLE_NAME_FUNCTION_CLASS, variableNameFunction); + try { // register additional mappings, if querydsl-spatial is on the classpath Class.forName("com.mysema.query.spatial.GeometryExpression"); @@ -475,4 +493,9 @@ public class DefaultConfiguration implements Configuration { this.unknownAsEmbedded = unknownAsEmbedded; } + @Override + public Function getVariableNameFunction() { + return variableNameFunction; + } + } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java b/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java index e25079878..6ecdf34c0 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java @@ -23,6 +23,7 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.type.*; +import com.google.common.base.Function; import com.mysema.codegen.model.*; import com.mysema.query.annotations.QueryExclude; import com.mysema.query.codegen.*; @@ -53,6 +54,8 @@ public final class ExtendedTypeFactory { private boolean doubleIndexEntities = true; + private Function variableNameFunction; + private final TypeVisitor visitor = new SimpleTypeVisitorAdapter() { @Override @@ -252,7 +255,8 @@ public final class ExtendedTypeFactory { Configuration configuration, Set> annotations, TypeMappings typeMappings, - QueryTypeFactory queryTypeFactory) { + QueryTypeFactory queryTypeFactory, + Function variableNameFunction) { this.env = env; this.defaultType = new TypeExtends(Types.OBJECT); this.entityAnnotations = annotations; @@ -265,6 +269,7 @@ public final class ExtendedTypeFactory { this.mapType = getErasedType(Map.class); this.typeMappings = typeMappings; this.queryTypeFactory = queryTypeFactory; + this.variableNameFunction = variableNameFunction; } private TypeMirror getErasedType(Class clazz) { @@ -389,7 +394,7 @@ public final class ExtendedTypeFactory { for (Class entityAnn : entityAnnotations) { if (typeElement.getAnnotation(entityAnn) != null || (superTypeElement != null && superTypeElement.getAnnotation(entityAnn) != null)) { - EntityType entityType = new EntityType(type); + EntityType entityType = new EntityType(type, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); return entityType; } @@ -472,7 +477,7 @@ public final class ExtendedTypeFactory { if (value instanceof EntityType) { entityType = (EntityType)value; } else { - entityType = new EntityType(value); + entityType = new EntityType(value, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); } entityTypeCache.put(key, entityType); @@ -495,7 +500,7 @@ public final class ExtendedTypeFactory { for (Class entityAnn : entityAnnotations) { if (typeElement.getAnnotation(entityAnn) != null) { - EntityType entityType = new EntityType(enumType); + EntityType entityType = new EntityType(enumType, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); return entityType; } diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/TypeElementHandler.java b/querydsl-apt/src/main/java/com/mysema/query/apt/TypeElementHandler.java index 0afbecc1c..43e6e1be7 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/TypeElementHandler.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/TypeElementHandler.java @@ -176,7 +176,7 @@ public final class TypeElementHandler { public EntityType handleProjectionType(TypeElement e) { Type c = typeFactory.getType(e.asType(), true); - EntityType entityType = new EntityType(c.as(TypeCategory.ENTITY)); + EntityType entityType = new EntityType(c.as(TypeCategory.ENTITY), configuration.getVariableNameFunction()); typeMappings.register(entityType, queryTypeFactory.create(entityType)); List elements = e.getEnclosedElements(); handleConstructors(entityType, elements); diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/CodegenModule.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/CodegenModule.java index 74720117f..87498bfa1 100644 --- a/querydsl-codegen/src/main/java/com/mysema/query/codegen/CodegenModule.java +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/CodegenModule.java @@ -33,6 +33,8 @@ public class CodegenModule extends AbstractModule { public static final String IMPORTS = "imports"; + public static final String VARIABLE_NAME_FUNCTION_CLASS = "variableNameFunction"; + @Override protected void configure() { bind(TypeMappings.class, JavaTypeMappings.class); @@ -48,6 +50,7 @@ public class CodegenModule extends AbstractModule { bind(PACKAGE_SUFFIX, ""); bind(KEYWORDS, Collections.emptySet()); bind(IMPORTS, Collections.emptySet()); + bind(VARIABLE_NAME_FUNCTION_CLASS, DefaultVariableNameFunction.INSTANCE); } } \ No newline at end of file diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/DefaultVariableNameFunction.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/DefaultVariableNameFunction.java new file mode 100644 index 000000000..ce1396479 --- /dev/null +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/DefaultVariableNameFunction.java @@ -0,0 +1,23 @@ +package com.mysema.query.codegen; + +import com.google.common.base.Function; +import com.mysema.codegen.StringUtils; +import com.mysema.util.JavaSyntaxUtils; + +/** + * Default variable name generation strategy which un-capitalizes the first letter of the class name. + * + */ +public final class DefaultVariableNameFunction implements Function { + + public static final DefaultVariableNameFunction INSTANCE = new DefaultVariableNameFunction(); + + @Override + public String apply(EntityType entity) { + String uncapSimpleName = StringUtils.uncapitalize(entity.getInnerType().getSimpleName()); + if (JavaSyntaxUtils.isReserved(uncapSimpleName)) { + uncapSimpleName = uncapSimpleName + "$"; + } + return uncapSimpleName; + } +} diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntitySerializer.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntitySerializer.java index bbcfc8447..a975580e4 100644 --- a/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntitySerializer.java +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntitySerializer.java @@ -324,7 +324,7 @@ public class EntitySerializer implements Serializer { } protected void introDefaultInstance(CodeWriter writer, EntityType model, String defaultName) throws IOException { - String simpleName = !defaultName.isEmpty() ? defaultName : model.getUncapSimpleName(); + String simpleName = !defaultName.isEmpty() ? defaultName : model.getModifiedSimpleName(); Type queryType = typeMappings.getPathType(model, model, true); String alias = simpleName; if (keywords.contains(simpleName.toUpperCase())) { diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntityType.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntityType.java index a740cd3bd..3c88b55e0 100644 --- a/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntityType.java +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/EntityType.java @@ -13,16 +13,17 @@ */ package com.mysema.query.codegen; -import javax.annotation.Nullable; import java.lang.annotation.Annotation; import java.util.*; +import javax.annotation.Nullable; + +import com.google.common.base.Function; import com.mysema.codegen.StringUtils; import com.mysema.codegen.model.Constructor; import com.mysema.codegen.model.Type; import com.mysema.codegen.model.TypeAdapter; import com.mysema.codegen.model.TypeCategory; -import com.mysema.util.JavaSyntaxUtils; /** * EntityType represents a model of a query domain type with properties @@ -49,15 +50,25 @@ public class EntityType extends TypeAdapter implements Comparable { private final Map data = new HashMap(); - private String uncapSimpleName; - + private String modifiedSimpleName; + /** * Create a new EntityType instance for the given type * * @param type */ public EntityType(Type type) { - this(type, new LinkedHashSet()); + this(type, new LinkedHashSet(), DefaultVariableNameFunction.INSTANCE); + } + + /** + * Create a new {@code EntityType} instance for the given type + * + * @param type the type to be used + * @param variableNameFunction the variable name function to be used + */ + public EntityType(Type type, Function variableNameFunction) { + this(type, new LinkedHashSet(), variableNameFunction); } /** @@ -67,12 +78,20 @@ public class EntityType extends TypeAdapter implements Comparable { * @param superTypes */ public EntityType(Type type, Set superTypes) { + this(type, superTypes, DefaultVariableNameFunction.INSTANCE); + } + + /** + * Create a new {@code EntityType} instance for the given type and superTypes + * + * @param type the type to be used + * @param superTypes the super types to be used + * @param variableNameFunction the variable name function to be used + */ + private EntityType(Type type, Set superTypes, Function variableNameFunction) { super(type); - this.uncapSimpleName = StringUtils.uncapitalize(type.getSimpleName()); - if (JavaSyntaxUtils.isReserved(uncapSimpleName)) { - this.uncapSimpleName = uncapSimpleName + "$"; - } this.superTypes = superTypes; + this.modifiedSimpleName = variableNameFunction.apply(this); } public void addAnnotation(Annotation annotation) { @@ -171,8 +190,16 @@ public class EntityType extends TypeAdapter implements Comparable { return superTypes; } + /** + * Use {@link #getModifiedSimpleName()} + */ + @Deprecated public String getUncapSimpleName() { - return uncapSimpleName; + return modifiedSimpleName; + } + + public String getModifiedSimpleName() { + return modifiedSimpleName; } @Override @@ -233,10 +260,10 @@ public class EntityType extends TypeAdapter implements Comparable { } private Property validateField(Property field) { - if (field.getName().equals(uncapSimpleName) || field.getEscapedName().equals(uncapSimpleName)) { + if (field.getName().equals(modifiedSimpleName) || field.getEscapedName().equals(modifiedSimpleName)) { do { - uncapSimpleName = StringUtils.uncapitalize(getType().getSimpleName()) + (escapeSuffix++); - } while (propertyNames.contains(uncapSimpleName)); + modifiedSimpleName = StringUtils.uncapitalize(getType().getSimpleName()) + (escapeSuffix++); + } while (propertyNames.contains(modifiedSimpleName)); } return field; } @@ -252,4 +279,5 @@ public class EntityType extends TypeAdapter implements Comparable { public Type getInnerType() { return type; } + } diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/GenericExporter.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/GenericExporter.java index 7a3d2df04..a3c4c033f 100644 --- a/querydsl-codegen/src/main/java/com/mysema/query/codegen/GenericExporter.java +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/GenericExporter.java @@ -21,6 +21,7 @@ import java.util.*; import javax.annotation.Nullable; +import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -203,7 +204,7 @@ public class GenericExporter { private void innerExport() { typeMappings = codegenModule.get(TypeMappings.class); queryTypeFactory = codegenModule.get(QueryTypeFactory.class); - typeFactory = new TypeFactory(ImmutableList.of(entityAnnotation, supertypeAnnotation, embeddableAnnotation)); + typeFactory = new TypeFactory(ImmutableList.of(entityAnnotation, supertypeAnnotation, embeddableAnnotation), codegenModule.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS)); // copy annotations helpers to typeFactory for (AnnotationHelper helper : annotationHelpers){ diff --git a/querydsl-codegen/src/main/java/com/mysema/query/codegen/TypeFactory.java b/querydsl-codegen/src/main/java/com/mysema/query/codegen/TypeFactory.java index cca45e8dc..d6143e5d3 100644 --- a/querydsl-codegen/src/main/java/com/mysema/query/codegen/TypeFactory.java +++ b/querydsl-codegen/src/main/java/com/mysema/query/codegen/TypeFactory.java @@ -13,8 +13,8 @@ */ package com.mysema.query.codegen; -import com.google.common.collect.ImmutableList; import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; @@ -23,19 +23,14 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.primitives.Primitives; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.SimpleType; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.TypeCategory; -import com.mysema.codegen.model.TypeExtends; -import com.mysema.codegen.model.TypeSuper; -import com.mysema.codegen.model.Types; +import com.mysema.codegen.model.*; import com.mysema.util.ReflectionUtils; -import java.lang.reflect.AnnotatedElement; /** * TypeFactory is a factory class for {@link Type} instances @@ -57,12 +52,19 @@ public final class TypeFactory { private boolean unknownAsEntity = false; + private Function variableNameFunction; + public TypeFactory() { - this(Lists.>newArrayList()); + this(Lists.>newArrayList(), DefaultVariableNameFunction.INSTANCE); } public TypeFactory(List> entityAnnotations) { + this(entityAnnotations, DefaultVariableNameFunction.INSTANCE); + } + + public TypeFactory(List> entityAnnotations, Function variableNameFunction) { this.entityAnnotations = entityAnnotations; + this.variableNameFunction = variableNameFunction; } public EntityType getEntityType(Class cl) { @@ -110,7 +112,7 @@ public final class TypeFactory { if (cache.containsKey(key)) { Type value = cache.get(key); if (entity && !(value instanceof EntityType)) { - value = new EntityType(value); + value = new EntityType(value, variableNameFunction); cache.put(key, value); } return value; @@ -167,7 +169,7 @@ public final class TypeFactory { } if (entity && !(value instanceof EntityType)) { - value = new EntityType(value); + value = new EntityType(value, variableNameFunction); } return value; } diff --git a/querydsl-codegen/src/test/java/com/mysema/query/codegen/EntityTypeTest.java b/querydsl-codegen/src/test/java/com/mysema/query/codegen/EntityTypeTest.java index 11c119376..d2b0244af 100644 --- a/querydsl-codegen/src/test/java/com/mysema/query/codegen/EntityTypeTest.java +++ b/querydsl-codegen/src/test/java/com/mysema/query/codegen/EntityTypeTest.java @@ -28,28 +28,28 @@ public class EntityTypeTest { public void UncapSimpleName_Escaped() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class); EntityType entityModel = new EntityType(typeModel); - assertEquals("object", entityModel.getUncapSimpleName()); + assertEquals("object", entityModel.getModifiedSimpleName()); entityModel.addProperty(new Property(entityModel, "object", typeModel)); - assertEquals("object1", entityModel.getUncapSimpleName()); + assertEquals("object1", entityModel.getModifiedSimpleName()); } @Test public void UncapSimpleName_Escaped2() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class); EntityType entityModel = new EntityType(typeModel); - assertEquals("object", entityModel.getUncapSimpleName()); + assertEquals("object", entityModel.getModifiedSimpleName()); entityModel.addProperty(new Property(entityModel, "OBJECT", "object", typeModel, Collections. emptyList(), false)); - assertEquals("object1", entityModel.getUncapSimpleName()); + assertEquals("object1", entityModel.getModifiedSimpleName()); } @Test public void UncapSimpleName_Escaped3() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Void.class); EntityType entityModel = new EntityType(typeModel); - assertEquals("void$", entityModel.getUncapSimpleName()); + assertEquals("void$", entityModel.getModifiedSimpleName()); } } diff --git a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/AbstractDomainExporter.java b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/AbstractDomainExporter.java index 151069a16..3b4932f61 100644 --- a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/AbstractDomainExporter.java +++ b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/AbstractDomainExporter.java @@ -13,9 +13,6 @@ */ package com.mysema.query.jpa.codegen; -import javax.annotation.Nullable; -import javax.persistence.Embeddable; -import javax.persistence.Entity; import java.io.*; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; @@ -24,6 +21,14 @@ import java.lang.reflect.Method; import java.nio.charset.Charset; import java.util.*; +import javax.annotation.Nullable; +import javax.persistence.Embeddable; +import javax.persistence.Entity; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -38,8 +43,6 @@ import com.mysema.query.annotations.QueryType; import com.mysema.query.codegen.*; import com.mysema.util.Annotations; import com.mysema.util.ReflectionUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * AbstractDomainExporter is a common supertype for DomainExporters @@ -83,6 +86,8 @@ public abstract class AbstractDomainExporter { private final Set generatedFiles = new HashSet(); + private final Function variableNameFunction; + /** * @param namePrefix * @param nameSuffix @@ -90,6 +95,7 @@ public abstract class AbstractDomainExporter { * @param serializerConfig * @param charset */ + @SuppressWarnings("unchecked") public AbstractDomainExporter(String namePrefix, String nameSuffix, File targetFolder, SerializerConfig serializerConfig, Charset charset) { this.targetFolder = targetFolder; @@ -104,6 +110,7 @@ public abstract class AbstractDomainExporter { this.embeddableSerializer = module.get(EmbeddableSerializer.class); this.entitySerializer = module.get(EntitySerializer.class); this.supertypeSerializer = module.get(SupertypeSerializer.class); + this.variableNameFunction = module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS); } /** @@ -202,7 +209,7 @@ public abstract class AbstractDomainExporter { if (allTypes.containsKey(key)) { return allTypes.get(key); } else { - EntityType entityType = new EntityType(type); + EntityType entityType = new EntityType(type, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); Class superClass = key.getSuperclass(); if (entityType.getSuperType() == null && superClass != null && !superClass.equals(Object.class)) { diff --git a/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaBeanSerializer.scala b/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaBeanSerializer.scala index d2b9f7e16..db184181a 100644 --- a/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaBeanSerializer.scala +++ b/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaBeanSerializer.scala @@ -59,9 +59,9 @@ object Serializer { def writeCompanionObject(model: EntityType, typeMappings: TypeMappings, writer: ScalaWriter) { val queryType = typeMappings.getPathType(model, model, true) val modelName = writer.getRawName(model) - val queryTypeName = writer.getRawName(queryType) - val variable = model.getUncapSimpleName - + val queryTypeName = writer.getRawName(queryType) + val variable = model.getModifiedSimpleName + writer.beginObject(modelName + " extends "+queryTypeName+"(\""+variable+"\")") writer.line("override def as(variable: String) = new ", queryTypeName, "(variable)") writer.line("") diff --git a/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaEntitySerializer.scala b/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaEntitySerializer.scala index 0870a4f36..0616f5a69 100644 --- a/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaEntitySerializer.scala +++ b/querydsl-scala/src/main/scala/com/mysema/query/scala/ScalaEntitySerializer.scala @@ -111,9 +111,9 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se } def writeCompanionObject(model: EntityType, queryType: Type, writer: ScalaWriter) = { - val queryTypeName = writer.getRawName(queryType) - val variable = model.getUncapSimpleName - + val queryTypeName = writer.getRawName(queryType) + val variable = model.getModifiedSimpleName + writer.beginObject(queryTypeName + " extends "+queryTypeName+"(\""+variable+"\")") writer.line("override def as(variable: String) = new ", queryTypeName, "(variable)") writer.line("") diff --git a/querydsl-sql-codegen/src/main/java/com/mysema/query/sql/codegen/MetaDataExporter.java b/querydsl-sql-codegen/src/main/java/com/mysema/query/sql/codegen/MetaDataExporter.java index a9483b062..28bfd670b 100644 --- a/querydsl-sql-codegen/src/main/java/com/mysema/query/sql/codegen/MetaDataExporter.java +++ b/querydsl-sql-codegen/src/main/java/com/mysema/query/sql/codegen/MetaDataExporter.java @@ -27,6 +27,7 @@ import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Function; import com.google.common.io.Files; import com.mysema.codegen.CodeWriter; import com.mysema.codegen.JavaWriter; @@ -126,7 +127,7 @@ public class MetaDataExporter { String simpleName = module.getPrefix() + className + module.getSuffix(); Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + simpleName, packageName, simpleName, false, false); - classModel = new EntityType(classTypeModel); + classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS)); typeMappings.register(classModel, classModel); } else { @@ -134,7 +135,7 @@ public class MetaDataExporter { String simpleName = module.getBeanPrefix() + className + module.getBeanSuffix(); Type classTypeModel = new SimpleType(TypeCategory.ENTITY, beanPackage + "." + simpleName, beanPackage, simpleName, false, false); - classModel = new EntityType(classTypeModel); + classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS)); Type mappedType = queryTypeFactory.create(classModel); entityToWrapped.put(classModel, mappedType);