Merge "Remove unused imports"
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / TemplateFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.api.DependencyResolver;
14 import org.opendaylight.controller.config.api.IdentityAttributeRef;
15 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
16 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
17 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
18 import org.opendaylight.controller.config.spi.Module;
19 import org.opendaylight.controller.config.yangjmxgenerator.AbstractEntry;
20 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
21 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
22 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
23 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
26 import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency;
27 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
28 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
29 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
30 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
31 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
32 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
33 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation;
34 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Annotation.Parameter;
35 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Constructor;
36 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Field;
37 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.Header;
38 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.IdentityRefModuleField;
39 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDeclaration;
40 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.MethodDefinition;
41 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.ModuleField;
42 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper;
43 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
44 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
45 import org.opendaylight.yangtools.sal.binding.model.api.Type;
46
47 import javax.management.openmbean.SimpleType;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.Collections;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Map.Entry;
55
56 public class TemplateFactory {
57
58     /**
59      * Get map of file name as key, FtlFile instance representing runtime mx
60      * bean as value that should be persisted from this instance.
61      */
62     public static Map<String, FtlTemplate> getTOAndMXInterfaceFtlFiles(
63             RuntimeBeanEntry entry) {
64         Map<String, FtlTemplate> result = new HashMap<>();
65         { // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will
66           // be transformed to getter methods
67             String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean();
68             List<String> extendedInterfaces = Arrays.asList(RuntimeBean.class
69                     .getCanonicalName());
70             List<MethodDeclaration> methods = new ArrayList<>();
71
72             // convert attributes to getters
73             for (AttributeIfc attributeIfc : entry.getAttributes()) {
74                 String returnType;
75                 returnType = getReturnType(attributeIfc);
76                 String getterName = "get"
77                         + attributeIfc.getUpperCaseCammelCase();
78                 MethodDeclaration getter = new MethodDeclaration(returnType,
79                         getterName, Collections.<Field> emptyList());
80                 methods.add(getter);
81             }
82
83             // add rpc methods
84             for (Rpc rpc : entry.getRpcs()) {
85                 // convert JavaAttribute parameters into fields
86                 List<Field> fields = new ArrayList<>();
87                 for (JavaAttribute ja : rpc.getParameters()) {
88                     Field field = new Field(Collections.<String> emptyList(),
89                             ja.getType().getFullyQualifiedName(),
90                             ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
91                     fields.add(field);
92                 }
93                 MethodDeclaration operation = new MethodDeclaration(
94                         getReturnType(rpc.getReturnType()), rpc.getName(), fields);
95                 methods.add(operation);
96             }
97
98             // FIXME header
99             GeneralInterfaceTemplate runtimeMxBeanIfc = new GeneralInterfaceTemplate(
100                     null, entry.getPackageName(), mxBeanTypeName,
101                     extendedInterfaces, methods);
102
103             result.put(runtimeMxBeanIfc.getTypeDeclaration().getName()
104                     + ".java", runtimeMxBeanIfc);
105         }
106
107         result.putAll(TemplateFactory.tOsFromRbe(entry));
108
109         return result;
110     }
111
112     // FIXME: put into Type.toString
113     static String serializeType(Type type, boolean addWildcards) {
114         if (type instanceof ParameterizedType){
115             ParameterizedType parameterizedType = (ParameterizedType) type;
116             StringBuilder sb = new StringBuilder();
117             sb.append(parameterizedType.getRawType().getFullyQualifiedName());
118             sb.append(addWildcards ? "<? extends " : "<");
119             boolean first = true;
120             for(Type parameter: parameterizedType.getActualTypeArguments()) {
121                 if (first) {
122                     first = false;
123                 } else {
124                     sb.append(",");
125                 }
126                 sb.append(serializeType(parameter));
127             }
128             sb.append(">");
129             return sb.toString();
130         } else {
131             return type.getFullyQualifiedName();
132         }
133     }
134
135     static String serializeType(Type type) {
136         return serializeType(type, false);
137     }
138
139     private static String getReturnType(AttributeIfc attributeIfc) {
140         String returnType;
141         if (attributeIfc instanceof TypedAttribute) {
142             Type type = ((TypedAttribute) attributeIfc).getType();
143             returnType = serializeType(type);
144         } else if (attributeIfc == VoidAttribute.getInstance()) {
145             return "void";
146         } else {
147             throw new UnsupportedOperationException(
148                     "Attribute not supported: "
149                             + attributeIfc.getClass());
150         }
151         return returnType;
152     }
153
154     public static GeneralInterfaceTemplate serviceInterfaceFromSie(
155             ServiceInterfaceEntry sie) {
156
157         List<String> extendedInterfaces = Lists
158                 .newArrayList(AbstractServiceInterface.class.getCanonicalName());
159         if (sie.getBase().isPresent()) {
160             extendedInterfaces.add(sie.getBase().get().getFullyQualifiedName());
161         }
162
163         // FIXME header
164         GeneralInterfaceTemplate sieTemplate = new GeneralInterfaceTemplate(
165                 getHeaderFromEntry(sie), sie.getPackageName(),
166                 sie.getTypeName(), extendedInterfaces,
167                 Lists.<MethodDeclaration> newArrayList());
168         sieTemplate.setJavadoc(sie.getNullableDescription());
169
170         if (sie.getNullableDescription() != null)
171             sieTemplate.getAnnotations().add(
172                     Annotation.createDescriptionAnnotation(sie
173                             .getNullableDescription()));
174         sieTemplate.getAnnotations().addAll(Annotation.createSieAnnotations(sie));
175
176         return sieTemplate;
177     }
178
179     public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe(
180             ModuleMXBeanEntry mbe) {
181         AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor();
182         attrProcessor.processAttributes(mbe.getAttributes(),
183                 mbe.getPackageName());
184
185
186
187         return new AbstractFactoryTemplate(getHeaderFromEntry(mbe),
188                 mbe.getPackageName(), mbe.getAbstractFactoryName(),
189                 attrProcessor.getFields()
190         );
191     }
192
193     public static AbstractModuleTemplate abstractModuleTemplateFromMbe(
194             ModuleMXBeanEntry mbe) {
195         AbstractModuleAttributesProcessor attrProcessor = new AbstractModuleAttributesProcessor(mbe.getAttributes());
196
197         List<ModuleField> moduleFields = attrProcessor.getModuleFields();
198         List<String> implementedIfcs = Lists.newArrayList(
199                 Module.class.getCanonicalName(),
200                 mbe.getFullyQualifiedName(mbe.getMXBeanInterfaceName()));
201
202         for (String implementedService : mbe.getProvidedServices().keySet()) {
203             implementedIfcs.add(implementedService);
204         }
205
206         boolean generateRuntime = false;
207         String registratorFullyQualifiedName = null;
208         if (mbe.getRuntimeBeans() != null
209                 && mbe.getRuntimeBeans().isEmpty() == false) {
210             generateRuntime = true;
211             RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate
212                     .findRoot(mbe.getRuntimeBeans());
213             registratorFullyQualifiedName = rootEntry
214                     .getPackageName()
215                     .concat(".")
216                     .concat(RuntimeRegistratorFtlTemplate.getJavaNameOfRuntimeRegistrator(rootEntry));
217             implementedIfcs.add(RuntimeBeanRegistratorAwareModule.class
218                     .getCanonicalName());
219         }
220
221         AbstractModuleTemplate abstractModuleTemplate = new AbstractModuleTemplate(
222                 getHeaderFromEntry(mbe), mbe.getPackageName(),
223                 mbe.getAbstractModuleName(), implementedIfcs, moduleFields,
224                 attrProcessor.getMethods(), generateRuntime,
225                 registratorFullyQualifiedName);
226
227         if (mbe.getNullableDescription() != null)
228             abstractModuleTemplate.getAnnotations().add(
229                     Annotation.createDescriptionAnnotation(mbe
230                             .getNullableDescription()));
231         return abstractModuleTemplate;
232     }
233
234     public static StubFactoryTemplate stubFactoryTemplateFromMbe(
235             ModuleMXBeanEntry mbe) {
236         return new StubFactoryTemplate(getHeaderFromEntry(mbe),
237                 mbe.getPackageName(), mbe.getStubFactoryName(),
238                 mbe.getFullyQualifiedName(mbe.getAbstractFactoryName())
239         );
240     }
241
242     public static GeneralInterfaceTemplate mXBeanInterfaceTemplateFromMbe(
243             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());
251         return ifcTemplate;
252     }
253
254     public static Map<String, GeneralClassTemplate> tOsFromMbe(
255             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
260                 .getTOs()) {
261             List<Constructor> constructors = Lists.newArrayList();
262             constructors.add(new Constructor(to.getName(), "super();"));
263
264             Header header = getHeaderFromEntry(mbe);
265             retVal.put(
266                     to.getType(),
267                     new GeneralClassTemplate(header, mbe.getPackageName(), to
268                             .getName(), Collections.<String> emptyList(),
269                             Collections.<String> emptyList(), to.getFields(),
270                             to.getMethods(), false, false, constructors));
271         }
272         return retVal;
273     }
274
275     public static Map<String, GeneralClassTemplate> tOsFromRbe(
276             RuntimeBeanEntry rbe) {
277         Map<String, GeneralClassTemplate> retVal = Maps.newHashMap();
278         TOAttributesProcessor processor = new TOAttributesProcessor();
279         Map<String, AttributeIfc> yangPropertiesToTypesMap = Maps.newHashMap(rbe.getYangPropertiesToTypesMap());
280
281         // Add TOs from output parameters
282         for (Rpc rpc : rbe.getRpcs()) {
283             AttributeIfc returnType = rpc.getReturnType();
284
285             if (returnType == VoidAttribute.getInstance()) {
286                 continue;
287             }
288             if (returnType instanceof JavaAttribute) {
289                 continue;
290             }
291             if (returnType instanceof ListAttribute && returnType.getOpenType() instanceof SimpleType) {
292                 continue;
293             }
294
295             Preconditions.checkState(yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()) == false,
296                     "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe);
297             yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType);
298         }
299
300         processor.processAttributes(yangPropertiesToTypesMap);
301         for (org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.TemplateFactory.TOAttributesProcessor.TOInternal to : processor
302                 .getTOs()) {
303             List<Constructor> constructors = Lists.newArrayList();
304             constructors.add(new Constructor(to.getName(), "super();"));
305
306             // TODO header
307             retVal.put(
308                     to.getType(),
309                     new GeneralClassTemplate(null, rbe.getPackageName(), to
310                             .getName(), Collections.<String> emptyList(),
311                             Collections.<String> emptyList(), to.getFields(),
312                             to.getMethods(), false, false, constructors));
313         }
314         return retVal;
315     }
316
317     private static Header getHeaderFromEntry(AbstractEntry mbe) {
318         return new Header(mbe.getYangModuleName(), mbe.getYangModuleLocalname());
319     }
320
321     // TODO refactor attribute processors
322
323     private static class TOAttributesProcessor {
324
325         private final List<TOInternal> tos = Lists.newArrayList();
326
327         void processAttributes(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);
332                 }
333                 if (attributeIfc instanceof ListAttribute) {
334                     AttributeIfc innerAttr = ((ListAttribute) attributeIfc)
335                             .getInnerAttribute();
336                     if (innerAttr instanceof TOAttribute) {
337                         createTOInternal((TOAttribute) innerAttr);
338                     }
339                 }
340             }
341         }
342
343         private void createTOInternal(TOAttribute toAttribute) {
344
345             Map<String, AttributeIfc> attrs = toAttribute.getCapitalizedPropertiesToTypesMap();
346             // recursive processing of TO's attributes
347             processAttributes(attrs);
348
349             tos.add(new TOInternal(toAttribute.getType(), attrs));
350         }
351
352         List<TOInternal> getTOs() {
353             return tos;
354         }
355
356         private static class TOInternal {
357             private final String fullyQualifiedName, name;
358             private List<Field> fields;
359             private List<MethodDefinition> methods;
360
361             public TOInternal(Type type, Map<String, AttributeIfc> attrs) {
362                 this(type.getFullyQualifiedName(), type.getName(), attrs, type.getPackageName());
363             }
364
365             public TOInternal(String fullyQualifiedName, String name,
366                     Map<String, AttributeIfc> attrs, String packageName) {
367                 this.fullyQualifiedName = fullyQualifiedName;
368                 this.name = name;
369                 processAttrs(attrs, packageName);
370             }
371
372             private final static String dependencyResolverVarName = "dependencyResolver";
373             private final static String dependencyResolverInjectMethodName = "injectDependencyResolver";
374
375             private void processAttrs(Map<String, AttributeIfc> attrs, String packageName) {
376                 fields = Lists.newArrayList();
377                 methods = Lists.newArrayList();
378
379                 // FIXME conflict if "dependencyResolver" field from yang
380                 Field depRes = new Field(DependencyResolver.class.getName(), dependencyResolverVarName);
381                 fields.add(depRes);
382                 methods.add(new MethodDefinition("void", dependencyResolverInjectMethodName, Lists.newArrayList(depRes),
383                         "this." + dependencyResolverVarName + " = " + dependencyResolverVarName + ";"));
384
385                 for (Entry<String, AttributeIfc> attrEntry : attrs.entrySet()) {
386                     String innerName = attrEntry.getKey();
387                     String varName = BindingGeneratorUtil
388                             .parseToValidParamName(attrEntry.getKey());
389
390                     String fullyQualifiedName, nullableDefault = null;
391                     if (attrEntry.getValue() instanceof TypedAttribute) {
392                         Type type = ((TypedAttribute) attrEntry.getValue()).getType();
393                         if(attrEntry.getValue() instanceof JavaAttribute) {
394                             nullableDefault = ((JavaAttribute)attrEntry.getValue()).getNullableDefaultWrappedForCode();
395                             if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
396
397                                 String fieldType = serializeType(type, true);
398                                 String innerType = getInnerTypeFromIdentity(type);
399                                 methods.add(new MethodDefinition(fieldType, "resolve" + attrEntry.getKey(), Collections.<Field>emptyList(),
400                                         "return " + varName + ".resolveIdentity(" + dependencyResolverVarName + "," +  innerType + ".class);"));
401                                 type = identityRefType;
402                             }
403                         }
404                         fullyQualifiedName = serializeType(type);
405                     } else {
406                         fullyQualifiedName = FullyQualifiedNameHelper
407                                 .getFullyQualifiedName(packageName, attrEntry.getValue().getUpperCaseCammelCase());
408                     }
409                     fields.add(new Field(fullyQualifiedName, varName, nullableDefault, needsDepResolver(attrEntry.getValue())));
410
411                     String getterName = "get" + innerName;
412                     MethodDefinition getter = new MethodDefinition(
413                             fullyQualifiedName, getterName,
414                             Collections.<Field> emptyList(), "return "
415                                     + varName + ";");
416
417                     String setterName = "set" + innerName;
418                     MethodDefinition setter = new MethodDefinition("void",
419                             setterName, Lists.newArrayList(new Field(
420                                     fullyQualifiedName, varName)), "this."
421                                     + varName + " = " + varName + ";");
422                     methods.add(getter);
423                     methods.add(setter);
424                 }
425
426             }
427
428             String getType() {
429                 return fullyQualifiedName;
430             }
431
432             String getName() {
433                 return name;
434             }
435
436             List<Field> getFields() {
437                 return fields;
438             }
439
440             List<MethodDefinition> getMethods() {
441                 return methods;
442             }
443         }
444     }
445
446
447     private static class MXBeanInterfaceAttributesProcessor {
448         private final List<MethodDeclaration> methods = Lists.newArrayList();
449
450         void processAttributes(Map<String, AttributeIfc> attributes) {
451             for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
452                 String returnType;
453                 AttributeIfc attributeIfc = attrEntry.getValue();
454
455                 if (attributeIfc instanceof TypedAttribute) {
456                     TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
457                     returnType = serializeType(typedAttribute.getType());
458
459                     if (attributeIfc instanceof JavaAttribute && ((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
460                         returnType = serializeType(identityRefType);
461                     }
462
463                 } else {
464                     throw new UnsupportedOperationException(
465                             "Attribute not supported: "
466                                     + attributeIfc.getClass());
467                 }
468
469                 String getterName = "get"
470                         + attributeIfc.getUpperCaseCammelCase();
471                 MethodDeclaration getter = new MethodDeclaration(returnType,
472                         getterName, Collections.<Field> emptyList());
473
474                 String varName = BindingGeneratorUtil
475                         .parseToValidParamName(attrEntry.getKey());
476                 String setterName = "set"
477                         + attributeIfc.getUpperCaseCammelCase();
478                 MethodDeclaration setter = new MethodDeclaration("void",
479                         setterName, Lists.newArrayList(new Field(returnType,
480                                 varName)));
481
482                 methods.add(getter);
483                 methods.add(setter);
484
485                 if (attributeIfc.getNullableDescription() != null) {
486                     setter.setJavadoc(attrEntry.getValue()
487                             .getNullableDescription());
488                 }
489             }
490         }
491
492         List<MethodDeclaration> getMethods() {
493             return methods;
494         }
495     }
496
497     private static final Type identityRefType = new Type() {
498         public final Class<IdentityAttributeRef> IDENTITY_ATTRIBUTE_REF_CLASS = IdentityAttributeRef.class;
499
500         @Override
501         public String getPackageName() {
502             return IDENTITY_ATTRIBUTE_REF_CLASS.getPackage().getName();
503         }
504
505         @Override
506         public String getName() {
507             return IDENTITY_ATTRIBUTE_REF_CLASS.getSimpleName();
508         }
509
510         @Override
511         public String getFullyQualifiedName() {
512             return IDENTITY_ATTRIBUTE_REF_CLASS.getName();
513         }
514     };
515
516     private static class AbstractFactoryAttributesProcessor {
517
518         private final List<Field> fields = Lists.newArrayList();
519
520         void processAttributes(Map<String, AttributeIfc> attributes,
521                 String packageName) {
522             for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
523                 String type;
524                 String nullableDefaultWrapped = null;
525                 AttributeIfc attributeIfc = attrEntry.getValue();
526
527                 if (attributeIfc instanceof TypedAttribute) {
528                     TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
529                     type = serializeType(typedAttribute.getType());
530                 } else {
531                     throw new UnsupportedOperationException(
532                             "Attribute not supported: "
533                                     + attributeIfc.getClass());
534                 }
535
536                 fields.add(new Field(type, attributeIfc
537                         .getUpperCaseCammelCase(), nullableDefaultWrapped));
538             }
539         }
540
541         List<Field> getFields() {
542             return fields;
543         }
544     }
545
546     private static class AbstractModuleAttributesProcessor {
547         private static class Holder {
548             private final List<ModuleField> moduleFields;
549             private final List<MethodDefinition> methods;
550
551             private Holder(List<ModuleField> moduleFields, List<MethodDefinition> methods) {
552                 this.moduleFields = Collections.unmodifiableList(moduleFields);
553                 this.methods = Collections.unmodifiableList(methods);
554             }
555         }
556
557         private final Holder holder;
558
559
560         private AbstractModuleAttributesProcessor(Map<String, AttributeIfc> attributes) {
561             this.holder = processAttributes(attributes);
562         }
563
564         private static Holder processAttributes(Map<String, AttributeIfc> attributes) {
565             List<ModuleField> moduleFields = new ArrayList<>();
566             List<MethodDefinition> methods = new ArrayList<>();
567             for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
568                 String type, nullableDefaultWrapped = null;
569                 AttributeIfc attributeIfc = attrEntry.getValue();
570                 boolean isIdentity = false;
571                 boolean needsDepResolver = needsDepResolver(attrEntry.getValue());
572
573                 if (attributeIfc instanceof TypedAttribute) {
574                     TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
575                     type = serializeType(typedAttribute.getType());
576                     if (attributeIfc instanceof JavaAttribute) {
577                         nullableDefaultWrapped = ((JavaAttribute) attributeIfc).getNullableDefaultWrappedForCode();
578                         if(((JavaAttribute)attrEntry.getValue()).isIdentityRef()) {
579                             isIdentity = true;
580                             type = serializeType(typedAttribute.getType(), true);
581                         }
582                     }
583                 } else {
584                     throw new UnsupportedOperationException(
585                             "Attribute not supported: "
586                                     + attributeIfc.getClass());
587                 }
588
589                 boolean isDependency = false;
590                 boolean isListOfDependencies = false;
591                 Dependency dependency = null;
592                 Annotation overrideAnnotation = new Annotation("Override",
593                         Collections.<Parameter> emptyList());
594                 List<Annotation> annotations = Lists
595                         .newArrayList(overrideAnnotation);
596
597                 if (attributeIfc instanceof AbstractDependencyAttribute) {
598                     isDependency = true;
599                     dependency = ((AbstractDependencyAttribute) attributeIfc)
600                             .getDependency();
601                     annotations.add(Annotation
602                             .createRequireIfcAnnotation(dependency.getSie()));
603                     if (attributeIfc instanceof ListDependenciesAttribute) {
604                         isListOfDependencies = true;
605                     }
606                 }
607
608                 String varName = BindingGeneratorUtil
609                         .parseToValidParamName(attrEntry.getKey());
610
611                 ModuleField field;
612                 if (isIdentity) {
613                     String identityBaseClass = getInnerTypeFromIdentity(((TypedAttribute) attributeIfc).getType());
614                     IdentityRefModuleField identityField = new IdentityRefModuleField(type, varName,
615                             attributeIfc.getUpperCaseCammelCase(), identityBaseClass);
616
617                     String getterName = "get"
618                             + attributeIfc.getUpperCaseCammelCase() + "Identity";
619                     MethodDefinition additionalGetter = new MethodDefinition(type, getterName, Collections.<Field> emptyList(),
620                             Collections.<Annotation> emptyList(), "return " + identityField.getIdentityClassName()
621                                     + ";");
622                     methods.add(additionalGetter);
623
624                     String setterName = "set"
625                             + attributeIfc.getUpperCaseCammelCase();
626
627                     String setterBody = "this." + identityField.getIdentityClassName() + " = " + identityField.getIdentityClassName() + ";";
628                     MethodDefinition additionalSetter = new MethodDefinition("void",
629                             setterName,
630                             Lists.newArrayList(new Field(type, identityField.getIdentityClassName())),
631                             Collections.<Annotation> emptyList(), setterBody);
632                     additionalSetter.setJavadoc(attributeIfc.getNullableDescription());
633
634                     methods.add(additionalSetter);
635
636                     type = serializeType(identityRefType);
637                     field = identityField;
638                 } else {
639                     field = new ModuleField(type, varName, attributeIfc.getUpperCaseCammelCase(),
640                             nullableDefaultWrapped, isDependency, dependency, isListOfDependencies, needsDepResolver);
641                 }
642                 moduleFields.add(field);
643
644
645                 String getterName = "get"
646                         + attributeIfc.getUpperCaseCammelCase();
647                 MethodDefinition getter = new MethodDefinition(type,
648                         getterName, Collections.<Field> emptyList(),
649                         Lists.newArrayList(overrideAnnotation), "return "
650                         + varName + ";");
651
652                 methods.add(getter);
653
654                 String setterName = "set"
655                         + attributeIfc.getUpperCaseCammelCase();
656
657                 if (attributeIfc.getNullableDescription() != null) {
658                     annotations.add(Annotation
659                             .createDescriptionAnnotation(attributeIfc.getNullableDescription()));
660                 }
661
662                 String setterBody = "this." + varName + " = " + varName + ";";
663                 if (isListOfDependencies) {
664                     String nullCheck = String.format("if (%s == null) throw new IllegalArgumentException(\"Null not supported\");%n",
665                             varName);
666                     setterBody = nullCheck + setterBody;
667                 }
668                 MethodDefinition setter = new MethodDefinition("void",
669                         setterName,
670                         Lists.newArrayList(new Field(type, varName)),
671                         annotations, setterBody);
672                 setter.setJavadoc(attributeIfc.getNullableDescription());
673
674                 methods.add(setter);
675             }
676             return new Holder(moduleFields, methods);
677         }
678
679         List<ModuleField> getModuleFields() {
680             return holder.moduleFields;
681         }
682
683         List<MethodDefinition> getMethods() {
684             return holder.methods;
685         }
686
687     }
688
689
690     private static boolean needsDepResolver(AttributeIfc value) {
691         if(value instanceof TOAttribute)
692             return true;
693         if(value instanceof ListAttribute) {
694             AttributeIfc innerAttribute = ((ListAttribute) value).getInnerAttribute();
695             return needsDepResolver(innerAttribute);
696         }
697
698         return false;
699     }
700
701     private static String getInnerTypeFromIdentity(Type type) {
702         Preconditions.checkArgument(type instanceof ParameterizedType);
703         Type[] args = ((ParameterizedType) type).getActualTypeArguments();
704         Preconditions.checkArgument(args.length ==1);
705         return serializeType(args[0]);
706     }
707 }