Author: tchemit Date: 2011-02-02 10:15:10 +0100 (Wed, 02 Feb 2011) New Revision: 2190 Url: http://nuiton.org/repositories/revision/jaxx/2190 Log: yet optimize generated code... Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObjectDecorator.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/BoxedCompiledObjectDecorator.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/DefaultCompiledObjectDecorator.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/HelpRootCompiledObjectDecorator.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/finalizers/ValidatorFinalizer.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObjectDecorator.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObjectDecorator.java 2011-02-01 17:43:44 UTC (rev 2189) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObjectDecorator.java 2011-02-02 09:15:10 UTC (rev 2190) @@ -91,7 +91,7 @@ JavaFile javaFile, String packageName, String className, - String fullClassName); + String fullClassName) throws ClassNotFoundException; /** * Obtain the creation code of the given {@code object} from the {@code compiler} to inject in generate method Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/BoxedCompiledObjectDecorator.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/BoxedCompiledObjectDecorator.java 2011-02-01 17:43:44 UTC (rev 2189) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/BoxedCompiledObjectDecorator.java 2011-02-02 09:15:10 UTC (rev 2190) @@ -53,7 +53,7 @@ JavaFile javaFile, String packageName, String className, - String fullClassName) { + String fullClassName) throws ClassNotFoundException { CompiledObject parent = object.getParent(); if (parent == null) { parent = root; Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/DefaultCompiledObjectDecorator.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/DefaultCompiledObjectDecorator.java 2011-02-01 17:43:44 UTC (rev 2189) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/DefaultCompiledObjectDecorator.java 2011-02-02 09:15:10 UTC (rev 2190) @@ -69,7 +69,7 @@ JavaFile javaFile, String packageName, String className, - String fullClassName) { + String fullClassName) throws ClassNotFoundException { if (object instanceof ScriptInitializer) { Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/HelpRootCompiledObjectDecorator.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/HelpRootCompiledObjectDecorator.java 2011-02-01 17:43:44 UTC (rev 2189) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/decorators/HelpRootCompiledObjectDecorator.java 2011-02-02 09:15:10 UTC (rev 2190) @@ -26,11 +26,13 @@ package jaxx.compiler.decorators; import jaxx.compiler.CompiledObject; +import jaxx.compiler.CompiledObjectDecorator; import jaxx.compiler.CompilerConfiguration; import jaxx.compiler.JAXXCompiler; import jaxx.compiler.finalizers.JAXXCompilerFinalizer; import jaxx.compiler.java.JavaElementFactory; import jaxx.compiler.java.JavaFile; +import jaxx.compiler.reflect.ClassDescriptor; import jaxx.runtime.swing.help.JAXXHelpUI; import java.awt.Component; @@ -78,7 +80,7 @@ JavaFile javaFile, String packageName, String className, - String fullClassName) { + String fullClassName) throws ClassNotFoundException { super.finalizeCompiler(compiler, root, object, @@ -94,9 +96,25 @@ // add JAXXHelpUI interface Class<?> validatorInterface = JAXXHelpUI.class; String helpBrokerFQN = getBrokerFQN(compiler); - javaFile.addInterface(validatorInterface.getName() + - "<" + helpBrokerFQN + ">"); + boolean needInterface = isNeedInterface(compiler, + validatorInterface); + + if (needInterface) { + + // only add the contract if needed + + if (log.isDebugEnabled()) { + log.debug("Add " + validatorInterface + " on " + + javaFile.getName() + " : parent " + + JAXXCompiler.getCanonicalName( + compiler.getRootObject())); + } + + javaFile.addInterface(validatorInterface.getName() + + "<" + helpBrokerFQN + ">"); + } + javaFile.addMethod(JavaElementFactory.newMethod( Modifier.PUBLIC, JAXXCompilerFinalizer.TYPE_VOID, @@ -158,6 +176,49 @@ } } + /** + * Detects if the given {@code compiler} need the validatorInterface. + * <p/> + * We need to test it deeply since the interface is added by the decorator + * and is not present on the symbol table of compiled objects. + * + * @param compiler the compiler to test + * @param validatorInterface the validator interface to seek for + * @return {@code true} if we need to add the interface, {@code false} otherwise + * @throws ClassNotFoundException if could not find a class + * @since 2.4 + */ + protected boolean isNeedInterface(JAXXCompiler compiler, + Class<?> validatorInterface) throws ClassNotFoundException { + if (compiler.isSuperClassAware(validatorInterface)) { + + // parent has already the interface + return false; + } + CompiledObject root = compiler.getRootObject(); + ClassDescriptor rootObjectClass = root.getObjectClass(); + String superClassName = JAXXCompiler.getCanonicalName(rootObjectClass); + JAXXCompiler parentCompiler = compiler.getEngine().getJAXXCompiler(superClassName); + if (parentCompiler == null) { + + // parent was not compiled + return true; + } + + CompiledObjectDecorator decorator = parentCompiler.getRootObject().getDecorator(); + + if (decorator != null && decorator instanceof HelpRootCompiledObjectDecorator) { + + // parent is already with help, no need of the interface + return false; + } + + boolean superClassResult = isNeedInterface(parentCompiler, validatorInterface); + + // ok must add the interface + return superClassResult; + } + public static Set<String> getHelpIds() { return new HashSet<String>(helpIds); } Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/finalizers/ValidatorFinalizer.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/finalizers/ValidatorFinalizer.java 2011-02-01 17:43:44 UTC (rev 2189) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/finalizers/ValidatorFinalizer.java 2011-02-02 09:15:10 UTC (rev 2190) @@ -61,16 +61,14 @@ /** Logger. */ static Log log = LogFactory.getLog(ValidatorFinalizer.class); - protected static final String VALIDATOR_UTIL_PREFIX = - SwingValidatorUtil.class.getSimpleName() + "."; - protected static final JavaField VALIDATOR_IDS_FIELD = JavaElementFactory.newField( Modifier.PROTECTED, List.class.getName() + "<String>", "validatorIds", true, - "new java.util.ArrayList<String>()" + "new %s<String>()", + ArrayList.class.getName() ); @Override @@ -104,7 +102,7 @@ ); if (found) { - + compiler.setNeedSwingUtil(true); // box the child component in a JxLayer child.setChildJavaCode( SwingUtil.class.getSimpleName() + @@ -117,16 +115,18 @@ // register validators List<CompiledBeanValidator> validators = BeanValidatorHandler.getValidators(compiler); - javaFile.addImport(Validator.class); - javaFile.addImport(ValidatorField.class); +// javaFile.addImport(Validator.class); +// javaFile.addImport(ValidatorField.class); javaFile.addImport(SwingValidatorUtil.class); - javaFile.addImport(ArrayList.class); - + String validatorUtilPrefix = + compiler.getImportManager().getType(SwingValidatorUtil.class) + + "."; + compiler.getJavaFile().addMethod(JavaElementFactory.newMethod( Modifier.PUBLIC, TYPE_VOID, "registerValidatorFields", - VALIDATOR_UTIL_PREFIX + "installFields(this);", + validatorUtilPrefix + "installFields(this);", true) ); builder.append("// register "); @@ -135,10 +135,10 @@ builder.append(eol); builder.append("validatorIds = "); - builder.append(VALIDATOR_UTIL_PREFIX).append("initUI(this);"); + builder.append(validatorUtilPrefix).append("initUI(this);"); builder.append(eol); - builder.append(VALIDATOR_UTIL_PREFIX).append("installUI(this);"); + builder.append(validatorUtilPrefix).append("installUI(this);"); builder.append(eol); compiler.appendLateInitializer(builder.toString()); @@ -173,18 +173,20 @@ // implements JAXXValidator addField(javaFile, VALIDATOR_IDS_FIELD); - String initializer = "return (" + validatorClass.getSimpleName() + + String type = compiler.getImportManager().getType(validatorClass); + + String initializer = "return (" + type + "<?>) (validatorIds.contains(validatorId) ? " + "getObjectById(validatorId) : null);"; + javaFile.addMethod(JavaElementFactory.newMethod( Modifier.PUBLIC, - validatorClass.getName() + "<?>", + type + "<?>", "getValidator", initializer, true, - JavaElementFactory.newArgument( - TYPE_STRING, "validatorId")) + JavaElementFactory.newArgument(TYPE_STRING, "validatorId")) ); } @@ -196,7 +198,10 @@ String validatorId = TypeManager.getJavaCode(validator.getId()); - String validatorAnnotation = Validator.class.getSimpleName() + + String type = compiler.getImportManager().getType(Validator.class); + String fieldType = compiler.getImportManager().getType(ValidatorField.class); + + String validatorAnnotation = type + "( validatorId = " + validatorId + ")"; validatorField.addAnnotation(validatorAnnotation); Map<String, String> fields = validator.getFields(); @@ -249,7 +254,7 @@ ); } - String annotation = ValidatorField.class.getSimpleName() + + String annotation = fieldType + "( validatorId = " + validatorId + "," + " propertyName = " + keyCode + "," + " editorName = " + editorCode + "" + ")";