summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/main/java/io/trygvis/container
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-03 12:33:39 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-03 12:33:39 +0200
commit34137b599dbea13c94224dff2955376b1394dbc9 (patch)
tree889b4c334014965e451f8d9b308b952a13b4d8d5 /container-compiler-plugin/src/main/java/io/trygvis/container
parent4e794b5ed03e5020770becb068d11e6838feec64 (diff)
downloadcontainer-playground-34137b599dbea13c94224dff2955376b1394dbc9.tar.gz
container-playground-34137b599dbea13c94224dff2955376b1394dbc9.tar.bz2
container-playground-34137b599dbea13c94224dff2955376b1394dbc9.tar.xz
container-playground-34137b599dbea13c94224dff2955376b1394dbc9.zip
wip
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/container')
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java89
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java53
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/entity/EntityMirror.java38
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java27
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/MethodRef.java13
5 files changed, 125 insertions, 95 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 df7a673..f9247e0 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
@@ -1,43 +1,90 @@
package io.trygvis.container.compiler;
-import io.trygvis.container.compiler.entity.EntityMirror;
import io.trygvis.container.compiler.model.ClassG;
import io.trygvis.container.compiler.model.Parameters;
+import io.trygvis.persistence.EntityMirror;
+import io.trygvis.persistence.FieldMirror;
+import io.trygvis.persistence.GeneratorSupport;
+import io.trygvis.persistence.SqlEntity;
+import io.trygvis.persistence.TypeHandler;
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.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeMirror;
import javax.tools.JavaFileObject;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
-import static io.trygvis.container.compiler.entity.EntityMirror.FieldMirror;
+import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror;
import static java.lang.Character.isUpperCase;
import static javax.lang.model.util.ElementFilter.fieldsIn;
-import static org.springframework.util.StringUtils.collectionToDelimitedString;
public class EntityHandler extends AbstractHandler {
+ GeneratorSupport generatorSupport = new GeneratorSupport();
+
public EntityHandler(ProcessingEnvironment processingEnv) {
super(processingEnv);
}
+ public void phase1(Set<? extends Element> sqlEntities) throws Exception {
+ System.out.println("io.trygvis.container.compiler.EntityHandler.phase1");
+ for (Element entity : sqlEntities) {
+// SqlEntity sqlEntity = entity.getAnnotation(SqlEntity.class);
+// Class<? extends TypeHandler> typeHandlerClass = sqlEntity.value();
+
+ AnnotationMirror sqlEntity = findAnnotation(SqlEntity.class, entity.getAnnotationMirrors());
+ for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> v : sqlEntity.getElementValues().entrySet()) {
+ switch (v.getKey().getSimpleName().toString()) {
+ case "value":
+ Class<?> typeHandlerClass = getClass().getClassLoader().loadClass(v.getValue().getValue().toString());
+
+ TypeHandler typeHandler = (TypeHandler) typeHandlerClass.newInstance();
+ String type = entity.asType().toString();
+ generatorSupport.addTypeHandler(type, typeHandler);
+ System.out.println("Loaded TypeHandler for " + type + " through " + typeHandlerClass.getCanonicalName());
+ break;
+ }
+ }
+ System.out.println("sqlEntity.getElementValues() = " + sqlEntity.getElementValues());
+ }
+ }
+
+ private AnnotationMirror findAnnotation(Class<?> c, List<? extends AnnotationMirror> annotations) {
+ TypeElement t = elements.getTypeElement(c.getCanonicalName());
+ for (AnnotationMirror a : annotations) {
+ if (types.isSameType(a.getAnnotationType(), t.asType())) {
+ return a;
+ }
+ }
+
+ throw new RuntimeException("Could not find annotation " + c.getSimpleName());
+ }
+
public void processEntity(TypeElement element) throws Exception {
- EntityMirror entityMirror = new EntityMirror(sqlName(element.getSimpleName().toString()));
+ EntityMirror entityMirror = new EntityMirror(generatorSupport, element.asType(), sqlName(element.getSimpleName().toString()));
for (VariableElement f : fieldsIn(elements.getAllMembers(element))) {
- entityMirror.add(fromElement(f));
+ entityMirror.add(fromElement(generatorSupport, f));
}
String p = elements.getPackageOf(element).getQualifiedName().toString();
String className = element.getSimpleName() + "_Sql";
ClassG g = new ClassG(p, className, null);
- g.addPublicFinalField(String.class, "insertInto");
- String insertInto = insertInto(entityMirror);
+ g.addPublicFinalField(String.class, "insertIntoSql");
+ String insertInto = entityMirror.insertIntoSql();
List<String> body = new ArrayList<>();
- body.add("this.insertInto = \"" + insertInto + "\";");
+ body.add("this.insertIntoSql = \"" + insertInto + "\";");
g.addConstructor(new Parameters(), body);
+ entityMirror.insertInto(g);
String fileName = (p.length() == 0 ? "" : p + ".") + className;
JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(fileName, element);
@@ -46,25 +93,21 @@ public class EntityHandler extends AbstractHandler {
}
}
- public static String insertInto(EntityMirror entityMirror) {
- List<String> names = new ArrayList<>();
- List<String> placeholders = new ArrayList<>();
- for (FieldMirror field : entityMirror.fields) {
- names.add(field.sqlName);
- placeholders.add("?");
- }
- return "INSERT INTO " + entityMirror.tableName + "(" + collectionToDelimitedString(names, ", ") + ") " +
- "VALUES(" + collectionToDelimitedString(placeholders, ", ") + ");";
- }
-
- public FieldMirror fromElement(VariableElement var) {
- System.out.println("io.trygvis.container.compiler.entity.EntityMirror.FieldMirror.fromElement");
+ public FieldMirror fromElement(GeneratorSupport generatorSupport, VariableElement var) {
System.out.print("element = ");
+ TypeMirror type = var.asType();
elements.printElements(new PrintWriter(System.out), var);
String javaName = var.getSimpleName().toString();
String sqlName = sqlName(javaName);
boolean notNull = false;
- FieldMirror field = new FieldMirror(javaName, sqlName, notNull);
+ FieldMirror field;
+ if (generatorSupport.isPrimitive(type)) {
+ field = new PrimitiveFieldMirror(var, javaName, sqlName, notNull);
+ } else if (generatorSupport.hasTypeHandler(type)) {
+ field = new FieldMirror.ReferenceFieldMirror(var, javaName, sqlName, notNull);
+ } else {
+ throw new RuntimeException("Missing type handler for type: " + type);
+ }
System.out.println("field = " + field);
return field;
}
@@ -73,7 +116,7 @@ public class EntityHandler extends AbstractHandler {
StringBuilder builder = new StringBuilder();
for (char c : javaName.toCharArray()) {
char lower = Character.toLowerCase(c);
- if(isUpperCase(c) && builder.length() > 0) {
+ if (isUpperCase(c) && builder.length() > 0) {
builder.append("_");
}
builder.append(lower);
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java
index 6843659..735d688 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java
@@ -1,6 +1,7 @@
package io.trygvis.container.compiler;
import io.trygvis.container.log.Log;
+import io.trygvis.persistence.SqlEntity;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.processing.Completion;
@@ -31,19 +32,16 @@ public class MyProcessor implements Processor {
@Override
public Set<String> getSupportedOptions() {
- System.out.println("io.trygvis.container.compiler.MyProcessor.getSupportedOptions");
return emptySet();
}
@Override
public SourceVersion getSupportedSourceVersion() {
- System.out.println("io.trygvis.container.compiler.MyProcessor.getSupportedSourceVersion");
return SourceVersion.RELEASE_7;
}
@Override
public void init(ProcessingEnvironment processingEnv) {
- System.out.println("io.trygvis.container.compiler.MyProcessor.init");
this.processingEnv = processingEnv;
elements = processingEnv.getElementUtils();
types = processingEnv.getTypeUtils();
@@ -51,13 +49,11 @@ public class MyProcessor implements Processor {
@Override
public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
- System.out.println("io.trygvis.container.compiler.MyProcessor.getCompletions");
return emptyList();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
- System.out.println("io.trygvis.container.compiler.MyProcessor.getSupportedAnnotationTypes");
return new HashSet<>(asList(
Transactional.class.getName(),
Log.class.getName(),
@@ -66,34 +62,39 @@ public class MyProcessor implements Processor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
- System.out.println("io.trygvis.container.compiler.MyProcessor.process");
- System.out.println("annotations = " + annotations);
-// Set<? extends Element> transactional = roundEnv.getElementsAnnotatedWith(Transactional.class);
+ try {
+ return work(annotations, roundEnv);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ public boolean work(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws Exception {
TypeElement tx = elements.getTypeElement(Transactional.class.getCanonicalName());
TypeElement log = elements.getTypeElement(Log.class.getCanonicalName());
TypeElement entity = elements.getTypeElement(Entity.class.getCanonicalName());
+ EntityHandler entityHandler = new EntityHandler(processingEnv);
+
+ Set<? extends Element> sqlEntities = roundEnv.getElementsAnnotatedWith(SqlEntity.class);
+ entityHandler.phase1(sqlEntities);
+
for (Element element : roundEnv.getRootElements()) {
- try {
- System.out.println("Processing: " + element);
- for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
- DeclaredType annotationType = annotationMirror.getAnnotationType();
-
- if (types.isSameType(tx.asType(), annotationType)) {
- new TransactionalHandler(processingEnv).processTransactional((TypeElement) element);
- }
- if (types.isSameType(log.asType(), annotationType)) {
- new LogHandler(processingEnv).processLog((TypeElement) element);
- }
- if (types.isSameType(entity.asType(), annotationType)) {
- new EntityHandler(processingEnv).processEntity((TypeElement) element);
- }
+ System.out.println("Processing: " + element);
+ for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
+ DeclaredType annotationType = annotationMirror.getAnnotationType();
+
+ if (types.isSameType(tx.asType(), annotationType)) {
+ new TransactionalHandler(processingEnv).processTransactional((TypeElement) element);
+ }
+ if (types.isSameType(log.asType(), annotationType)) {
+ new LogHandler(processingEnv).processLog((TypeElement) element);
+ }
+ if (types.isSameType(entity.asType(), annotationType)) {
+ entityHandler.processEntity((TypeElement) element);
}
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException(e);
}
}
return true;
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/entity/EntityMirror.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/entity/EntityMirror.java
deleted file mode 100644
index ae7077c..0000000
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/entity/EntityMirror.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package io.trygvis.container.compiler.entity;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class EntityMirror {
- public final List<FieldMirror> fields = new ArrayList<>();
- public final String tableName;
-
- public EntityMirror(String tableName) {
- this.tableName = tableName;
- }
-
- public void add(FieldMirror field) {
- fields.add(field);
- }
-
- public static class FieldMirror {
- private final String javaName;
- public final String sqlName;
- private final boolean notNull;
-
- public FieldMirror(String javaName, String sqlName, boolean notNull) {
- this.javaName = javaName;
- this.sqlName = sqlName;
- this.notNull = notNull;
- }
-
- @Override
- public String toString() {
- return "FieldMirror{" +
- "javaName='" + javaName + '\'' +
- ", sqlName='" + sqlName + '\'' +
- ", notNull=" + notNull +
- '}';
- }
- }
-}
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java
index cf68cba..8ff91a0 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java
@@ -31,7 +31,6 @@ public class ClassG {
}
String canonicalName = type.toString();
- System.out.println("canonicalName = " + canonicalName);
for (TypeRef i : imports) {
if (i.canonicalName().equals(canonicalName)) {
@@ -45,10 +44,6 @@ public class ClassG {
}
public TypeRef addImport(Class<?> klass) {
-// if (klass.isPrimitive()) {
-// return TypeRef.PRIMITIVE;
-// }
-
String canonicalName = klass.getCanonicalName();
String simpleName = klass.getSimpleName();
String name = simpleName;
@@ -104,6 +99,9 @@ public class ClassG {
if (i.isPrimitive()) {
continue;
}
+ if(i.canonicalName().indexOf('.') == -1) {
+ continue;
+ }
writer.println("import " + i.canonicalName() + ";");
}
writer.println();
@@ -144,7 +142,24 @@ public class ClassG {
parameters.add("final " + p.klass.name + " " + p.name);
}
- writer.println(" public " + returnString + " " + method.name + "(" + collectionToDelimitedString(parameters, ", ") + ") {");
+ writer.print(" public " + returnString + " " + method.name + "(" + collectionToDelimitedString(parameters, ", ") + ")");
+ if(method.exceptions.isEmpty()) {
+ writer.println(" {");
+ }
+ else {
+ writer.println(" throws");
+ ArrayList<TypeRef> typeRefs = new ArrayList<>(method.exceptions);
+ for (int i = 0; i < typeRefs.size(); i++) {
+ TypeRef e = typeRefs.get(i);
+ writer.print(" " + e.name);
+ if(i < typeRefs.size() - 1) {
+ writer.println(",");
+ }
+ else {
+ writer.println(" {");
+ }
+ }
+ }
for (String s : method.body) {
writer.print(" ");
writer.println(s);
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/MethodRef.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/MethodRef.java
index 36f61e8..a59e9a6 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/MethodRef.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/MethodRef.java
@@ -1,18 +1,27 @@
package io.trygvis.container.compiler.model;
import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import static java.util.Collections.addAll;
public class MethodRef {
public final TypeRef returnType;
public final String name;
public final ParameterRef[] parameters;
+ public final Set<TypeRef> exceptions = new TreeSet<>();
public final List<String> body;
- public
- MethodRef(TypeRef returnType, String name, ParameterRef[] parameters, List<String> body) {
+ public MethodRef(TypeRef returnType, String name, ParameterRef[] parameters, List<String> body) {
this.returnType = returnType;
this.name = name;
this.parameters = parameters;
this.body = body;
}
+
+ public MethodRef exception(TypeRef... exceptions) {
+ addAll(this.exceptions, exceptions);
+ return this;
+ }
}