use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class AbstractAdapterTest method testWithSuperClassParam.
@Test
public void testWithSuperClassParam() {
T input = getInput(ClassWithSuperClassParam.class);
TypeDef typeDef = Adapters.adaptType(input, getContext());
List<ClassRef> extendsList = typeDef.getExtendsList();
assertNotNull(extendsList);
assertEquals(1, extendsList.size());
ClassRef classRef = extendsList.get(0);
assertEquals(ClassWithParam.class.getName(), classRef.getFullyQualifiedName());
List<TypeRef> arguments = classRef.getArguments();
assertEquals(1, arguments.size());
assertEquals("java.lang.String", arguments.get(0).render());
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class ClassOrInterfaceToTypeRef method apply.
@Override
public TypeRef apply(ClassOrInterfaceType classOrInterfaceType) {
String boundPackage = PACKAGENAME.apply(classOrInterfaceType);
String boundName = classOrInterfaceType.getName();
List<TypeRef> arguments = new ArrayList<TypeRef>();
for (Type arg : classOrInterfaceType.getTypeArgs()) {
if (arg instanceof ReferenceType) {
// TODO: Need to check if this is valid for all cases...
ReferenceType referenceType = (ReferenceType) arg;
Type type = referenceType.getType();
int dimensions = referenceType.getArrayCount();
if (type instanceof ClassOrInterfaceType) {
TypeRef intermediateRef = apply((ClassOrInterfaceType) type);
if (intermediateRef instanceof ClassRef) {
arguments.add(new ClassRefBuilder((ClassRef) intermediateRef).withDimensions(dimensions).build());
} else if (intermediateRef instanceof TypeParamRef) {
arguments.add(new TypeParamRefBuilder((TypeParamRef) intermediateRef).withDimensions(dimensions).build());
} else {
throw new IllegalStateException("Expected class or type param reference");
}
} else {
String name = referenceType.toString();
arguments.add(new TypeParamRefBuilder().withName(name).withDimensions(dimensions).build());
}
} else if (arg instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) arg;
if (wildcardType.getExtends() != null) {
TypeRef bound = typeToTypeRef.apply(wildcardType.getExtends());
arguments.add(new WildcardRefBuilder().addToBounds(bound).build());
} else if (wildcardType.getSuper() != null) {
TypeRef bound = typeToTypeRef.apply(wildcardType.getSuper());
arguments.add(new WildcardRefBuilder().addToBounds(bound).withBoundKind(WildcardRef.BoundKind.SUPER).build());
} else {
arguments.add(new WildcardRef());
}
}
}
if (classOrInterfaceType.getParentNode() == classOrInterfaceType) {
return new TypeParamRefBuilder().withName(boundName).build();
}
String fqcn = boundPackage + "." + boundName;
// : knownDefinition.toReference(arguments);
if (classOrInterfaceType.getTypeArgs().isEmpty() && boundName.length() == 1) {
// parameter refs.
return new TypeParamRefBuilder().withName(boundName).build();
} else {
return new ClassRefBuilder().withFullyQualifiedName(fqcn).withArguments(arguments).build();
}
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class NodeToImports method apply.
@Override
public Set<ClassRef> apply(Node node) {
Set<ClassRef> imports = new LinkedHashSet<ClassRef>();
if (node instanceof NamedNode) {
String name = ((NamedNode) node).getName();
Node current = node;
while (!(current instanceof CompilationUnit)) {
current = current.getParentNode();
}
CompilationUnit compilationUnit = (CompilationUnit) current;
for (ImportDeclaration importDecl : compilationUnit.getImports()) {
String className = null;
String packageName = null;
NameExpr importExpr = importDecl.getName();
if (importExpr instanceof QualifiedNameExpr) {
QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr;
className = qualifiedNameExpr.getName();
packageName = qualifiedNameExpr.getQualifier().toString();
} else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) {
String importName = importDecl.getName().getName();
packageName = importName.substring(0, importName.length() - name.length() - 1);
}
if (className != null && !className.isEmpty()) {
imports.add(new ClassRefBuilder().withFullyQualifiedName(packageName + "." + className).build());
}
}
}
return imports;
}
Aggregations