Search code examples
javaactionscript-3groovycode-generationgraniteds

Customizing Granite DS Actionscript code generation from Java classes


I'm using GraniteDS Actionscript code generation templates that let's me take a Java object and convert it to an Actionscript class.

It's mainly used for BlazeDS Java to Flash communication but I'm adapting it to work with JSON webservices using XStream/JETTISON JSON.

Is it possible to use the Granite DS Groovy templates to inspect annotations on a Java class and use that to generate the code bindings?

For example I create an @XStreamAlias to shorten the class name when sent through JSON, but I need my Actionscript generated classes to support that as well.

package com.webwars.game;
@XStreamAlias("UnitStack")
public class UnitStack implements Serializable {

I want my Actionscript generated code to be:

package com.webwars.gameplatform.combat.pvp {

    [Bindable]
    [RemoteClass(alias="UnitStack")]
    public class UnitStack extends UnitStackBase {

Is this possible with the groovy templates?

I can't seem to find any documentation on what properties are available in the GraniteDS Groovy Template JavaType? The documentation listed on the GraniteDS site for JavaType goes to a broken Javadoc link.

For example in my bean.gsp can I do something like:

<%

    ///////////////////////////////////////////////////////////////////////////
    // Use the XStreamAlias annotation as the classname

    def alias = jClass.qualifiedName;
    if (jClass.hasAnnotation("XStreamAlias)) {
      alias = jClass.getAnnotation("XStreamAlias");    
    } 

%>

[Bindable]
[RemoteClass(alias="${alias}")]
public class ${jClass.as3Type.name} extends ${jClass.as3Type.name}Base {<%

Solution

  • I discovered source code for the JavaBean.java >> JavaAbstractType.java which contains a method to get the Class:

    public abstract class JavaAbstractType implements JavaType {
        public Class<?> getType() {
            return type;
        }
    

    So in the Groovy bean.gsp template I modified it to support XStream aliased classes:

    <%
        //////////////////////////////////
        /// Check if class has been Aliased by XStream
        def alias = jClass.qualifiedName;
        def actualClass = jClass.type;
    
        if (jClass.isAnnotationPresent(
                            com.thoughtworks.xstream.annotations.XStreamAlias.class)) {
            com.thoughtworks.xstream.annotations.XStreamAlias xstreamAlias = actualClass.getAnnotation(com.thoughtworks.xstream.annotations.XStreamAlias.class);
            alias = xstreamAlias.value();
        }
    %>
    [Bindable]
    [RemoteClass(alias="${alias}")]
    public class ${jClass.as3Type.name} extends ${jClass.as3Type.name}Base {<%