Rework MethodSerializer (+ fallout) 72/46472/2
authorStephen Kitt <skitt@redhat.com>
Thu, 22 Sep 2016 14:23:41 +0000 (16:23 +0200)
committerTom Pantelis <tpanteli@brocade.com>
Wed, 12 Oct 2016 11:24:48 +0000 (11:24 +0000)
The main aim of this patch is to avoid instanceof-determined behaviour
(between MethodDeclaration and MethodDefinition):
* move getThrowsExceptions() and getBody() to Method;
* make getVisibility() and getBody() Optional;
* method declarations can specify thrown exceptions (which are
  separated by commas, not spaces).

In addition, use Java 8 lambdas where appropriate in MethodSerializer
and a few places elsewhere, and use Modifier instead of String for
modifiers.

Some clean-up:
* drop inferrable type parameters;
* use new ArrayList<>() instead of Lists.newArrayList();
* use Collections.singletonList() where appropriate.

Change-Id: I9ec10bf64d366056afacce3fd4038ba92d3c4e6e
Signed-off-by: Stephen Kitt <skitt@redhat.com>
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/RuntimeRegistratorFtlTemplate.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/Field.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/FieldSerializer.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/Method.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/MethodDeclaration.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/MethodDefinition.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/MethodSerializer.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/ModuleField.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/ModuleFieldSerializer.java

index 27fb2bcba153f953ebd0559fc8a9ee377b5e7ca8..dd2cd8cc46306e919a7ac5d55762036e7477033f 100644 (file)
@@ -12,6 +12,7 @@ import static com.google.common.base.Preconditions.checkState;
 import static java.lang.String.format;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
 import com.google.common.collect.Lists;
 import java.io.Closeable;
 import java.util.ArrayList;
@@ -25,11 +26,11 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
+import javax.lang.model.element.Modifier;
 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation;
-import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation.Parameter;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper;
@@ -40,7 +41,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
             String name, List<Field> fields, List<MethodDefinition> methods) {
         // TODO header
         super(null, runtimeBeanEntry.getPackageName(), name, Collections
-                .<String> emptyList(), Collections.singletonList(Closeable.class
+                .emptyList(), Collections.singletonList(Closeable.class
                 .getCanonicalName()), fields, methods);
     }
 
@@ -85,7 +86,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
         String registratorName = getJavaNameOfRuntimeRegistrator(rootRB);
         List<MethodDefinition> methods = new ArrayList<>();
         Field rootRuntimeBeanRegistratorField = new Field(
-                Lists.newArrayList("final"),
+                Collections.singletonList(Modifier.FINAL),
                 RootRuntimeBeanRegistrator.class.getName(),
                 "rootRuntimeBeanRegistrator");
         List<Field> constructorParameters = Lists
@@ -95,8 +96,8 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
                 registratorName, constructorParameters, constructorBody);
         methods.add(constructor);
 
-        LinkedHashMap<String, RuntimeRegistratorFtlTemplate> RuntimeRegistratorFtlTemplates = createRegistrationHierarchy(
-                rootRB, Collections.<String> emptySet());
+        LinkedHashMap<String, RuntimeRegistratorFtlTemplate> RuntimeRegistratorFtlTemplates =
+                createRegistrationHierarchy(rootRB, Collections.emptySet());
         RuntimeRegistratorFtlTemplate rootFtlFile = RuntimeRegistratorFtlTemplates
                 .values().iterator().next();
 
