summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java')
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/Utils.java46
1 files changed, 44 insertions, 2 deletions
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 8bb1b33..5d69fc0 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,13 +1,29 @@
package io.trygvis.container.compiler;
+import io.trygvis.container.compiler.model.ClassG;
+import io.trygvis.container.compiler.model.Imports;
+import io.trygvis.container.compiler.model.TypeRef;
+
+import javax.annotation.processing.ProcessingEnvironment;
+import javax.lang.model.element.Element;
+import javax.persistence.PersistenceException;
+import javax.tools.JavaFileObject;
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.PrintWriter;
import java.io.StringReader;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import static io.trygvis.container.compiler.model.ClassG.addAll;
import static java.lang.Character.toLowerCase;
import static java.lang.Character.toUpperCase;
+import static org.apache.commons.lang.StringUtils.stripEnd;
public class Utils {
+ public static final String EOL = System.getProperty("line.separator");
+
public static String toFieldName(String s) {
if (s.length() < 1) {
return s.toLowerCase();
@@ -40,6 +56,11 @@ public class Utils {
return new String(chars, 0, j);
}
+ public static String toClassName(String s) {
+ s = toFieldName(s);
+ return toUpperCase(s.charAt(0)) + s.substring(1, s.length());
+ }
+
public static String toSetterName(String s) {
s = toFieldName(s);
return "set" + toUpperCase(s.charAt(0)) + s.substring(1, s.length());
@@ -60,8 +81,8 @@ public class Utils {
buffer.append(line.replace("\"", "\\\""));
buffer.append('"');
line = reader.readLine();
- if(line != null) {
- buffer.append(" +\n");
+ if (line != null) {
+ buffer.append(" +").append(EOL);
}
}
@@ -70,4 +91,25 @@ public class Utils {
throw new RuntimeException(e);
}
}
+
+ public static void writeFile(ProcessingEnvironment processingEnv, ClassG g, Element element) throws IOException {
+ JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(g.type.fqName, element);
+ try (PrintWriter w = new PrintWriter(sourceFile.openWriter())) {
+ for (String s : g.generate()) {
+ w.println(stripEnd(s, " "));
+ }
+ }
+ }
+
+ public static List<String> tryCatchSqlException(Imports imports, List<String> body) {
+ TypeRef sqlException = imports.add(SQLException.class);
+ TypeRef persistenceException = imports.add(PersistenceException.class);
+ List<String> newBody = new ArrayList<>();
+ newBody.add("try {");
+ addAll(1, newBody, body);
+ newBody.add("} catch (" + sqlException.plainName + " e) {");
+ newBody.add(" throw new " + persistenceException.plainName + "(e);"); // TODO: Add context info.
+ newBody.add("}");
+ return newBody;
+ }
}