use of io.sundr.model.PrimitiveRef in project sundrio by sundrio.
the class AbstractAdapterTest method testWithPrimitiveArray.
@Test
public void testWithPrimitiveArray() {
T input = getInput(ClassWithPrimitiveArray.class);
TypeDef typeDef = Adapters.adaptType(input, getContext());
assertNotNull(typeDef);
assertEquals("ClassWithPrimitiveArray", typeDef.getName());
assertEquals(1, typeDef.getProperties().size());
TypeRef typeRef = typeDef.getProperties().iterator().next().getTypeRef();
assertTrue(typeRef instanceof PrimitiveRef);
assertEquals(1, ((PrimitiveRef) typeRef).getDimensions());
}
use of io.sundr.model.PrimitiveRef in project sundrio by sundrio.
the class ToPojo method getDefaultValue.
private static String getDefaultValue(Attributeable attributeable) {
Object value = attributeable.getAttribute(DEFAULT_VALUE);
String stringVal = value != null ? String.valueOf(value) : null;
TypeRef typeRef = null;
if (attributeable instanceof Method) {
typeRef = ((Method) attributeable).getReturnType();
} else if (attributeable instanceof Property) {
typeRef = ((Property) attributeable).getTypeRef();
} else {
throw new IllegalArgumentException("Method 'getDefaultValue' accepts Property or Method as argument!");
}
if (typeRef.getDimensions() > 0) {
StringBuilder sb = new StringBuilder();
if (typeRef instanceof PrimitiveRef) {
sb.append(((PrimitiveRef) typeRef).getName());
} else if (typeRef instanceof ClassRef) {
sb.append("new ").append(((ClassRef) typeRef).getFullyQualifiedName());
}
for (int d = 0; d < typeRef.getDimensions(); d++) {
sb.append("[0]");
}
return sb.toString();
}
if (Types.STRING_REF.equals(typeRef) && value != null && !String.valueOf(value).startsWith("\"")) {
return "\"" + value + "\"";
} else if (value instanceof Element) {
Element element = (Element) value;
if (element.getKind() == ElementKind.ENUM_CONSTANT) {
return element.getEnclosingElement() + "." + element.getSimpleName();
}
} else if (stringVal != null && stringVal.startsWith("@")) {
String annotationFQCN = stringVal.substring(1);
TypeDef annotationDef = DefinitionRepository.getRepository().getDefinition(annotationFQCN);
if (annotationDef != null) {
TypeDef pojoDef = ClazzAs.POJO.apply(TypeArguments.apply(annotationDef));
Optional<AnnotationRef> pojoAnnotation = getPojoAnnotation(pojoDef);
Optional<Method> builderFromDefaults = getBuilderFromDefaults(pojoDef);
if (pojoAnnotation.isPresent() && builderFromDefaults.isPresent()) {
return pojoDef.getFullyQualifiedName() + "." + builderFromDefaults.get().getName() + "().build()";
} else if (BuilderUtils.hasDefaultConstructor(pojoDef)) {
return "new " + pojoDef.getFullyQualifiedName() + "()";
}
}
return "null";
} else if (stringVal == null && typeRef instanceof PrimitiveRef) {
if (((PrimitiveRef) typeRef).getName().equals("boolean")) {
return "false";
} else {
return "0";
}
}
return stringVal;
}
use of io.sundr.model.PrimitiveRef in project sundrio by sundrio.
the class TypeToTypeRef method apply.
@Override
public TypeRef apply(Type type) {
if (type instanceof VoidType) {
return new VoidRef();
} else if (type instanceof WildcardType) {
return new WildcardRef();
} else if (type instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) type;
int dimensions = referenceType.getArrayCount();
TypeRef typeRef = apply(referenceType.getType());
if (dimensions == 0) {
return typeRef;
} else if (typeRef instanceof ClassRef) {
return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof PrimitiveRef) {
return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof TypeParamRef) {
return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
}
} else if (type instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) type;
return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
} else if (type instanceof ClassOrInterfaceType) {
return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
}
throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
use of io.sundr.model.PrimitiveRef in project sundrio by sundrio.
the class ClassAssignable method from.
public Boolean from(TypeRef other) {
if (t == other || t.equals(other)) {
return true;
} else if (other == null) {
return false;
} else if (other instanceof PrimitiveRef) {
TypeDef definition = GetDefinition.of(t);
if (definition == null) {
return false;
}
if (definition != null && !Node.JAVA_LANG.equals(definition.getPackageName())) {
return false;
}
if (!t.getName().toUpperCase().startsWith(((PrimitiveRef) other).getName().toUpperCase())) {
return false;
}
return true;
}
if (!(other instanceof ClassRef)) {
return false;
}
ClassRef otherClassRef = (ClassRef) other;
if (otherClassRef.getFullyQualifiedName().equals(t.getFullyQualifiedName())) {
return true;
}
TypeDef definition = GetDefinition.of(t);
TypeDef otherDefinition = GetDefinition.of(otherClassRef);
return Assignable.isAssignable(definition).from(otherDefinition);
}
Aggregations