diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-05 15:16:34 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-05 15:16:34 +0000 |
commit | f0e9de8689abfa92a6f5b28afabb25e403a1a6bd (patch) | |
tree | e93933db273bb8cc9bed4167b9834c956624096f /mojo | |
parent | 0ad92036a417c001f88a0dc638d1e8e00c6da1e8 (diff) | |
download | chromium_src-f0e9de8689abfa92a6f5b28afabb25e403a1a6bd.zip chromium_src-f0e9de8689abfa92a6f5b28afabb25e403a1a6bd.tar.gz chromium_src-f0e9de8689abfa92a6f5b28afabb25e403a1a6bd.tar.bz2 |
Generate enums for mojo java bindings.
Review URL: https://codereview.chromium.org/300043006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275130 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
5 files changed, 64 insertions, 6 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java index ea77023..27fba62 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java @@ -8,6 +8,9 @@ import android.test.suitebuilder.annotation.SmallTest; import junit.framework.TestCase; +import org.chromium.mojo.bindings.test.imported.Color; +import org.chromium.mojo.bindings.test.imported.Shape; +import org.chromium.mojo.bindings.test.sample.Enum; import org.chromium.mojo.bindings.test.sample.InterfaceConstants; import org.chromium.mojo.bindings.test.sample.SampleServiceConstants; @@ -19,8 +22,7 @@ import java.lang.reflect.Modifier; */ public class BindingsTest extends TestCase { - private static void checkConstantField(Field field, Class<?> expectedClass) - { + private static void checkConstantField(Field field, Class<?> expectedClass) { assertEquals(expectedClass, field.getType()); assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); @@ -38,4 +40,25 @@ public class BindingsTest extends TestCase { checkConstantField(InterfaceConstants.class.getField("LONG"), long.class); } + /** + * Testing enums are correctly generated. + */ + @SmallTest + public void testEnums() throws NoSuchFieldException, SecurityException { + assertEquals(0, Color.COLOR_RED); + assertEquals(1, Color.COLOR_BLACK); + checkConstantField(Color.class.getField("COLOR_BLACK"), int.class); + checkConstantField(Color.class.getField("COLOR_RED"), int.class); + + assertEquals(0, Enum.ENUM_VALUE); + checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class); + + assertEquals(1, Shape.SHAPE_RECTANGLE); + assertEquals(2, Shape.SHAPE_CIRCLE); + assertEquals(3, Shape.SHAPE_TRIANGLE); + checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class); + checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class); + checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class); + } + } diff --git a/mojo/public/tools/bindings/generators/java_templates/enum.java.tmpl b/mojo/public/tools/bindings/generators/java_templates/enum.java.tmpl new file mode 100644 index 0000000..7096a18 --- /dev/null +++ b/mojo/public/tools/bindings/generators/java_templates/enum.java.tmpl @@ -0,0 +1,4 @@ +{% from "enum_definition.tmpl" import enum_def %} +{% include "header.java.tmpl" %} + +{{enum_def(enum, true)}} diff --git a/mojo/public/tools/bindings/generators/java_templates/enum_definition.tmpl b/mojo/public/tools/bindings/generators/java_templates/enum_definition.tmpl new file mode 100644 index 0000000..d72bd7f --- /dev/null +++ b/mojo/public/tools/bindings/generators/java_templates/enum_definition.tmpl @@ -0,0 +1,21 @@ +{%- macro enum_value(enum, field, index) -%} +{%- if field.value -%} +(int) ({{field.value|expression_to_text}}) +{%- elif index == 0 -%} +0 +{%- else -%} +{{enum.fields[index - 1].name}} + 1 +{%- endif -%} +{%- endmacro -%} + +{%- macro enum_def(enum, top_level) -%} +public {{ 'static ' if not top_level }}final class {{enum|name}} { + +{% for field in enum.fields %} + public static final int {{field.name}} = {{enum_value(enum, field, loop.index0)}}; +{% endfor %} + + private {{enum|name}}() {} + +} +{%- endmacro -%} diff --git a/mojo/public/tools/bindings/generators/mojom_java_generator.py b/mojo/public/tools/bindings/generators/mojom_java_generator.py index dbec5d0..7ba41df 100644 --- a/mojo/public/tools/bindings/generators/mojom_java_generator.py +++ b/mojo/public/tools/bindings/generators/mojom_java_generator.py @@ -109,7 +109,7 @@ def GetJavaType(kind): return "int" return _spec_to_java_type[kind.spec] -def TranslateConstants(token): +def ExpressionToText(token): def _TranslateNamedValue(named_value): entity_name = GetNameForElement(named_value) if named_value.parent_kind: @@ -127,9 +127,6 @@ def TranslateConstants(token): return token + 'L' return token -def ExpressionToText(token): - return TranslateConstants(token) - def GetConstantsMainEntityName(module): if 'JavaConstantsClassName' in module.attributes: return ParseStringAttribute(module.attributes['JavaConstantsClassName']) @@ -153,6 +150,13 @@ class Generator(generator.Generator): "package": GetPackage(self.module), } + @UseJinja("java_templates/enum.java.tmpl", filters=java_filters, + lstrip_blocks=True, trim_blocks=True) + def GenerateEnumSource(self, enum): + exports = self.GetJinjaExports() + exports.update({"enum": enum}) + return exports + @UseJinja("java_templates/constants.java.tmpl", filters=java_filters, lstrip_blocks=True, trim_blocks=True) def GenerateConstantsSource(self, module): @@ -175,6 +179,10 @@ class Generator(generator.Generator): # Ignore errors on directory creation. pass + for enum in self.module.enums: + self.Write(self.GenerateEnumSource(enum), + "%s.java" % GetNameForElement(enum)) + if self.module.constants: self.Write(self.GenerateConstantsSource(self.module), "%s.java" % GetConstantsMainEntityName(self.module)) diff --git a/mojo/public/tools/bindings/mojom_bindings_generator.gypi b/mojo/public/tools/bindings/mojom_bindings_generator.gypi index dcb273dd..1341685 100644 --- a/mojo/public/tools/bindings/mojom_bindings_generator.gypi +++ b/mojo/public/tools/bindings/mojom_bindings_generator.gypi @@ -37,6 +37,8 @@ '<(DEPTH)/mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_definition.tmpl', '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/constant_definition.tmpl', '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/constants.java.tmpl', + '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/enum_definition.tmpl', + '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/enum.java.tmpl', '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/header.java.tmpl', '<(DEPTH)/mojo/public/tools/bindings/generators/java_templates/java_macros.tmpl', '<(DEPTH)/mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl', |