2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Maps;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import javax.management.openmbean.SimpleType;
20 import org.opendaylight.controller.config.api.DependencyResolver;
21 import org.opendaylight.controller.config.api.IdentityAttributeRef;
22 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
23 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
24 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
25 import org.opendaylight.controller.config.spi.AbstractModule;
26 import org.opendaylight.controller.config.yangjmxgenerator.AbstractEntry;
27 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
28 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
29 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
30 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
31 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute;
32 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
33 import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency;
34 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
35 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
36 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
37 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
38 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
39 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
40 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation;
41 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation.Parameter;
42 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor;
43 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field;
44 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Header;
45 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField;
46 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration;
47 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition;
48 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField;
49 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper;
50 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
51 import org.opendaylight.yangtools.sal.binding.model.api.Type;
52 import org.opendaylight.yangtools.yang.binding.BindingMapping;
54 public class TemplateFactory {
57 * Get map of file name as key, FtlFile instance representing runtime mx
58 * bean as value that should be persisted from this instance.
60 public static Map<String, FtlTemplate> getTOAndMXInterfaceFtlFiles(
61 final RuntimeBeanEntry entry) {
62 Map<String, FtlTemplate> result = new HashMap<>();
63 { // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will
64 // be transformed to getter methods
65 String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean();
66 List<String> extendedInterfaces = Collections.singletonList(RuntimeBean.class
68 List<MethodDeclaration> methods = new ArrayList<>();
70 // convert attributes to getters
71 for (AttributeIfc attributeIfc : entry.getAttributes()) {
73 returnType = getReturnType(attributeIfc);
74 String getterName = "get"
75 + attributeIfc.getUpperCaseCammelCase();
76 MethodDeclaration getter = new MethodDeclaration(returnType,
77 getterName, Collections.<Field> emptyList());
82 for (Rpc rpc : entry.getRpcs()) {
83 // convert JavaAttribute parameters into fields
84 List<Field> fields = new ArrayList<>();
85 for (JavaAttribute ja : rpc.getParameters()) {
86 Field field = new Field(Collections.emptyList(),
87 ja.getType().getFullyQualifiedName(),
88 ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
91 MethodDeclaration operation = new MethodDeclaration(
92 getReturnType(rpc.getReturnType()), rpc.getName(), fields);
93 methods.add(operation);
97 GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate(
98 null, entry.getPackageName(), mxBeanTypeName,
99 extendedInterfaces, methods);
101 result.put(runtimeMxBeanIfc.getTypeDeclaration().getName()
102 + ".java", runtimeMxBeanIfc);
105 result.putAll(TemplateFactory.tOsFromRbe(entry));
110 // FIXME: put into Type.toString
111 static String serializeType(final Type type, final boolean addWildcards) {
112 if (type instanceof ParameterizedType){
113 ParameterizedType parameterizedType = (ParameterizedType) type;
114 StringBuilder sb = new StringBuilder();
115 sb.append(parameterizedType.getRawType().getFullyQualifiedName());
116 sb.append(addWildcards ? "<? extends " : "<");
117 boolean first = true;
118 for(Type parameter: parameterizedType.getActualTypeArguments()) {
124 sb.append(serializeType(parameter));
127 return sb.toString();
129 return type.getFullyQualifiedName();
133 static String serializeType(final Type type) {
134 return serializeType(type, false);
137 private static String getReturnType(final AttributeIfc attributeIfc) {
139 if (attributeIfc instanceof TypedAttribute) {
140 Type type = ((TypedAttribute) attributeIfc).getType();
141 returnType = serializeType(type);
142 } else if (attributeIfc == VoidAttribute.getInstance()) {
145 throw new UnsupportedOperationException(
146 "Attribute not supported: "
147 + attributeIfc.getClass());
152 public static GeneralInterfaceTemplate serviceInterfaceFromSie(
153 final ServiceInterfaceEntry sie) {
155 List<String> extendedInterfaces = Lists
156 .newArrayList(AbstractServiceInterface.class.getCanonicalName());
157 if (sie.getBase().isPresent()) {
158 extendedInterfaces.add(sie.getBase().get().getFullyQualifiedName());
162 GeneralInterfaceTemplate sieTemplate = new GeneralInterfaceTemplate(
163 getHeaderFromEntry(sie), sie.getPackageName(),
164 sie.getTypeName(), extendedInterfaces,
165 Lists.<MethodDeclaration> newArrayList());
166 sieTemplate.setJavadoc(sie.getNullableDescription());
168 if (sie.getNullableDescription() != null) {
169 sieTemplate.getAnnotations().add(
170 Annotation.createDescriptionAnnotation(sie
171 .getNullableDescription()));
173 sieTemplate.getAnnotations().addAll(Annotation.createSieAnnotations(sie));
178 public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe(
179 final ModuleMXBeanEntry mbe) {
180 AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor();
181 attrProcessor.processAttributes(mbe.getAttributes());
185 return new AbstractFactoryTemplate(getHeaderFromEntry(mbe),
186 mbe.getPackageName(), mbe.getAbstractFactoryName(),
187 attrProcessor.getFields()
191 public static AbstractModuleTemplate abstractModuleTemplateFromMbe(
192 final ModuleMXBeanEntry mbe) {
193 AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes());
195 List<ModuleField> moduleFields = attrProcessor.getModuleFields();
196 List<String> implementedIfcs = Lists.newArrayList(
197 mbe.getFullyQualifiedName(mbe.getMXBeanInterfaceName()));
199 for (String implementedService : mbe.getProvidedServices().keySet()) {
200 implementedIfcs.add(implementedService);
203 boolean generateRuntime = false;
204 String registratorFullyQualifiedName = null;
205 if (mbe.getRuntimeBeans() != null
206 && !mbe.getRuntimeBeans().isEmpty()) {
207 generateRuntime = true;
208 RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate
209 .findRoot(mbe.getRuntimeBeans());
210 registratorFullyQualifiedName = rootEntry
213 .concat(RuntimeRegistratorFtlTemplate.getJavaNameOfRuntimeRegistrator(rootEntry));
214 implementedIfcs.add(RuntimeBeanRegistratorAwareModule.class
215 .getCanonicalName());
218 List<String> extendedClasses = Collections.singletonList(AbstractModule.class.getCanonicalName() + "<" + mbe.getAbstractModuleName() + ">");
220 AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate(
221 getHeaderFromEntry(mbe), mbe.getPackageName(),
222 mbe.getAbstractModuleName(), extendedClasses, implementedIfcs, moduleFields,
223 attrProcessor.getMethods(), generateRuntime,
224 registratorFullyQualifiedName);
226 if (mbe.getNullableDescription() != null) {
227 abstractModuleTemplate.getAnnotations().add(
228 Annotation.createDescriptionAnnotation(mbe
229 .getNullableDescription()));
231 return abstractModuleTemplate;
234 public static StubFactoryTemplate stubFactoryTemplateFromMbe(
235 final ModuleMXBeanEntry mbe) {
236 return new StubFactoryTemplate(getHeaderFromEntry(mbe),
237 mbe.getPackageName(), mbe.getStubFactoryName(),
238 mbe.getFullyQualifiedName(mbe.getAbstractFactoryName())
242 public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe(
243 final ModuleMXBeanEntry mbe) {
244 MXBeanInterfaceAttributesProcessor attrProcessor = new MXBeanInterfaceAttributesProcessor();
245 attrProcessor.processAttributes(mbe.getAttributes());
246 GeneralInterfaceTemplate ifcTemplate = new GeneralInterfaceTemplate(
247 getHeaderFromEntry(mbe), mbe.getPackageName(),
248 mbe.getMXBeanInterfaceName(), Lists.<String> newArrayList(),
249 attrProcessor.getMethods());
250 ifcTemplate.setJavadoc(mbe.getNullableDescription());
254 public static Map<String, GeneralClassTemplate> tOsFromMbe(
255 final ModuleMXBeanEntry mbe) {
256 Map<String, GeneralClassTemplate> retVal = Maps.newHashMap();
257 TOAttributesProcessor processor = new TOAttributesProcessor();
258 processor.processAttributes(mbe.getAttributes());
259 for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor
261 List<Constructor> constructors = Lists.newArrayList();
262 constructors.add(new Constructor(to.getName(), "super();"));
264 Header header = getHeaderFromEntry(mbe);
267 new GeneralClassTemplate(header, mbe.getPackageName(), to
268 .getName(), Collections.<String> emptyList(),
269 Collections.<String> emptyList(), to.getFields(),
270 to.getMethods(), false, false, constructors));
275 public static Map<String, GeneralClassTemplate> tOsFromRbe(
276 final RuntimeBeanEntry rbe) {
277 Map<String, GeneralClassTemplate> retVal = Maps.newHashMap();
278 TOAttributesProcessor processor = new TOAttributesProcessor();
279 Map<String, AttributeIfc> yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap());
281 // Add TOs from output parameters
282 for (Rpc rpc : rbe.getRpcs()) {
283 AttributeIfc returnType = rpc.getReturnType();
285 if (returnType == VoidAttribute.getInstance()) {
288 if (returnType instanceof JavaAttribute) {
291 if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) {
295 Preconditions.checkState(!yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()),
296 "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe);
297 yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType);
300 processor.processAttributes(yangPropertiesToTypesMap);
301 for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor
303 List<Constructor> constructors = Lists.newArrayList();
304 constructors.add(new Constructor(to.getName(), "super();"));
309 new GeneralClassTemplate(null, rbe.getPackageName(), to
310 .getName(), Collections.<String> emptyList(),
311 Collections.<String> emptyList(), to.getFields(),
312 to.getMethods(), false, false, constructors));
317 private static Header getHeaderFromEntry(final AbstractEntry mbe) {
318 return new Header(mbe.getYangModuleName(), mbe.getYangModuleLocalname());
321 // TODO refactor attribute processors
323 private static class TOAttributesProcessor {
325 private final List<TOInternal> tos = Lists.newArrayList();
327 void processAttributes(final Map<String, AttributeIfc> attributes) {
328 for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
329 AttributeIfc attributeIfc = attrEntry.getValue();
330 if (attributeIfc instanceof TOAttribute) {
331 createTOInternal((TOAttribute) attributeIfc);
333 if (attributeIfc instanceof ListAttribute) {
334 AttributeIfc innerAttr = ((ListAttribute) attributeIfc)
335 .getInnerAttribute();
336 if (innerAttr instanceof TOAttribute) {
337 createTOInternal((TOAttribute) innerAttr);
343 private void createTOInternal(final TOAttribute toAttribute) {
345 Map<String, AttributeIfc> attrs = toAttribute.getCapitalizedPropertiesToTypesMap();
346 // recursive processing of TO's attributes
347 processAttributes(attrs);
349 tos.add(new TOInternal(toAttribute.getType(), attrs));
352 List<TOInternal> getTOs() {
356 private static class TOInternal {
357 private final String fullyQualifiedName, name;
358 private List<Field> fields;
359 private List<MethodDefinition> methods;
361 public TOInternal(final Type type, final Map<String, AttributeIfc> attrs) {
362 this(type.getFullyQualifiedName(), type.getName(), attrs, type.getPackageName());
365 public TOInternal(final String fullyQualifiedName, final String name,
366 final Map<String, AttributeIfc> attrs, final String packageName) {
367 this.fullyQualifiedName = fullyQualifiedName;
369 processAttrs(attrs, packageName);
372 private final static String dependencyResolverVarName = "dependencyResolver";
373 private final static String dependencyResolverInjectMethodName = "injectDependencyResolver";
375 private void processAttrs(final Map<String, AttributeIfc> attrs, final String packageName) {
376 fields = Lists.newArrayList();
377 methods = Lists.newArrayList();
379 // FIXME conflict if "dependencyResolver" field from yang
380 Field depRes = new Field(DependencyResolver.class.getName(), dependencyResolverVarName);
382 methods.add(new MethodDefinition("void", dependencyResolverInjectMethodName, Lists.newArrayList(depRes),
383 "this." + dependencyResolverVarName + " = " + dependencyResolverVarName + ";"));
385 for (Entry<String, AttributeIfc> attrEntry : attrs.entrySet()) {
386 String innerName = attrEntry.getKey();
387 String varName = BindingMapping.getPropertyName(attrEntry.getKey());
389 String fullyQualifiedName, nullableDefault = null;
390 if (attrEntry.getValue() instanceof TypedAttribute) {
391 Type type = ((TypedAttribute) attrEntry.getValue()).getType();
392 if(attrEntry.getValue() instanceof JavaAttribute) {
393 nullableDefault = ((JavaAttribute)attrEntry.getValue()).getNullableDefaultWrappedForCode();
394 if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
396 String fieldType = serializeType(type, true);
397 String innerType = getInnerTypeFromIdentity(type);
398 methods.add(new MethodDefinition(fieldType, "resolve" + attrEntry.getKey(), Collections.<Field>emptyList(),
399 "return " + varName + ".resolveIdentity(" + dependencyResolverVarName + "," + innerType + ".class);"));
400 type = identityRefType;
403 fullyQualifiedName = serializeType(type);
405 fullyQualifiedName = FullyQualifiedNameHelper
406 .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase());
408 fields.add(new Field(fullyQualifiedName, varName, nullableDefault, needsDepResolver(attrEntry.getValue())));
410 String getterName = "get" + innerName;
411 MethodDefinition getter = new MethodDefinition(
412 fullyQualifiedName, getterName,
413 Collections.<Field> emptyList(), "return "
416 String setterName = "set" + innerName;
417 MethodDefinition setter = new MethodDefinition("void",
418 setterName, Lists.newArrayList(new Field(
419 fullyQualifiedName, varName)), "this."
420 + varName + " = " + varName + ";");
426 final MethodDefinition hashCode = getHash(attrs);
427 methods.add(hashCode);
430 final MethodDefinition equals = getEquals(attrs);
434 private MethodDefinition getEquals(final Map<String, AttributeIfc> attrs) {
435 final StringBuilder equalsBodyBuilder = new StringBuilder(
436 " if (this == o) { return true; }\n" +
437 " if (o == null || getClass() != o.getClass()) { return false; }\n");
438 equalsBodyBuilder.append(String.format(
439 " final %s that = (%s) o;\n", name, name));
440 for (AttributeIfc s : attrs.values()) {
441 equalsBodyBuilder.append(String.format(
442 " if(java.util.Objects.equals(%1$s, that.%1$s) == false) {\n" +
444 " }\n\n", s.getLowerCaseCammelCase()));
446 equalsBodyBuilder.append(
448 return new MethodDefinition("boolean", "equals", Collections.singletonList(new Field("Object", "o")),
449 Collections.singletonList(new Annotation("Override", Collections.<Parameter>emptyList())), equalsBodyBuilder.toString());
452 private static MethodDefinition getHash(final Map<String, AttributeIfc> attrs) {
453 final StringBuilder hashBodyBuilder = new StringBuilder(
454 " return java.util.Objects.hash(");
455 for (AttributeIfc s : attrs.values()) {
456 hashBodyBuilder.append(s.getLowerCaseCammelCase());
457 hashBodyBuilder.append(", ");
459 hashBodyBuilder.replace(hashBodyBuilder.length() - 2, hashBodyBuilder.length(), ");\n");
460 return new MethodDefinition("int", "hashCode", Collections.<Field>emptyList(),
461 Collections.singletonList(new Annotation("Override", Collections.<Parameter>emptyList())), hashBodyBuilder.toString());
465 return fullyQualifiedName;
472 List<Field> getFields() {
476 List<MethodDefinition> getMethods() {
483 private static class MXBeanInterfaceAttributesProcessor {
484 private final List<MethodDeclaration> methods = Lists.newArrayList();
486 void processAttributes(final Map<String, AttributeIfc> attributes) {
487 for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
489 AttributeIfc attributeIfc = attrEntry.getValue();
491 if (attributeIfc instanceof TypedAttribute) {
492 TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
493 returnType = serializeType(typedAttribute.getType());
495 if (attributeIfc instanceof JavaAttribute && ((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
496 returnType = serializeType(identityRefType);
500 throw new UnsupportedOperationException(
501 "Attribute not supported: "
502 + attributeIfc.getClass());
505 String getterName = "get"
506 + attributeIfc.getUpperCaseCammelCase();
507 MethodDeclaration getter = new MethodDeclaration(returnType,
508 getterName, Collections.<Field> emptyList());
510 String varName = BindingMapping.getPropertyName(attrEntry.getKey());
511 String setterName = "set"
512 + attributeIfc.getUpperCaseCammelCase();
513 MethodDeclaration setter = new MethodDeclaration("void",
514 setterName, Lists.newArrayList(new Field(returnType,
520 if (attributeIfc.getNullableDescription() != null) {
521 setter.setJavadoc(attrEntry.getValue()
522 .getNullableDescription());
527 List<MethodDeclaration> getMethods() {
532 private static final Type identityRefType = new Type() {
533 public final Class<IdentityAttributeRef> IDENTITY_ATTRIBUTE_REF_CLASS = IdentityAttributeRef.class;
536 public String getPackageName() {
537 return IDENTITY_ATTRIBUTE_REF_CLASS.getPackage().getName();
541 public String getName() {
542 return IDENTITY_ATTRIBUTE_REF_CLASS.getSimpleName();
546 public String getFullyQualifiedName() {
547 return IDENTITY_ATTRIBUTE_REF_CLASS.getName();
551 private static class AbstractFactoryAttributesProcessor {
553 private final List<Field> fields = Lists.newArrayList();
555 void processAttributes(final Map<String, AttributeIfc> attributes) {
556 for (AttributeIfc attributeIfc : attributes.values()) {
557 if (attributeIfc instanceof TypedAttribute) {
558 TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
559 String type = serializeType(typedAttribute.getType());
561 fields.add(new Field(type, attributeIfc
562 .getUpperCaseCammelCase(), null));
564 throw new UnsupportedOperationException(
565 "Attribute not supported: "
566 + attributeIfc.getClass());
571 List<Field> getFields() {
576 private static class AbstractModuleAttributesProcessor {
577 private static class Holder {
578 private final List<ModuleField> moduleFields;
579 private final List<MethodDefinition> methods;
581 private Holder(final List<ModuleField> moduleFields, final List<MethodDefinition> methods) {
582 this.moduleFields = Collections.unmodifiableList(moduleFields);
583 this.methods = Collections.unmodifiableList(methods);
587 private final Holder holder;
590 private AbstractModuleAttributesProcessor(final Map<String, AttributeIfc> attributes) {
591 this.holder = processAttributes(attributes);
594 private static Holder processAttributes(final Map<String, AttributeIfc> attributes) {
595 List<ModuleField> moduleFields = new ArrayList<>();
596 List<MethodDefinition> methods = new ArrayList<>();
597 for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
598 String type, nullableDefaultWrapped = null;
599 AttributeIfc attributeIfc = attrEntry.getValue();
600 boolean isIdentity = false;
601 boolean needsDepResolver = needsDepResolver(attrEntry.getValue());
603 if (attributeIfc instanceof TypedAttribute) {
604 TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
605 type = serializeType(typedAttribute.getType());
606 if (attributeIfc instanceof JavaAttribute) {
607 nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode();
608 if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
610 type = serializeType(typedAttribute.getType(), true);
614 throw new UnsupportedOperationException(
615 "Attribute not supported: "
616 + attributeIfc.getClass());
619 boolean isDependency = false;
620 boolean isListOfDependencies = false;
621 Dependency dependency = null;
622 Annotation overrideAnnotation = new Annotation("Override",
623 Collections.<Parameter> emptyList());
624 List<Annotation> annotations = Lists
625 .newArrayList(overrideAnnotation);
627 if (attributeIfc instanceof AbstractDependencyAttribute) {
629 dependency = ((AbstractDependencyAttribute) attributeIfc)
631 annotations.add(Annotation
632 .createRequireIfcAnnotation(dependency.getSie()));
633 if (attributeIfc instanceof ListDependenciesAttribute) {
634 isListOfDependencies = true;
638 String varName = BindingMapping.getPropertyName(attrEntry.getKey());
642 String identityBaseClass = getInnerTypeFromIdentity(((TypedAttribute) attributeIfc).getType());
643 IdentityRefModuleField identityField = new IdentityRefModuleField(type, varName,
644 attributeIfc.getUpperCaseCammelCase(), identityBaseClass);
646 String getterName = "get"
647 + attributeIfc.getUpperCaseCammelCase() + "Identity";
648 MethodDefinition additionalGetter = new MethodDefinition(type, getterName, Collections.<Field> emptyList(),
649 Collections.<Annotation> emptyList(), "return " + identityField.getIdentityClassName()
651 methods.add(additionalGetter);
653 String setterName = "set"
654 + attributeIfc.getUpperCaseCammelCase();
656 String setterBody = "this." + identityField.getIdentityClassName() + " = " + identityField.getIdentityClassName() + ";";
657 MethodDefinition additionalSetter = new MethodDefinition("void",
659 Lists.newArrayList(new Field(type, identityField.getIdentityClassName())),
660 Collections.<Annotation> emptyList(), setterBody);
661 additionalSetter.setJavadoc(attributeIfc.getNullableDescription());
663 methods.add(additionalSetter);
665 type = serializeType(identityRefType);
666 field = identityField;
668 field = new ModuleField(type, varName, attributeIfc.getUpperCaseCammelCase(),
669 nullableDefaultWrapped, isDependency, dependency, isListOfDependencies, needsDepResolver);
671 moduleFields.add(field);
674 String getterName = "get"
675 + attributeIfc.getUpperCaseCammelCase();
676 MethodDefinition getter = new MethodDefinition(type,
677 getterName, Collections.<Field> emptyList(),
678 Lists.newArrayList(overrideAnnotation), "return "
683 String setterName = "set"
684 + attributeIfc.getUpperCaseCammelCase();
686 if (attributeIfc.getNullableDescription() != null) {
687 annotations.add(Annotation
688 .createDescriptionAnnotation(attributeIfc.getNullableDescription()));
691 String setterBody = "this." + varName + " = " + varName + ";";
692 if (isListOfDependencies) {
693 String nullCheck = String.format("if (%s == null) {\n%s = new java.util.ArrayList<>(); \n}%n",
695 setterBody = nullCheck + setterBody;
697 MethodDefinition setter = new MethodDefinition("void",
699 Lists.newArrayList(new Field(type, varName)),
700 annotations, setterBody);
701 setter.setJavadoc(attributeIfc.getNullableDescription());
705 return new Holder(moduleFields, methods);
708 List<ModuleField> getModuleFields() {
709 return holder.moduleFields;
712 List<MethodDefinition> getMethods() {
713 return holder.methods;
719 private static boolean needsDepResolver(final AttributeIfc value) {
720 if(value instanceof TOAttribute) {
723 if(value instanceof ListAttribute) {
724 AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute();
725 return needsDepResolver(innerAttribute);
731 private static String getInnerTypeFromIdentity(final Type type) {
732 Preconditions.checkArgument(type instanceof ParameterizedType);
733 Type[] args = ((ParameterizedType) type).getActualTypeArguments();
734 Preconditions.checkArgument(args.length ==1);
735 return serializeType(args[0]);