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