Search in sources :

Example 66 with ClassRef

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());
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef) ClassWithParam(io.sundr.adapter.testing.general.ClassWithParam) TypeRef(io.sundr.model.TypeRef) Test(org.junit.Test)

Example 67 with ClassRef

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();
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) WildcardRef(io.sundr.model.WildcardRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ReferenceType(com.github.javaparser.ast.type.ReferenceType) TypeParamRef(io.sundr.model.TypeParamRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReferenceType(com.github.javaparser.ast.type.ReferenceType) Type(com.github.javaparser.ast.type.Type) WildcardType(com.github.javaparser.ast.type.WildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) WildcardRefBuilder(io.sundr.model.WildcardRefBuilder)

Example 68 with ClassRef

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(com.github.javaparser.ast.CompilationUnit) QualifiedNameExpr(com.github.javaparser.ast.expr.QualifiedNameExpr) ClassRef(io.sundr.model.ClassRef) NamedNode(com.github.javaparser.ast.NamedNode) Node(com.github.javaparser.ast.Node) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) QualifiedNameExpr(com.github.javaparser.ast.expr.QualifiedNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) NamedNode(com.github.javaparser.ast.NamedNode)

Aggregations

ClassRef (io.sundr.model.ClassRef)68 TypeDef (io.sundr.model.TypeDef)41 TypeRef (io.sundr.model.TypeRef)35 RichTypeDef (io.sundr.model.RichTypeDef)22 ClassRefBuilder (io.sundr.model.ClassRefBuilder)21 ArrayList (java.util.ArrayList)21 Property (io.sundr.model.Property)19 Method (io.sundr.model.Method)18 TypeDefBuilder (io.sundr.model.TypeDefBuilder)18 Test (org.junit.Test)17 List (java.util.List)12 AnnotationRef (io.sundr.model.AnnotationRef)11 MethodBuilder (io.sundr.model.MethodBuilder)11 Collectors (java.util.stream.Collectors)11 TypedVisitor (io.sundr.builder.TypedVisitor)10 TypeParamDef (io.sundr.model.TypeParamDef)10 LinkedHashSet (java.util.LinkedHashSet)10 PropertyBuilder (io.sundr.model.PropertyBuilder)9 Map (java.util.Map)9 AttributeKey (io.sundr.model.AttributeKey)8