Search in sources :

Example 1 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.

the class TypeToTypeRef method apply.

@Override
public TypeRef apply(Type item) {
    if (item == null) {
        return new VoidRefBuilder().build();
    } else if (item instanceof WildcardType) {
        return new WildcardRefBuilder().withBounds(Arrays.asList(((WildcardType) item).getLowerBounds()).stream().map(t -> apply(t)).collect(Collectors.toList())).build();
    } else if (item instanceof TypeVariable) {
        return new TypeParamRefBuilder().withName(((TypeVariable) item).getName()).build();
    } else if (item instanceof GenericArrayType) {
        Type target = item;
        int dimensions = 0;
        while (target instanceof GenericArrayType) {
            target = ((GenericArrayType) target).getGenericComponentType();
            dimensions++;
        }
        if (target instanceof Class) {
            references.add((Class) target);
        }
        TypeRef targetRef = apply(target);
        return targetRef.withDimensions(dimensions + targetRef.getDimensions());
    } else if (item instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) item;
        Type rawType = parameterizedType.getRawType();
        List<TypeRef> arguments = new ArrayList<TypeRef>();
        for (Type arg : parameterizedType.getActualTypeArguments()) {
            arguments.add(apply(arg));
            if (arg instanceof Class) {
                references.add((Class) arg);
            }
        }
        if (rawType instanceof Class) {
            references.add((Class) rawType);
        }
        return new ClassRefBuilder((ClassRef) apply(rawType)).withArguments(arguments).build();
    } else if (Object.class.equals(item)) {
        return ClassRef.OBJECT;
    } else if (item instanceof Class) {
        Class c = (Class) item;
        if (c.isArray()) {
            Class target = c;
            int dimensions = 0;
            while (target.isArray()) {
                target = ((Class) target).getComponentType();
                dimensions++;
            }
            TypeRef targetRef = apply(target);
            references.add(target);
            return targetRef.withDimensions(dimensions + targetRef.getDimensions());
        }
        if (c.isPrimitive()) {
            return new PrimitiveRefBuilder().withName(c.getName()).withDimensions(0).build();
        } else {
            List<TypeRef> arguments = new ArrayList<TypeRef>();
            for (TypeVariable v : c.getTypeParameters()) {
                arguments.add(apply(v));
            }
            references.add((Class) item);
            String fqcn = c.getName().replaceAll(Pattern.quote("$"), ".");
            return new ClassRefBuilder().withFullyQualifiedName(fqcn).withArguments(arguments).build();
        }
    }
    throw new IllegalArgumentException("Can't convert type:" + item + " to a TypeRef");
}
Also used : PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) VoidRefBuilder(io.sundr.model.VoidRefBuilder) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) WildcardRefBuilder(io.sundr.model.WildcardRefBuilder)

Example 2 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.

the class TypeRefTypeVisitor method visitDeclared.

public TypeRef visitDeclared(DeclaredType t, Integer dimension) {
    List<TypeRef> arguments = new ArrayList<TypeRef>();
    for (TypeMirror typeMirror : t.getTypeArguments()) {
        TypeRef arg = typeMirror.accept(this, dimension);
        if (arg != null) {
            arguments.add(arg);
        }
    }
    TypeElement element = (TypeElement) t.asElement();
    // TODO: need a cleaner way to get this registered.
    if (!context.getDefinitionRepository().hasDefinition(element.toString())) {
        context.getReferences().add(element);
    }
    String fqcn = element.toString();
    return new ClassRefBuilder().withFullyQualifiedName(fqcn).withDimensions(dimension).withArguments(arguments).build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeRef(io.sundr.model.TypeRef) TypeElement(javax.lang.model.element.TypeElement) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList)

Example 3 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.

the class TypeMirrorToTypeRef method apply.

