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%2FTemplateFactory.java;h=22201699119d85e937a7e801fea0ee9fc92638f5;hb=d3d5d329914eddb066680f7e22ce5dd7c09616e7;hp=19e875f9b1805f851322e82ebb565752c9d710cc;hpb=742edf369379d28ef00822774f071d253a7b8a9f;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java index 19e875f9b1..2220169911 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java @@ -11,7 +11,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -48,9 +47,9 @@ import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Meth import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition; import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField; import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper; -import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil; -import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType; -import org.opendaylight.yangtools.sal.binding.model.api.Type; +import org.opendaylight.mdsal.binding.model.api.ParameterizedType; +import org.opendaylight.mdsal.binding.model.api.Type; +import org.opendaylight.yangtools.yang.binding.BindingMapping; public class TemplateFactory { @@ -59,43 +58,43 @@ public class TemplateFactory { * bean as value that should be persisted from this instance. */ public static Map getTOAndMXInterfaceFtlFiles( - RuntimeBeanEntry entry) { - Map result = new HashMap<>(); + final RuntimeBeanEntry entry) { + final Map result = new HashMap<>(); { // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will // be transformed to getter methods - String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean(); - List extendedInterfaces = Arrays.asList(RuntimeBean.class + final String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean(); + final List extendedInterfaces = Collections.singletonList(RuntimeBean.class .getCanonicalName()); - List methods = new ArrayList<>(); + final List methods = new ArrayList<>(); // convert attributes to getters - for (AttributeIfc attributeIfc : entry.getAttributes()) { + for (final AttributeIfc attributeIfc : entry.getAttributes()) { String returnType; returnType = getReturnType(attributeIfc); - String getterName = "get" + final String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); - MethodDeclaration getter = new MethodDeclaration(returnType, + final MethodDeclaration getter = new MethodDeclaration(returnType, getterName, Collections. emptyList()); methods.add(getter); } // add rpc methods - for (Rpc rpc : entry.getRpcs()) { + for (final Rpc rpc : entry.getRpcs()) { // convert JavaAttribute parameters into fields - List fields = new ArrayList<>(); - for (JavaAttribute ja : rpc.getParameters()) { - Field field = new Field(Collections. emptyList(), + final List fields = new ArrayList<>(); + for (final JavaAttribute ja : rpc.getParameters()) { + final Field field = new Field(Collections.emptyList(), ja.getType().getFullyQualifiedName(), ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode()); fields.add(field); } - MethodDeclaration operation = new MethodDeclaration( + final MethodDeclaration operation = new MethodDeclaration( getReturnType(rpc.getReturnType()), rpc.getName(), fields); methods.add(operation); } // FIXME header - GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate( + final GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate( null, entry.getPackageName(), mxBeanTypeName, extendedInterfaces, methods); @@ -109,14 +108,14 @@ public class TemplateFactory { } // FIXME: put into Type.toString - static String serializeType(Type type, boolean addWildcards) { + static String serializeType(final Type type, final boolean addWildcards) { if (type instanceof ParameterizedType){ - ParameterizedType parameterizedType = (ParameterizedType) type; - StringBuilder sb = new StringBuilder(); + final ParameterizedType parameterizedType = (ParameterizedType) type; + final StringBuilder sb = new StringBuilder(); sb.append(parameterizedType.getRawType().getFullyQualifiedName()); sb.append(addWildcards ? " extendedInterfaces = Lists + final List extendedInterfaces = Lists .newArrayList(AbstractServiceInterface.class.getCanonicalName()); if (sie.getBase().isPresent()) { extendedInterfaces.add(sie.getBase().get().getFullyQualifiedName()); } // FIXME header - GeneralInterfaceTemplate sieTemplate = new GeneralInterfaceTemplate( + final GeneralInterfaceTemplate sieTemplate = new GeneralInterfaceTemplate( getHeaderFromEntry(sie), sie.getPackageName(), sie.getTypeName(), extendedInterfaces, Lists. newArrayList()); @@ -177,10 +176,9 @@ public class TemplateFactory { } public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe( - ModuleMXBeanEntry mbe) { - AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor(); - attrProcessor.processAttributes(mbe.getAttributes(), - mbe.getPackageName()); + final ModuleMXBeanEntry mbe) { + final AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor(); + attrProcessor.processAttributes(mbe.getAttributes()); @@ -191,23 +189,23 @@ public class TemplateFactory { } public static AbstractModuleTemplate abstractModuleTemplateFromMbe( - ModuleMXBeanEntry mbe) { - AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes()); + final ModuleMXBeanEntry mbe) { + final AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes()); - List moduleFields = attrProcessor.getModuleFields(); - List implementedIfcs = Lists.newArrayList( + final List moduleFields = attrProcessor.getModuleFields(); + final List implementedIfcs = Lists.newArrayList( mbe.getFullyQualifiedName(mbe.getMXBeanInterfaceName())); - for (String implementedService : mbe.getProvidedServices().keySet()) { + for (final String implementedService : mbe.getProvidedServices().keySet()) { implementedIfcs.add(implementedService); } boolean generateRuntime = false; String registratorFullyQualifiedName = null; - if (mbe.getRuntimeBeans() != null - && mbe.getRuntimeBeans().isEmpty() == false) { + if ((mbe.getRuntimeBeans() != null) + && !mbe.getRuntimeBeans().isEmpty()) { generateRuntime = true; - RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate + final RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate .findRoot(mbe.getRuntimeBeans()); registratorFullyQualifiedName = rootEntry .getPackageName() @@ -217,9 +215,9 @@ public class TemplateFactory { .getCanonicalName()); } - List extendedClasses = Collections.singletonList(AbstractModule.class.getCanonicalName() + "<" + mbe.getAbstractModuleName() + ">"); + final List extendedClasses = Collections.singletonList(AbstractModule.class.getCanonicalName() + "<" + mbe.getAbstractModuleName() + ">"); - AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate( + final AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate( getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getAbstractModuleName(), extendedClasses, implementedIfcs, moduleFields, attrProcessor.getMethods(), generateRuntime, @@ -234,7 +232,7 @@ public class TemplateFactory { } public static StubFactoryTemplate stubFactoryTemplateFromMbe( - ModuleMXBeanEntry mbe) { + final ModuleMXBeanEntry mbe) { return new StubFactoryTemplate(getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getStubFactoryName(), mbe.getFullyQualifiedName(mbe.getAbstractFactoryName()) @@ -242,10 +240,10 @@ public class TemplateFactory { } public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe( - ModuleMXBeanEntry mbe) { - MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor(); + final ModuleMXBeanEntry mbe) { + final MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor(); attrProcessor.processAttributes(mbe.getAttributes()); - GeneralInterfaceTemplate ifcTemplate = new GeneralInterfaceTemplate( + final GeneralInterfaceTemplate ifcTemplate = new GeneralInterfaceTemplate( getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getMXBeanInterfaceName(), Lists. newArrayList(), attrProcessor.getMethods()); @@ -254,16 +252,16 @@ public class TemplateFactory { } public static Map tOsFromMbe( - ModuleMXBeanEntry mbe) { - Map retVal = Maps.newHashMap(); - TOAttributesProcessor processor = new TOAttributesProcessor(); + final ModuleMXBeanEntry mbe) { + final Map retVal = Maps.newHashMap(); + final TOAttributesProcessor processor = new TOAttributesProcessor(); processor.processAttributes(mbe.getAttributes()); - for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor + for (final org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { - List constructors = Lists.newArrayList(); + final List constructors = Lists.newArrayList(); constructors.add(new Constructor(to.getName(), "super();")); - Header header = getHeaderFromEntry(mbe); + final Header header = getHeaderFromEntry(mbe); retVal.put( to.getType(), new GeneralClassTemplate(header, mbe.getPackageName(), to @@ -275,14 +273,14 @@ public class TemplateFactory { } public static Map tOsFromRbe( - RuntimeBeanEntry rbe) { - Map retVal = Maps.newHashMap(); - TOAttributesProcessor processor = new TOAttributesProcessor(); - Map yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap()); + final RuntimeBeanEntry rbe) { + final Map retVal = Maps.newHashMap(); + final TOAttributesProcessor processor = new TOAttributesProcessor(); + final Map yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap()); // Add TOs from output parameters - for (Rpc rpc : rbe.getRpcs()) { - AttributeIfc returnType = rpc.getReturnType(); + for (final Rpc rpc : rbe.getRpcs()) { + final AttributeIfc returnType = rpc.getReturnType(); if (returnType == VoidAttribute.getInstance()) { continue; @@ -290,19 +288,19 @@ public class TemplateFactory { if (returnType instanceof JavaAttribute) { continue; } - if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) { + if ((returnType instanceof ListAttribute) && (returnType.getOpenType() instanceof SimpleType)) { continue; } - Preconditions.checkState(yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()) == false, + Preconditions.checkState(!yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()), "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe); yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType); } processor.processAttributes(yangPropertiesToTypesMap); - for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor + for (final org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { - List constructors = Lists.newArrayList(); + final List constructors = Lists.newArrayList(); constructors.add(new Constructor(to.getName(), "super();")); // TODO header @@ -316,7 +314,7 @@ public class TemplateFactory { return retVal; } - private static Header getHeaderFromEntry(AbstractEntry mbe) { + private static Header getHeaderFromEntry(final AbstractEntry mbe) { return new Header(mbe.getYangModuleName(), mbe.getYangModuleLocalname()); } @@ -326,14 +324,14 @@ public class TemplateFactory { private final List tos = Lists.newArrayList(); - void processAttributes(Map attributes) { - for (Entry attrEntry : attributes.entrySet()) { - AttributeIfc attributeIfc = attrEntry.getValue(); + void processAttributes(final Map attributes) { + for (final Entry attrEntry : attributes.entrySet()) { + final AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TOAttribute) { createTOInternal((TOAttribute) attributeIfc); } if (attributeIfc instanceof ListAttribute) { - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) + final AttributeIfc innerAttr = ((ListAttribute) attributeIfc) .getInnerAttribute(); if (innerAttr instanceof TOAttribute) { createTOInternal((TOAttribute) innerAttr); @@ -342,17 +340,17 @@ public class TemplateFactory { } } - private void createTOInternal(TOAttribute toAttribute) { + private void createTOInternal(final TOAttribute toAttribute) { - Map attrs = toAttribute.getCapitalizedPropertiesToTypesMap(); + final Map attrs = toAttribute.getCapitalizedPropertiesToTypesMap(); // recursive processing of TO's attributes processAttributes(attrs); - tos.add(new TOInternal(toAttribute.getType(), attrs)); + this.tos.add(new TOInternal(toAttribute.getType(), attrs)); } List getTOs() { - return tos; + return this.tos; } private static class TOInternal { @@ -360,12 +358,12 @@ public class TemplateFactory { private List fields; private List methods; - public TOInternal(Type type, Map attrs) { + public TOInternal(final Type type, final Map attrs) { this(type.getFullyQualifiedName(), type.getName(), attrs, type.getPackageName()); } - public TOInternal(String fullyQualifiedName, String name, - Map attrs, String packageName) { + public TOInternal(final String fullyQualifiedName, final String name, + final Map attrs, final String packageName) { this.fullyQualifiedName = fullyQualifiedName; this.name = name; processAttrs(attrs, packageName); @@ -374,20 +372,19 @@ public class TemplateFactory { private final static String dependencyResolverVarName = "dependencyResolver"; private final static String dependencyResolverInjectMethodName = "injectDependencyResolver"; - private void processAttrs(Map attrs, String packageName) { - fields = Lists.newArrayList(); - methods = Lists.newArrayList(); + private void processAttrs(final Map attrs, final String packageName) { + this.fields = Lists.newArrayList(); + this.methods = Lists.newArrayList(); // FIXME conflict if "dependencyResolver" field from yang - Field depRes = new Field(DependencyResolver.class.getName(), dependencyResolverVarName); - fields.add(depRes); - methods.add(new MethodDefinition("void", dependencyResolverInjectMethodName, Lists.newArrayList(depRes), + final Field depRes = new Field(DependencyResolver.class.getName(), dependencyResolverVarName); + this.fields.add(depRes); + this.methods.add(new MethodDefinition("void", dependencyResolverInjectMethodName, Lists.newArrayList(depRes), "this." + dependencyResolverVarName + " = " + dependencyResolverVarName + ";")); - for (Entry attrEntry : attrs.entrySet()) { - String innerName = attrEntry.getKey(); - String varName = BindingGeneratorUtil - .parseToValidParamName(attrEntry.getKey()); + for (final Entry attrEntry : attrs.entrySet()) { + final String innerName = attrEntry.getKey(); + final String varName = BindingMapping.getPropertyName(attrEntry.getKey()); String fullyQualifiedName, nullableDefault = null; if (attrEntry.getValue() instanceof TypedAttribute) { @@ -396,9 +393,9 @@ public class TemplateFactory { nullableDefault = ((JavaAttribute)attrEntry.getValue()).getNullableDefaultWrappedForCode(); if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) { - String fieldType = serializeType(type, true); - String innerType = getInnerTypeFromIdentity(type); - methods.add(new MethodDefinition(fieldType, "resolve" + attrEntry.getKey(), Collections.emptyList(), + final String fieldType = serializeType(type, true); + final String innerType = getInnerTypeFromIdentity(type); + this.methods.add(new MethodDefinition(fieldType, "resolve" + attrEntry.getKey(), Collections.emptyList(), "return " + varName + ".resolveIdentity(" + dependencyResolverVarName + "," + innerType + ".class);")); type = identityRefType; } @@ -408,39 +405,76 @@ public class TemplateFactory { fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase()); } - fields.add(new Field(fullyQualifiedName, varName, nullableDefault, needsDepResolver(attrEntry.getValue()))); + this.fields.add(new Field(fullyQualifiedName, varName, nullableDefault, needsDepResolver(attrEntry.getValue()))); - String getterName = "get" + innerName; - MethodDefinition getter = new MethodDefinition( + final String getterName = "get" + innerName; + final MethodDefinition getter = new MethodDefinition( fullyQualifiedName, getterName, Collections. emptyList(), "return " + varName + ";"); - String setterName = "set" + innerName; - MethodDefinition setter = new MethodDefinition("void", + final String setterName = "set" + innerName; + final MethodDefinition setter = new MethodDefinition("void", setterName, Lists.newArrayList(new Field( fullyQualifiedName, varName)), "this." + varName + " = " + varName + ";"); - methods.add(getter); - methods.add(setter); + this.methods.add(getter); + this.methods.add(setter); } + // Add hashCode + final MethodDefinition hashCode = getHash(attrs); + this.methods.add(hashCode); + + // Add equals + final MethodDefinition equals = getEquals(attrs); + this.methods.add(equals); + } + + private MethodDefinition getEquals(final Map attrs) { + final StringBuilder equalsBodyBuilder = new StringBuilder( + " if (this == o) { return true; }\n" + + " if (o == null || getClass() != o.getClass()) { return false; }\n"); + equalsBodyBuilder.append(String.format( + " final %s that = (%s) o;\n", this.name, this.name)); + for (final AttributeIfc s : attrs.values()) { + equalsBodyBuilder.append(String.format( + " if (!java.util.Objects.equals(%1$s, that.%1$s)) {\n" + + " return false;\n" + + " }\n\n", s.getLowerCaseCammelCase())); + } + equalsBodyBuilder.append( + " return true;\n"); + return new MethodDefinition("boolean", "equals", Collections.singletonList(new Field("Object", "o")), + Collections.singletonList(new Annotation("Override", Collections.emptyList())), equalsBodyBuilder.toString()); + } + + private static MethodDefinition getHash(final Map attrs) { + final StringBuilder hashBodyBuilder = new StringBuilder( + " return java.util.Objects.hash("); + for (final AttributeIfc s : attrs.values()) { + hashBodyBuilder.append(s.getLowerCaseCammelCase()); + hashBodyBuilder.append(", "); + } + hashBodyBuilder.replace(hashBodyBuilder.length() - 2, hashBodyBuilder.length(), ");\n"); + return new MethodDefinition("int", "hashCode", Collections.emptyList(), + Collections.singletonList(new Annotation("Override", Collections.emptyList())), hashBodyBuilder.toString()); } String getType() { - return fullyQualifiedName; + return this.fullyQualifiedName; } String getName() { - return name; + return this.name; } List getFields() { - return fields; + return this.fields; } List getMethods() { - return methods; + return this.methods; } } } @@ -449,16 +483,16 @@ public class TemplateFactory { private static class MXBeanInterfaceAttributesProcessor { private final List methods = Lists.newArrayList(); - void processAttributes(Map attributes) { - for (Entry attrEntry : attributes.entrySet()) { + void processAttributes(final Map attributes) { + for (final Entry attrEntry : attributes.entrySet()) { String returnType; - AttributeIfc attributeIfc = attrEntry.getValue(); + final AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + final TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; returnType = serializeType(typedAttribute.getType()); - if (attributeIfc instanceof JavaAttribute && ((JavaAttribute)attrEntry.getValue()).isIdentityRef()) { + if ((attributeIfc instanceof JavaAttribute) && ((JavaAttribute)attrEntry.getValue()).isIdentityRef()) { returnType = serializeType(identityRefType); } @@ -468,21 +502,20 @@ public class TemplateFactory { + attributeIfc.getClass()); } - String getterName = "get" + final String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); - MethodDeclaration getter = new MethodDeclaration(returnType, + final MethodDeclaration getter = new MethodDeclaration(returnType, getterName, Collections. emptyList()); - String varName = BindingGeneratorUtil - .parseToValidParamName(attrEntry.getKey()); - String setterName = "set" + final String varName = BindingMapping.getPropertyName(attrEntry.getKey()); + final String setterName = "set" + attributeIfc.getUpperCaseCammelCase(); - MethodDeclaration setter = new MethodDeclaration("void", + final MethodDeclaration setter = new MethodDeclaration("void", setterName, Lists.newArrayList(new Field(returnType, varName))); - methods.add(getter); - methods.add(setter); + this.methods.add(getter); + this.methods.add(setter); if (attributeIfc.getNullableDescription() != null) { setter.setJavadoc(attrEntry.getValue() @@ -492,7 +525,7 @@ public class TemplateFactory { } List getMethods() { - return methods; + return this.methods; } } @@ -501,17 +534,17 @@ public class TemplateFactory { @Override public String getPackageName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getPackage().getName(); + return this.IDENTITY_ATTRIBUTE_REF_CLASS.getPackage().getName(); } @Override public String getName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getSimpleName(); + return this.IDENTITY_ATTRIBUTE_REF_CLASS.getSimpleName(); } @Override public String getFullyQualifiedName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getName(); + return this.IDENTITY_ATTRIBUTE_REF_CLASS.getName(); } }; @@ -519,29 +552,24 @@ public class TemplateFactory { private final List fields = Lists.newArrayList(); - void processAttributes(Map attributes, - String packageName) { - for (Entry attrEntry : attributes.entrySet()) { - String type; - String nullableDefaultWrapped = null; - AttributeIfc attributeIfc = attrEntry.getValue(); - + void processAttributes(final Map attributes) { + for (final AttributeIfc attributeIfc : attributes.values()) { if (attributeIfc instanceof TypedAttribute) { - TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; - type = serializeType(typedAttribute.getType()); + final TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + final String type = serializeType(typedAttribute.getType()); + + this.fields.add(new Field(type, attributeIfc + .getUpperCaseCammelCase(), null)); } else { throw new UnsupportedOperationException( "Attribute not supported: " + attributeIfc.getClass()); } - - fields.add(new Field(type, attributeIfc - .getUpperCaseCammelCase(), nullableDefaultWrapped)); } } List getFields() { - return fields; + return this.fields; } } @@ -550,7 +578,7 @@ public class TemplateFactory { private final List moduleFields; private final List methods; - private Holder(List moduleFields, List methods) { + private Holder(final List moduleFields, final List methods) { this.moduleFields = Collections.unmodifiableList(moduleFields); this.methods = Collections.unmodifiableList(methods); } @@ -559,21 +587,21 @@ public class TemplateFactory { private final Holder holder; - private AbstractModuleAttributesProcessor(Map attributes) { + private AbstractModuleAttributesProcessor(final Map attributes) { this.holder = processAttributes(attributes); } - private static Holder processAttributes(Map attributes) { - List moduleFields = new ArrayList<>(); - List methods = new ArrayList<>(); - for (Entry attrEntry : attributes.entrySet()) { + private static Holder processAttributes(final Map attributes) { + final List moduleFields = new ArrayList<>(); + final List methods = new ArrayList<>(); + for (final Entry attrEntry : attributes.entrySet()) { String type, nullableDefaultWrapped = null; - AttributeIfc attributeIfc = attrEntry.getValue(); + final AttributeIfc attributeIfc = attrEntry.getValue(); boolean isIdentity = false; - boolean needsDepResolver = needsDepResolver(attrEntry.getValue()); + final boolean needsDepResolver = needsDepResolver(attrEntry.getValue()); if (attributeIfc instanceof TypedAttribute) { - TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + final TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; type = serializeType(typedAttribute.getType()); if (attributeIfc instanceof JavaAttribute) { nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode(); @@ -591,9 +619,9 @@ public class TemplateFactory { boolean isDependency = false; boolean isListOfDependencies = false; Dependency dependency = null; - Annotation overrideAnnotation = new Annotation("Override", + final Annotation overrideAnnotation = new Annotation("Override", Collections. emptyList()); - List annotations = Lists + final List annotations = Lists .newArrayList(overrideAnnotation); if (attributeIfc instanceof AbstractDependencyAttribute) { @@ -607,27 +635,26 @@ public class TemplateFactory { } } - String varName = BindingGeneratorUtil - .parseToValidParamName(attrEntry.getKey()); + final String varName = BindingMapping.getPropertyName(attrEntry.getKey()); ModuleField field; if (isIdentity) { - String identityBaseClass = getInnerTypeFromIdentity(((TypedAttribute) attributeIfc).getType()); - IdentityRefModuleField identityField = new IdentityRefModuleField(type, varName, + final String identityBaseClass = getInnerTypeFromIdentity(((TypedAttribute) attributeIfc).getType()); + final IdentityRefModuleField identityField = new IdentityRefModuleField(type, varName, attributeIfc.getUpperCaseCammelCase(), identityBaseClass); - String getterName = "get" + final String getterName = "get" + attributeIfc.getUpperCaseCammelCase() + "Identity"; - MethodDefinition additionalGetter = new MethodDefinition(type, getterName, Collections. emptyList(), + final MethodDefinition additionalGetter = new MethodDefinition(type, getterName, Collections. emptyList(), Collections. emptyList(), "return " + identityField.getIdentityClassName() + ";"); methods.add(additionalGetter); - String setterName = "set" + final String setterName = "set" + attributeIfc.getUpperCaseCammelCase(); - String setterBody = "this." + identityField.getIdentityClassName() + " = " + identityField.getIdentityClassName() + ";"; - MethodDefinition additionalSetter = new MethodDefinition("void", + final String setterBody = "this." + identityField.getIdentityClassName() + " = " + identityField.getIdentityClassName() + ";"; + final MethodDefinition additionalSetter = new MethodDefinition("void", setterName, Lists.newArrayList(new Field(type, identityField.getIdentityClassName())), Collections. emptyList(), setterBody); @@ -644,16 +671,16 @@ public class TemplateFactory { moduleFields.add(field); - String getterName = "get" + final String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); - MethodDefinition getter = new MethodDefinition(type, + final MethodDefinition getter = new MethodDefinition(type, getterName, Collections. emptyList(), Lists.newArrayList(overrideAnnotation), "return " + varName + ";"); methods.add(getter); - String setterName = "set" + final String setterName = "set" + attributeIfc.getUpperCaseCammelCase(); if (attributeIfc.getNullableDescription() != null) { @@ -663,11 +690,11 @@ public class TemplateFactory { String setterBody = "this." + varName + " = " + varName + ";"; if (isListOfDependencies) { - String nullCheck = String.format("if (%s == null) throw new IllegalArgumentException(\"Null not supported\");%n", - varName); + final String nullCheck = String.format("if (%s == null) {\n%s = new java.util.ArrayList<>(); \n}%n", + varName, varName); setterBody = nullCheck + setterBody; } - MethodDefinition setter = new MethodDefinition("void", + final MethodDefinition setter = new MethodDefinition("void", setterName, Lists.newArrayList(new Field(type, varName)), annotations, setterBody); @@ -679,31 +706,31 @@ public class TemplateFactory { } List getModuleFields() { - return holder.moduleFields; + return this.holder.moduleFields; } List getMethods() { - return holder.methods; + return this.holder.methods; } } - private static boolean needsDepResolver(AttributeIfc value) { + private static boolean needsDepResolver(final AttributeIfc value) { if(value instanceof TOAttribute) { return true; } if(value instanceof ListAttribute) { - AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute(); + final AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute(); return needsDepResolver(innerAttribute); } return false; } - private static String getInnerTypeFromIdentity(Type type) { + private static String getInnerTypeFromIdentity(final Type type) { Preconditions.checkArgument(type instanceof ParameterizedType); - Type[] args = ((ParameterizedType) type).getActualTypeArguments(); + final Type[] args = ((ParameterizedType) type).getActualTypeArguments(); Preconditions.checkArgument(args.length ==1); return serializeType(args[0]); }