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:
@@ -46,7 +46,7 @@ public class ActionScriptUtils
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static String addImportAndGetShortType (Class<?> type, boolean isField,
|
protected static String addImportAndGetShortType (Class<?> type, boolean isField,
|
||||||
Set<String> imports)
|
ImportSet imports)
|
||||||
{
|
{
|
||||||
String full = toActionScriptType(type, isField);
|
String full = toActionScriptType(type, isField);
|
||||||
if (needsActionScriptImport(type, isField)) {
|
if (needsActionScriptImport(type, isField)) {
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ public class GenActionScriptStreamableTask extends GenTask
|
|||||||
protected void processClass (File javaSource, Class<?> sclass)
|
protected void processClass (File javaSource, Class<?> sclass)
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
if (!Streamable.class.isAssignableFrom(sclass)
|
boolean streamable = Streamable.class.isAssignableFrom(sclass) || sclass.isEnum();
|
||||||
|
if (!streamable
|
||||||
|| InvocationMarshaller.class.isAssignableFrom(sclass)
|
|| InvocationMarshaller.class.isAssignableFrom(sclass)
|
||||||
|| Modifier.isInterface(sclass.getModifiers())
|
|| Modifier.isInterface(sclass.getModifiers())
|
||||||
|| ActionScriptUtils.hasOmitAnnotation(sclass)) {
|
|| ActionScriptUtils.hasOmitAnnotation(sclass)) {
|
||||||
@@ -81,10 +82,15 @@ public class GenActionScriptStreamableTask extends GenTask
|
|||||||
// Generate the current version of the streamable
|
// Generate the current version of the streamable
|
||||||
StreamableClassRequirements reqs = new StreamableClassRequirements(sclass);
|
StreamableClassRequirements reqs = new StreamableClassRequirements(sclass);
|
||||||
|
|
||||||
Set<String> imports = Sets.newLinkedHashSet();
|
ImportSet imports = new ImportSet();
|
||||||
imports.add(ObjectInputStream.class.getName());
|
|
||||||
imports.add(ObjectOutputStream.class.getName());
|
|
||||||
String extendsName = "";
|
String extendsName = "";
|
||||||
|
if (sclass.isEnum()) {
|
||||||
|
imports.add("com.threerings.util.Enum");
|
||||||
|
extendsName = "Enum";
|
||||||
|
} else {
|
||||||
|
imports.add(ObjectInputStream.class.getName());
|
||||||
|
imports.add(ObjectOutputStream.class.getName());
|
||||||
|
}
|
||||||
if (!sclass.getSuperclass().equals(Object.class)) {
|
if (!sclass.getSuperclass().equals(Object.class)) {
|
||||||
extendsName =
|
extendsName =
|
||||||
ActionScriptUtils.addImportAndGetShortType(sclass.getSuperclass(), false, imports);
|
ActionScriptUtils.addImportAndGetShortType(sclass.getSuperclass(), false, imports);
|
||||||
@@ -119,16 +125,27 @@ public class GenActionScriptStreamableTask extends GenTask
|
|||||||
continue;
|
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,
|
"header", _header,
|
||||||
"package", sclass.getPackage().getName(),
|
"package", sclass.getPackage().getName(),
|
||||||
"classname", sclass.getSimpleName(),
|
"classname", ActionScriptUtils.toSimpleName(sclass),
|
||||||
"imports", imports,
|
"imports", imports.toList(),
|
||||||
"extends", extendsName,
|
"extends", extendsName,
|
||||||
"implements", Joiner.on(", ").join(implemented),
|
"implements", Joiner.on(", ").join(implemented),
|
||||||
"superclassStreamable", reqs.superclassStreamable,
|
"superclassStreamable", reqs.superclassStreamable,
|
||||||
"pubFields", pubFields,
|
"pubFields", pubFields,
|
||||||
|
"enumFields", enumFields,
|
||||||
"protFields", protFields,
|
"protFields", protFields,
|
||||||
"dobject", isDObject);
|
"dobject", isDObject);
|
||||||
|
|
||||||
@@ -137,9 +154,16 @@ public class GenActionScriptStreamableTask extends GenTask
|
|||||||
output = new GeneratedSourceMerger().merge(output, existing);
|
output = new GeneratedSourceMerger().merge(output, existing);
|
||||||
}
|
}
|
||||||
writeFile(outputLocation.getAbsolutePath(), output);
|
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
|
throws Exception
|
||||||
{
|
{
|
||||||
// Discover the location of the 'public class' declaration.
|
// Discover the location of the 'public class' declaration.
|
||||||
@@ -168,7 +192,7 @@ public class GenActionScriptStreamableTask extends GenTask
|
|||||||
public boolean array;
|
public boolean array;
|
||||||
public boolean oidList;
|
public boolean oidList;
|
||||||
|
|
||||||
public ASField (Field f, Set<String> imports)
|
public ASField (Field f, ImportSet imports)
|
||||||
{
|
{
|
||||||
name = f.getName();
|
name = f.getName();
|
||||||
capitalName = StringUtil.capitalize(name);
|
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. */
|
/** The path to our ActionScript source files. */
|
||||||
protected File _asroot;
|
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
|
||||||
Reference in New Issue
Block a user