2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.mdsal.binding.java.api.generator;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import java.util.HashMap;
17 import java.util.HashSet;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.mdsal.binding.model.api.Enumeration;
24 import org.opendaylight.mdsal.binding.model.api.Enumeration.Pair;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
27 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
28 import org.opendaylight.mdsal.binding.model.api.Type;
29 import org.opendaylight.mdsal.binding.model.api.WildcardType;
32 * Abstract class representing a generated type, either top-level or nested. It takes care of tracking references
33 * to other Java types and resolving them as best as possible. This class is NOT thread-safe.
35 * @author Robert Varga
38 abstract class AbstractJavaGeneratedType {
39 private final Map<JavaTypeName, @Nullable String> nameCache = new HashMap<>();
40 private final ImmutableMap<String, NestedJavaGeneratedType> enclosedTypes;
41 private final ImmutableSet<String> conflictingNames;
43 private final JavaTypeName name;
45 AbstractJavaGeneratedType(final GeneratedType genType) {
46 name = genType.getIdentifier();
47 final Builder<String, NestedJavaGeneratedType> b = ImmutableMap.builder();
48 for (GeneratedType type : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
49 b.put(type.getIdentifier().simpleName(), new NestedJavaGeneratedType(this, type));
51 enclosedTypes = b.build();
53 final Set<String> cb = new HashSet<>();
54 if (genType instanceof Enumeration) {
55 ((Enumeration) genType).getValues().stream().map(Pair::getMappedName).forEach(cb::add);
57 // TODO: perhaps we can do something smarter to actually access the types
58 collectAccessibleTypes(cb, genType);
60 conflictingNames = ImmutableSet.copyOf(cb);
63 private void collectAccessibleTypes(final Set<String> set, final GeneratedType type) {
64 for (Type impl : type.getImplements()) {
65 if (impl instanceof GeneratedType) {
66 final GeneratedType genType = (GeneratedType) impl;
67 for (GeneratedType inner : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
68 set.add(inner.getIdentifier().simpleName());
70 collectAccessibleTypes(set, genType);
75 final JavaTypeName getName() {
79 final String getSimpleName() {
80 return name.simpleName();
83 private String annotateReference(final String ref, final Type type, final String annotation) {
84 return type instanceof ParameterizedType ? getReferenceString(annotate(ref, annotation), type,
85 ((ParameterizedType) type).getActualTypeArguments())
86 : annotate(ref, annotation).toString();
89 final String getFullyQualifiedReference(final Type type, final String annotation) {
90 return annotateReference(type.getFullyQualifiedName(), type ,annotation);
93 final String getReferenceString(final Type type) {
94 final String ref = getReferenceString(type.getIdentifier());
95 return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
96 ((ParameterizedType) type).getActualTypeArguments())
100 final String getReferenceString(final Type type, final String annotation) {
101 // Package-private method, all callers who would be passing an empty array are bound to the more special
102 // case above, hence we know annotations.length >= 1
103 final String ref = getReferenceString(type.getIdentifier());
104 return annotateReference(ref, type, annotation);
107 private String getReferenceString(final StringBuilder sb, final Type type, final @NonNull Type[] arguments) {
108 if (arguments.length == 0) {
109 return sb.append("<?>").toString();
113 for (int i = 0; i < arguments.length; i++) {
114 final Type arg = arguments[i];
115 if (arg instanceof WildcardType) {
116 sb.append("? extends ");
118 sb.append(getReferenceString(arg));
119 if (i != arguments.length - 1) {
123 return sb.append('>').toString();
126 final String getReferenceString(final JavaTypeName type) {
127 if (type.packageName().isEmpty()) {
128 // This is a packageless primitive type, refer to it directly
129 return type.simpleName();
132 // Self-reference, return simple name
133 if (name.equals(type)) {
134 return name.simpleName();
137 // Fast path: we have already resolved how to refer to this type
138 final String existing = nameCache.get(type);
139 if (existing != null) {
143 // Fork based on whether the class is in this compilation unit, package or neither
145 if (name.topLevelClass().equals(type.topLevelClass())) {
146 result = localTypeName(type);
147 } else if (name.packageName().equals(type.packageName())) {
148 result = packageTypeName(type);
150 result = foreignTypeName(type);
153 nameCache.put(type, result);
157 final NestedJavaGeneratedType getEnclosedType(final JavaTypeName type) {
158 return requireNonNull(enclosedTypes.get(type.simpleName()));
161 final boolean checkAndImportType(final JavaTypeName type) {
162 // We can import the type only if it does not conflict with us or our immediately-enclosed types
163 final String simpleName = type.simpleName();
164 return !simpleName.equals(getSimpleName()) && !enclosedTypes.containsKey(simpleName)
165 && !conflictingNames.contains(simpleName) && importCheckedType(type);
168 abstract boolean importCheckedType(JavaTypeName type);
170 abstract String localTypeName(JavaTypeName type);
172 private String foreignTypeName(final JavaTypeName type) {
173 return checkAndImportType(type) ? type.simpleName() : type.toString();
176 private String packageTypeName(final JavaTypeName type) {
177 // Try to anchor the top-level type and use a local reference
178 return checkAndImportType(type.topLevelClass()) ? type.localName() : type.toString();
181 private static StringBuilder annotate(final String ref, final String annotation) {
182 final StringBuilder sb = new StringBuilder();
183 final int dot = ref.lastIndexOf('.');
185 sb.append(ref, 0, dot + 1);
187 return sb.append('@').append(annotation).append(' ').append(ref, dot + 1, ref.length());