Specialize JavaFileTemplate.importedName(Type) 17/85317/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Oct 2019 18:46:29 +0000 (20:46 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Oct 2019 21:06:04 +0000 (23:06 +0200)
95% of call sites do not use annotations, hence we specialize
this method and support it through specialized
AbstractJavaGeneratedType.getReferenceString(Type).

This clearly separates the two codepaths and removes superfluous
checks and indirections in both paths.

Change-Id: I17bfef631d446d71147b17146d5b2cf71a8be623
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit dbaae85acd37ba622f4f53a7335af14e768fb180)

binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AbstractJavaGeneratedType.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/JavaFileTemplate.java

index 2326fbda61559a10a32f5d61285f7690d94861ec..a02843f01c383a96e756342c4b13e2d5fadfcb6d 100644 (file)
@@ -79,29 +79,38 @@ abstract class AbstractJavaGeneratedType {
         return name.simpleName();
     }
 
+    final String getReferenceString(final Type type) {
+        final String ref = getReferenceString(type.getIdentifier());
+        return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
+            ((ParameterizedType) type).getActualTypeArguments())
+                : ref;
+    }
+
     final String getReferenceString(final Type type, final String... annotations) {
+        // Package-private method, all callers who would be passing an empty array are bound to the more special
+        // case above, hence we know annotations.length >= 1
         final String ref = getReferenceString(type.getIdentifier());
-        if (!(type instanceof ParameterizedType)) {
-            return annotations.length == 0 ? ref : annotate(ref, annotations).toString();
-        }
+        return type instanceof ParameterizedType ? getReferenceString(annotate(ref, annotations), type,
+            ((ParameterizedType) type).getActualTypeArguments())
+                : annotate(ref, annotations).toString();
+    }
 
-        final StringBuilder sb = annotate(ref, annotations).append('<');
-        final Type[] types = ((ParameterizedType) type).getActualTypeArguments();
-        if (types.length == 0) {
-            return sb.append("?>").toString();
+    private String getReferenceString(final StringBuilder sb, final Type type, final Type[] arguments) {
+        if (arguments.length == 0) {
+            return sb.append("<?>").toString();
         }
 
-        for (int i = 0; i < types.length; i++) {
-            final Type t = types[i];
-            if (t instanceof WildcardType) {
+        sb.append('<');
+        for (int i = 0; i < arguments.length; i++) {
+            final Type arg = arguments[i];
+            if (arg instanceof WildcardType) {
                 sb.append("? extends ");
             }
-            sb.append(getReferenceString(t));
-            if (i != types.length - 1) {
+            sb.append(getReferenceString(arg));
+            if (i != arguments.length - 1) {
                 sb.append(", ");
             }
         }
-
         return sb.append('>').toString();
     }
 
@@ -162,10 +171,6 @@ abstract class AbstractJavaGeneratedType {
 
     private static StringBuilder annotate(final String ref, final String... annotations) {
         final StringBuilder sb = new StringBuilder();
-        if (annotations.length == 0) {
-            return sb.append(ref);
-        }
-
         final int dot = ref.lastIndexOf('.');
         if (dot != -1) {
             sb.append(ref, 0, dot + 1);
index 7db1bc4e801a75f9c92245a891f8fd75906b0f82..58282d64e275cc6dc1ea7ae0ef110f2d05e7f0fd 100644 (file)
@@ -67,6 +67,10 @@ class JavaFileTemplate {
                 .collect(Collectors.joining());
     }
 
+    final String importedName(final Type intype) {
+        return javaType.getReferenceString(intype);
+    }
+
     final String importedName(final Type intype, final String... annotations) {
         return javaType.getReferenceString(intype, annotations);
     }