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.yangtools.sal.java.api.generator;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.yangtools.sal.java.api.generator.Constants.COMMA;
12 import java.util.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.List;
16 import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
17 import org.opendaylight.yangtools.binding.generator.util.Types;
18 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
24 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
28 public final class GeneratorUtil {
31 * It doesn't have the sense to create the instances of this class.
33 private GeneratorUtil() {
34 throw new UnsupportedOperationException();
38 * Returns the map of imports. The map maps the type name to the package
39 * name. To the map are added packages for <code>genType</code> and for all
40 * enclosed types, constants, methods (parameter types, return values),
44 * generated type for which the map of the imports is created
45 * @return map of the necessary imports
46 * @throws IllegalArgumentException
47 * if <code>genType</code> equals <code>null</code>
49 public static Map<String, String> createImports(final GeneratedType genType) {
50 if (genType == null) {
51 throw new IllegalArgumentException("Generated Type cannot be NULL!");
53 final Map<String, String> imports = new LinkedHashMap<>();
55 List<GeneratedType> childGeneratedTypes = genType.getEnclosedTypes();
56 if (!childGeneratedTypes.isEmpty()) {
57 for (GeneratedType genTypeChild : childGeneratedTypes) {
58 imports.putAll(createImports(genTypeChild));
63 if (genType instanceof GeneratedTransferObject
64 && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
65 putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
66 putTypeIntoImports(genType, Types.typeForClass(java.util.Arrays.class), imports);
67 putTypeIntoImports(genType, Types.typeForClass(java.util.ArrayList.class), imports);
70 final List<MethodSignature> methods = genType.getMethodDefinitions();
72 if (methods != null) {
73 for (final MethodSignature method : methods) {
74 final Type methodReturnType = method.getReturnType();
75 putTypeIntoImports(genType, methodReturnType, imports);
76 for (final MethodSignature.Parameter methodParam : method.getParameters()) {
77 putTypeIntoImports(genType, methodParam.getType(), imports);
79 for (final AnnotationType at : method.getAnnotations()) {
80 putTypeIntoImports(genType, at, imports);
86 if (genType instanceof GeneratedTransferObject) {
87 final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
88 final List<GeneratedProperty> properties = genTO.getProperties();
89 if (properties != null) {
90 for (GeneratedProperty property : properties) {
91 final Type propertyType = property.getReturnType();
92 putTypeIntoImports(genType, propertyType, imports);
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>.
105 * @param parentGenType
106 * generated type for which is the map of the necessary imports
109 * JAVA <code>Type</code> for which is the necessary of the
110 * package import evaluated
112 * map of the imports for <code>parentGenType</code>
113 * @throws IllegalArgumentException
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>
124 * <li>if the name of the package of <code>type</code> equals
125 * <code>null</code></li>
128 public 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 "
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!");
137 checkArgument(type.getName() != null, "Type name cannot be NULL!");
138 checkArgument(type.getPackageName() != null, "Type cannot have Package Name referenced as NULL!");
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()) {
146 if (!imports.containsKey(typeName)) {
147 imports.put(typeName, typePackageName);
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);
161 * Checks if the constant with the name <code>constName</code> is in the
162 * list of the constant definition for <code>genTO</code>.
165 * string with the name of constant which is sought
167 * generated transfer object in which is <code>constName</code>
169 * @return boolean value
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>
175 * @throws IllegalArgumentException
177 * <li>if <code>constName</code> equals <code>null</code></li>
178 * <li>if <code>genTO</code> equals <code>null</code></li>
181 public static boolean isConstantInTO(final String constName, final GeneratedTransferObject genTO) {
182 if (constName == null || genTO == null) {
183 throw new IllegalArgumentException();
185 List<Constant> consts = genTO.getConstantDefinitions();
186 for (Constant cons : consts) {
187 if (cons.getName().equals(constName)) {
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.
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
204 public 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());
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>.
221 * @param parentGenType
222 * generated type which contains <code>type</code>
224 * JAVA <code>Type</code> for which is the string with type info
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
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>
240 public static String getExplicitType(final GeneratedType parentGenType, final Type type,
241 final Map<String, String> imports) {
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!");
248 final String typePackageName = type.getPackageName();
249 final String typeName = type.getName();
250 final String importedPackageName = imports.get(typeName);
251 final StringBuilder builder;
252 if (typePackageName.equals(importedPackageName)) {
253 builder = new StringBuilder(type.getName());
254 addActualTypeParameters(builder, type, parentGenType, imports);
255 if (builder.toString().equals("Void")) {
259 builder = new StringBuilder();
260 if (!typePackageName.isEmpty()) {
261 builder.append(typePackageName + Constants.DOT + type.getName());
263 builder.append(type.getName());
265 if (type.equals(Types.voidType())) {
268 addActualTypeParameters(builder, type, parentGenType, imports);
270 return builder.toString();
275 * Adds actual type parameters from <code>type</code> to
276 * <code>builder</code> if <code>type</code> is
277 * <code>ParametrizedType</code>.
280 * string builder which contains type name
282 * JAVA <code>Type</code> for which is the string with type info
284 * @param parentGenType
285 * generated type which contains <code>type</code>
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>
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();
298 builder.append(getParameters(parentGenType, pTypes, imports));
305 * Generates the string with all actual type parameters from
306 * <code>pTypes</code>
308 * @param parentGenType
309 * generated type for which is the JAVA code generated
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>
316 private static String getParameters(final GeneratedType parentGenType, final Type[] pTypes,
317 final Map<String, String> availableImports) {
318 if (pTypes == null || pTypes.length == 0) {
321 final StringBuilder builder = new StringBuilder();
322 for (int i = 0; i < pTypes.length; i++) {
323 final Type t = pTypes[i];
325 String separator = COMMA;
326 if (i == (pTypes.length - 1)) {
330 String wildcardParam = "";
331 if (t.equals(Types.voidType())) {
332 builder.append("java.lang.Void" + separator);
336 if (t instanceof WildcardType) {
337 wildcardParam = "? extends ";
340 builder.append(wildcardParam + getExplicitType(parentGenType, t, availableImports) + separator);
343 return builder.toString();
347 * Returns the reference to highest (top parent) Generated Transfer Object.
349 * @param childTransportObject
350 * is generated transfer object which can be extended by other
351 * generated transfer object
352 * @return in first case that <code>childTransportObject</code> isn't
353 * extended then <code>childTransportObject</code> is returned. In
354 * second case the method is recursive called until first case.
355 * @throws IllegalArgumentException
356 * if <code>childTransportObject</code> equals <code>null</code>
358 public static GeneratedTransferObject getTopParrentTransportObject(final GeneratedTransferObject childTransportObject) {
359 if (childTransportObject == null) {
360 throw new IllegalArgumentException("Parameter childTransportObject can't be null.");
362 if (childTransportObject.getSuperType() == null) {
363 return childTransportObject;
365 return getTopParrentTransportObject(childTransportObject.getSuperType());
370 * Selects from input list of properties only those which have read only
371 * attribute set to true.
374 * list of properties of generated transfer object
375 * @return subset of <code>properties</code> which have read only attribute
378 public static List<GeneratedProperty> resolveReadOnlyPropertiesFromTO(final List<GeneratedProperty> properties) {
379 List<GeneratedProperty> readOnlyProperties = new ArrayList<GeneratedProperty>();
380 if (properties != null) {
381 for (final GeneratedProperty property : properties) {
382 if (property.isReadOnly()) {
383 readOnlyProperties.add(property);
387 return readOnlyProperties;
391 * Returns the list of the read only properties of all extending generated
392 * transfer object from <code>genTO</code> to highest parent generated
396 * generated transfer object for which is the list of read only
397 * properties generated
398 * @return list of all read only properties from actual to highest parent
399 * generated transfer object. In case when extension exists the
400 * method is recursive called.
402 public static List<GeneratedProperty> getPropertiesOfAllParents(final GeneratedTransferObject genTO) {
403 List<GeneratedProperty> propertiesOfAllParents = new ArrayList<GeneratedProperty>();
404 if (genTO.getSuperType() != null) {
405 final List<GeneratedProperty> allPropertiesOfTO = genTO.getSuperType().getProperties();
406 List<GeneratedProperty> readOnlyPropertiesOfTO = resolveReadOnlyPropertiesFromTO(allPropertiesOfTO);
407 propertiesOfAllParents.addAll(readOnlyPropertiesOfTO);
408 propertiesOfAllParents.addAll(getPropertiesOfAllParents(genTO.getSuperType()));
410 return propertiesOfAllParents;