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");
}
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();
}
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;
}
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();
}
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;
}
Aggregations