Search in sources :

Example 21 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder 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 22 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder 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

ClassRefBuilder (io.sundr.model.ClassRefBuilder)22 ClassRef (io.sundr.model.ClassRef)18 TypeRef (io.sundr.model.TypeRef)13 ArrayList (java.util.ArrayList)8 TypedVisitor (io.sundr.builder.TypedVisitor)6 TypeDef (io.sundr.model.TypeDef)6 TypeParamRef (io.sundr.model.TypeParamRef)6 TypeDefBuilder (io.sundr.model.TypeDefBuilder)5 AttributeKey (io.sundr.model.AttributeKey)3 Property (io.sundr.model.Property)3 PropertyBuilder (io.sundr.model.PropertyBuilder)3 RichTypeDef (io.sundr.model.RichTypeDef)3 LinkedHashSet (java.util.LinkedHashSet)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 Construct (io.sundr.builder.internal.functions.Construct)2 PrimitiveRefBuilder (io.sundr.model.PrimitiveRefBuilder)2 PropertyFluent (io.sundr.model.PropertyFluent)2 TypeParamRefBuilder (io.sundr.model.TypeParamRefBuilder)2