Support converstion of java enum to com.threerings.util.Enum as part of actionscript streamable conversion.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6600 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2011-04-04 21:11:58 +00:00
parent 7120c9c292
commit cae3a3f1b9
3 changed files with 91 additions and 10 deletions
@@ -46,7 +46,7 @@ public class ActionScriptUtils
}
protected static String addImportAndGetShortType (Class<?> type, boolean isField,
Set<String> imports)
ImportSet imports)
{
String full = toActionScriptType(type, isField);
if (needsActionScriptImport(type, isField)) {
@@ -62,7 +62,8 @@ public class GenActionScriptStreamableTask extends GenTask
protected void processClass (File javaSource, Class<?> sclass)
throws Exception
{
if (!Streamable.class.isAssignableFrom(sclass)
boolean streamable = Streamable.class.isAssignableFrom(sclass) || sclass.isEnum();
if (!streamable
|| InvocationMarshaller.class.isAssignableFrom(sclass)
|| Modifier.isInterface(sclass.getModifiers())
|| ActionScriptUtils.hasOmitAnnotation(sclass)) {
@@ -81,10 +82,15 @@ public class GenActionScriptStreamableTask extends GenTask
// Generate the current version of the streamable
StreamableClassRequirements reqs = new StreamableClassRequirements(sclass);
Set<String> imports = Sets.newLinkedHashSet();
ImportSet imports = new ImportSet();
String extendsName = "";
if (sclass.isEnum()) {
imports.add("com.threerings.util.Enum");
extendsName = "Enum";
} else {
imports.add(ObjectInputStream.class.getName());
imports.add(ObjectOutputStream.class.getName());
String extendsName = "";
}
if (!sclass.getSuperclass().equals(Object.class)) {
extendsName =
ActionScriptUtils.addImportAndGetShortType(sclass.getSuperclass(), false, imports);
@@ -119,16 +125,27 @@ public class GenActionScriptStreamableTask extends GenTask
continue;
}
}
List<ASEnum> enumFields = Lists.newArrayList();
if (sclass.isEnum()) {
Object[] enums = sclass.getEnumConstants();
for (Object e : enums) {
enumFields.add(new ASEnum((Enum<?>)e));
}
}
String output = mergeTemplate("com/threerings/presents/tools/streamable_as.tmpl",
imports.removeGlobals();
String template = sclass.isEnum() ? "enum_as.tmpl" : "streamable_as.tmpl";
String output = mergeTemplate("com/threerings/presents/tools/" + template,
"header", _header,
"package", sclass.getPackage().getName(),
"classname", sclass.getSimpleName(),
"imports", imports,
"classname", ActionScriptUtils.toSimpleName(sclass),
"imports", imports.toList(),
"extends", extendsName,
"implements", Joiner.on(", ").join(implemented),
"superclassStreamable", reqs.superclassStreamable,
"pubFields", pubFields,
"enumFields", enumFields,
"protFields", protFields,
"dobject", isDObject);
@@ -137,9 +154,16 @@ public class GenActionScriptStreamableTask extends GenTask
output = new GeneratedSourceMerger().merge(output, existing);
}
writeFile(outputLocation.getAbsolutePath(), output);
// generate inner enums
for (Class<?> inner : sclass.getDeclaredClasses()) {
if (inner.isEnum()) {
processClass(javaSource, inner);
}
}
}
protected static void addExistingImports (String asFile, Set<String> imports)
protected static void addExistingImports (String asFile, ImportSet imports)
throws Exception
{
// Discover the location of the 'public class' declaration.
@@ -168,7 +192,7 @@ public class GenActionScriptStreamableTask extends GenTask
public boolean array;
public boolean oidList;
public ASField (Field f, Set<String> imports)
public ASField (Field f, ImportSet imports)
{
name = f.getName();
capitalName = StringUtil.capitalize(name);
@@ -194,6 +218,16 @@ public class GenActionScriptStreamableTask extends GenTask
}
}
protected static class ASEnum
{
public final String name;
public ASEnum (Enum<?> e)
{
name = e.name();
}
}
/** The path to our ActionScript source files. */
protected File _asroot;
@@ -0,0 +1,47 @@
// GENERATED PREAMBLE START
{{header}}
package {{package}} {
{{#imports}}
import {{this}};
{{/imports}}
// GENERATED PREAMBLE END
// GENERATED CLASSDECL START
public final class {{classname}} extends Enum
{
// GENERATED CLASSDECL END
// GENERATED ENUM START
{{#enumFields}}
public static const {{name}} :{{classname}} = new {{classname}}("{{name}}");
{{/enumFields}}
finishedEnumerating({{classname}});
/**
* Gets the values of the {{classname}} enum.
*/
public static function values () :Array
{
return Enum.values({{classname}});
}
/**
* Gets the {{classname}} instance that corresponds to the specified string.
* If no such value exists, an ArgumentError will be thrown.
*/
public static function valueOf (name :String) :{{classname}}
{
return Enum.valueOf({{classname}}, name) as {{classname}};
}
/** @private */
public function {{classname}} (name :String)
{
super(name);
}
// GENERATED ENUM END
// GENERATED CLASSFINISH START
}
}
// GENERATED CLASSFINISH END