diff --git a/src/main/java/com/threerings/presents/tools/ActionScriptUtils.java b/src/main/java/com/threerings/presents/tools/ActionScriptUtils.java index 0b53122e4..c046fee88 100644 --- a/src/main/java/com/threerings/presents/tools/ActionScriptUtils.java +++ b/src/main/java/com/threerings/presents/tools/ActionScriptUtils.java @@ -46,7 +46,7 @@ public class ActionScriptUtils } protected static String addImportAndGetShortType (Class type, boolean isField, - Set imports) + ImportSet imports) { String full = toActionScriptType(type, isField); if (needsActionScriptImport(type, isField)) { diff --git a/src/main/java/com/threerings/presents/tools/GenActionScriptStreamableTask.java b/src/main/java/com/threerings/presents/tools/GenActionScriptStreamableTask.java index 426c2898d..d7ce9b296 100644 --- a/src/main/java/com/threerings/presents/tools/GenActionScriptStreamableTask.java +++ b/src/main/java/com/threerings/presents/tools/GenActionScriptStreamableTask.java @@ -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 imports = Sets.newLinkedHashSet(); - imports.add(ObjectInputStream.class.getName()); - imports.add(ObjectOutputStream.class.getName()); + 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()); + } if (!sclass.getSuperclass().equals(Object.class)) { extendsName = ActionScriptUtils.addImportAndGetShortType(sclass.getSuperclass(), false, imports); @@ -119,16 +125,27 @@ public class GenActionScriptStreamableTask extends GenTask continue; } } + List 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 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 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; diff --git a/src/main/resources/com/threerings/presents/tools/enum_as.tmpl b/src/main/resources/com/threerings/presents/tools/enum_as.tmpl new file mode 100644 index 000000000..80643ed75 --- /dev/null +++ b/src/main/resources/com/threerings/presents/tools/enum_as.tmpl @@ -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