import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import javax.annotation.concurrent.NotThreadSafe;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.mdsal.binding.model.api.Enumeration;
/**
* Abstract class representing a generated type, either top-level or nested. It takes care of tracking references
- * to other Java types and resolving them as best as possible.
+ * to other Java types and resolving them as best as possible. This class is NOT thread-safe.
*
* @author Robert Varga
*/
@NonNullByDefault
-@NotThreadSafe
abstract class AbstractJavaGeneratedType {
private final Map<JavaTypeName, @Nullable String> nameCache = new HashMap<>();
- private final Map<String, NestedJavaGeneratedType> enclosedTypes;
- private final Set<String> conflictingNames;
+ private final ImmutableMap<String, NestedJavaGeneratedType> enclosedTypes;
+ private final ImmutableSet<String> conflictingNames;
private final JavaTypeName name;
conflictingNames = ImmutableSet.copyOf(cb);
}
- private void collectAccessibleTypes(Set<String> set, GeneratedType type) {
+ private void collectAccessibleTypes(final Set<String> set, final GeneratedType type) {
for (Type impl : type.getImplements()) {
if (impl instanceof GeneratedType) {
final GeneratedType genType = (GeneratedType) impl;
}
final String getReferenceString(final Type type) {
- if (!(type instanceof ParameterizedType)) {
- return getReferenceString(type.getIdentifier());
- }
+ final String ref = getReferenceString(type.getIdentifier());
+ return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
+ ((ParameterizedType) type).getActualTypeArguments())
+ : ref;
+ }
- final StringBuilder sb = new StringBuilder();
- sb.append(getReferenceString(type.getIdentifier())).append('<');
- final Type[] types = ((ParameterizedType) type).getActualTypeArguments();
- if (types.length == 0) {
- return sb.append("?>").toString();
+ final String getReferenceString(final Type type, final String annotation) {
+ // Package-private method, all callers who would be passing an empty array are bound to the more special
+ // case above, hence we know annotations.length >= 1
+ final String ref = getReferenceString(type.getIdentifier());
+ return type instanceof ParameterizedType ? getReferenceString(annotate(ref, annotation), type,
+ ((ParameterizedType) type).getActualTypeArguments())
+ : annotate(ref, annotation).toString();
+ }
+
+ private String getReferenceString(final StringBuilder sb, final Type type, final @NonNull Type[] arguments) {
+ if (arguments.length == 0) {
+ return sb.append("<?>").toString();
}
- for (int i = 0; i < types.length; i++) {
- final Type t = types[i];
- if (t instanceof WildcardType) {
+ sb.append('<');
+ for (int i = 0; i < arguments.length; i++) {
+ final Type arg = arguments[i];
+ if (arg instanceof WildcardType) {
sb.append("? extends ");
}
- sb.append(getReferenceString(t));
- if (i != types.length - 1) {
+ sb.append(getReferenceString(arg));
+ if (i != arguments.length - 1) {
sb.append(", ");
}
}
-
return sb.append('>').toString();
}
// Try to anchor the top-level type and use a local reference
return checkAndImportType(type.topLevelClass()) ? type.localName() : type.toString();
}
+
+ private static StringBuilder annotate(final String ref, final String annotation) {
+ final StringBuilder sb = new StringBuilder();
+ final int dot = ref.lastIndexOf('.');
+ if (dot != -1) {
+ sb.append(ref, 0, dot + 1);
+ }
+ return sb.append('@').append(annotation).append(' ').append(ref, dot + 1, ref.length());
+ }
}