Author: tchemit Date: 2011-02-02 13:17:37 +0100 (Wed, 02 Feb 2011) New Revision: 2198 Url: http://nuiton.org/repositories/revision/jaxx/2198 Log: introduce a new property on overridden compiled object : simpleType to have the simpliest type to use in cast Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObject.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObject.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObject.java 2011-02-02 12:16:34 UTC (rev 2197) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompiledObject.java 2011-02-02 12:17:37 UTC (rev 2198) @@ -32,6 +32,7 @@ import jaxx.compiler.tags.TagHandler; import jaxx.compiler.tags.TagManager; import jaxx.compiler.types.TypeManager; +import jaxx.runtime.JAXXUtil; import org.apache.commons.lang.StringUtils; import java.awt.Container; @@ -69,6 +70,14 @@ private boolean override; /** + * The type of the object without fqn if possible. + * <p/> + * This is used when casting on an overridden object and only available if + * {@link #override} is set to {@code true}. + */ + private String simpleType; + + /** * Comma-separated Java code snippets representing the parameters that * should be passed to the object's constructor. */ @@ -118,6 +127,10 @@ /** initializer of the object */ private String initializer; + public String getSimpleType() { + return simpleType; + } + public class ChildRef { CompiledObject child; @@ -302,6 +315,19 @@ } /** + * Sets the simple type of the object. + * <p/> + * Used when castin an overridden object. + * + * @param simpleType the simple type to use (or the fqn if there is a + * conflict with already imported types of the compiler). + * @since 2.4 + */ + public void setSimpleType(String simpleType) { + this.simpleType = simpleType; + } + + /** * Returns this object's CSS style class. * * @return the value of the <code>styleClass</code> attribute @@ -395,16 +421,24 @@ String result = javaCode; if (isOverride()) { // handle cases where object is overridden to be a different class - result = "((" + JAXXCompiler.getCanonicalName(this) + ") " + javaCode + ")"; + if (simpleType == null) { + + // means overridden but stil on the same type + result = javaCode; + } else { + result = "((" + simpleType + ") " + javaCode + ")"; +// result = "((" + JAXXCompiler.getCanonicalName(this) + ") " + javaCode + ")"; + } } return result; } public String getJavaCodeForProperty(String property) { - if (!isOverride()) { + if (!isOverride() || simpleType == null) { return javaCode; } - String result = "((" + JAXXCompiler.getCanonicalName(this) + ") " + javaCode + ")"; + String result = "((" + simpleType + ") " + javaCode + ")"; +// String result = "((" + JAXXCompiler.getCanonicalName(this) + ") " + javaCode + ")"; // String result = "((" + JAXXCompiler.getCanonicalName(getObjectClass()) + ") " + javaCode + ")"; String methodName = StringUtils.capitalize(property); @@ -474,9 +508,11 @@ MethodDescriptor addMethod = handler.getAddMethod(); ClassDescriptor listenerClass = addMethod.getParameterTypes()[0]; String type = compiler.getImportManager().getType(listenerClass.getName()); + String prefix = compiler.getImportManager().getType(JAXXUtil.class); + //TC-20091026 use 'this' instead of root object javaCode //TC-20091105 JAXXUtil.getEventListener is generic, no more need cast and use simple name - return getJavaCode() + '.' + addMethod.getName() + "(JAXXUtil.getEventListener(" + type + ".class, " + + return getJavaCode() + '.' + addMethod.getName() + "(" + prefix + ".getEventListener(" + type + ".class, " + TypeManager.getJavaCode(handler.getListenerMethod().getName()) + ", this, " + TypeManager.getJavaCode(compiler.getEventHandlerMethodName(handler)) + "));" + JAXXCompiler.getLineSeparator(); } Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java 2011-02-02 12:16:34 UTC (rev 2197) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java 2011-02-02 12:17:37 UTC (rev 2198) @@ -780,6 +780,19 @@ } object.setOverride(true); object.setOverrideType(f.getType()); + if (!getCanonicalName(f.getType()).equals(getCanonicalName(object))) { + + // types are not the same + String simpleType = + getImportManager().getType(getCanonicalName(object)); + if (log.isDebugEnabled()) { + log.debug("Simple type for " + object.getId() + + " : " + getCanonicalName(object) + + " against : " + + getCanonicalName(f.getType())); + } + object.setSimpleType(simpleType); + } break; } catch (NoSuchFieldException e) { if (log.isDebugEnabled()) { Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2011-02-02 12:16:34 UTC (rev 2197) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/java/JavaFile.java 2011-02-02 12:17:37 UTC (rev 2198) @@ -206,19 +206,11 @@ } public void addImport(String importString) { -// if (GeneratorUtil.isPrimitiveType(importString)) { -// // do nothing -// return; -// } try { - boolean wasAdded = importManager.addImport(importString); -// if (!wasAdded) { -// return; -// } + importManager.addImport(importString); } catch (Exception e) { - log.info("Could not determine simple name of import " + importString); + log.error("Could not determine simple name of import " + importString); } - imports.add(importString); } public void addImport(Class<?> importString) { @@ -350,7 +342,7 @@ "property" + capitalizedName); addSimpleField(JavaElementFactory.newField( Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL, - String.class.getSimpleName(), + JAXXCompilerFinalizer.TYPE_STRING, constantId, false, "\"" + id + "\"") );