r1668 - branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java
Author: tchemit Date: 2009-12-02 13:21:40 +0100 (Wed, 02 Dec 2009) New Revision: 1668 Modified: branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaMethod.java Log: - add special method $registerDataBinding - improve field order in generation - imports are a set to avoid duplicate ones - use TypeManager to obtain constant name Modified: branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java =================================================================== --- branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java 2009-12-02 12:18:40 UTC (rev 1667) +++ branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaField.java 2009-12-02 12:21:40 UTC (rev 1668) @@ -1,27 +1,29 @@ -/* - * *##% - * JAXX Compiler - * Copyright (C) 2008 - 2009 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * ##%* - */ +/* + * *##% + * JAXX Compiler + * Copyright (C) 2008 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ package jaxx.compiler.java; import java.lang.reflect.Modifier; import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Represents a field in a Java source file being generated for output. <code>JavaFields</code> are created @@ -54,7 +56,7 @@ * @param modifiers the modifier keywords that should appear as part of the field's declaration * @param type the type of the field as it would appear in Java source code * @param name the field's name - * @param override + * @param override */ public JavaField(int modifiers, String type, String name, boolean override) { this(modifiers, type, name, override, null); @@ -70,7 +72,7 @@ * @param modifiers the modifier keywords that should appear as part of the field's declaration * @param type the type of the field as it would appear in Java source code * @param name the field's name - * @param override + * @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) { @@ -112,18 +114,26 @@ public int compare(JavaField o1, JavaField o2) { int result; if ((result = compareStatic(o1, o2)) != 0) { + // les deux fields ne sont pas comparables sur static return result; } // data sources must be on the last after all other fields if ((result = compareDataSource(o1, o2)) != 0) { + // les deux fields ne sont pas comparables sur datasource return result; } // same static if ((result = compareVisibility(o1, o2)) != 0) { + // les deux fields ne sont pas de la même visibilité return result; } + // sort on return type + if ((result = compareReturnType(o1, o2)) != 0) { + // les deux fields ne sont pas du même type + return result; + } // same visibility, test name - return o1.getName().compareTo(o2.getName()); + return compareName(o1.getName(), o2.getName()); } public int compareStatic(JavaField o1, JavaField o2) { @@ -137,6 +147,10 @@ return 0; } + public int compareReturnType(JavaField o1, JavaField o2) { + return o1.getType().compareTo(o2.getType()); + } + public int compareDataSource(JavaField o1, JavaField o2) { // first comparator modifiers : static always before none static if (o1.getName().startsWith("$DataSource") && !o2.getName().startsWith("$DataSource")) { @@ -170,5 +184,29 @@ } return 0; } + + static final Pattern NAME_PATTERN = Pattern.compile("(.+)([0-9]+)"); + + public int compareName(String n1, String n2) { + Matcher matcher1 = NAME_PATTERN.matcher(n1); + Matcher matcher2 = NAME_PATTERN.matcher(n2); + if (matcher1.matches() && matcher2.matches()) { + // les deux noms finissent par un nombre + String p1 = matcher1.group(1); + String p2 = matcher1.group(2); + int i = p1.compareTo(p2); + if (i != 0) { + // on est sur des noms de prefix différents, donc pas de tri sur les suffixes + return i; + } + // les deux noms doivent être triés sur les suffixes entiers + int i1 = Integer.valueOf(matcher1.group(2)); + int i2 = Integer.valueOf(matcher2.group(2)); + return i1 - i2; + } + + // les deux noms sont simplement comparé en alpha + return n1.compareTo(n2); + } } } Modified: branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java =================================================================== --- branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2009-12-02 12:18:40 UTC (rev 1667) +++ branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2009-12-02 12:21:40 UTC (rev 1668) @@ -20,13 +20,10 @@ */ package jaxx.compiler.java; -import jaxx.compiler.JAXXCompiler; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import jaxx.compiler.types.TypeManager; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * A Java source file being generated for output. Once the class is completely initialized, use the @@ -34,15 +31,11 @@ */ public class JavaFile extends JavaElement { - /** - * Logger - */ - static private final Log log = LogFactory.getLog(JavaFile.class); protected static final String GETTER_PATTERN = "return %1$s;"; protected static final String BOOLEAN_GETTER_PATTERN = "return %1$s !=null && %1$s;"; protected static final String SETTER_PATTERN = "%1$s oldValue = this.%2$s;\nthis.%2$s = newValue;\nfirePropertyChange(%3$s, oldValue, newValue);"; // protected static final String SETTER_PATTERN = "%1$s oldValue = this.%2$s;\nthis.%2$s = newValue;\nfirePropertyChange(\"%2$s\", oldValue, newValue);"; - private List<String> imports = new ArrayList<String>(); + private Set<String> imports = new HashSet<String>(); private List<JavaField> fields = new ArrayList<JavaField>(); private List<JavaMethod> methods = new ArrayList<JavaMethod>(); private List<JavaFile> innerClasses = new ArrayList<JavaFile>(); @@ -73,11 +66,13 @@ } public void addImport(Class<?> importString) { - imports.add(importString.getName()); + addImport(importString.getName()); } public String[] getImports() { - return imports.toArray(new String[imports.size()]); + List<String> result = new ArrayList<String>(imports); + Collections.sort(result); + return result.toArray(new String[result.size()]); } public String getSuperClass() { @@ -128,7 +123,7 @@ if (javaBean) { // add full javabean support accessor + mutator + constante with name of property to make it easier to use // compute the property constante - String constantId = JAXXCompiler.convertVariableNameToConstantName("property" + capitalizedName); + String constantId = TypeManager.convertVariableNameToConstantName("property" + capitalizedName); addSimpleField(JavaFileGenerator.newField(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL, String.class.getSimpleName(), constantId, false, "\"" + id + "\"")); if (Boolean.class.getName().equals(field.getType())) { Modified: branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaMethod.java =================================================================== --- branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaMethod.java 2009-12-02 12:18:40 UTC (rev 1667) +++ branches/jaxx-2.X/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaMethod.java 2009-12-02 12:21:40 UTC (rev 1668) @@ -1,23 +1,23 @@ -/* - * *##% - * JAXX Compiler - * Copyright (C) 2008 - 2009 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * ##%* - */ +/* + * *##% + * JAXX Compiler + * Copyright (C) 2008 - 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ package jaxx.compiler.java; import java.lang.reflect.Modifier; @@ -230,6 +230,7 @@ public boolean accept(JavaMethod method) { return method.getName().startsWith("create") || method.getName().startsWith("add") || method.getName().equals("$completeSetup") || + method.getName().equals("$registerDataBinding") || method.getName().equals("$initialize"); } },
participants (1)
-
tchemit@users.nuiton.org