From 824753b36cbdb20539f28959a7b2d399475d0350 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Wed, 8 Feb 2012 22:38:07 +0000 Subject: [PATCH] OPENJPA-2119: Added support for generating a constructor and greater support for tabbing. Also fixed a number of bugs. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1242154 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/persistence/util/SourceCode.java | 132 ++++++++++++++++-- .../persistence/util/localizer.properties | 1 + 2 files changed, 121 insertions(+), 12 deletions(-) diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/util/SourceCode.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/util/SourceCode.java index afa91e137..6612393a1 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/util/SourceCode.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/util/SourceCode.java @@ -127,7 +127,7 @@ public class SourceCode { * @param name fully-qualified name of a class * @return an existing class name instance or a new one. */ - ClassName getOrCreateImport(String name) { + public ClassName getOrCreateImport(String name) { for (Import i : imports) { if (i.name.getFullName().equals(name)) return i.name; @@ -368,6 +368,7 @@ public class SourceCode { private List interfaces = new ArrayList(); private Set fields = new TreeSet(); private Set methods = new TreeSet(); + private Set constructors = new TreeSet(); public Class(String name) { super(name, getOrCreateImport(name)); @@ -439,6 +440,13 @@ public class SourceCode { return method; } + public Constructor addConstructor(){ + Constructor c = new Constructor(type.simpleName); + if (!constructors.add(c)) + throw new IllegalArgumentException(_loc.get( + "src-duplicate-constructor", c, this).toString()); + return c; + } public void write(PrintWriter out, int tab) { super.write(out, tab); if (isAbstract) @@ -452,6 +460,9 @@ public class SourceCode { out.println(SPACE + BLOCK_DELIMITER.start); for (Field field:fields) field.write(out, 1); + for(Constructor ctor : constructors){ + ctor.write(out, 1); + } for (Method method:methods) method.write(out, 1); out.println(BLOCK_DELIMITER.end); @@ -536,11 +547,13 @@ public class SourceCode { * * */ - class Method extends Element { + public class Method extends Element { private boolean isAbstract; private List> args = new ArrayList>(); private List codeLines = new ArrayList(); - + int tabCount = 0; + String tab = ""; + Method(String n, String t) { this(n, getOrCreateImport(t)); } @@ -555,16 +568,42 @@ public class SourceCode { return this; } - public Method addCodeLine(String line) { - if (isAbstract) - throw new IllegalStateException("abstract method " + name - + " can not have body"); - if (!line.endsWith(SEMICOLON)) - line = line + SEMICOLON; - codeLines.add(line); - return this; + public Method addArgument(String className, String argName){ + ClassName cn = getOrCreateImport(className); + args.add(new Argument(cn, argName," ")); + return this; } + public void setTab(boolean inc) { + if (inc) + tabCount++; + else + tabCount--; + tab = ""; + for (int i = 0; i < tabCount * TABSIZE; i++) { + tab += SPACE; + } + } + + public Method addCodeLine(String line) { + if (isAbstract) + throw new IllegalStateException("abstract method " + name + " can not have body"); + // This doesn't handle try{ ... catch(){ if{ + if (line.endsWith("{") || line.endsWith("}")) { + + } + if (!line.endsWith(SEMICOLON) + && !(line.isEmpty() || line.endsWith("{") || line.endsWith("}") || line.startsWith("if"))) + line = line + SEMICOLON; + codeLines.add(tab + line); + return this; + } + // if tabInc = true, the current line, and all following lines will be tabbed. If false, a tab will be removed. + public Method addCodeLine(String line, boolean tabInc) { + setTab(tabInc); + return addCodeLine(line); + } + public Method makeAbstract() { if (codeLines.isEmpty()) isAbstract = true; @@ -607,6 +646,75 @@ public class SourceCode { } } + public class Constructor extends Element { + private List> args = new ArrayList>(); + private List codeLines = new ArrayList(); + int tabCount = 0; + String tab = ""; + + public Constructor(String name) { + super(name, null); + makePublic(); + } + + public Constructor addArgument(Argument arg) { + args.add(arg); + return this; + } + + public Constructor addArgument(String className, String argName) { + ClassName cn = getOrCreateImport(className); + args.add(new Argument(cn, argName, " ")); + return this; + } + + public Constructor addCodeLine(String line) { + // This doesn't handle try{ ... catch(){ if{ + if (line.endsWith("{") || line.endsWith("}")) { + + } + if (!line.endsWith(SEMICOLON) + && !(line.isEmpty() || line.endsWith("{") || line.endsWith("}") || line.startsWith("if"))) + line = line + SEMICOLON; + codeLines.add(tab + line); + return this; + } + /** + * if tabInc = true, the current line, and all following lines will be tabbed. If false, a tab will be removed. + */ + public Constructor addCodeLine(String line, boolean tabInc) { + setTab(tabInc); + return addCodeLine(line); + } + + public void setTab(boolean inc) { + if (inc) + tabCount++; + else + tabCount--; + tab = ""; + for (int i = 0; i < tabCount * TABSIZE; i++) { + tab += SPACE; + } + } + + @Override + public void write(PrintWriter out, int tab) { + out.println(BLANK); + super.write(out, tab); + out.print(name); + writeList(out, BLANK, args, ARGS_DELIMITER, true); + + out.println(SPACE + BLOCK_DELIMITER.start); + for (String line : codeLines) { + tab(out, tab+1); + out.println(line); + } + tab(out, tab); + out.println(BLOCK_DELIMITER.end); + } + + } /** * Represents import statement. * @@ -875,7 +983,7 @@ public class SourceCode { final char end; public Delimiter() { - this((char)0, (char)0); + this((char)' ', (char)' '); } public Delimiter(String pair) { diff --git a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/util/localizer.properties b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/util/localizer.properties index 8ea69dd3d..812144b3d 100644 --- a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/util/localizer.properties +++ b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/util/localizer.properties @@ -16,6 +16,7 @@ # under the License. src-duplicate-field: "{0}" is found to be a duplicated field in Class {1}. src-duplicate-method: "{0}" is found to be a duplicated method in Class {1}. +src-duplicate-constructor: "{0}" is found to be a duplicated constructor in Class {1}. src-invalid-method: "{0}" is not a valid method name. \ It must be a non-reserved Java token and a valid Java identifier. src-invalid-type: "{0}" is not a valid type name. It must be a valid Java package name, a non-reserved \