r1541 - in trunk/guix-compiler-swing/src: main/java/org/nuiton/guix/generator test/java/org/nuiton/guix/generator
Author: kmorin Date: 2009-07-30 16:59:12 +0200 (Thu, 30 Jul 2009) New Revision: 1541 Modified: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingGenerator.java trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/generator/SwingGeneratorTest.java Log: Add the acceptation for tags that represents classes defined in the src directory Modified: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java =================================================================== --- trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java 2009-07-30 14:59:00 UTC (rev 1540) +++ trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingAbstractClassGenerator.java 2009-07-30 14:59:12 UTC (rev 1541) @@ -20,6 +20,10 @@ //~--- non-JDK imports -------------------------------------------------------- import java.beans.IntrospectionException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStreamReader; +import java.io.Reader; import org.nuiton.guix.tags.swing.MenuBarHandler; ; import org.nuiton.guix.model.GuixModelObject; @@ -62,6 +66,8 @@ /** Bindings to generate */ Map<String,Map<String, String>> bindings2Generate = new HashMap<String,Map<String, String>>(); + SwingGenerator swingGenerator; + /** * Constructor * @@ -80,6 +86,10 @@ script = sh.decomposeScript(gmo.getClassDescriptor().getScript()); } + public void setSwingGenerator(SwingGenerator swingGenerator) { + this.swingGenerator = swingGenerator; + } + @Override public JavaFile generate() { super.addImports(gmo); @@ -331,92 +341,101 @@ } //if the class hasn't been found if (clazz == null) { - File f = new File(gmo.getClassDescriptor().toString().replace('.', File.separatorChar)); - if(f.exists()) { - + File f = new File(swingGenerator.getSrcDir(), gmo.getClassDescriptor().toString().replace('.', File.separatorChar) + ".java"); + try { + Reader isr = new InputStreamReader(new FileInputStream(f)); + JavaFile jFile = JavaFileParser.parseJavaFile(gmo.getClassDescriptor().toString(), isr); + jf.addField(new JavaField(Modifier.PRIVATE, gmo.getClassDescriptor().toString(), gmo.getId(), gmo.getJavadoc()), true); + StringBuffer creationMethod = new StringBuffer(); + String capitalizedId = (gmo.getId().length() > 0) ? Character.toUpperCase(gmo.getId().charAt(0)) + gmo.getId().substring(1) : gmo.getId(); + creationMethod.append(gmo.getId()).append(" = new ").append(gmo.getClassDescriptor().toString()).append("(").append(gmo.getConstructor() != null ? gmo.getConstructor() : "").append(");\n"); + componentsCreation.append("create").append(capitalizedId).append("();\n"); + processAttributes(jFile, gmo, creationMethod); } - else { + catch (FileNotFoundException eee) { + log.error(eee); 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 - && th != null && th.getClassToGenerate() != null) { - jf.addField(new JavaField(Modifier.PRIVATE, - clazz.getName(), - gmo.getId(), gmo.getJavadoc(), th), - true); - } - //if gmo is the root ModelObject, it equals "this" - else if(gmo.getId() == null) { - jf.addField(new JavaField(Modifier.PRIVATE, - (classes != null && classes.contains(gmo.getClassDescriptor().toString())) ? gmo.getClassDescriptor().toString() + "Abstract" : gmo.getClassDescriptor().toString(), - gmo.getClassDescriptor().getName().toLowerCase(), "this", gmo.getJavadoc(), th), - true); - } else { - jf.addField(new JavaField(Modifier.PRIVATE, - (classes != null && classes.contains(gmo.getClassDescriptor().toString())) ? gmo.getClassDescriptor().toString() + "Abstract" : gmo.getClassDescriptor().toString(), - gmo.getId(), gmo.getJavadoc(), th), - true); - } + //if gmo has a TagHandler, add a field to the JavaFile with this TagHandler (used for the databinding generation) + if(gmo.getClassDescriptor().getPackageName() == null + && th != null && th.getClassToGenerate() != null) { + jf.addField(new JavaField(Modifier.PRIVATE, + clazz.getName(), + gmo.getId(), gmo.getJavadoc(), th), + true); + } + //if gmo is the root ModelObject, it equals "this" + else if(gmo.getId() == null) { + jf.addField(new JavaField(Modifier.PRIVATE, + (classes != null && classes.contains(gmo.getClassDescriptor().toString())) ? gmo.getClassDescriptor().toString() + "Abstract" : gmo.getClassDescriptor().toString(), + gmo.getClassDescriptor().getName().toLowerCase(), "this", gmo.getJavadoc(), th), + true); + } + else { + jf.addField(new JavaField(Modifier.PRIVATE, + (classes != null && classes.contains(gmo.getClassDescriptor().toString())) ? gmo.getClassDescriptor().toString() + "Abstract" : gmo.getClassDescriptor().toString(), + gmo.getId(), gmo.getJavadoc(), th), + true); + } - //if gmo does not represents the first tag - if (gmo.getParent() != null) { - //id with the first letter capitalized - String capitalizedId = (gmo.getId().length() > 0) ? Character.toUpperCase(gmo.getId().charAt(0)) + gmo.getId().substring(1) : gmo.getId(); - StringBuffer creationMethod = new StringBuffer(); - creationMethod.append(gmo.getId()).append(" = new ").append(classes.contains(gmo.getClassDescriptor().toString()) ? gmo.getClassDescriptor().toString() + "Impl" : clazz.getName()).append("(").append(gmo.getConstructor() != null ? gmo.getConstructor() : "").append(");\n"); - componentsCreation.append("create").append(capitalizedId).append("();\n"); + //if gmo does not represents the first tag + if (gmo.getParent() != null) { + //id with the first letter capitalized + String capitalizedId = (gmo.getId().length() > 0) ? Character.toUpperCase(gmo.getId().charAt(0)) + gmo.getId().substring(1) : gmo.getId(); + StringBuffer creationMethod = new StringBuffer(); + creationMethod.append(gmo.getId()).append(" = new ").append(classes.contains(gmo.getClassDescriptor().toString()) ? gmo.getClassDescriptor().toString() + "Impl" : clazz.getName()).append("(").append(gmo.getConstructor() != null ? gmo.getConstructor() : "").append(");\n"); + componentsCreation.append("create").append(capitalizedId).append("();\n"); - //FIXME - if (gmo.getStyleSheets() != null) { - styleSheets.addAll(gmo.getStyleSheets()); - } - - constraint = processAttributes(clazz, gmo, creationMethod, null, seh, null, th); + //FIXME + if (gmo.getStyleSheets() != null) { + styleSheets.addAll(gmo.getStyleSheets()); + } - //if gmo is the child of a cell - if (!((gmo.getParent().getClassDescriptor().getPackageName() == null || gmo.getParent().getClassDescriptor().getPackageName().equals("org.nuiton.guix.tags.swing")) && gmo.getParent().getClassDescriptor().getName().equalsIgnoreCase("Cell"))) { - //if gmo represents a JMenuBar - if (gmo.getClassDescriptor().toString().equals("javax.swing.JMenuBar")) { - //the method to add a JMenuBar is not add but setJMenuBar - componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".setJMenuBar(").append(gmo.getId()).append(");\n"); - } - else { - //add the component to its parent - if(java.awt.Component.class.isAssignableFrom(clazz)) { - componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".add(").append(gmo.getId()); - //with a constraint if it is not null - if (constraint != null) { - componentsTree.append(",").append(constraint); + constraint = processAttributes(clazz, gmo, creationMethod, null, seh, null, th); + + //if gmo is the child of a cell + if (!((gmo.getParent().getClassDescriptor().getPackageName() == null || gmo.getParent().getClassDescriptor().getPackageName().equals("org.nuiton.guix.tags.swing")) && gmo.getParent().getClassDescriptor().getName().equalsIgnoreCase("Cell"))) { + //if gmo represents a JMenuBar + if (gmo.getClassDescriptor().toString().equals("javax.swing.JMenuBar")) { + //the method to add a JMenuBar is not add but setJMenuBar + componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".setJMenuBar(").append(gmo.getId()).append(");\n"); + } + else { + //add the component to its parent + if(java.awt.Component.class.isAssignableFrom(clazz)) { + componentsTree.append(gmo.getParent().getId() == null ? "this" : gmo.getParent().getId()).append(".add(").append(gmo.getId()); + //with a constraint if it is not null + if (constraint != null) { + componentsTree.append(",").append(constraint); + } + componentsTree.append(");\n"); } - componentsTree.append(");\n"); } } - } - } - //if gmo represents the first tag - else { - boolean layoutDefined = false; - //set superclass and interface - if(th != null) { - jf.setSuperClass(th.getClassToGenerate().getName()); } + //if gmo represents the first tag else { - jf.setSuperClass(gmo.getClassDescriptor().getSuperClass().getPackageName() + "." + gmo.getClassDescriptor().getSuperClass().getName()); - } - jf.addInterface(gmo.getClassDescriptor().getName()); + boolean layoutDefined = false; + //set superclass and interface + if(th != null) { + jf.setSuperClass(th.getClassToGenerate().getName()); + } + else { + jf.setSuperClass(gmo.getClassDescriptor().getSuperClass().getPackageName() + "." + gmo.getClassDescriptor().getSuperClass().getName()); + } + jf.addInterface(gmo.getClassDescriptor().getName()); - //FIXME - if (gmo.getStyleSheets() != null) { - styleSheets.addAll(gmo.getStyleSheets()); + //FIXME + if (gmo.getStyleSheets() != null) { + styleSheets.addAll(gmo.getStyleSheets()); + } + + constraint = processAttributes(clazz, gmo, null, componentsTree, seh, layoutDefined, th); } - - constraint = processAttributes(clazz, gmo, null, componentsTree, seh, layoutDefined, th); } //browse the children of gmo for (GuixModelObject child : gmo.getChildren()) { @@ -483,7 +502,7 @@ m++; } //if yes - if(m < methods.length) { + if(m < methods.length) { //if the attribute binds the value of another component attribute if (binding != null) { if(!bindings2Generate.containsKey(gmo.getId())) { @@ -491,14 +510,17 @@ } bindings2Generate.get(gmo.getId()).put(attr.getName(), binding); } - else { //checks if the parameter of the setter is a String + else { + //checks if the parameter of the setter is a String addQuote = methods[m].getParameterTypes()[0].equals(String.class); //generates the code to set the attribute to object - if(creationMethod != null) + if(creationMethod != null) { creationMethod.append(gmo.getId()).append(".set").append(capitalizedAttribute).append("(").append(addQuote ? "\"" : "").append(attr.getValue()).append(addQuote ? "\"" : "").append(");\n"); + } //generates the code to set the attribute of the class - if(componentsTree != null) + if(componentsTree != null) { componentsTree.append("this.set").append(capitalizedAttribute).append("(").append(addQuote ? "\"" : "").append(attr.getValue()).append(addQuote ? "\"" : "").append(");\n"); + } //if the attribute name is layout if (layoutDefined != null && attr.getName().equals("layout")) { layoutDefined = true; @@ -536,6 +558,57 @@ } /** + * + * @param jFile + * @param gmo + * @param creationMethod + */ + private void processAttributes(JavaFile jFile, GuixModelObject gmo, StringBuffer creationMethod) { + //processCSSAttributes(gmo, seh, clazz); + //browses the attributes + for (AttributeDescriptor attr : gmo.getAttributeDescriptors()) { + boolean addQuote = false; + //the attribute name with the first letter capitalized + String capitalizedAttribute = (attr.getName().length() > 0) ? Character.toUpperCase(attr.getName().charAt(0)) + attr.getName().substring(1) : attr.getName(); + + JavaMethod method = null; + for(JavaMethod m : jFile.getMethods()) { + if(m.getName().equals("set" + capitalizedAttribute)) { + method = m; + break; + } + } + + if(method != null) { + String binding = BindingUtils.processDataBindings(attr.getValue()); + if(binding != null) { + if(!bindings2Generate.containsKey(gmo.getId())) { + bindings2Generate.put(gmo.getId(), new HashMap<String, String>()); + } + bindings2Generate.get(gmo.getId()).put(attr.getName(), binding); + } + else { + //checks if the parameter of the setter is a String + addQuote = method.getArguments()[0].getType().equals(String.class.getName()); + //generates the code to set the attribute to object + creationMethod.append(gmo.getId()).append(".set").append(capitalizedAttribute).append("(").append(addQuote ? "\"" : "").append(attr.getValue()).append(addQuote ? "\"" : "").append(");\n"); + } + } + else if(log.isErrorEnabled()) { + log.error(attr.getName() + " cannot be set."); + } + } + //if gmo does not represents the first tag + if(gmo.getId() != null && creationMethod != null) { + //id with the first letter capitalized + String capitalizedId = (gmo.getId().length() > 0) ? Character.toUpperCase(gmo.getId().charAt(0)) + gmo.getId().substring(1) : gmo.getId(); + + //add to the file the creation method for this object + jf.addMethod(new JavaMethod(Modifier.PRIVATE, "void", "create" + capitalizedId, null, null, creationMethod.toString(), gmo.getId() + " creation")); + } + } + + /** * Transform the css attributes into AttributeDescriptors * * @param gmo the GuixModelObject which has got the css attributes Modified: trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingGenerator.java =================================================================== --- trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingGenerator.java 2009-07-30 14:59:00 UTC (rev 1540) +++ trunk/guix-compiler-swing/src/main/java/org/nuiton/guix/generator/SwingGenerator.java 2009-07-30 14:59:12 UTC (rev 1541) @@ -52,9 +52,13 @@ public JavaFile generate() { try { String gmoClassName = gmo.getClassDescriptor().getName(); - File out = new File(destDir, gmoClassName + ".java"); - File outAbstract = new File(destDir, gmoClassName + "Abstract.java"); - File outImpl = new File(destDir, gmoClassName + "Impl.java"); + File outDir = new File(destDir, gmo.getClassDescriptor().getPackageName().replace('.', File.separatorChar)); + if(!outDir.exists()) { + outDir.mkdirs(); + } + File out = new File(outDir, gmoClassName + ".java"); + File outAbstract = new File(outDir, gmoClassName + "Abstract.java"); + File outImpl = new File(outDir, gmoClassName + "Impl.java"); if (lastModification > out.lastModified()) { try { @@ -73,6 +77,7 @@ } SwingInterfaceGenerator sing = new SwingInterfaceGenerator(gmo, classes); SwingAbstractClassGenerator sacg = new SwingAbstractClassGenerator(gmo, classes); + sacg.setSwingGenerator(this); SwingImplementationGenerator simg = new SwingImplementationGenerator(gmo, classes); sing.generate(); @@ -84,7 +89,7 @@ if (mainClass) { SwingMainClassGenerator smcg = new SwingMainClassGenerator(gmo); - File outMain = new File(destDir, launcherName + ".java"); + File outMain = new File(outDir, launcherName + ".java"); smcg.generate(outMain); } @@ -298,134 +303,145 @@ removeMethod = "removePropertyChangeListener"; } } - else { - listener = PropertyChangeListener.class; - addMethod = "addPropertyChangeListener"; - removeMethod = "removePropertyChangeListener"; + try { + nextClazz = Class.forName(returnType); + if(listener == null && nextClazz.isAssignableFrom(java.awt.Container.class)) { + listener = PropertyChangeListener.class; + addMethod = "addPropertyChangeListener"; + removeMethod = "removePropertyChangeListener"; + } } - - //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)); - for(int k = 1 ; k <= j ; k++) { - dbCreation.append(".").append(alreadyChecked.get(k)); - dbDeletion.append(".").append(alreadyChecked.get(k)); + catch (ClassNotFoundException eee) { + for(JavaFile javaFile : generatedFiles.values()) { + if(returnType.equals(javaFile.getPackageName() + "." + javaFile.getClassName())) { + nextFile = javaFile; + break; } - dbCreation.append(" != null && "); - dbDeletion.append(" != null && "); } + try { + Class c = Class.forName(nextFile.getSuperClass()); + if(listener == null && c.isAssignableFrom(java.awt.Container.class)) { + listener = PropertyChangeListener.class; + addMethod = "addPropertyChangeListener"; + removeMethod = "removePropertyChangeListener"; + } + } + catch(ClassNotFoundException eeee) { + } + catch(NullPointerException eeee) { + } + } + if(listener != 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)); + for(int k = 1 ; k <= j ; k++) { + dbCreation.append(".").append(alreadyChecked.get(k)); + dbDeletion.append(".").append(alreadyChecked.get(k)); + } + dbCreation.append(" != null && "); + dbDeletion.append(" != null && "); + } - if(binding.length > alreadyChecked.size() + 1) { + if(binding.length > alreadyChecked.size() + 1) { + for(int j = 0 ; j < alreadyChecked.size() ; j++) { + dbCreation.append(alreadyChecked.get(j)).append("."); + dbDeletion.append(alreadyChecked.get(j)).append("."); + } + dbCreation.append(getter).append(" != null"); + dbDeletion.append(getter).append(" != null"); + } + else { + dbCreation.delete(dbCreation.length() - 4, dbCreation.length()); + dbDeletion.delete(dbDeletion.length() - 4, dbDeletion.length()); + } + } + else { + dbCreation.append(getter).append(" != null"); + dbDeletion.append(getter).append(" != null"); + } + dbCreation.append(") {\n"); + dbDeletion.append(") {\n"); + + if(alreadyChecked != null) { for(int j = 0 ; j < alreadyChecked.size() ; j++) { dbCreation.append(alreadyChecked.get(j)).append("."); dbDeletion.append(alreadyChecked.get(j)).append("."); } - dbCreation.append(getter).append(" != null"); - dbDeletion.append(getter).append(" != null"); } + if(model != null) { + dbCreation.append("get").append(Character.toUpperCase(model.charAt(0))) + .append(model.substring(1)).append("()"); + dbDeletion.append("get").append(Character.toUpperCase(model.charAt(0))) + .append(model.substring(1)).append("()"); + } else { - dbCreation.delete(dbCreation.length() - 4, dbCreation.length()); - dbDeletion.delete(dbDeletion.length() - 4, dbDeletion.length()); + dbCreation.append(getter); + dbDeletion.append(getter); } - } - else { - dbCreation.append(getter).append(" != null"); - dbDeletion.append(getter).append(" != null"); - } - dbCreation.append(") {\n"); - dbDeletion.append(") {\n"); + // dbCreation.append(".").append(addMethod).append("((") + // .append(listener.getName()).append(") org.nuiton.guix.runtime.Util.getEventListener(") + // .append(listener.getName()).append(".class,this,\"onChangeFrom") + // .append(methodToInvoke).append("\"));\n}\n"); + // dbDeletion.append(".").append(removeMethod).append("((") + // .append(listener.getName()).append(") org.nuiton.guix.runtime.Util.getEventListener(") + // .append(listener.getName()).append(".class,this,\"onChangeFrom") + // .append(methodToInvoke).append("\"));\n}\n"); - if(alreadyChecked != null) { - for(int j = 0 ; j < alreadyChecked.size() ; j++) { - dbCreation.append(alreadyChecked.get(j)).append("."); - dbDeletion.append(alreadyChecked.get(j)).append("."); + dbCreation.append(".").append(addMethod).append("(new ").append(listener.getName()).append("() {\n"); + dbDeletion.append(".").append(removeMethod).append("(new ").append(listener.getName()).append("() {\n"); + for(Method m : listener.getMethods()) { + StringBuffer sb = new StringBuffer(); + for(Annotation a : m.getDeclaredAnnotations()) { + dbCreation.append(a.toString()).append("\n"); + } + //add the method that handles the event + sb.append("public ") + .append(m.getReturnType().getCanonicalName()) + .append(" ") + .append(m.getName()) + .append("("); + for(int n = 0 ; n < m.getParameterTypes().length ; n++) { + sb.append(m.getParameterTypes()[n].getCanonicalName()) + .append(" arg") + .append(n) + .append(", "); + } + sb.delete(sb.length() - 2, sb.length()) + .append(")") + .append(" {\n") + .append("onChangeFrom").append(methodToInvoke) + .append("();\n}\n"); + dbCreation.append(sb); + dbDeletion.append(sb); } - } - if(model != null) { - dbCreation.append("get").append(Character.toUpperCase(model.charAt(0))) - .append(model.substring(1)).append("()"); - dbDeletion.append("get").append(Character.toUpperCase(model.charAt(0))) - .append(model.substring(1)).append("()"); - } - else { - dbCreation.append(getter); - dbDeletion.append(getter); - } -// dbCreation.append(".").append(addMethod).append("((") -// .append(listener.getName()).append(") org.nuiton.guix.runtime.Util.getEventListener(") -// .append(listener.getName()).append(".class,this,\"onChangeFrom") -// .append(methodToInvoke).append("\"));\n}\n"); -// dbDeletion.append(".").append(removeMethod).append("((") -// .append(listener.getName()).append(") org.nuiton.guix.runtime.Util.getEventListener(") -// .append(listener.getName()).append(".class,this,\"onChangeFrom") -// .append(methodToInvoke).append("\"));\n}\n"); - - dbCreation.append(".").append(addMethod).append("(new ").append(listener.getName()).append("() {\n"); - dbDeletion.append(".").append(removeMethod).append("(new ").append(listener.getName()).append("() {\n"); - for(Method m : listener.getMethods()) { - StringBuffer sb = new StringBuffer(); - for(Annotation a : m.getDeclaredAnnotations()) { - dbCreation.append(a.toString()).append("\n"); + dbCreation.append("});\n}\n"); + dbDeletion.append("});\n}\n"); + + if(alreadyChecked == null){ + alreadyChecked = new ArrayList<String>(); } - //add the method that handles the event - sb.append("public ") - .append(m.getReturnType().getCanonicalName()) - .append(" ") - .append(m.getName()) - .append("("); - for(int n = 0 ; n < m.getParameterTypes().length ; n++) { - sb.append(m.getParameterTypes()[n].getCanonicalName()) - .append(" arg") - .append(n) - .append(", "); - } - sb.delete(sb.length() - 2, sb.length()) - .append(")") - .append(" {\n") - .append("onChangeFrom").append(methodToInvoke) - .append("();\n}\n"); - dbCreation.append(sb); - dbDeletion.append(sb); - } - dbCreation.append("});\n}\n"); - dbDeletion.append("});\n}\n"); - - try { - nextClazz = Class.forName(returnType); - } - catch (ClassNotFoundException eee) { - for(JavaFile javaFile : generatedFiles.values()) { - if(returnType.equals(javaFile.getPackageName() + "." + javaFile.getClassName())) { - nextFile = javaFile; - break; + alreadyChecked.add(getter); + try { + //if it is not the last element of the binding + if(i + 1 < binding.length) { + result.addAll(generateBindings(dbCreation, dbDeletion, th, nextFile, nextClazz, binding, i+1, alreadyChecked, methodToInvoke, generatedFiles)); } + if(listener != null && !result.contains(listener)) { + result.add(listener); + } } - } - if(alreadyChecked == null){ - alreadyChecked = new ArrayList<String>(); - } - alreadyChecked.add(getter); - try { - //if it is not the last element of the binding - if(i + 1 < binding.length) { - result.addAll(generateBindings(dbCreation, dbDeletion, th, nextFile, nextClazz, binding, i+1, alreadyChecked, methodToInvoke, generatedFiles)); - } - if(listener == null) { - log.error("Unable to find the right listener"); + catch(NullPointerException eee) { + log.error(i); + eee.printStackTrace(); return null; } - if(!result.contains(listener)) { - result.add(listener); - } } - catch(NullPointerException eee) { - return null; - } } else if(log.isErrorEnabled()) { log.error("unable to bind " + binding[i]); Modified: trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/generator/SwingGeneratorTest.java =================================================================== --- trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/generator/SwingGeneratorTest.java 2009-07-30 14:59:00 UTC (rev 1540) +++ trunk/guix-compiler-swing/src/test/java/org/nuiton/guix/generator/SwingGeneratorTest.java 2009-07-30 14:59:12 UTC (rev 1541) @@ -59,9 +59,7 @@ //tests without TagHandlers List<Class> l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, null, JToggleButton.class, new String[]{"isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>()); Assert.assertNotNull(l); - Assert.assertEquals(l.size(), 1); - Assert.assertNotNull(l.get(0)); - Assert.assertEquals(l.get(0), PropertyChangeListener.class); + Assert.assertEquals(l.size(), 0); //tests a successful generation with a method as a binding l = sg.generateBindings(new StringBuffer(), new StringBuffer(), new ToggleButtonHandler(), null, JToggleButton.class, new String[]{"isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>()); Assert.assertNotNull(l); @@ -76,16 +74,14 @@ Assert.assertEquals(l.get(0), ChangeListener.class); //tests with a javafile parameter not null - //tests if the result is null without the taghandler associated to the field + //tests if the result is empty without the taghandler associated to the field JavaFile jf = new JavaFile(); jf.addField(new JavaField(Modifier.PRIVATE, "javax.swing.JToggleButton", "button", null)); jf.addMethod(new JavaMethod(Modifier.PUBLIC, "javax.swing.JToggleButton", "getButton", null, null, "", null)); l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getButton()", "isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>()); Assert.assertNotNull(l); - Assert.assertEquals(l.size(), 1); - Assert.assertNotNull(l.get(0)); - Assert.assertEquals(l.get(0), PropertyChangeListener.class); - + Assert.assertEquals(l.size(), 0); + //tests a successful generation with a method as a binding jf = new JavaFile(); jf.addField(new JavaField(Modifier.PRIVATE, "javax.swing.JToggleButton", "button", null, new ToggleButtonHandler())); @@ -107,22 +103,36 @@ Assert.assertEquals(l.get(1), PropertyChangeListener.class); //tests with a javafile parameter not null with an attribute whose type is another generated file - //tests if the result is null without the taghandler associated to the field + //tests if the result is empty without the taghandler associated to the field jf = new JavaFile(); jf.addField(new JavaField(Modifier.PRIVATE, "test.Test", "test", null)); jf.addMethod(new JavaMethod(Modifier.PUBLIC, "test.Test", "getTest", null, null, "", null)); JavaFile jf2 = new JavaFile(Modifier.PUBLIC, 0, "test", "Test", null, null); jf2.addField(new JavaField(Modifier.PRIVATE, "javax.swing.JToggleButton", "button", null)); jf2.addMethod(new JavaMethod(Modifier.PUBLIC, "javax.swing.JToggleButton", "getButton", null, null, "", null)); - Assert.assertNull(sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()","isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>())); + l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()","isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>()); + Assert.assertNotNull(l); + Assert.assertEquals(l.size(), 0); //test without jf2 in the map of generators with JavaFile jf2 = new JavaFile(Modifier.PUBLIC, 0, "test", "Test", null, null); jf2.addField(new JavaField(Modifier.PRIVATE, "javax.swing.JToggleButton", "button", null, new ToggleButtonHandler())); jf2.addMethod(new JavaMethod(Modifier.PUBLIC, "javax.swing.JToggleButton", "getButton", null, null, "", null)); - Assert.assertNull(sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()","isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>())); + l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()","isSelected()"}, 0, null, "", new HashMap<GuixGenerator, JavaFile>()); + Assert.assertNotNull(l); + Assert.assertEquals(l.size(), 0); + Map<GuixGenerator,JavaFile> map = new HashMap<GuixGenerator, JavaFile>(); map.put(sg, jf2); + //tests an unsuccessful binding generation with a method as a binding because the superclass of j2 is not assignable from java.awt.Container + l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()", "isSelected()"}, 0, null, "", map); + Assert.assertNotNull(l); + Assert.assertEquals(l.size(), 0); //tests a successful generation with a method as a binding + jf2 = new JavaFile(Modifier.PUBLIC, 0, "test", "Test", "java.awt.Container", null); + jf2.addField(new JavaField(Modifier.PRIVATE, "javax.swing.JToggleButton", "button", null, new ToggleButtonHandler())); + jf2.addMethod(new JavaMethod(Modifier.PUBLIC, "javax.swing.JToggleButton", "getButton", null, null, "", null)); + map = new HashMap<GuixGenerator, JavaFile>(); + map.put(sg, jf2); l = sg.generateBindings(new StringBuffer(), new StringBuffer(), null, jf, null, new String[]{"getTest()","getButton()", "isSelected()"}, 0, null, "", map); Assert.assertNotNull(l); Assert.assertEquals(l.size(), 2);
participants (1)
-
kmorin@users.labs.libre-entreprise.org