@@ -127,7 +128,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
         // TODO add header
         GeneralClassTemplate registrator = new GeneralClassTemplate(null,
                 rootRB.getPackageName(), registratorName,
-                Collections.<String> emptyList(), Collections.singletonList(Closeable.class
+                Collections.emptyList(), Collections.singletonList(Closeable.class
                 .getCanonicalName()), constructorParameters, methods);
 
         checkState(!RuntimeRegistratorFtlTemplates.containsKey(registrator
@@ -140,7 +141,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
     }
 
     private static Field hierachicalRegistration = new Field(
-            Lists.newArrayList("final"),
+            Collections.singletonList(Modifier.FINAL),
             HierarchicalRuntimeBeanRegistration.class.getCanonicalName(),
             "registration");
 
@@ -172,7 +173,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
         Set<String> currentOccupiedKeys = new HashSet<>(occupiedKeys);
         currentOccupiedKeys.add(parent.getJavaNamePrefix());
 
-        Field registratorsMapField = new Field(Collections.singletonList("final"),
+        Field registratorsMapField = new Field(Collections.singletonList(Modifier.FINAL),
                 TypeHelper.getGenericType(Map.class, String.class,
                         AtomicInteger.class), "unkeyedMap", "new "
                         + TypeHelper.getGenericType(HashMap.class,
@@ -209,8 +210,9 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
                         "String key = \"%s\"; //TODO: check for conflicts\n",
                         key));
 
-                if (child.getKeyJavaName().isPresent()) {
-                    value = "bean.get" + child.getKeyJavaName().get() + "()";
+                Optional<String> childKeyJavaName = child.getKeyJavaName();
+                if (childKeyJavaName.isPresent()) {
+                    value = "bean.get" + childKeyJavaName.get() + "()";
                     value = "String.valueOf(" + value + ")";
                 } else {
                     body.append("java.util.concurrent.atomic.AtomicInteger counter = unkeyedMap.get(key);\n"
@@ -227,13 +229,13 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
                 body.append(format("return new %s(r);",
                         childRegistrator.getFullyQualifiedName()));
 
-                Field param = new Field(Lists.newArrayList("final"),
+                Field param = new Field(Collections.singletonList(Modifier.FINAL),
                         child.getJavaNameOfRuntimeMXBean(), "bean");
                 MethodDefinition register = new MethodDefinition(
-                        Collections.singletonList("synchronized"),
+                        Collections.singletonList(Modifier.SYNCHRONIZED),
                         childRegistrator.getFullyQualifiedName(), "register",
-                        Collections.singletonList(param), Collections.<String> emptyList(),
-                        Collections.<Annotation> emptyList(), body.toString());
+                        Collections.singletonList(param), Collections.emptyList(),
+                        Collections.emptyList(), body.toString());
                 methods.add(register);
 
             }
@@ -274,10 +276,10 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate {
         // Arrays.asList(IOException.class.getCanonicalName()),
         // Collections.<Annotation> emptyList(), body);
         List<Annotation> annotations = Lists.newArrayList(new Annotation(
-                "Override", Collections.<Parameter> emptyList()));
-        return new MethodDefinition(Collections.<String> emptyList(), "void",
-                "close", Collections.<Field> emptyList(),
-                Collections.<String> emptyList(), annotations, body);
+                "Override", Collections.emptyList()));
+        return new MethodDefinition(Collections.emptyList(), "void",
+                "close", Collections.emptyList(),
+                Collections.emptyList(), annotations, body);
     }
 
     @VisibleForTesting
index 4c67a67f42487d337c9ec8de0b42aa0562a93e2a..6e1f54ee53184dbf9fc000b2ef06225248b87268 100644 (file)
@@ -83,7 +83,7 @@ public class TemplateFactory {
                 // convert JavaAttribute parameters into fields
                 List<Field> fields = new ArrayList<>();
                 for (JavaAttribute ja : rpc.getParameters()) {
-                    Field field = new Field(Collections.<String> emptyList(),
+                    Field field = new Field(Collections.emptyList(),
                             ja.getType().getFullyQualifiedName(),
                             ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
                     fields.add(field);
index 312bb4a1db22b7384f35cf79b7511b4e0b4da346..c08a440f7fe303bd5fed16780b3ac88da7a2481d 100644 (file)
@@ -9,34 +9,35 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import com.google.common.collect.Lists;
+import java.util.ArrayList;
 import java.util.List;
+import javax.lang.model.element.Modifier;
 
 public class Field {
     private final String type;
     private final String name;
     private final String definition;
-    private final List<String> modifiers;
+    private final List<Modifier> modifiers;
     private final boolean needsDepResolver;
 
     public Field(String type, String name) {
-        this(Lists.<String> newArrayList(), type, name, null, false);
+        this(new ArrayList<>(), type, name, null, false);
     }
 
     public Field(String type, String name, String definition) {
-        this(Lists.<String> newArrayList(), type, name, definition, false);
+        this(new ArrayList<>(), type, name, definition, false);
     }
 
-    public Field(List<String> modifiers, String type, String name) {
+    public Field(List<Modifier> modifiers, String type, String name) {
         this(modifiers, type, name, null, false);
     }
 
-    public Field(List<String> modifiers, String type, String name,
+    public Field(List<Modifier> modifiers, String type, String name,
             String definition) {
         this(modifiers, type, name, definition, false);
     }
 
-    public Field(List<String> modifiers, String type, String name,
+    public Field(List<Modifier> modifiers, String type, String name,
             String nullableDefinition, boolean needsDepResolver) {
         this.modifiers = checkNotNull(modifiers);
         this.type = checkNotNull(type);
@@ -46,7 +47,7 @@ public class Field {
     }
 
     public Field(String type, String name, String definition, boolean needsDepResolver) {
-        this(Lists.<String> newArrayList(), type, name, definition, needsDepResolver);
+        this(new ArrayList<>(), type, name, definition, needsDepResolver);
     }
 
     public boolean isNeedsDepResolver() {
@@ -61,7 +62,7 @@ public class Field {
         return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
     }
 
-    public List<String> getModifiers() {
+    public List<Modifier> getModifiers() {
         return modifiers;
     }
 
index 7105c39fe7d5863594fc3cacdef15e883b8ea50d..1f61f8f0eebe4ad9c667b481dca1850c2ea4836c 100644 (file)
@@ -13,9 +13,7 @@ public class FieldSerializer {
     public static String toString(Field field) {
         StringBuilder build = new StringBuilder();
         build.append("private ");
-        for (String mod : field.getModifiers()) {
-            build.append(mod).append(" ");
-        }
+        field.getModifiers().forEach(mod -> build.append(mod).append(" "));
         build.append(field.getType()).append(" ");
         build.append(field.getName());
         if (field.getDefinition() != null) {
index d83a70ccee376c9ddfd24c6406e862ad2b1ce3a9..2394fc0d1dbb22e6d5fe3327cc1b016e811753b5 100644 (file)
@@ -8,11 +8,13 @@
 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
 import java.util.List;
+import java.util.Optional;
+import javax.lang.model.element.Modifier;
 
 public interface Method {
-    String getVisibility();
+    Optional<Modifier> getVisibility();
 
-    List<String> getModifiers();
+    List<Modifier> getModifiers();
 
     String getReturnType();
 
@@ -23,4 +25,8 @@ public interface Method {
     String getJavadoc();
 
     List<Annotation> getAnnotations();
+
+    List<String> getThrowsExceptions();
+
+    Optional<String> getBody();
 }
index 425f85fe25d1259e8cda5b14f91f106911ee4443..958eaba9fcf5bc2cac7fc8a1f5934f289ca7b9c1 100644 (file)
@@ -9,7 +9,8 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
 import java.util.Collections;
 import java.util.List;
-import org.apache.commons.lang3.StringUtils;
+import java.util.Optional;
+import javax.lang.model.element.Modifier;
 
 public class MethodDeclaration implements Method {
     private final String returnType;
@@ -20,7 +21,7 @@ public class MethodDeclaration implements Method {
 
     public MethodDeclaration(String returnType, String name,
             List<Field> parameters) {
-        this(returnType, name, parameters, Collections.<Annotation> emptyList());
+        this(returnType, name, parameters, Collections.emptyList());
     }
 
     public MethodDeclaration(String returnType, String name,
@@ -36,6 +37,16 @@ public class MethodDeclaration implements Method {
         return annotations;
     }
 
+    @Override
+    public List<String> getThrowsExceptions() {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public Optional<String> getBody() {
+        return Optional.empty();
+    }
+
     @Override
     public String getJavadoc() {
         return javadoc;
@@ -46,8 +57,8 @@ public class MethodDeclaration implements Method {
     }
 
     @Override
-    public String getVisibility() {
-        return StringUtils.EMPTY;
+    public Optional<Modifier> getVisibility() {
+        return Optional.empty();
     }
 
     @Override
@@ -66,7 +77,7 @@ public class MethodDeclaration implements Method {
     }
 
     @Override
-    public List<String> getModifiers() {
+    public List<Modifier> getModifiers() {
         return Collections.emptyList();
     }
 
index 1d0e3a40bf6b2064e4c48e24d8409cfba961f7b3..8a2909d81c3567535a2202742cf51fbc7e135d77 100644 (file)
@@ -9,12 +9,11 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import javax.lang.model.element.Modifier;
 
 public class MethodDefinition implements Method {
-    private static final String VISIBILITY_PUBLIC = Modifier.PUBLIC.toString();
-
-    private final List<String> modifiers;
+    private final List<Modifier> modifiers;
     private final String returnType;
     private final String name;
     private final List<Field> parameters;
@@ -32,18 +31,17 @@ public class MethodDefinition implements Method {
 
     public MethodDefinition(String returnType, String name,
             List<Field> parameters, String body) {
-        this(Collections.<String> emptyList(), returnType, name, parameters,
-                Collections.<String> emptyList(), Collections
-                        .<Annotation> emptyList(), body);
+        this(Collections.emptyList(), returnType, name, parameters,
+                Collections.emptyList(), Collections.emptyList(), body);
     }
 
     public MethodDefinition(String returnType, String name,
             List<Field> parameters, List<Annotation> annotations, String body) {
-        this(Collections.<String> emptyList(), returnType, name, parameters,
-                Collections.<String> emptyList(), annotations, body);
+        this(Collections.emptyList(), returnType, name, parameters,
+                Collections.emptyList(), annotations, body);
     }
 
-    public MethodDefinition(List<String> modifiers, String returnType,
+    public MethodDefinition(List<Modifier> modifiers, String returnType,
             String name, List<Field> parameters, List<String> throwsExceptions,
             List<Annotation> annotations, String body) {
         this.modifiers = modifiers;
@@ -70,8 +68,8 @@ public class MethodDefinition implements Method {
     }
 
     @Override
-    public String getVisibility() {
-        return VISIBILITY_PUBLIC;
+    public Optional<Modifier> getVisibility() {
+        return Optional.of(Modifier.PUBLIC);
     }
 
     @Override
@@ -89,16 +87,18 @@ public class MethodDefinition implements Method {
         return parameters;
     }
 
+    @Override
     public List<String> getThrowsExceptions() {
         return throwsExceptions;
     }
 
-    public String getBody() {
-        return body;
+    @Override
+    public Optional<String> getBody() {
+        return Optional.of(body);
     }
 
     @Override
-    public List<String> getModifiers() {
+    public List<Modifier> getModifiers() {
         return modifiers;
     }
 
index 4d727ae5b7582176bd8670d434cc9bf92bc85c66..a37a8d653bf358751c33afdf4e6b12331718155c 100644 (file)
@@ -8,25 +8,27 @@
 
 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 
+import java.util.Optional;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import javax.lang.model.element.Modifier;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
 
 class MethodSerializer {
 
     static String toString(Method method) {
         StringBuilder build = new StringBuilder();
+        Consumer<Modifier> appendWithSpace = string -> build.append(string).append(" ");
+
         if (method.getJavadoc() != null) {
             build.append(StringUtil.writeComment(method.getJavadoc(), true));
         }
 
-        for(Annotation a: method.getAnnotations()) {
-            build.append(a);
-        }
+        method.getAnnotations().forEach(build::append);
 
         build.append("    ");
-        build.append(method.getVisibility()).append(" ");
-        for (String mod : method.getModifiers()) {
-            build.append(mod).append(" ");
-        }
+        method.getVisibility().ifPresent(appendWithSpace);
+        method.getModifiers().forEach(appendWithSpace);
         build.append(method.getReturnType()).append(" ");
 
         build.append(method.getName()).append("(");
@@ -35,30 +37,27 @@ class MethodSerializer {
             if (!firstParam) {
                 build.append(", ");
             }
-            for (String mod : param.getModifiers()) {
-                build.append(mod).append(" ");
-            }
+            param.getModifiers().forEach(appendWithSpace);
             build.append(param.getType()).append(" ");
             build.append(param.getName());
             firstParam = false;
         }
         build.append(")");
 
-        if (method instanceof MethodDeclaration) {
+        if (!method.getThrowsExceptions().isEmpty()) {
+            build.append(" throws ");
+            build.append(method.getThrowsExceptions().stream().collect(Collectors.joining(", ")));
+        }
+
+        Optional<String> body = method.getBody();
+        if (!body.isPresent()) {
             build.append(";");
             build.append("\n");
-        } else if (method instanceof MethodDefinition) {
-            MethodDefinition definition = (MethodDefinition) method;
-            if (!definition.getThrowsExceptions().isEmpty()) {
-                build.append(" throws ");
-            }
-            for (String ex : definition.getThrowsExceptions()) {
-                build.append(ex).append(" ");
-            }
+        } else {
             build.append(" {");
             build.append("\n");
             build.append("        ");
-            build.append(definition.getBody());
+            build.append(body.get());
             build.append("\n");
             build.append("    ");
             build.append("}");
index 7dc864b67688629428ed2a2830c9a1124bd98ebe..bd28d93756497828d09b491b9dcb793c9b2a6d47 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import javax.lang.model.element.Modifier;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency;
 
 public class ModuleField extends Field {
@@ -19,8 +20,9 @@ public class ModuleField extends Field {
     private final boolean dependent, isListOfDependencies;
     private final Dependency dependency;
 
-    private ModuleField(List<String> modifiers, String type, String name, String attributeName, String nullableDefault,
-            boolean isDependency, Dependency dependency, boolean isListOfDependencies, boolean needsDepResolver) {
+    private ModuleField(List<Modifier> modifiers, String type, String name, String attributeName,
+            String nullableDefault, boolean isDependency, Dependency dependency, boolean isListOfDependencies,
+            boolean needsDepResolver) {
         super(modifiers, type, name, null, needsDepResolver);
         this.dependent = isDependency;
         this.dependency = dependency;
@@ -35,7 +37,7 @@ public class ModuleField extends Field {
 
     public ModuleField(String type, String name, String attributeName, String nullableDefault, boolean isDependency,
             Dependency dependency, boolean isListOfDependencies, boolean needsDepResolve) {
-        this(Collections.<String> emptyList(), type, name, attributeName, nullableDefault, isDependency, dependency,
+        this(Collections.emptyList(), type, name, attributeName, nullableDefault, isDependency, dependency,
                 isListOfDependencies, needsDepResolve);
     }
 
index abb3e4ccafb4d8ad684c4eef542aca6485d7e03f..bad7618c12c4a61a1e95b6c49fe1674e34103176 100644 (file)
@@ -28,9 +28,7 @@ public class ModuleFieldSerializer {
         builder.append("\n");
 
         builder.append("     private ");
-        for (String mod : moduleField.getModifiers()) {
-            builder.append(mod).append(" ");
-        }
+        moduleField.getModifiers().forEach(mod -> builder.append(mod).append(" "));
         builder.append(moduleField.getType()).append(" ");
         builder.append(moduleField.getName());
         if (moduleField.getNullableDefault() != null) {