X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2Fftl%2FTemplateFactory.java;h=2f63795ae0baac1b0564676c04b023abb042e0d1;hp=00454d8acf14507a518e63b5d71b79020ce89bfd;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hpb=2510f61324167d667aa799101648965ea99325ee 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 00454d8acf..2f63795ae0 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,8 +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.sal.binding.model.api.ParameterizedType; -import org.opendaylight.yangtools.sal.binding.model.api.Type; +import org.opendaylight.mdsal.binding.model.api.JavaTypeName; +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 { @@ -60,42 +60,42 @@ public class TemplateFactory { */ public static Map getTOAndMXInterfaceFtlFiles( final RuntimeBeanEntry entry) { - Map result = new HashMap<>(); + 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); @@ -111,12 +111,12 @@ public class TemplateFactory { // FIXME: put into Type.toString 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()); @@ -178,9 +178,8 @@ public class TemplateFactory { public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe( final ModuleMXBeanEntry mbe) { - AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor(); - attrProcessor.processAttributes(mbe.getAttributes(), - mbe.getPackageName()); + final AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor(); + attrProcessor.processAttributes(mbe.getAttributes()); @@ -192,22 +191,22 @@ public class TemplateFactory { public static AbstractModuleTemplate abstractModuleTemplateFromMbe( final ModuleMXBeanEntry mbe) { - AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes()); + 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) { + && !mbe.getRuntimeBeans().isEmpty()) { generateRuntime = true; - RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate + final RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate .findRoot(mbe.getRuntimeBeans()); registratorFullyQualifiedName = rootEntry .getPackageName() @@ -217,9 +216,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, @@ -243,9 +242,9 @@ public class TemplateFactory { public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe( final ModuleMXBeanEntry mbe) { - MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor(); + 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()); @@ -255,15 +254,15 @@ public class TemplateFactory { public static Map tOsFromMbe( final ModuleMXBeanEntry mbe) { - Map retVal = Maps.newHashMap(); - TOAttributesProcessor processor = new TOAttributesProcessor(); + 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 @@ -276,13 +275,13 @@ public class TemplateFactory { public static Map tOsFromRbe( final RuntimeBeanEntry rbe) { - Map retVal = Maps.newHashMap(); - TOAttributesProcessor processor = new TOAttributesProcessor(); - Map yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap()); + 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; @@ -294,15 +293,15 @@ public class TemplateFactory { 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 @@ -327,13 +326,13 @@ public class TemplateFactory { private final List tos = Lists.newArrayList(); void processAttributes(final Map attributes) { - for (Entry attrEntry : attributes.entrySet()) { - AttributeIfc attributeIfc = attrEntry.getValue(); + 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); @@ -344,15 +343,15 @@ public class TemplateFactory { 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 { @@ -375,18 +374,18 @@ public class TemplateFactory { private final static String dependencyResolverInjectMethodName = "injectDependencyResolver"; private void processAttrs(final Map attrs, final String packageName) { - fields = Lists.newArrayList(); - methods = Lists.newArrayList(); + 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 = BindingMapping.getPropertyName(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) { @@ -395,9 +394,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; } @@ -407,39 +406,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,12 +485,12 @@ public class TemplateFactory { private final List methods = Lists.newArrayList(); void processAttributes(final Map attributes) { - for (Entry attrEntry : attributes.entrySet()) { + 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()) { @@ -467,20 +503,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 = BindingMapping.getPropertyName(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() @@ -490,26 +526,17 @@ public class TemplateFactory { } List getMethods() { - return methods; + return this.methods; } } private static final Type identityRefType = new Type() { public final Class IDENTITY_ATTRIBUTE_REF_CLASS = IdentityAttributeRef.class; + private final JavaTypeName identifier = JavaTypeName.create(IDENTITY_ATTRIBUTE_REF_CLASS); @Override - public String getPackageName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getPackage().getName(); - } - - @Override - public String getName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getSimpleName(); - } - - @Override - public String getFullyQualifiedName() { - return IDENTITY_ATTRIBUTE_REF_CLASS.getName(); + public JavaTypeName getIdentifier() { + return identifier; } }; @@ -517,29 +544,24 @@ public class TemplateFactory { private final List fields = Lists.newArrayList(); - void processAttributes(final Map attributes, - final 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; } } @@ -562,16 +584,16 @@ public class TemplateFactory { } private static Holder processAttributes(final Map attributes) { - List moduleFields = new ArrayList<>(); - List methods = new ArrayList<>(); - for (Entry attrEntry : attributes.entrySet()) { + 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(); @@ -589,9 +611,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) { @@ -605,26 +627,26 @@ public class TemplateFactory { } } - String varName = BindingMapping.getPropertyName(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); @@ -641,16 +663,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) { @@ -660,11 +682,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); @@ -676,11 +698,11 @@ public class TemplateFactory { } List getModuleFields() { - return holder.moduleFields; + return this.holder.moduleFields; } List getMethods() { - return holder.methods; + return this.holder.methods; } } @@ -691,7 +713,7 @@ public class TemplateFactory { return true; } if(value instanceof ListAttribute) { - AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute(); + final AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute(); return needsDepResolver(innerAttribute); } @@ -700,7 +722,7 @@ public class TemplateFactory { 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]); }