final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
// Do not refer to annotation class, as it may not be available at runtime
+ // FIXME: migrate this to some other annotation type
method.addAnnotation("javax.annotation", "CheckReturnValue");
addComment(method, rpc);
method.addParameter(
getMethod.setReturnType(returnType);
annotateDeprecatedIfNecessary(node.getStatus(), getMethod);
- if (!returnType.getPackageName().isEmpty()) {
- // The return type has a package, so it's not a primitive type
- getMethod.addAnnotation("javax.annotation", "Nullable");
- }
addComment(getMethod, node);
return getMethod;
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;
return name.simpleName();
}
- final String getReferenceString(final Type type) {
+ final String getReferenceString(final Type type, final String... annotations) {
+ final String ref = getReferenceString(type.getIdentifier());
if (!(type instanceof ParameterizedType)) {
- return getReferenceString(type.getIdentifier());
+ return annotations.length == 0 ? ref : annotate(ref, annotations).toString();
}
- final StringBuilder sb = new StringBuilder();
- sb.append(getReferenceString(type.getIdentifier())).append('<');
+ final StringBuilder sb = annotate(ref, annotations).append('<');
final Type[] types = ((ParameterizedType) type).getActualTypeArguments();
if (types.length == 0) {
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... annotations) {
+ final StringBuilder sb = new StringBuilder();
+ if (annotations.length == 0) {
+ return sb.append(ref);
+ }
+
+ final int dot = ref.lastIndexOf('.');
+ if (dot != -1) {
+ sb.append(ref, 0, dot + 1);
+ }
+ for (String annotation : annotations) {
+ sb.append('@').append(annotation).append(' ');
+ }
+
+ return sb.append(ref, dot + 1, ref.length());
+ }
}
import org.opendaylight.mdsal.binding.model.api.Constant
import org.opendaylight.mdsal.binding.model.api.Enumeration
import org.opendaylight.mdsal.binding.model.api.GeneratedType
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName
import org.opendaylight.mdsal.binding.model.api.MethodSignature
+import org.opendaylight.mdsal.binding.model.api.Type
import org.opendaylight.mdsal.binding.model.util.TypeConstants
/**
* Template for generating JAVA interfaces.
*/
class InterfaceTemplate extends BaseTemplate {
+ static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable")
/**
* List of constant instances which are generated as JAVA public static final attributes.
def private generateMethods() '''
«IF !methods.empty»
«FOR m : methods SEPARATOR "\n"»
- «IF !m.isAccessor»
- «m.comment.asJavadoc»
- «ELSE»
- «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
+ «val accessor = m.isAccessor»
+ «val ret = m.returnType»
+ «IF accessor»
+ «formatDataForJavaDoc(m, "@return " + asCode(ret.fullyQualifiedName) + " "
+ asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
+ «ELSE»
+ «m.comment.asJavadoc»
«ENDIF»
«m.annotations.generateAnnotations»
- «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
+ «nullableType(ret, accessor)» «m.name»(«m.parameters.generateParameters»);
«ENDFOR»
«ENDIF»
'''
-}
+ def private String nullableType(Type type, boolean accessor) {
+ if (accessor && type.isObject) {
+ return importedName(type, NULLABLE.importedName)
+ }
+ return type.importedName
+ }
+ def private static boolean isObject(Type type) {
+ // The return type has a package, so it's not a primitive type
+ return !type.getPackageName().isEmpty()
+ }
+}
.collect(Collectors.joining());
}
- final String importedName(final Type intype) {
- return javaType.getReferenceString(intype);
+ final String importedName(final Type intype, final String... annotations) {
+ return javaType.getReferenceString(intype, annotations);
}
final String importedName(final Class<?> cls) {
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
-import javax.annotation.Nullable;
import org.junit.Test;
import org.opendaylight.yangtools.yang.binding.ChildOf;
import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
throw new AssertionError("Method getId() not found", e);
}
- assertEquals(ImmutableSet.of(RoutingContext.class, Nullable.class), Arrays.stream(getId.getAnnotations())
+ assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
.map(Annotation::annotationType).collect(Collectors.toSet()));
CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
}