Author: tchemit Date: 2011-01-25 12:44:23 +0100 (Tue, 25 Jan 2011) New Revision: 2159 Url: http://nuiton.org/repositories/revision/jaxx/2159 Log: Evolution #1238: Add getField method on JavaFile Evolution #1236: Can generate annotations on fields Reformat code Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaElement.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFileGenerator.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaElement.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaElement.java 2011-01-24 20:12:47 UTC (rev 2158) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaElement.java 2011-01-25 11:44:23 UTC (rev 2159) @@ -25,22 +25,35 @@ package jaxx.compiler.java; +import org.apache.commons.collections.CollectionUtils; + import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Base Java element + * * @author tchemit <chemit@codelutin.com> * @since 2.0.0 */ public abstract class JavaElement { -// private String lineSeparator; + // private String lineSeparator; private String name; + private int modifiers; + /** + * List of annoations. + * + * @since 2.3 + */ + private List<String> annotations; + public JavaElement(int modifiers, String name) { this.modifiers = modifiers; this.name = name; @@ -58,8 +71,8 @@ return name; } - public final void setName(String className) { - this.name = className; + public final void setName(String name) { + this.name = name; } public final String getModifiersText() { @@ -70,6 +83,21 @@ } } + public List<String> getAnnotations() { + if (annotations == null) { + annotations = new ArrayList<String>(); + } + return annotations; + } + + public boolean hasAnnotations() { + return CollectionUtils.isNotEmpty(annotations); + } + + public void addAnnotation(String annotation) { + getAnnotations().add(annotation); + } + public static final Comparator<JavaElement> JavaElementComparator = new Comparator<JavaElement>() { final Pattern NAME_PATTERN = Pattern.compile("(.+)([0-9]+)"); Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java 2011-01-24 20:12:47 UTC (rev 2158) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java 2011-01-25 11:44:23 UTC (rev 2159) @@ -26,7 +26,13 @@ package jaxx.compiler.java; import java.lang.reflect.Modifier; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; /** * Represents a field in a Java source file being generated for output. <code>JavaFields</code> are created @@ -34,17 +40,13 @@ */ public class JavaField extends JavaElement implements Comparable<JavaField> { - /** - * type of field (fqn) - */ + /** type of field (fqn) */ private String type; - /** - * initializer of field (can be null) - */ + + /** initializer of field (can be null) */ private String initializer; - /** - * flag to known where a field overrides a super-field - */ + + /** flag to known where a field overrides a super-field */ private boolean override; /** @@ -57,7 +59,10 @@ * @param name the field's name * @param override flag to add @Override annotation on getter and setter */ - public JavaField(int modifiers, String type, String name, boolean override) { + public JavaField(int modifiers, + String type, + String name, + boolean override) { this(modifiers, type, name, override, null); } @@ -74,7 +79,11 @@ * @param override {@code true} if method should be marked as overriden * @param initializer the initial value of the field, as it would appear in Java source code */ - public JavaField(int modifiers, String type, String name, boolean override, String initializer) { + public JavaField(int modifiers, + String type, + String name, + boolean override, + String initializer) { super(modifiers, name); this.type = type; this.initializer = initializer; @@ -166,8 +175,11 @@ @Override public boolean accept(JavaField field) { return true; - }}; + } + }; + private final String header; + private int modifier; FieldOrder(int modifier, String header) { Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2011-01-24 20:12:47 UTC (rev 2158) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2011-01-25 11:44:23 UTC (rev 2159) @@ -205,4 +205,13 @@ public void setSuperGenericType(String superGenericType) { this.superGenericType = superGenericType; } + + public JavaField getField(String componentId) { + for (JavaField field : fields) { + if (componentId.equals(field.getName())) { + return field; + } + } + return null; + } } Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFileGenerator.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFileGenerator.java 2011-01-24 20:12:47 UTC (rev 2158) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFileGenerator.java 2011-01-25 11:44:23 UTC (rev 2159) @@ -45,9 +45,7 @@ */ public class JavaFileGenerator { - /** - * Logger - */ + /** Logger */ static private final Log log = LogFactory.getLog(JavaFileGenerator.class); public static JavaField newField(int modifiers, String returnType, String name, boolean override) { @@ -82,17 +80,13 @@ return all + eol + "/*--" + new String(prefix) + " " + header + " " + new String(prefix) + "--*/" + eol + all + eol; } - /** - * End of line - */ + /** End of line */ protected final String eol; - /** - * verbose flag when generates - */ + + /** verbose flag when generates */ protected final boolean verbose; - /** - * current prefix indent size - */ + + /** current prefix indent size */ protected int indentationLevel = 0; public JavaFileGenerator(String eol, boolean verbose) { @@ -245,6 +239,7 @@ } } StringBuffer result = new StringBuffer(); + generateAnnotations(f, result, eol); result.append(f.getModifiersText()); result.append(f.getType()).append(' ').append(f.getName()); if (f.getInitializer() != null) { @@ -254,6 +249,19 @@ return result.toString(); } + protected void generateAnnotations(JavaElement element, + StringBuffer result, + String separator) { + if (element.hasAnnotations()) { + for (String annotation : element.getAnnotations()) { + if (!annotation.startsWith("@")) { + result.append("@"); + } + result.append(annotation).append(separator); + } + } + } + public String generateMethod(JavaMethod m) { if (verbose) { log.info(m.getName());