@Override
public TypeRef apply(TypeMirror item) {
    if (item instanceof NoType) {
        return new VoidRef();
    }
    if (item == null) {
        throw new IllegalArgumentException("TypeMirror cannot be null.");
    }
    Element element = AptContext.getContext().getTypes().asElement(item);
    TypeRef typeRef = item.accept(new TypeRefTypeVisitor(context), 0);
    if (typeRef instanceof ClassRef && element instanceof TypeElement) {
        TypeElement typeElement = (TypeElement) element;
        String fqcn = typeElement.toString();
        context.getReferences().add((TypeElement) element);
        return new ClassRefBuilder((ClassRef) typeRef).withNewFullyQualifiedName(fqcn).build();
    }
    return typeRef;
}
Also used : ClassRef(io.sundr.model.ClassRef) NoType(javax.lang.model.type.NoType) TypeRef(io.sundr.model.TypeRef) TypeElement(javax.lang.model.element.TypeElement) VoidRef(io.sundr.model.VoidRef) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeRefTypeVisitor(io.sundr.adapter.apt.visitors.TypeRefTypeVisitor)

Example 4 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.

the class AbstractBuilderProcessor method inlineableOf.

static TypeDef inlineableOf(BuilderContext ctx, TypeDef type, Inline inline) {
    final String inlineableName = !inline.name().isEmpty() ? inline.name() : inline.prefix() + type.getName() + inline.suffix();
    List<Method> constructors = new ArrayList<Method>();
    final TypeDef builderType = TypeAs.BUILDER.apply(type);
    TypeDef inlineType = BuilderUtils.getInlineType(ctx, inline);
    TypeDef returnType = BuilderUtils.getInlineReturnType(ctx, inline, type);
    final ClassRef inlineTypeRef = inlineType.toReference(returnType.toReference());
    // Use the builder as the base of the inlineable. Just add interface and change name.
    final TypeDef shallowInlineType = new TypeDefBuilder(builderType).withName(inlineableName).withImplementsList(inlineTypeRef).withProperties().withMethods().withConstructors().build();
    TypeRef functionType = Constants.FUNCTION.toReference(type.toInternalReference(), returnType.toInternalReference());
    Property builderProperty = new PropertyBuilder().withTypeRef(TypeAs.BUILDER.apply(type).toInternalReference()).withName(BUILDER).withModifiers(Types.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)).build();
    Property functionProperty = new PropertyBuilder().withTypeRef(functionType).withName(FUNCTION).withModifiers(Types.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)).build();
    Method inlineMethod = new MethodBuilder().withReturnType(returnType.toInternalReference()).withName(inline.value()).withNewBlock().addNewStringStatementStatement(BUILD_AND_APPLY_FUNCTION).endBlock().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).build();
    constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(FUNCTION).withTypeRef(functionType).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BULDER_AND_SET_FUNCTION_FORMAT, builderType.getName())).endBlock().build());
    constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(ITEM).withTypeRef(type.toInternalReference()).and().addNewArgument().withName(FUNCTION).withTypeRef(functionType).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BULDER_WITH_ITEM_AND_SET_FUNCTION_FORMAT, builderType.getName())).endBlock().build());
    if (type.equals(returnType)) {
        constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(ITEM).withTypeRef(type.toInternalReference()).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BUILDER_AND_EMTPY_FUNCTION_FORMAT, builderType.getName(), String.format(EMPTY_FUNCTION_TEXT, type.toInternalReference(), returnType.toInternalReference(), returnType.toInternalReference(), type.toInternalReference()))).endBlock().build());
    }
    return new TypeDefBuilder(shallowInlineType).withAnnotations().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withConstructors(constructors).addToProperties(builderProperty, functionProperty).addToMethods(inlineMethod).accept(new TypedVisitor<ClassRefBuilder>() {

        @Override
        public void visit(ClassRefBuilder builder) {
            List<TypeRef> updatedArguments = new ArrayList<TypeRef>();
            for (TypeRef arg : builder.getArguments()) {
                if (arg.equals(builderType.toInternalReference())) {
                    updatedArguments.add(shallowInlineType.toInternalReference());
                } else {
                    updatedArguments.add(arg);
                }
            }
            builder.withArguments(updatedArguments);
        }
    }).build();
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 5 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(ClassRef classRef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    ClassRef ignored = new ClassRefBuilder(classRef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor)

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