X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2Fftl%2FRuntimeRegistratorFtlTemplate.java;h=47a1dfcfb1c19341dcc0f11540ec4d9507941a67;hb=refs%2Fchanges%2F54%2F57354%2F8;hp=d07edae14ee42283827ddf8c52cb6ce9d92aba44;hpb=9fb64948564e252018f9b1e13e7cea2c92f991aa;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/RuntimeRegistratorFtlTemplate.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/RuntimeRegistratorFtlTemplate.java index d07edae14e..47a1dfcfb1 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/RuntimeRegistratorFtlTemplate.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/RuntimeRegistratorFtlTemplate.java @@ -11,9 +11,11 @@ import static com.google.common.base.Preconditions.checkArgument; 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; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -24,26 +26,22 @@ 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; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; - public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { private RuntimeRegistratorFtlTemplate(RuntimeBeanEntry runtimeBeanEntry, String name, List fields, List methods) { // TODO header super(null, runtimeBeanEntry.getPackageName(), name, Collections - . emptyList(), Arrays.asList(Closeable.class + .emptyList(), Collections.singletonList(Closeable.class .getCanonicalName()), fields, methods); } @@ -67,7 +65,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { private static String constructConstructorBody( List constructorParameters) { - StringBuffer constructorBody = new StringBuffer(); + StringBuilder constructorBody = new StringBuilder(); for (Field field : constructorParameters) { constructorBody.append("this."); constructorBody.append(field.getName()); @@ -88,7 +86,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { String registratorName = getJavaNameOfRuntimeRegistrator(rootRB); List methods = new ArrayList<>(); Field rootRuntimeBeanRegistratorField = new Field( - Lists.newArrayList("final"), + Collections.singletonList(Modifier.FINAL), RootRuntimeBeanRegistrator.class.getName(), "rootRuntimeBeanRegistrator"); List constructorParameters = Lists @@ -98,8 +96,8 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { registratorName, constructorParameters, constructorBody); methods.add(constructor); - LinkedHashMap RuntimeRegistratorFtlTemplates = createRegistrationHierarchy( - rootRB, Collections. emptySet()); + LinkedHashMap RuntimeRegistratorFtlTemplates = + createRegistrationHierarchy(rootRB, Collections.emptySet()); RuntimeRegistratorFtlTemplate rootFtlFile = RuntimeRegistratorFtlTemplates .values().iterator().next(); @@ -108,19 +106,17 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { .getFullyQualifiedName(rootRB.getPackageName(), rootRB.getJavaNameOfRuntimeMXBean()); String childRegistratorFQN = rootFtlFile.getFullyQualifiedName(); Field rbParameter = new Field(fullyQualifiedNameOfMXBean, "rb"); - StringBuffer registerBody = new StringBuffer(); - registerBody.append(format("%s %s = this.%s.registerRoot(%s);\n", - HierarchicalRuntimeBeanRegistration.class - .getCanonicalName(), hierachchicalRegistration - .getName(), rootRuntimeBeanRegistratorField - .getName(), rbParameter.getName())); - registerBody.append(format("return new %s(%s);\n", - rootFtlFile.getFullyQualifiedName(), - hierachchicalRegistration.getName())); + String registerBody = format("%s %s = this.%s.registerRoot(%s);\n" + + "return new %s(%2$s);\n", + HierarchicalRuntimeBeanRegistration.class.getCanonicalName(), + hierachicalRegistration.getName(), + rootRuntimeBeanRegistratorField.getName(), + rbParameter.getName(), + rootFtlFile.getFullyQualifiedName()); MethodDefinition registerMethod = new MethodDefinition( childRegistratorFQN, "register", - Arrays.asList(rbParameter), registerBody.toString()); + Collections.singletonList(rbParameter), registerBody); methods.add(registerMethod); } @@ -130,11 +126,11 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { // TODO add header GeneralClassTemplate registrator = new GeneralClassTemplate(null, rootRB.getPackageName(), registratorName, - Collections. emptyList(), Arrays.asList(Closeable.class - .getCanonicalName()), constructorParameters, methods); + Collections.emptyList(), Collections.singletonList(Closeable.class + .getCanonicalName()), constructorParameters, methods); - checkState(RuntimeRegistratorFtlTemplates.containsKey(registrator - .getTypeDeclaration().getName()) == false, "Name conflict: " + checkState(!RuntimeRegistratorFtlTemplates.containsKey(registrator + .getTypeDeclaration().getName()), "Name conflict: " + registrator.getTypeDeclaration().getName()); Map result = new HashMap<>(); result.putAll(RuntimeRegistratorFtlTemplates); @@ -142,8 +138,8 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { return result; } - private static Field hierachchicalRegistration = new Field( - Lists.newArrayList("final"), + private static Field hierachicalRegistration = new Field( + Collections.singletonList(Modifier.FINAL), HierarchicalRuntimeBeanRegistration.class.getCanonicalName(), "registration"); @@ -175,7 +171,7 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { Set currentOccupiedKeys = new HashSet<>(occupiedKeys); currentOccupiedKeys.add(parent.getJavaNamePrefix()); - Field registratorsMapField = new Field(Arrays.asList("final"), + Field registratorsMapField = new Field(Collections.singletonList(Modifier.FINAL), TypeHelper.getGenericType(Map.class, String.class, AtomicInteger.class), "unkeyedMap", "new " + TypeHelper.getGenericType(HashMap.class, @@ -200,20 +196,21 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { unorderedResult.put(entry.getKey(), entry.getValue()); } - if (childRegistratorMap.size() > 0) { + if (!childRegistratorMap.isEmpty()) { // first entry is the direct descendant according to the create // contract RuntimeRegistratorFtlTemplate childRegistrator = childRegistratorMap .values().iterator().next(); - StringBuffer body = new StringBuffer(); + StringBuilder body = new StringBuilder(); String key, value; key = child.getJavaNamePrefix(); body.append(format( "String key = \"%s\"; //TODO: check for conflicts\n", key)); - if (child.getKeyJavaName().isPresent()) { - value = "bean.get" + child.getKeyJavaName().get() + "()"; + Optional 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" @@ -225,18 +222,18 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { body.append(format("String value = %s;\n", value)); body.append(format("%s r = %s.register(key, value, bean);\n", HierarchicalRuntimeBeanRegistration.class - .getCanonicalName(), hierachchicalRegistration + .getCanonicalName(), hierachicalRegistration .getName())); 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( - Arrays.asList("synchronized"), + Collections.singletonList(Modifier.SYNCHRONIZED), childRegistrator.getFullyQualifiedName(), "register", - Arrays.asList(param), Collections. emptyList(), - Collections. emptyList(), body.toString()); + Collections.singletonList(param), Collections.emptyList(), + Collections.emptyList(), body.toString()); methods.add(register); } @@ -245,14 +242,13 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { // create parent registration String createdName = getJavaNameOfRuntimeRegistration(parent.getJavaNamePrefix()); - List constructorParameters = Arrays - .asList(hierachchicalRegistration); + List constructorParameters = Collections.singletonList(hierachicalRegistration); String constructorBody = constructConstructorBody(constructorParameters); MethodDefinition constructor = MethodDefinition.createConstructor( createdName, constructorParameters, constructorBody); - MethodDefinition closeRegistrator = createCloseMethodToCloseField(hierachchicalRegistration); + MethodDefinition closeRegistrator = createCloseMethodToCloseField(hierachicalRegistration); methods.add(closeRegistrator); methods.add(constructor); List privateFields = Lists.newArrayList(registratorsMapField); @@ -263,8 +259,8 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { LinkedHashMap result = new LinkedHashMap<>(); result.put(created.getTypeDeclaration().getName(), created); - checkState(unorderedResult.containsKey(created.getTypeDeclaration() - .getName()) == false, "Naming conflict: " + checkState(!unorderedResult.containsKey(created.getTypeDeclaration() + .getName()), "Naming conflict: " + created.getTypeDeclaration().getName()); result.putAll(unorderedResult); return result; @@ -278,10 +274,10 @@ public class RuntimeRegistratorFtlTemplate extends GeneralClassTemplate { // Arrays.asList(IOException.class.getCanonicalName()), // Collections. emptyList(), body); List annotations = Lists.newArrayList(new Annotation( - "Override", Collections. emptyList())); - return new MethodDefinition(Collections. emptyList(), "void", - "close", Collections. emptyList(), - Collections. emptyList(), annotations, body); + "Override", Collections.emptyList())); + return new MethodDefinition(Collections.emptyList(), "void", + "close", Collections.emptyList(), + Collections.emptyList(), annotations, body); } @VisibleForTesting