Bug 6859: Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorUtil.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.mdsal.binding.java.api.generator;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.mdsal.binding.java.api.generator.Constants.COMMA;
12
13 import java.util.ArrayList;
14 import java.util.LinkedHashMap;
15 import java.util.List;
16 import java.util.Map;
17 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
18 import org.opendaylight.mdsal.binding.model.api.Constant;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.mdsal.binding.model.api.WildcardType;
26 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
27 import org.opendaylight.mdsal.binding.model.util.Types;
28
29 public final class GeneratorUtil {
30
31     /**
32      * It doesn't have the sense to create the instances of this class.
33      */
34     private GeneratorUtil() {
35         throw new UnsupportedOperationException();
36     }
37
38     /**
39      * Returns the map of imports. The map maps the type name to the package
40      * name. To the map are added packages for <code>genType</code> and for all
41      * enclosed types, constants, methods (parameter types, return values),
42      * implemented types.
43      *
44      * @param genType
45      *            generated type for which the map of the imports is created
46      * @return map of the necessary imports
47      * @throws IllegalArgumentException
48      *             if <code>genType</code> equals <code>null</code>
49      */
50     static Map<String, String> createImports(final GeneratedType genType) {
51         if (genType == null) {
52             throw new IllegalArgumentException("Generated Type cannot be NULL!");
53         }
54         final Map<String, String> imports = new LinkedHashMap<>();
55
56         List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
57         if (!childGeneratedTypes.isEmpty()) {
58             for (GeneratedType genTypeChild : childGeneratedTypes) {
59                 imports.putAll(createImports(genTypeChild));
60             }
61         }
62
63         // REGULAR EXPRESSION
64         if (genType instanceof GeneratedTransferObject
65                 && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
66             putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
67             putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
68             putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
69         }
70
71         final List<MethodSignature> methods = genType.getMethodDefinitions();
72         // METHODS
73         if (methods != null) {
74             for (final MethodSignature method : methods) {
75                 final Type methodReturnType = method.getReturnType();
76                 putTypeIntoImports(genType, methodReturnType, imports);
77                 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
78                     putTypeIntoImports(genType, methodParam.getType(), imports);
79                 }
80                 for (final AnnotationType at : method.getAnnotations()) {
81                     putTypeIntoImports(genType, at, imports);
82                 }
83             }
84         }
85
86         // PROPERTIES
87         if (genType instanceof GeneratedTransferObject) {
88             final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
89             final List<GeneratedProperty> properties = genTO.getProperties();
90             if (properties != null) {
91                 for (GeneratedProperty property : properties) {
92                     final Type propertyType = property.getReturnType();
93                     putTypeIntoImports(genType, propertyType, imports);
94                 }
95             }
96         }
97         return imports;
98     }
99
100     /**
101      * Evaluates if it is necessary to add the package name for
102      * <code>type</code> to the map of imports for <code>parentGenType</code>.
103      * If it is so the package name is saved to the map <code>imports</code>.
104      *
105      * @param parentGenType
106      *            generated type for which is the map of the necessary imports
107      *            built
108      * @param type
109      *            JAVA <code>Type</code> for which is the necessary of the
110      *            package import evaluated
111      * @param imports
112      *            map of the imports for <code>parentGenType</code>
113      * @throws IllegalArgumentException
114      *             <ul>
115      *             <li>if the <code>parentGenType</code> equals
116      *             <code>null</code></li>
117      *             <li>if the name of <code>parentGenType</code> equals
118      *             <code>null</code></li>
119      *             <li>if the name of the package of <code>parentGenType</code>
120      *             equals <code>null</code></li>
121      *             <li>if the <code>type</code> equals <code>null</code></li>
122      *             <li>if the name of <code>type</code> equals <code>null</code>
123      *             </li>
124      *             <li>if the name of the package of <code>type</code> equals
125      *             <code>null</code></li>
126      *             </ul>
127      */
128     static void putTypeIntoImports(final GeneratedType parentGenType, final Type type,
129                                    final Map<String, String> imports) {
130         checkArgument(parentGenType != null, "Parent Generated Type parameter MUST be specified and cannot be "
131                 + "NULL!");
132         checkArgument(parentGenType.getName() != null, "Parent Generated Type name cannot be NULL!");
133         checkArgument(parentGenType.getPackageName() != null,
134                 "Parent Generated Type cannot have Package Name referenced as NULL!");
135         checkArgument(type != null, "Type parameter MUST be specified and cannot be NULL!");
136
137         checkArgument(type.getName() != null, "Type name cannot be NULL!");
138         checkArgument(type.getPackageName() != null, "Type cannot have Package Name referenced as NULL!");
139
140         final String typeName = type.getName();
141         final String typePackageName = type.getPackageName();
142         final String parentTypeName = parentGenType.getName();
143         if (typeName.equals(parentTypeName) || typePackageName.startsWith("java.lang") || typePackageName.isEmpty()) {
144             return;
145         }
146         if (!imports.containsKey(typeName)) {
147             imports.put(typeName, typePackageName);
148         }
149         if (type instanceof ParameterizedType) {
150             final ParameterizedType paramType = (ParameterizedType) type;
151             final Type[] params = paramType.getActualTypeArguments();
152             if (params != null) {
153                 for (Type param : params) {
154                     putTypeIntoImports(parentGenType, param, imports);
155                 }
156             }
157         }
158     }
159
160     /**
161      * Checks if the constant with the name <code>constName</code> is in the
162      * list of the constant definition for <code>genTO</code>.
163      *
164      * @param constName
165      *            string with the name of constant which is sought
166      * @param genTO
167      *            generated transfer object in which is <code>constName</code>
168      *            sought
169      * @return boolean value
170      *         <ul>
171      *         <li>true - if <code>constName</code> is in the list of the
172      *         constant definition for <code>genTO</code></li>
173      *         <li>false - in other cases</li>
174      *         </ul>
175      * @throws IllegalArgumentException
176      *             <ul>
177      *             <li>if <code>constName</code> equals <code>null</code></li>
178      *             <li>if <code>genTO</code> equals <code>null</code></li>
179      *             </ul>
180      */
181     static boolean isConstantInTO(final String constName, final GeneratedTransferObject genTO) {
182         if (constName == null || genTO == null) {
183             throw new IllegalArgumentException();
184         }
185         List<Constant> consts = genTO.getConstantDefinitions();
186         for (Constant cons : consts) {
187             if (cons.getName().equals(constName)) {
188                 return true;
189             }
190         }
191         return false;
192     }
193
194     /**
195      * Creates the map which maps the type name to package name and contains
196      * only package names for enclosed types of <code>genType</code> and
197      * recursivelly their enclosed types.
198      *
199      * @param genType
200      *            JAVA <code>Type</code> for which is the map created
201      * @return map of the package names for all the enclosed types and
202      *         recursivelly their enclosed types
203      */
204     static Map<String, String> createChildImports(final GeneratedType genType) {
205         Map<String, String> childImports = new LinkedHashMap<>();
206         List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
207         if (!childGeneratedTypes.isEmpty()) {
208             for (GeneratedType genTypeChild : childGeneratedTypes) {
209                 createChildImports(genTypeChild);
210                 childImports.put(genTypeChild.getName(), genTypeChild.getPackageName());
211             }
212         }
213         return childImports;
214     }
215
216     /**
217      * Builds the string which contains either the full path to the type
218      * (package name with type) or only type name if the package is among
219      * <code>imports</code>.
220      *
221      * @param parentGenType
222      *            generated type which contains <code>type</code>
223      * @param type
224      *            JAVA <code>Type</code> for which is the string with type info
225      *            generated
226      * @param imports
227      *            map of necessary imports for <code>parentGenType</code>
228      * @return string with type name for <code>type</code> in the full format or
229      *         in the short format
230      * @throws IllegalArgumentException
231      *             <ul>
232      *             <li>if the <code>type</code> equals <code>null</code></li>
233      *             <li>if the name of the <code>type</code> equals
234      *             <code>null</code></li>
235      *             <li>if the name of the package of the <code>type</code>
236      *             equals <code>null</code></li>
237      *             <li>if the <code>imports</code> equals <code>null</code></li>
238      *             </ul>
239      */
240     static String getExplicitType(final GeneratedType parentGenType, final Type type,
241                                   final Map<String, String> imports) {
242
243         checkArgument(type != null, "Type parameter MUST be specified and cannot be NULL!");
244         checkArgument(type.getName() != null, "Type name cannot be NULL!");
245         checkArgument(type.getPackageName() != null, "Type cannot have Package Name referenced as NULL!");
246         checkArgument(imports != null, "Imports Map cannot be NULL!");
247
248         final String typePackageName = type.getPackageName();
249         final String typeName = type.getName();
250         final String importedPackageName = imports.get(typeName);
251         final StringBuilder builder;
252
253         if (typePackageName.equals(importedPackageName)) {
254             builder = new StringBuilder(type.getName());
255             addActualTypeParameters(builder, type, parentGenType, imports);
256             if (builder.toString().equals("Void")) {
257                 return "void";
258             }
259         } else {
260             builder = new StringBuilder();
261             if (!typePackageName.isEmpty()) {
262                 builder.append(typePackageName).append(Constants.DOT).append(type.getName());
263             } else {
264                 builder.append(type.getName());
265             }
266             if (type.equals(Types.voidType())) {
267                 return "void";
268             }
269             addActualTypeParameters(builder, type, parentGenType, imports);
270         }
271         return builder.toString();
272     }
273
274     /**
275      * Adds actual type parameters from <code>type</code> to
276      * <code>builder</code> if <code>type</code> is
277      * <code>ParametrizedType</code>.
278      *
279      * @param builder
280      *            string builder which contains type name
281      * @param type
282      *            JAVA <code>Type</code> for which is the string with type info
283      *            generated
284      * @param parentGenType
285      *            generated type which contains <code>type</code>
286      * @param imports
287      *            map of necessary imports for <code>parentGenType</code>
288      * @return if <code>type</code> is of the type <code>ParametrizedType</code> <br />
289      *         <li>then <code>builder</code> + actual <code>type</code>
290      *         parameters</li> <li>else only <code>builder</code></li>
291      */
292     private static StringBuilder addActualTypeParameters(final StringBuilder builder, final Type type,
293                                                          final GeneratedType parentGenType, final Map<String, String> imports) {
294         if (type instanceof ParameterizedType) {
295             final ParameterizedType pType = (ParameterizedType) type;
296             final Type[] pTypes = pType.getActualTypeArguments();
297             builder.append("<");
298             builder.append(getParameters(parentGenType, pTypes, imports));
299             builder.append(">");
300         }
301         return builder;
302     }
303
304     /**
305      * Generates the string with all actual type parameters from
306      * <code>pTypes</code>
307      *
308      * @param parentGenType
309      *            generated type for which is the JAVA code generated
310      * @param pTypes
311      *            array of <code>Type</code> instances = actual type parameters
312      * @param availableImports
313      *            map of imports for <code>parentGenType</code>
314      * @return string with all actual type parameters from <code>pTypes</code>
315      */
316     private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,
317                                         final Map<String, String> availableImports) {
318
319         if (pTypes == null || pTypes.length == 0) {
320             return "?";
321         }
322         final StringBuilder builder = new StringBuilder();
323         for (int i = 0; i < pTypes.length; i++) {
324             final Type t = pTypes[i];
325
326             String separator = COMMA;
327             if (i == (pTypes.length - 1)) {
328                 separator = "";
329             }
330
331             if (Types.voidType().equals(t)) {
332                 builder.append("java.lang.Void").append(separator);
333             } else {
334                 if (t instanceof WildcardType) {
335                     builder.append("? extends ");
336                 }
337                 builder.append(getExplicitType(parentGenType, t, availableImports)).append(separator);
338             }
339         }
340         return builder.toString();
341     }
342
343     /**
344      * Returns the reference to highest (top parent) Generated Transfer Object.
345      *
346      * @param childTransportObject
347      *            is generated transfer object which can be extended by other
348      *            generated transfer object
349      * @return in first case that <code>childTransportObject</code> isn't
350      *         extended then <code>childTransportObject</code> is returned. In
351      *         second case the method is recursive called until first case.
352      * @throws IllegalArgumentException
353      *             if <code>childTransportObject</code> equals <code>null</code>
354      */
355     static GeneratedTransferObject getTopParentTransportObject(final GeneratedTransferObject childTransportObject) {
356         if (childTransportObject == null) {
357             throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
358         }
359         if (childTransportObject.getSuperType() == null) {
360             return childTransportObject;
361         } else {
362             return getTopParentTransportObject(childTransportObject.getSuperType());
363         }
364     }
365
366     /**
367      * Selects from input list of properties only those which have read only
368      * attribute set to true.
369      *
370      * @param properties
371      *            list of properties of generated transfer object
372      * @return subset of <code>properties</code> which have read only attribute
373      *         set to true
374      */
375     static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(final List<GeneratedProperty> properties) {
376         List<GeneratedProperty> readOnlyProperties = new ArrayList<>();
377         if (properties != null) {
378             for (final GeneratedProperty property : properties) {
379                 if (property.isReadOnly()) {
380                     readOnlyProperties.add(property);
381                 }
382             }
383         }
384         return readOnlyProperties;
385     }
386
387     /**
388      * Returns the list of the read only properties of all extending generated
389      * transfer object from <code>genTO</code> to highest parent generated
390      * transfer object
391      *
392      * @param genTO
393      *            generated transfer object for which is the list of read only
394      *            properties generated
395      * @return list of all read only properties from actual to highest parent
396      *         generated transfer object. In case when extension exists the
397      *         method is recursive called.
398      */
399     static List<GeneratedProperty> getPropertiesOfAllParents(final GeneratedTransferObject genTO) {
400         List<GeneratedProperty> propertiesOfAllParents = new ArrayList<>();
401         if (genTO.getSuperType() != null) {
402             final List<GeneratedProperty> allPropertiesOfTO = genTO.getSuperType().getProperties();
403             List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);
404             propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);
405             propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getSuperType()));
406         }
407         return propertiesOfAllParents;
408     }
409 }