Search in sources :

Example 1 with PrimitiveRef

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

Example 2 with PrimitiveRef

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;
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Element(javax.lang.model.element.Element) Method(io.sundr.model.Method) PrimitiveRef(io.sundr.model.PrimitiveRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef)

Example 3 with PrimitiveRef

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 + "].");
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) VoidRef(io.sundr.model.VoidRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) WildcardRef(io.sundr.model.WildcardRef) PrimitiveRef(io.sundr.model.PrimitiveRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ReferenceType(com.github.javaparser.ast.type.ReferenceType) TypeParamRef(io.sundr.model.TypeParamRef) WildcardType(com.github.javaparser.ast.type.WildcardType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType)

Example 4 with PrimitiveRef

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);
}
Also used : TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef) PrimitiveRef(io.sundr.model.PrimitiveRef)

Aggregations

PrimitiveRef (io.sundr.model.PrimitiveRef)4 ClassRef (io.sundr.model.ClassRef)3 TypeDef (io.sundr.model.TypeDef)3 TypeRef (io.sundr.model.TypeRef)3 RichTypeDef (io.sundr.model.RichTypeDef)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 ReferenceType (com.github.javaparser.ast.type.ReferenceType)1 VoidType (com.github.javaparser.ast.type.VoidType)1 WildcardType (com.github.javaparser.ast.type.WildcardType)1 AnnotationRef (io.sundr.model.AnnotationRef)1 ClassRefBuilder (io.sundr.model.ClassRefBuilder)1 Method (io.sundr.model.Method)1 PrimitiveRefBuilder (io.sundr.model.PrimitiveRefBuilder)1 Property (io.sundr.model.Property)1 TypeParamRef (io.sundr.model.TypeParamRef)1 TypeParamRefBuilder (io.sundr.model.TypeParamRefBuilder)1 VoidRef (io.sundr.model.VoidRef)1 WildcardRef (io.sundr.model.WildcardRef)1 Element (javax.lang.model.element.Element)1