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=53ab4ef335973029568f0820968c830d59112f17;hp=a10f62be79027dbe01e9a6e4992eb76309981939;hb=070893bfd87c7c45ecb4d19ed7848cc9698f37e8;hpb=545b424ba9278d7aee12b9a6173e23c1b1d39dd3 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 a10f62be79..53ab4ef335 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 @@ -7,11 +7,19 @@ */ package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl; -import com.google.common.base.Function; import com.google.common.base.Preconditions; -import com.google.common.collect.Collections2; 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; +import java.util.Map; +import java.util.Map.Entry; +import javax.management.openmbean.SimpleType; +import org.opendaylight.controller.config.api.DependencyResolver; +import org.opendaylight.controller.config.api.IdentityAttributeRef; import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule; import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface; import org.opendaylight.controller.config.api.runtime.RuntimeBean; @@ -21,63 +29,31 @@ import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc; import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry; -import org.opendaylight.controller.config.yangjmxgenerator.attribute.*; -import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute.Dependency; -import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.*; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute; +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.Constructor; +import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field; +import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Header; +import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField; +import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration; +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 javax.management.openmbean.SimpleType; -import java.util.*; -import java.util.Map.Entry; - public class TemplateFactory { - public static Map getFtlTemplates( - ModuleMXBeanEntry entry) { - Map result = new HashMap<>(); - - result.putAll(TemplateFactory.tOsFromMbe(entry)); - - // IFC - result.put(entry.getMXBeanInterfaceName() + ".java", - TemplateFactory.mXBeanInterfaceTemplateFromMbe(entry)); - - // ABS fact - result.put(entry.getAbstractFactoryName() + ".java", - TemplateFactory.abstractFactoryTemplateFromMbe(entry)); - - // ABS module - result.put(entry.getAbstractModuleName() + ".java", - TemplateFactory.abstractModuleTemplateFromMbe(entry)); - - return result; - } - - public static Map getFtlStubTemplates( - ModuleMXBeanEntry entry) { - Map result = new HashMap<>(); - // STUB fact - result.put(entry.getStubFactoryName() + ".java", - TemplateFactory.stubFactoryTemplateFromMbe(entry)); - - result.put(entry.getStubModuleName() + ".java", - TemplateFactory.stubModuleTemplateFromMbe(entry)); - return result; - } - - public static Map getFtlTemplates( - ServiceInterfaceEntry entry) { - - Map result = new HashMap<>(); - result.put(entry.getTypeName() + ".java", - TemplateFactory.serviceInterfaceFromSie(entry)); - - return result; - } - /** * Get map of file name as key, FtlFile instance representing runtime mx * bean as value that should be persisted from this instance. @@ -94,8 +70,8 @@ public class TemplateFactory { // convert attributes to getters for (AttributeIfc attributeIfc : entry.getAttributes()) { - String returnType = null; - returnType = getReturnType(entry, attributeIfc); + String returnType; + returnType = getReturnType(attributeIfc); String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); MethodDeclaration getter = new MethodDeclaration(returnType, @@ -110,11 +86,11 @@ public class TemplateFactory { for (JavaAttribute ja : rpc.getParameters()) { Field field = new Field(Collections. emptyList(), ja.getType().getFullyQualifiedName(), - ja.getLowerCaseCammelCase()); + ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode()); fields.add(field); } MethodDeclaration operation = new MethodDeclaration( - getReturnType(entry, rpc.getReturnType()), rpc.getName(), fields); + getReturnType(rpc.getReturnType()), rpc.getName(), fields); methods.add(operation); } @@ -132,28 +108,38 @@ public class TemplateFactory { return result; } - private static String getReturnType(RuntimeBeanEntry entry, AttributeIfc attributeIfc) { + // FIXME: put into Type.toString + static String serializeType(Type type, boolean addWildcards) { + if (type instanceof ParameterizedType){ + ParameterizedType parameterizedType = (ParameterizedType) type; + StringBuilder sb = new StringBuilder(); + sb.append(parameterizedType.getRawType().getFullyQualifiedName()); + sb.append(addWildcards ? ""); + return sb.toString(); + } else { + return type.getFullyQualifiedName(); + } + } + + static String serializeType(Type type) { + return serializeType(type, false); + } + + private static String getReturnType(AttributeIfc attributeIfc) { String returnType; if (attributeIfc instanceof TypedAttribute) { - returnType = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(entry.getPackageName(), - attributeIfc.getUpperCaseCammelCase()); - - returnType = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - - String innerTpe = innerAttr instanceof TypedAttribute ? ((TypedAttribute) innerAttr) - .getType().getFullyQualifiedName() - : FullyQualifiedNameHelper.getFullyQualifiedName( - entry.getPackageName(), - attributeIfc.getUpperCaseCammelCase()); - - returnType = "java.util.List<" + innerTpe + ">"; + Type type = ((TypedAttribute) attributeIfc).getType(); + returnType = serializeType(type); } else if (attributeIfc == VoidAttribute.getInstance()) { return "void"; } else { @@ -180,12 +166,12 @@ public class TemplateFactory { Lists. newArrayList()); sieTemplate.setJavadoc(sie.getNullableDescription()); - if (sie.getNullableDescription() != null) + if (sie.getNullableDescription() != null) { sieTemplate.getAnnotations().add( Annotation.createDescriptionAnnotation(sie .getNullableDescription())); - sieTemplate.getAnnotations().add(Annotation.createSieAnnotation(sie.getQName(), sie.getExportedOsgiClassName - ())); + } + sieTemplate.getAnnotations().addAll(Annotation.createSieAnnotations(sie)); return sieTemplate; } @@ -196,28 +182,17 @@ public class TemplateFactory { attrProcessor.processAttributes(mbe.getAttributes(), mbe.getPackageName()); - Collection transformed = Collections2.transform(mbe - .getProvidedServices().keySet(), - new Function() { - @Override - public String apply(String input) { - return input + ".class"; - } - }); return new AbstractFactoryTemplate(getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getAbstractFactoryName(), - mbe.getGloballyUniqueName(), mbe.getFullyQualifiedName(mbe - .getStubModuleName()), attrProcessor.getFields(), - Lists.newArrayList(transformed)); + attrProcessor.getFields() + ); } public static AbstractModuleTemplate abstractModuleTemplateFromMbe( ModuleMXBeanEntry mbe) { - AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(); - attrProcessor.processAttributes(mbe.getAttributes(), - mbe.getPackageName()); + AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes()); List moduleFields = attrProcessor.getModuleFields(); List implementedIfcs = Lists.newArrayList( @@ -249,10 +224,11 @@ public class TemplateFactory { attrProcessor.getMethods(), generateRuntime, registratorFullyQualifiedName); - if (mbe.getNullableDescription() != null) + if (mbe.getNullableDescription() != null) { abstractModuleTemplate.getAnnotations().add( Annotation.createDescriptionAnnotation(mbe .getNullableDescription())); + } return abstractModuleTemplate; } @@ -260,22 +236,14 @@ public class TemplateFactory { ModuleMXBeanEntry mbe) { return new StubFactoryTemplate(getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getStubFactoryName(), - mbe.getFullyQualifiedName(mbe.getAbstractFactoryName()), - mbe.getStubModuleName()); - } - - public static StubModuleTemplate stubModuleTemplateFromMbe( - ModuleMXBeanEntry mbe) { - return new StubModuleTemplate(getHeaderFromEntry(mbe), - mbe.getPackageName(), mbe.getStubModuleName(), - mbe.getFullyQualifiedName(mbe.getAbstractModuleName())); + mbe.getFullyQualifiedName(mbe.getAbstractFactoryName()) + ); } public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe( ModuleMXBeanEntry mbe) { MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor(); - attrProcessor.processAttributes(mbe.getAttributes(), - mbe.getPackageName()); + attrProcessor.processAttributes(mbe.getAttributes()); GeneralInterfaceTemplate ifcTemplate = new GeneralInterfaceTemplate( getHeaderFromEntry(mbe), mbe.getPackageName(), mbe.getMXBeanInterfaceName(), Lists. newArrayList(), @@ -288,7 +256,7 @@ public class TemplateFactory { ModuleMXBeanEntry mbe) { Map retVal = Maps.newHashMap(); TOAttributesProcessor processor = new TOAttributesProcessor(); - processor.processAttributes(mbe.getAttributes(), mbe.getPackageName()); + processor.processAttributes(mbe.getAttributes()); for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { List constructors = Lists.newArrayList(); @@ -315,19 +283,22 @@ public class TemplateFactory { for (Rpc rpc : rbe.getRpcs()) { AttributeIfc returnType = rpc.getReturnType(); - if (returnType == VoidAttribute.getInstance()) + if (returnType == VoidAttribute.getInstance()) { continue; - if (returnType instanceof JavaAttribute) + } + 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, "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe); yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType); } - processor.processAttributes(yangPropertiesToTypesMap, rbe.getPackageName()); + processor.processAttributes(yangPropertiesToTypesMap); for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor .getTOs()) { List constructors = Lists.newArrayList(); @@ -354,36 +325,29 @@ public class TemplateFactory { private final List tos = Lists.newArrayList(); - void processAttributes(Map attributes, - String packageName) { + void processAttributes(Map attributes) { for (Entry attrEntry : attributes.entrySet()) { AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TOAttribute) { - createTOInternal(packageName, attributeIfc); + createTOInternal((TOAttribute) attributeIfc); } if (attributeIfc instanceof ListAttribute) { AttributeIfc innerAttr = ((ListAttribute) attributeIfc) .getInnerAttribute(); if (innerAttr instanceof TOAttribute) { - createTOInternal(packageName, innerAttr); + createTOInternal((TOAttribute) innerAttr); } } } } - private void createTOInternal(String packageName, - AttributeIfc attributeIfc) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); + private void createTOInternal(TOAttribute toAttribute) { - String type = fullyQualifiedName; - String name = attributeIfc.getUpperCaseCammelCase(); - Map attrs = ((TOAttribute) attributeIfc) - .getCapitalizedPropertiesToTypesMap(); - // recursive processing - processAttributes(attrs, packageName); + Map attrs = toAttribute.getCapitalizedPropertiesToTypesMap(); + // recursive processing of TO's attributes + processAttributes(attrs); - tos.add(new TOInternal(type, name, attrs, packageName)); + tos.add(new TOInternal(toAttribute.getType(), attrs)); } List getTOs() { @@ -391,48 +355,59 @@ public class TemplateFactory { } private static class TOInternal { - private final String type, name; + private final String fullyQualifiedName, name; private List fields; private List methods; - public TOInternal(String type, String name, + public TOInternal(Type type, Map attrs) { + this(type.getFullyQualifiedName(), type.getName(), attrs, type.getPackageName()); + } + + public TOInternal(String fullyQualifiedName, String name, Map attrs, String packageName) { - super(); - this.type = type; + this.fullyQualifiedName = fullyQualifiedName; this.name = name; processAttrs(attrs, packageName); } - private void processAttrs(Map attrs, - String packageName) { + 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(); + // 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), + "this." + dependencyResolverVarName + " = " + dependencyResolverVarName + ";")); + for (Entry attrEntry : attrs.entrySet()) { String innerName = attrEntry.getKey(); String varName = BindingGeneratorUtil .parseToValidParamName(attrEntry.getKey()); - String fullyQualifiedName = null; + String fullyQualifiedName, nullableDefault = null; if (attrEntry.getValue() instanceof TypedAttribute) { - Type innerType = ((TypedAttribute) attrEntry.getValue()) - .getType(); - fullyQualifiedName = innerType.getFullyQualifiedName(); - } else if (attrEntry.getValue() instanceof ListAttribute) { - AttributeIfc innerAttr = ((ListAttribute) attrEntry - .getValue()).getInnerAttribute(); - - String innerTpe = innerAttr instanceof TypedAttribute ? ((TypedAttribute) innerAttr) - .getType().getFullyQualifiedName() - : FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase()); - - fullyQualifiedName = "java.util.List<" + innerTpe + ">"; - } else + Type type = ((TypedAttribute) attrEntry.getValue()).getType(); + if(attrEntry.getValue() instanceof JavaAttribute) { + 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(), + "return " + varName + ".resolveIdentity(" + dependencyResolverVarName + "," + innerType + ".class);")); + type = identityRefType; + } + } + fullyQualifiedName = serializeType(type); + } else { fullyQualifiedName = FullyQualifiedNameHelper .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase()); - - fields.add(new Field(fullyQualifiedName, varName)); + } + fields.add(new Field(fullyQualifiedName, varName, nullableDefault, needsDepResolver(attrEntry.getValue()))); String getterName = "get" + innerName; MethodDefinition getter = new MethodDefinition( @@ -452,7 +427,7 @@ public class TemplateFactory { } String getType() { - return type; + return fullyQualifiedName; } String getName() { @@ -469,39 +444,23 @@ public class TemplateFactory { } } + private static class MXBeanInterfaceAttributesProcessor { - private static final String STRING_FULLY_QUALIFIED_NAME = "java.util.List"; private final List methods = Lists.newArrayList(); - void processAttributes(Map attributes, - String packageName) { + void processAttributes(Map attributes) { for (Entry attrEntry : attributes.entrySet()) { String returnType; AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - returnType = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); - - returnType = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - String fullyQualifiedName = null; + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + returnType = serializeType(typedAttribute.getType()); - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - if (innerAttr instanceof JavaAttribute) { - fullyQualifiedName = ((JavaAttribute) innerAttr) - .getType().getFullyQualifiedName(); - } else if (innerAttr instanceof TOAttribute) { - fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); + if (attributeIfc instanceof JavaAttribute && ((JavaAttribute)attrEntry.getValue()).isIdentityRef()) { + returnType = serializeType(identityRefType); } - returnType = STRING_FULLY_QUALIFIED_NAME.concat("<") - .concat(fullyQualifiedName).concat(">"); } else { throw new UnsupportedOperationException( "Attribute not supported: " @@ -520,6 +479,7 @@ public class TemplateFactory { MethodDeclaration setter = new MethodDeclaration("void", setterName, Lists.newArrayList(new Field(returnType, varName))); + methods.add(getter); methods.add(setter); @@ -535,40 +495,39 @@ public class TemplateFactory { } } + private static final Type identityRefType = new Type() { + public final Class IDENTITY_ATTRIBUTE_REF_CLASS = IdentityAttributeRef.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(); + } + }; + private static class AbstractFactoryAttributesProcessor { private final List fields = Lists.newArrayList(); - private static final String STRING_FULLY_QUALIFIED_NAME = "java.util.List"; void processAttributes(Map attributes, String packageName) { for (Entry attrEntry : attributes.entrySet()) { String type; + String nullableDefaultWrapped = null; AttributeIfc attributeIfc = attrEntry.getValue(); if (attributeIfc instanceof TypedAttribute) { - type = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); - - type = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - String fullyQualifiedName = null; - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - if (innerAttr instanceof JavaAttribute) { - fullyQualifiedName = ((JavaAttribute) innerAttr) - .getType().getFullyQualifiedName(); - } else if (innerAttr instanceof TOAttribute) { - fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); - } - - type = STRING_FULLY_QUALIFIED_NAME.concat("<") - .concat(fullyQualifiedName).concat(">"); - + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + type = serializeType(typedAttribute.getType()); } else { throw new UnsupportedOperationException( "Attribute not supported: " @@ -576,7 +535,7 @@ public class TemplateFactory { } fields.add(new Field(type, attributeIfc - .getUpperCaseCammelCase())); + .getUpperCaseCammelCase(), nullableDefaultWrapped)); } } @@ -586,40 +545,42 @@ public class TemplateFactory { } private static class AbstractModuleAttributesProcessor { + private static class Holder { + private final List moduleFields; + private final List methods; - private static final String STRING_FULLY_QUALIFIED_NAME = "java.util.List"; + private Holder(List moduleFields, List methods) { + this.moduleFields = Collections.unmodifiableList(moduleFields); + this.methods = Collections.unmodifiableList(methods); + } + } - private final List moduleFields = Lists.newArrayList(); - private final List methods = Lists.newArrayList(); + private final Holder holder; - void processAttributes(Map attributes, - String packageName) { + + private AbstractModuleAttributesProcessor(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()) { - String type; + String type, nullableDefaultWrapped = null; AttributeIfc attributeIfc = attrEntry.getValue(); + boolean isIdentity = false; + boolean needsDepResolver = needsDepResolver(attrEntry.getValue()); if (attributeIfc instanceof TypedAttribute) { - type = ((TypedAttribute) attributeIfc).getType() - .getFullyQualifiedName(); - } else if (attributeIfc instanceof TOAttribute) { - String fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, attributeIfc.getUpperCaseCammelCase()); - - type = fullyQualifiedName; - } else if (attributeIfc instanceof ListAttribute) { - String fullyQualifiedName = null; - AttributeIfc innerAttr = ((ListAttribute) attributeIfc) - .getInnerAttribute(); - if (innerAttr instanceof JavaAttribute) { - fullyQualifiedName = ((JavaAttribute) innerAttr) - .getType().getFullyQualifiedName(); - } else if (innerAttr instanceof TOAttribute) { - fullyQualifiedName = FullyQualifiedNameHelper - .getFullyQualifiedName(packageName, innerAttr.getUpperCaseCammelCase()); + TypedAttribute typedAttribute = (TypedAttribute) attributeIfc; + type = serializeType(typedAttribute.getType()); + if (attributeIfc instanceof JavaAttribute) { + nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode(); + if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) { + isIdentity = true; + type = serializeType(typedAttribute.getType(), true); + } } - - type = STRING_FULLY_QUALIFIED_NAME.concat("<") - .concat(fullyQualifiedName).concat(">"); } else { throw new UnsupportedOperationException( "Attribute not supported: " @@ -627,32 +588,69 @@ public class TemplateFactory { } boolean isDependency = false; + boolean isListOfDependencies = false; Dependency dependency = null; Annotation overrideAnnotation = new Annotation("Override", Collections. emptyList()); List annotations = Lists .newArrayList(overrideAnnotation); - if (attributeIfc instanceof DependencyAttribute) { + if (attributeIfc instanceof AbstractDependencyAttribute) { isDependency = true; - dependency = ((DependencyAttribute) attributeIfc) + dependency = ((AbstractDependencyAttribute) attributeIfc) .getDependency(); annotations.add(Annotation .createRequireIfcAnnotation(dependency.getSie())); + if (attributeIfc instanceof ListDependenciesAttribute) { + isListOfDependencies = true; + } } String varName = BindingGeneratorUtil .parseToValidParamName(attrEntry.getKey()); - moduleFields.add(new ModuleField(type, varName, attributeIfc - .getUpperCaseCammelCase(), attributeIfc - .getNullableDefault(), isDependency, dependency)); + + ModuleField field; + if (isIdentity) { + String identityBaseClass = getInnerTypeFromIdentity(((TypedAttribute) attributeIfc).getType()); + IdentityRefModuleField identityField = new IdentityRefModuleField(type, varName, + attributeIfc.getUpperCaseCammelCase(), identityBaseClass); + + String getterName = "get" + + attributeIfc.getUpperCaseCammelCase() + "Identity"; + MethodDefinition additionalGetter = new MethodDefinition(type, getterName, Collections. emptyList(), + Collections. emptyList(), "return " + identityField.getIdentityClassName() + + ";"); + methods.add(additionalGetter); + + String setterName = "set" + + attributeIfc.getUpperCaseCammelCase(); + + String setterBody = "this." + identityField.getIdentityClassName() + " = " + identityField.getIdentityClassName() + ";"; + MethodDefinition additionalSetter = new MethodDefinition("void", + setterName, + Lists.newArrayList(new Field(type, identityField.getIdentityClassName())), + Collections. emptyList(), setterBody); + additionalSetter.setJavadoc(attributeIfc.getNullableDescription()); + + methods.add(additionalSetter); + + type = serializeType(identityRefType); + field = identityField; + } else { + field = new ModuleField(type, varName, attributeIfc.getUpperCaseCammelCase(), + nullableDefaultWrapped, isDependency, dependency, isListOfDependencies, needsDepResolver); + } + moduleFields.add(field); + String getterName = "get" + attributeIfc.getUpperCaseCammelCase(); MethodDefinition getter = new MethodDefinition(type, getterName, Collections. emptyList(), Lists.newArrayList(overrideAnnotation), "return " - + varName + ";"); + + varName + ";"); + + methods.add(getter); String setterName = "set" + attributeIfc.getUpperCaseCammelCase(); @@ -662,25 +660,50 @@ public class TemplateFactory { .createDescriptionAnnotation(attributeIfc.getNullableDescription())); } + String setterBody = "this." + varName + " = " + varName + ";"; + if (isListOfDependencies) { + String nullCheck = String.format("if (%s == null) throw new IllegalArgumentException(\"Null not supported\");%n", + varName); + setterBody = nullCheck + setterBody; + } MethodDefinition setter = new MethodDefinition("void", setterName, Lists.newArrayList(new Field(type, varName)), - annotations, "this." + varName + " = " + varName + ";"); + annotations, setterBody); setter.setJavadoc(attributeIfc.getNullableDescription()); - methods.add(getter); methods.add(setter); } + return new Holder(moduleFields, methods); } List getModuleFields() { - return moduleFields; + return holder.moduleFields; } List getMethods() { - return methods; + return holder.methods; } } + + private static boolean needsDepResolver(AttributeIfc value) { + if(value instanceof TOAttribute) { + return true; + } + if(value instanceof ListAttribute) { + AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute(); + return needsDepResolver(innerAttribute); + } + + return false; + } + + private static String getInnerTypeFromIdentity(Type type) { + Preconditions.checkArgument(type instanceof ParameterizedType); + Type[] args = ((ParameterizedType) type).getActualTypeArguments(); + Preconditions.checkArgument(args.length ==1); + return serializeType(args[0]); + } }