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 18:00:10 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-03 18:00:10 +0200
commite1cf8889628d2d31cf7067b8c002f229fc22007d (patch)
treeb6fda3f7f23f445ca0030e88368cb39e27badb96 /container-compiler-plugin/src/main/java/io/trygvis/container
parent054cfb131292893d100a43ea2cb20c591c17c810 (diff)
downloadcontainer-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.gz
container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.bz2
container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.tar.xz
container-playground-e1cf8889628d2d31cf7067b8c002f229fc22007d.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.java36
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/TransactionalHandler.java2
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java25
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java63
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/Constructor.java2
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/TypeRef.java56
6 files changed, 111 insertions, 73 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 ad36988..cf21887 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,6 +1,7 @@
package io.trygvis.container.compiler;
import io.trygvis.container.compiler.model.ClassG;
+import io.trygvis.container.compiler.model.TypeRef;
import io.trygvis.persistence.EntityMirror;
import io.trygvis.persistence.FieldMirror;
import io.trygvis.persistence.GeneratorSupport;
@@ -14,7 +15,6 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
-import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.persistence.Id;
import javax.tools.JavaFileObject;
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import static io.trygvis.container.compiler.Utils.toJavaString;
import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror;
import static java.lang.Character.isUpperCase;
import static javax.lang.model.util.ElementFilter.fieldsIn;
@@ -47,7 +48,7 @@ public class EntityHandler extends AbstractHandler {
TypeHandler typeHandler = (TypeHandler) typeHandlerClass.newInstance();
String type = entity.asType().toString();
- generatorSupport.addTypeHandler(type, typeHandler);
+ generatorSupport.addTypeHandler(new TypeRef(type, type), typeHandler);
System.out.println("Loaded TypeHandler for " + type + " through " + typeHandlerClass.getCanonicalName());
break;
}
@@ -78,8 +79,8 @@ public class EntityHandler extends AbstractHandler {
}
public void processEntity(TypeElement element) throws Exception {
- DeclaredType declaredType = types.getDeclaredType(element);
- EntityMirror entityMirror = new EntityMirror(generatorSupport, declaredType, sqlName(element.getSimpleName().toString()));
+ EntityMirror entityMirror = new EntityMirror(generatorSupport, new TypeRef(types.getDeclaredType(element)),
+ sqlName(element.getSimpleName().toString()));
for (VariableElement f : fieldsIn(elements.getAllMembers(element))) {
entityMirror.add(fromElement(generatorSupport, f));
@@ -101,16 +102,14 @@ public class EntityHandler extends AbstractHandler {
String p = elements.getPackageOf(element).getQualifiedName().toString();
- ClassG g = new ClassG(p, entityMirror.daoName);
- String insertInto = entityMirror.insertIntoSql();
- g.addPublicStaticFinalField(String.class, "insertIntoSql").value("\"" + insertInto + "\"");
-// List<String> body = new ArrayList<>();
-// body.add("this.insertIntoSql = \"" + insertInto + "\";");
-// g.addConstructor(new Parameters(), body);
+ ClassG g = new ClassG(entityMirror.daoType);
+ g.addPublicStaticFinalField(String.class, "createTableSql").value(toJavaString(entityMirror.createTableSql()));
+ g.addPublicStaticFinalField(String.class, "dropTableSql").value(toJavaString(entityMirror.dropTableSql()));
+ g.addPublicStaticFinalField(String.class, "insertIntoSql").value(toJavaString(entityMirror.insertIntoSql()));
+ g.addPublicStaticFinalField(String.class, "deleteFromSql").value(toJavaString(entityMirror.deleteFromSql()));
entityMirror.insertInto(g);
- String fileName = (p.length() == 0 ? "" : p + ".") + entityMirror.daoName;
- JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(fileName, element);
+ JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(entityMirror.daoType.fqName, element);
try (PrintWriter w = new PrintWriter(sourceFile.openWriter())) {
g.write(w);
}
@@ -119,7 +118,7 @@ public class EntityHandler extends AbstractHandler {
}
public FieldMirror fromElement(GeneratorSupport generatorSupport, VariableElement var) {
- TypeMirror type = var.asType();
+ TypeRef type = new TypeRef(var.asType().toString());
// System.out.print("element = ");
// elements.printElements(new PrintWriter(System.out), var);
String javaName = var.getSimpleName().toString();
@@ -128,13 +127,13 @@ public class EntityHandler extends AbstractHandler {
FieldMirror field;
boolean id = isId(var);
if (generatorSupport.isPrimitive(type)) {
- field = new PrimitiveFieldMirror(var, javaName, sqlName, id, notNull);
+ field = new PrimitiveFieldMirror(type, javaName, sqlName, id, notNull);
} else if (generatorSupport.hasTypeHandler(type)) {
if (id) {
throw new CompilerException(var, "A @Id field has to be a primitive or embedded.");
}
- field = new FieldMirror.ReferenceFieldMirror(var, javaName, sqlName, notNull);
+ field = new FieldMirror.ReferenceFieldMirror(type, javaName, sqlName, notNull);
} else {
throw new CompilerException(var, "Missing type handler for type: " + type);
}
@@ -151,10 +150,9 @@ public class EntityHandler extends AbstractHandler {
String p = packageElement.getQualifiedName().toString();
// TODO: Support a name prefix from @SqlEntitySet
- String className = "Daos";
+ TypeRef type = new TypeRef(p + ".Daos");
- String fileName = (p.length() == 0 ? "" : p + ".") + className;
- ClassG g = new ClassG(p, className);
+ ClassG g = new ClassG(type);
/*
TypeRef conType = g.addImport(Connection.class);
@@ -170,7 +168,7 @@ public class EntityHandler extends AbstractHandler {
g.addConstructor(parameters, body);
*/
- JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(fileName, packageElement);
+ JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(type.fqName, packageElement);
try (PrintWriter w = new PrintWriter(sourceFile.openWriter())) {
g.write(w);
}
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/TransactionalHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/TransactionalHandler.java
index d24a697..fbd7660 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/TransactionalHandler.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/TransactionalHandler.java
@@ -42,7 +42,7 @@ public class TransactionalHandler extends AbstractHandler {
String p = elements.getPackageOf(element).getQualifiedName().toString();
- ClassG g = new ClassG(p, className, targetClassName.toString());
+ ClassG g = new ClassG(new TypeRef(p + "." + className), new TypeRef(targetClassName.toString()));
FieldRef transactionManager = g.addField(PlatformTransactionManager.class, "transactionManager");
for (ExecutableElement constructor : constructorsIn(elements.getAllMembers(element))) {
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java
index 8bd0de3..5057968 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java
@@ -1,5 +1,9 @@
package io.trygvis.container.compiler;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
import static java.lang.Character.toLowerCase;
import static java.lang.Character.toUpperCase;
@@ -28,4 +32,25 @@ public class Utils {
}
return new String(chars, 0, j);
}
+
+ public static String toJavaString(String s) {
+ try {
+ BufferedReader reader = new BufferedReader(new StringReader(s));
+ String line = reader.readLine();
+ StringBuilder buffer = new StringBuilder();
+ while (line != null) {
+ buffer.append('"');
+ buffer.append(line.replace("\"", "\\\""));
+ buffer.append('"');
+ line = reader.readLine();
+ if(line != null) {
+ buffer.append(" +\n");
+ }
+ }
+
+ return buffer.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
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 9befa36..857311d 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
@@ -12,58 +12,46 @@ import static java.lang.reflect.Modifier.*;
import static org.springframework.util.StringUtils.collectionToDelimitedString;
public class ClassG {
- private final String packageName;
- public final String className;
- private final String extendsClass;
+ public final TypeRef type;
+ private final TypeRef extendsClass;
private final Set<TypeRef> imports = new TreeSet<>();
private final Set<FieldRef> fields = new TreeSet<>();
private final List<MethodRef> methods = new ArrayList<>();
private final List<Constructor> constructors = new ArrayList<>();
- public ClassG(String packageName, String className) {
- this(packageName, className, null);
+ public ClassG(TypeRef type) {
+ this(type, null);
}
- public ClassG(String packageName, String className, String extendsClass) {
- this.packageName = packageName;
- this.className = className;
+ public ClassG(TypeRef type, TypeRef extendsClass) {
+ this.type = type;
this.extendsClass = extendsClass;
}
public TypeRef addImport(final TypeMirror type) {
- if (type.getKind().isPrimitive()) {
- return TypeRef.find(type.getKind());
- }
-
- String canonicalName = type.toString();
-
- for (TypeRef i : imports) {
- if (i.canonicalName().equals(canonicalName)) {
- return i;
- }
- }
+ return addImport(new TypeRef(type));
+ }
- TypeRef ref = new TypeRef(canonicalName, canonicalName);
- imports.add(ref);
- return ref;
+ public TypeRef addImport(Class<?> c) {
+ return addImport(new TypeRef(c));
}
- public TypeRef addImport(Class<?> klass) {
- String canonicalName = klass.getCanonicalName();
- String simpleName = klass.getSimpleName();
- String name = simpleName;
+ public TypeRef addImport(TypeRef klass) {
+ String fqName = klass.fqName;
+ String name = klass.name;
for (TypeRef i : imports) {
- if (i.canonicalName().equals(canonicalName)) {
+ if (i.fqName.equals(fqName)) {
return i;
}
- if (i.name.equals(simpleName)) {
- name = canonicalName;
+ // If we've already found an import with the same name, use the fq version
+ if (i.name.equals(name)) {
+ name = fqName;
break;
}
}
- TypeRef ref = new TypeRef(name, canonicalName);
+ TypeRef ref = new TypeRef(name, fqName);
imports.add(ref);
return ref;
@@ -114,26 +102,23 @@ public class ClassG {
}
public void write(PrintWriter writer) {
- if (packageName.length() > 0) {
- writer.println("package " + packageName + ";");
+ if (!type.inUnnamedPackage()) {
+ writer.println("package " + type.packageName() + ";");
writer.println();
}
for (TypeRef i : imports) {
- if (i.isPrimitive()) {
- continue;
- }
- if (i.canonicalName().indexOf('.') == -1) {
+ if (i.isPrimitive() || i.inUnnamedPackage()) {
continue;
}
- writer.println("import " + i.canonicalName() + ";");
+ writer.println("import " + i.fqName + ";");
}
if (!imports.isEmpty()) {
writer.println();
}
- String extendsString = extendsClass == null ? "" : " extends " + extendsClass;
+ String extendsString = extendsClass == null ? "" : " extends " + extendsClass.name;
- writer.println("public class " + className + extendsString + " {");
+ writer.println("public class " + type.className + extendsString + " {");
for (FieldRef field : fields) {
writer.println();
writer.println(" " + field.toJava() + ";");
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/Constructor.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/Constructor.java
index 43800ce..f669273 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/Constructor.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/Constructor.java
@@ -27,7 +27,7 @@ public class Constructor {
String params = collectionToDelimitedString(ps, ", ");
- body.add(" public " + g.className + "(" + params + ") {");
+ body.add(" public " + g.type.className + "(" + params + ") {");
for (String s : this.body) {
body.add(" " + s);
}
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/TypeRef.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/TypeRef.java
index aabfb45..c6f958a 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/TypeRef.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/TypeRef.java
@@ -1,6 +1,7 @@
package io.trygvis.container.compiler.model;
import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
public class TypeRef implements Comparable<TypeRef> {
@@ -15,27 +16,56 @@ public class TypeRef implements Comparable<TypeRef> {
public static final TypeRef DOUBLE = new TypeRef("double", null);
/**
- * The name of a class used within a class file. Is either the simple name or the canonical name.
+ * The name of a class used within a class file. Is either the just the name or the fully qualified name.
*/
public final String name;
- private final String canonicalName;
+ public final String className;
- public String canonicalName() {
- if (canonicalName == null) {
- throw new RuntimeException("This type doesn't have a canonical name");
+ public final String fqName;
+ private final boolean primitive;
+
+ public TypeRef(String name, String fqName) {
+ this.name = name;
+ this.primitive = fqName == null;
+ this.fqName = fqName == null ? name : fqName;
+
+ int i = this.fqName.lastIndexOf('.');
+ if (i == -1) {
+ this.className = this.fqName;
+ } else {
+ this.className = this.fqName.substring(i + 1, this.fqName.length());
}
+ }
+
+ public TypeRef(TypeMirror type) {
+ this(type.toString());
+ }
+
+ public TypeRef(Class<?> klass) {
+ this(klass.getCanonicalName(), klass.getCanonicalName());
+ }
- return canonicalName;
+ public TypeRef(String fqName) {
+ this(fqName, fqName);
}
public boolean isPrimitive() {
- return canonicalName == null;
+ return primitive;
}
- public TypeRef(String name, String canonicalName) {
- this.name = name;
- this.canonicalName = canonicalName;
+ public boolean inUnnamedPackage() {
+ return fqName.indexOf('.') == -1;
+ }
+
+ public String packageName() {
+ int i = fqName.lastIndexOf('.');
+
+ if (i == -1) {
+ throw new RuntimeException("This type is in the unnamed package: " + name);
+ }
+
+ return fqName.substring(0, i);
}
@Override
@@ -45,17 +75,17 @@ public class TypeRef implements Comparable<TypeRef> {
TypeRef classRef = (TypeRef) o;
- return canonicalName.equals(classRef.canonicalName);
+ return fqName.equals(classRef.fqName);
}
@Override
public int hashCode() {
- return canonicalName.hashCode();
+ return fqName.hashCode();
}
@Override
public int compareTo(TypeRef o) {
- return canonicalName.compareTo(o.canonicalName);
+ return fqName.compareTo(o.fqName);
}
public static TypeRef find(TypeKind kind) {