xdoclet2 update: Autogenerating AS3 Value Objects from Java (and AS2 too)
April 14, 2006 on 1:06 pm | In Flex, Programming |Late last year I contributed an xdoclet2 plugin that allows ActionScript value objects to be directly generated from Java source code.
I’ve recently updated the plugin to support the latest Flex Enterprise and AS3 features. The plugin will:
- convert JavaBeans with accessor methods to simple ActionScript value objects with public var properties. The new int type is supported in AS3.
- generate appropriate Object.registerClass() or [RemoteClass] preambles.
- convert public static final constants from Java to public static const in ActionScript.
- for AS3 only, generate [Bindable] and/or [Managed] metadata tags.
The currently downloadable xdoclet-plugins 1.0.3 distribution was cut by the xdoclet2 maintainers before Flex Enterprise Server settled down, so I’ve built an updated Actionscript Plugin that supports the new FES2 metadata correctly. Download it here and use it as a replacement for xdoclet-plugin-actionscript-1.0.3.jar in the xdoclet2 1.0.3 distro.
Some tips on use…
The General Idea
For AS3, all you do is start with a JavaBean and annotate it a little bit. For example,
package com.example;
/**
* Example of a JavaBean with tags
* to generate the associated BeanInfo class.
*
* @actionscript.class
* managed=true
*/
public class SimpleBean {
/** A String field. */
protected String baseName = "";
public String getBaseName() {
return baseName;
}
public void setBaseName(String name) {
this.baseName = baseName;
}
}
You’ll wind up generating something like this:
package com.example
{
import com.example.SimpleBean;
/** Example of a JavaBean with tags
to generate the associated BeanInfo class. */
[Managed]
[RemoteClass(alias="com.example.SimpleBean")]
public class SimpleBean
{
public var baseName:String;
}
}
Tagging Java Classes for AS Generation
All you need to do is include an @actionscript.class or @actionscript.property javadoc tag in the class comment or getter-method comment. This tells the generation plugin what to do.
Support for [Bindable] and [Managed]
After an @actionscript.class or @actionscript.property javadoc tag, add either bindable=true or managed=true to generate the corresponding AS3 metadata tags at the class or property level respectively.
Running with Ant
The xdoclet2 site documentation is not the greatest. My friend Ilya Devèrs recently struggled with getting Ant to run the plugin, and kindly sent me back the scripts he used to do it. Here’s a somewhat bastardized version that illustrates the general approach to using the plugin.
Understanding Errors
Look for a velocity.log file located in the directory where you ran Ant. That will probably help.
3 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Entries and comments feeds.
Valid XHTML and CSS.
All content copyright (c) 2006-2007 Joseph Berkovitz. All Rights Reserved.


Great plugin. I have written a little tutorial showing how to do the instalation add the ant xml file and incorporate it all into the Eclipse IDE. I hope people find it useful, cheers Joe!
http://flextastic.blogspot.com/2006/09/adding-as3-code-generation-from.html
David
Comment by David — September 6, 2006 #
Hi,
I just had one problem when trying this.
I have a Java ‘model’ class with an attribute that is the type java.util.Date.
The generated ActionScript class keep the import line :
import java.util.Date;
I can fix this by hand. Just thought I would mention it.
Cheers
Matthew
Comment by Matthew Ryan — July 30, 2007 #
The code doesnt work when there are other custom annotations present in the comment section. if you have generated model object, you will have to manually remove the unrecognized annotations for it to work.
Comment by stephen — June 10, 2008 #