diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-04 20:09:06 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-04 20:09:06 +0200 |
commit | fd6f806608d1047296cbb1ae7ea4a989fe23553b (patch) | |
tree | be38c3b783ad656605bdf8808aa32a497d0ebd0f /container-compiler-plugin | |
parent | f0a9ad191465bba0e7ddaea700d9667be699fec3 (diff) | |
download | container-playground-fd6f806608d1047296cbb1ae7ea4a989fe23553b.tar.gz container-playground-fd6f806608d1047296cbb1ae7ea4a989fe23553b.tar.bz2 container-playground-fd6f806608d1047296cbb1ae7ea4a989fe23553b.tar.xz container-playground-fd6f806608d1047296cbb1ae7ea4a989fe23553b.zip |
wip
o Adding support for enums. Will automatically use the name style, not ordinal.
Diffstat (limited to 'container-compiler-plugin')
4 files changed, 66 insertions, 13 deletions
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java index ae3443c..74f303f 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java @@ -15,6 +15,7 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.PackageElement; @@ -40,14 +41,13 @@ import static io.trygvis.persistence.FieldMirror.AccessorType.FIELD; import static io.trygvis.persistence.FieldMirror.AccessorType.METHOD; import static io.trygvis.persistence.FieldMirror.FieldType.PRIMITIVE; import static io.trygvis.persistence.FieldMirror.FieldType.REFERENCE; +import static io.trygvis.persistence.TypeHandler.StringEnumTypeHandler; import static java.lang.Character.isUpperCase; import static java.lang.String.format; import static java.lang.reflect.Modifier.PUBLIC; import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.methodsIn; -import static org.apache.commons.lang.StringUtils.capitalize; -import static org.apache.commons.lang.StringUtils.join; -import static org.apache.commons.lang.StringUtils.stripEnd; +import static org.apache.commons.lang.StringUtils.*; public class EntityHandler extends AbstractHandler { private GeneratorConfiguration generatorConfiguration = new GeneratorConfiguration(); @@ -119,7 +119,6 @@ public class EntityHandler extends AbstractHandler { } } - // Iterate over all fields. Consume relevant getters and setters. for (VariableElement f : fieldsIn(elements.getAllMembers(element))) { String name = f.getSimpleName().toString(); ExecutableElement getter = getters.remove(name); @@ -253,6 +252,7 @@ public class EntityHandler extends AbstractHandler { // TODO: check for transient and @Transient FieldMirror.AccessorType accessorType; TypeRef type; + Element element; String javaName; String sqlName; boolean id; @@ -263,6 +263,7 @@ public class EntityHandler extends AbstractHandler { } accessorType = FIELD; type = new TypeRef(var.asType()); + element = types.asElement(var.asType()); javaName = var.getSimpleName().toString(); id = isId(var); processSequenceGenerators(var); @@ -277,6 +278,7 @@ public class EntityHandler extends AbstractHandler { } accessorType = METHOD; type = new TypeRef(getter.getReturnType()); + element = types.asElement(getter.getReturnType()); id = isId(getter); processSequenceGenerators(getter); // TODO: this might be relaxed, just find the common type and use that. @@ -288,9 +290,16 @@ public class EntityHandler extends AbstractHandler { } sqlName = sqlName(javaName); + // Register type handler if this is an enum + if (element != null && element.getKind() == ElementKind.ENUM) { + if (!generatorConfiguration.hasTypeHandler(type)) { + generatorConfiguration.addTypeHandler(type, new StringEnumTypeHandler(type)); + } + } + boolean notNull = false; boolean unique = false; - boolean primitive = generatorConfiguration.isPrimitive(type); + boolean primitive = generatorConfiguration.hasTypeHandler(type); if (id && !primitive) { throw new CompilerException(var, "A @Id field has to be a primitive or embedded."); } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java index 48072de..3bf7b08 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java @@ -10,7 +10,6 @@ import java.util.TreeMap; public class GeneratorConfiguration { - private final Map<TypeRef, TypeHandler> primitiveTypeHandlers = new TreeMap<>(); private final Map<TypeRef, TypeHandler> typeHandlers = new TreeMap<>(); { @@ -22,8 +21,6 @@ public class GeneratorConfiguration { typeHandlers.put(new TypeRef(Date.class), new TypeHandler.DateTypeHandler()); typeHandlers.put(new TypeRef("org.joda.time.DateTime"), new TypeHandler.JodaDateTimeTypeHandler()); typeHandlers.put(new TypeRef("java.util.UUID"), new TypeHandler.UuidTypeHandler()); - - primitiveTypeHandlers.putAll(typeHandlers); } public void addTypeHandler(TypeRef type, TypeHandler typeHandler) { @@ -38,10 +35,6 @@ public class GeneratorConfiguration { return typeHandler; } - public boolean isPrimitive(TypeRef type) { - return primitiveTypeHandlers.containsKey(type); - } - public boolean hasTypeHandler(TypeRef type) { return typeHandlers.containsKey(type); } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java index dfb2be3..f13018a 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java @@ -1,6 +1,7 @@ package io.trygvis.persistence; import io.trygvis.container.compiler.InternalErrorException; +import io.trygvis.container.compiler.model.TypeRef; import java.lang.reflect.Field; import java.sql.Types; @@ -69,6 +70,49 @@ public abstract class TypeHandler { } } +// public static class OrdinalEnumTypeHandler extends TypeHandler { +// private final TypeRef enumType; +// public OrdinalEnumTypeHandler(TypeRef enumType) { +// super("INTEGER", INTEGER, true); +// this.enumType = enumType; +// } +// +// @Override +// String setter(int i, String expr) { +// return "setInt(" + i + ", " + expr + ".ordinal())"; +// } +// +// @Override +// String getter(String rs, int i) { +// return "int ordinal = -1; for (Gender v : Gender.values()) if (v.ordinal() == ordinal) return v;"; +// int ordinal = -1; for (EnumType v : EnumType.values()) if (v.ordinal() == ordinal) return v; +// return enumType.plainName + ".ordinal(" + rs + ".getInt(" + i + "))"; +// } +// } + + public static class StringEnumTypeHandler extends TypeHandler { + private final TypeRef enumType; + + public StringEnumTypeHandler(TypeRef enumType) { + super("VARCHAR", VARCHAR, true); + this.enumType = enumType; + } + + @Override + String setter(int i, String expr) { + return "setString(" + i + ", " + expr + ".name())"; + } + + @Override + String getter(String rs, int i) { + return enumType.plainName + ".valueOf(" + rs + ".getString(" + i + "))"; + } + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + public static class StringTypeHandler extends PrimitiveTypeHandler { protected StringTypeHandler() { super(null, VARCHAR, "String", true); diff --git a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/Person.java b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/Person.java index 755042b..47a2fdd 100644 --- a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/Person.java +++ b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/Person.java @@ -17,6 +17,12 @@ public class Person { @ManyToOne public Person mother; + enum Gender { + MALE, FEMALE + } + + public final Gender gender; + // @ManyToOne // public Person father; @@ -24,9 +30,10 @@ public class Person { // @OrderBy("birthDate asc") // private List<Person> children = new ArrayList<>(); - public Person(Long id, Date birthDate, Person mother) { + public Person(Long id, Date birthDate, Person mother, Gender gender) { this.id = id; this.birthDate = birthDate; this.mother = mother; + this.gender = gender; } } |