Author: kmorin Date: 2009-07-24 17:20:05 +0200 (Fri, 24 Jul 2009) New Revision: 1528 Modified: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtGenerator.java Log: Comments + javadoc Modified: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java =================================================================== --- trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java 2009-07-24 15:19:02 UTC (rev 1527) +++ trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtAbstractClassGenerator.java 2009-07-24 15:20:05 UTC (rev 1528) @@ -18,16 +18,12 @@ */ package org.nuiton.guix.generator; -//~--- non-JDK imports -------------------------------------------------------- import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.MenuBar; import com.google.gwt.user.client.ui.TabPanel; import java.beans.IntrospectionException; import org.nuiton.guix.model.GuixModelObject; -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; @@ -52,7 +48,7 @@ /** * Generates a GWT abstract class * - * @author morin + * @author kmorin */ public class GwtAbstractClassGenerator extends GwtJavaFileGenerator { @@ -122,7 +118,7 @@ jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", getMethodName(Method.COMPONENTS_CREATION), null, null, methodBodies.get(Method.COMPONENTS_CREATION), "Components creation")); jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", getMethodName(Method.COMPONENTS_TREE), null, null, methodBodies.get(Method.COMPONENTS_TREE), "Components initialization")); - jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", getMethodName(Method.DATABINDING_INIT), null, null, "//TODO", "initilization of databinding")); + jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", getMethodName(Method.DATABINDING_INIT), null, null, "", "initilization of databinding")); //add script methods for (JavaMethod m : (List<JavaMethod>) script.get(ScriptPart.METHODS)) { @@ -289,8 +285,10 @@ } //if gmo represents a "normal" tag else { + //TagHandler of gmo's class TagHandler th = null; - + + //check if gmo's class has a TagHandler and get the class to generate instead if(TagManager.getGuixClassHandler(gmo.getClassDescriptor().getName()) != null) { try { th = (TagHandler) TagManager.getGuixClassHandler(gmo.getClassDescriptor().getName()).newInstance(); @@ -307,6 +305,7 @@ } } } + //check if gmo's superclass has a TagHandler and get the class to generate instead else if(gmo.getClassDescriptor().getSuperClass() != null && TagManager.getGuixClassHandler(gmo.getClassDescriptor().getSuperClass().getName()) != null) { try { th = (TagHandler) TagManager.getGuixClassHandler(gmo.getClassDescriptor().getSuperClass().getName()).newInstance(); @@ -323,9 +322,11 @@ } } } - + + //the GWT classloader ClassLoader cl = Label.class.getClassLoader(); ClassDescriptor cd = gmo.getClassDescriptor(); + //while cd represents a generated class, take cd's superclass while (clazz == null && cd != null) { try { clazz = cl.loadClass(cd.getPackageName() + "." + cd.getName()); @@ -334,6 +335,7 @@ cd = cd.getSuperClass(); } } + //if gmo's class or superclasses are not GWT classes, check with the System classloader if (clazz == null) { cl = ClassLoader.getSystemClassLoader(); cd = gmo.getClassDescriptor(); @@ -350,6 +352,7 @@ throw new ClassNotFoundException(); } + //if gmo has a TagHandler, add a field to the JavaFile with this TagHandler (used for the databinding generation) if (gmo.getClassDescriptor().getPackageName() == null && TagManager.getGuixClassHandler(gmo.getClassDescriptor().getName()) != null) { jf.addField(new JavaField(Modifier.FINAL | Modifier.PRIVATE, @@ -358,7 +361,8 @@ ((gmo.getConstructor() != null) ? gmo.getConstructor() : "") + ")", gmo.getJavadoc(), th), false); } - else if(gmo.getId() == null) { + //if gmo is the root ModelObject, it equals "this" + else if(gmo.getParent() == null) { jf.addField(new JavaField(Modifier.FINAL | Modifier.PRIVATE, (classes != null && classes.contains(gmo.getClassDescriptor().toString())) ? gmo.getClassDescriptor().getPackageName() + ".client." + gmo.getClassDescriptor().getName() + "Abstract" : gmo.getClassDescriptor().getPackageName() + "." + gmo.getClassDescriptor().getName(), gmo.getClassDescriptor().getName().toLowerCase(), "this", gmo.getJavadoc(), th), @@ -371,7 +375,7 @@ ((gmo.getConstructor() != null) ? gmo.getConstructor() : "") + ")", gmo.getJavadoc(), th), false); } - + //if gmo is not the root ModelObject if (gmo.getParent() != null) { String capitalizedId = (gmo.getId().length() > 0) ? Character.toUpperCase(gmo.getId().charAt(0)) + gmo.getId().substring(1) : gmo.getId(); StringBuffer creationMethod = new StringBuffer(); @@ -379,11 +383,14 @@ componentsCreation.append("create").append(capitalizedId).append("();\n"); //if gmo is the child of a cell if (gmo.getParent().getClassDescriptor().getPackageName() != null || !gmo.getParent().getClassDescriptor().getName().equalsIgnoreCase("Cell")) { - componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".add(").append(gmo.getId()); - if(tabName != null) { - componentsTree.append(",").append(tabName); + //if gmo herits from widget + if(com.google.gwt.user.client.ui.Widget.class.isAssignableFrom(clazz)) { + componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".add(").append(gmo.getId()); + if(tabName != null) { + componentsTree.append(",").append(tabName); + } + componentsTree.append(");\n"); } - componentsTree.append(");\n"); } processAttributes(clazz, gmo, creationMethod, null, geh, th); @@ -421,7 +428,7 @@ * @param creationMethod the StringBuffer containing the code of the creation method for the object represented by gmo (null if gmo represents the first tag) * @param componentsTree the StringBuffer containing the code of the settings of the attribute of the class (null if gmo does not represent the first tag) * @param geh the event handler - * @return + * @param th gmo's TagHandler */ private void processAttributes(Class clazz, GuixModelObject gmo, StringBuffer creationMethod, StringBuffer componentsTree, GwtEventHandler geh, TagHandler th) { //browses the attributes @@ -472,8 +479,8 @@ componentsTree.append("this.set").append(capitalizedAttribute).append("(").append(addQuote ? "\"" : "").append(attr.getValue()).append(addQuote ? "\"" : "").append(");\n"); } } - else if (log.isErrorEnabled()) { - log.error(attr.getName() + " cannot be set."); + else if (log.isWarnEnabled()) { + log.warn(attr.getName() + " cannot be set."); } } } @@ -497,6 +504,11 @@ } } + /** + * Browse the MenuBar children and generate the code + * @param gmo the GuixModelObject representing the MenuBar + * @return A map containing the creation, initialization and bindings methods + */ private Map<Method, String> browseMenuBar(GuixModelObject gmo) { StringBuffer componentsCreation = new StringBuffer(""); StringBuffer componentsTree = new StringBuffer(""); Modified: trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtGenerator.java =================================================================== --- trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtGenerator.java 2009-07-24 15:19:02 UTC (rev 1527) +++ trunk/guix-compiler-gwt/src/main/java/org/nuiton/guix/generator/GwtGenerator.java 2009-07-24 15:20:05 UTC (rev 1528) @@ -22,8 +22,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -46,6 +44,7 @@ /** log */ private Log log = LogFactory.getLog(GwtGenerator.class); + /** Maps the different generators with the file to save the generated JavaFile */ Map<GwtJavaFileGenerator, File> generators = new HashMap<GwtJavaFileGenerator, File>(); @Override @@ -151,21 +150,38 @@ @Override public List<Class> generateBindings(StringBuffer dbCreation, StringBuffer dbDeletion, TagHandler prevTh, JavaFile jf, Class clazz, String[] binding, int i, List<String> alreadyChecked, String methodToInvoke, Map<GuixGenerator, JavaFile> generatedFiles) { + //does the attribute or method to bind exists ? boolean bindingExists = false; + //the return type of the binding String returnType = null; + //the getter method for the binding String getter = null; + //the JavaFile to pass as an argument to the next call of this method JavaFile nextFile = null; + //the Class to pass as an argument to the next call of this method Class nextClazz = null; + //the TagHandler of this binding TagHandler th = null; + //ProxyEventInfo model of this binding String model = null; + //Listener to add to the bound object Class listener = null; + //the name of the listener adding method String addMethod = null; + //the name of the listener removing method String removeMethod = null; + //the Listener list result List<Class> result = new ArrayList<Class>(); - + + String realMethodName = null; + String realAttributename = null; + + //if the part of the binding is a method if (binding[i].endsWith(")")) { - String realMethodName = binding[i].substring(0, binding[i].indexOf("(")); - String realAttributename = null; + //the method name is all what is before the first open bracket + realMethodName = binding[i].substring(0, binding[i].indexOf("(")); + //if the parent of the method has a TagHandler, then check that the method is the generic method name for all the libraries + //or if it is the right name, and then determine the corresponding attribute if(prevTh != null) { if(binding[i].startsWith("get")) { realAttributename = prevTh.getAttrToGenerate(Character.toLowerCase(binding[i].charAt(3)) + binding[i].substring(4, binding[i].indexOf("("))); @@ -180,6 +196,7 @@ } } } + //if the method name is the real one, get the corresponding attribute name if(realAttributename == null) { if(binding[i].startsWith("get")) { realAttributename = Character.toLowerCase(binding[i].charAt(3)) + binding[i].substring(4, binding[i].indexOf("(")); @@ -191,6 +208,7 @@ realAttributename = binding[i].substring(0, binding[i].indexOf("(")); } } + //if the parent of the method is an instance of a generated class if (jf != null) { bindingExists = jf.getMethod(realMethodName, null) != null; if (bindingExists) { @@ -203,8 +221,7 @@ } else { try { - clazz.getMethod(realMethodName, null); - returnType = clazz.getMethod(realMethodName, null).getReturnType().getName(); + returnType = clazz.getMethod(realMethodName, (Class[])null).getReturnType().getName(); bindingExists = true; } catch (NoSuchMethodException eee) { @@ -212,16 +229,21 @@ } } getter = realMethodName + binding[i].substring(binding[i].indexOf("(")); + //replace the value of the binding by the getter method binding[i] = getter; } + //if the part of the binding is an attribute else { - String realAttributename = null; + //if the parent of the attribute has a TagHandler, then check that the attribute is the generic attribute name for all the libraries + //or if it is the right name if(prevTh != null) { realAttributename = prevTh.getAttrToGenerate(binding[i]); } + //if it is its real name if(realAttributename == null) { realAttributename = binding[i]; } + //if the parent of the attribute is an instance of a generated class if (jf != null) { bindingExists = jf.getMethod("get" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null) != null || jf.getMethod("is" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null) != null; if (bindingExists) { @@ -239,15 +261,13 @@ } else { try { - clazz.getMethod("get" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null); - returnType = clazz.getMethod("get" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null).getReturnType().getName(); + returnType = clazz.getMethod("get" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), (Class[])null).getReturnType().getName(); getter = "get" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1) + "()"; bindingExists = true; } catch (NoSuchMethodException eee) { try { - clazz.getMethod("is" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null); - returnType = clazz.getMethod("is" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), null).getReturnType().getName(); + returnType = clazz.getMethod("is" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1), (Class[])null).getReturnType().getName(); getter = "is" + Character.toUpperCase(realAttributename.charAt(0)) + realAttributename.substring(1) + "()"; bindingExists = true; } @@ -258,23 +278,28 @@ } binding[i] = getter; } + //if the binding binds an existing attribute or method if (bindingExists) { + //if it is the last element of the binding and its parent has a taghandler if (i == binding.length - 1 && prevTh != null) { - String getterWoBraces = getter.substring(0, getter.indexOf("(")); - if (prevTh.hasEventInfosAboutMethod(getterWoBraces)) { - model = prevTh.getEventInfosModelName(getterWoBraces); - listener = prevTh.getEventInfosListenerClass(getterWoBraces); - addMethod = prevTh.getEventInfosAddListenerMethodName(getterWoBraces); - removeMethod = prevTh.getEventInfosRemoveListenerMethodName(getterWoBraces); + String getterWoBrackets = getter.substring(0, getter.indexOf("(")); + //for GWT the only objects that can be bound are the ones which have got a TagHandler that defines methods which can be bound + if (prevTh.hasEventInfosAboutMethod(getterWoBrackets)) { + model = prevTh.getEventInfosModelName(getterWoBrackets); + listener = prevTh.getEventInfosListenerClass(getterWoBrackets); + addMethod = prevTh.getEventInfosAddListenerMethodName(getterWoBrackets); + removeMethod = prevTh.getEventInfosRemoveListenerMethodName(getterWoBrackets); } else { - log.error("Impossible binding"); + log.error("Impossible binding for the " + (realMethodName != null ? "method '" : "attribute '") + realAttributename + "' of the class '" + prevTh.getClassToGenerate().getName() + "'"); return null; } + //starts to generate the code dbCreation.append("if("); dbDeletion.append("if("); if (alreadyChecked != null && !alreadyChecked.isEmpty()) { + //add not null parent condition for (int j = 0; j < alreadyChecked.size(); j++) { dbCreation.append(alreadyChecked.get(0)); dbDeletion.append(alreadyChecked.get(0)); @@ -369,6 +394,7 @@ alreadyChecked = new ArrayList<String>(); } alreadyChecked.add(getter); + //if it is not the last element of the binding if (i + 1 < binding.length) { return generateBindings(dbCreation, dbDeletion, th, nextFile, nextClazz, binding, i + 1, alreadyChecked, methodToInvoke, generatedFiles); }