Bug 1209574 - Generate C++ name for any Java class in chain of declared classes; r=snorp
authorJim Chen <nchen@mozilla.com>
Thu, 08 Oct 2015 15:25:49 -0400
changeset 266981 25b2caf967ea2adefefe4b215d0bc4558fea9ac6
parent 266980 dee668cc4bb8a13d8225ee45628d55b9e824baa5
child 266982 e3c955ddee7b89986d99ff8d61f0c433a6d446f9
push id29504
push usercbook@mozilla.com
push dateFri, 09 Oct 2015 09:43:23 +0000
treeherdermozilla-central@d01dd42e654b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1209574
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1209574 - Generate C++ name for any Java class in chain of declared classes; r=snorp Right now, when we generate bindings for Java class A, and we encounter a Java type B, we generate a corresponding C++ name only if A == B, otherwise we generate a generic "jni::Object" C++ name. For example, class Foo { class Bar { Foo getFoo(Bar bar); } } In C++, Foo.Bar.getFoo would become, class Foo { class Bar { jni::Object::LocalRef getFoo(Bar::Param bar); }; }; This patch extends the code generator so that any Java class in the chain of declared classes gets a corresponding C++ name. The above example now becomes, class Foo { class Bar { Foo::LocalRef getFoo(Bar::Param bar); }; };
build/annotationProcessors/CodeGenerator.java
--- a/build/annotationProcessors/CodeGenerator.java
+++ b/build/annotationProcessors/CodeGenerator.java
@@ -61,25 +61,47 @@ public class CodeGenerator {
                         "public mozilla::jni::NativeImpl<" + unqualifiedName + ", Impl>\n" +
                 "{\n");
     }
 
     private String getTraitsName(String uniqueName, boolean includeScope) {
         return (includeScope ? clsName + "::" : "") + uniqueName + "_t";
     }
 
+    /**
+     * Return the C++ type name for this class or any class within the chain
+     * of declaring classes, if the target class matches the given type.
+     *
+     * Return null if the given type does not match any class searched.
+     */
+    private String getMatchingClassType(final Class<?> type) {
+        Class<?> cls = this.cls;
+        String clsName = this.clsName;
+
+        while (cls != null) {
+            if (type == cls) {
+                return clsName;
+            }
+            cls = cls.getDeclaringClass();
+            clsName = clsName.substring(0, Math.max(0, clsName.lastIndexOf("::")));
+        }
+        return null;
+    }
+
     private String getNativeParameterType(Class<?> type, AnnotationInfo info) {
-        if (type == cls) {
+        final String clsName = getMatchingClassType(type);
+        if (clsName != null) {
             return Utils.getUnqualifiedName(clsName) + "::Param";
         }
         return Utils.getNativeParameterType(type, info);
     }
 
     private String getNativeReturnType(Class<?> type, AnnotationInfo info) {
-        if (type == cls) {
+        final String clsName = getMatchingClassType(type);
+        if (clsName != null) {
             return Utils.getUnqualifiedName(clsName) + "::LocalRef";
         }
         return Utils.getNativeReturnType(type, info);
     }
 
     private void generateMember(AnnotationInfo info, Member member,
                                 String uniqueName, Class<?> type, Class<?>[] argTypes) {
         final StringBuilder args = new StringBuilder();