Search in sources :

Example 21 with Method

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

the class ToPojo method readPrimitiveArrayProperty.

/**
 * Returns the string representation of the code that reads a primitive array property.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readPrimitiveArrayProperty(String ref, TypeDef source, Property property) {
    StringBuilder sb = new StringBuilder();
    Method getter = getterOf(source, property);
    sb.append("Arrays.asList(").append(ref).append(".").append(getter.getName()).append("())").append(".stream()").append(".collect(Collectors.toList())).toArray(new " + getter.getReturnType().toString() + "[])");
    return sb.toString();
}
Also used : Method(io.sundr.model.Method)

Example 22 with Method

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

the class ToPojo method readProperty.

/**
 * Returns the string representation of the code that given a reference of the specified type, reads the specified property.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readProperty(String ref, TypeDef source, Property property) {
    TypeRef propertyTypeRef = property.getTypeRef();
    Method getter = getterOf(source, property);
    if (getter == null) {
        return "null";
    }
    TypeRef getterTypeRef = getter.getReturnType();
    if (propertyTypeRef.getDimensions() == getterTypeRef.getDimensions() && Assignable.isAssignable(propertyTypeRef).from(getterTypeRef)) {
        return readObjectProperty(ref, source, property);
    }
    if (property.getTypeRef().getDimensions() > 0) {
        return readArrayProperty(ref, source, property);
    }
    if (property.getTypeRef() instanceof ClassRef && GetDefinition.of((ClassRef) getterTypeRef).isAnnotation()) {
        return readAnnotationProperty(ref + "." + getter.getName() + "()", GetDefinition.of((ClassRef) getterTypeRef), property);
    }
    return readObjectProperty(ref, source, property);
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Method(io.sundr.model.Method)

Example 23 with Method

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

the class ToPojo method readMapValue.

/**
 * Returns the string representation of the code that given a reference of the specified type, reads the specified property.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readMapValue(String ref, TypeDef source, Property property) {
    TypeRef propertyTypeRef = property.getTypeRef();
    Method getter = getterOf(source, property);
    if (getter == null) {
        return "null";
    }
    TypeRef getterTypeRef = getter.getReturnType();
    if (propertyTypeRef.getDimensions() == getterTypeRef.getDimensions() && Assignable.isAssignable(propertyTypeRef).from(getterTypeRef)) {
        return readObjectValue(ref, source, property);
    }
    if (property.getTypeRef().getDimensions() > 0) {
        return readArrayValue(ref, source, property);
    }
    if (getterTypeRef instanceof ClassRef) {
        TypeDef getterTypeDef = GetDefinition.of((ClassRef) getterTypeRef);
        if (getterTypeDef.isEnum()) {
            return readEnumValue(ref, source, property);
        }
        if (getterTypeDef.isAnnotation()) {
            return readAnnotationValue("((Map)(" + ref + " instanceof Map ? ((Map)" + ref + ").get(\"" + getterOf(source, property).getName() + "\") : " + getDefaultValue(property) + "))", GetDefinition.of((ClassRef) getterTypeRef), property);
        }
    }
    return readObjectValue(ref, source, property);
}
Also used : ClassRef(io.sundr.model.ClassRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) Method(io.sundr.model.Method)

Example 24 with Method

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

the class ClazzAsTest method testToFluent.

@Test
public void testToFluent() {
    TypeDef type = new TypeDefBuilder().withName("MyClass").withPackageName(getClass().getPackage().getName()).withParameters().build();
    Method constructor = new MethodBuilder().withReturnType(type.toReference()).withAnnotations(Constants.BUILDABLE_ANNOTATION).build();
    type = new TypeDefBuilder(type).withConstructors(constructor).build();
    TypeDef result = ClazzAs.FLUENT_IMPL.apply(TypeArguments.apply(type));
    System.out.println(result);
}
Also used : TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) AbstractProcessorTest(io.sundr.builder.internal.processor.AbstractProcessorTest) Test(org.junit.Test)

Example 25 with Method

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

the class TypeElementToTypeDef method apply.

@Override
public TypeDef apply(TypeElement classElement) {
    // Check SuperClass
    Kind kind = Kind.CLASS;
    Element enclosing = classElement.getEnclosingElement();
    if (enclosing != null && ANY.equals(enclosing.getSimpleName().toString())) {
        throw new SundrException("Failed to read class element:" + classElement.getQualifiedName().toString() + ". " + Messages.POTENTIAL_UNRESOLVED_SYMBOL);
    }
    TypeMirror superClass = classElement.getSuperclass();
    TypeRef superClassType = TypeDef.OBJECT_REF;
    if (superClass == null) {
    // ignore
    } else if (superClass instanceof NoType) {
    // ignore
    } else if (superClass.toString().equals(TypeDef.OBJECT.getFullyQualifiedName())) {
    // ignore
    } else {
        superClassType = referenceAdapterFunction.apply(superClass);
    }
    List<TypeParamDef> genericTypes = new ArrayList<TypeParamDef>();
    List<ClassRef> interfaces = new ArrayList<ClassRef>();
    if (classElement.getKind() == ElementKind.INTERFACE) {
        kind = Kind.INTERFACE;
    } else if (classElement.getKind() == ElementKind.CLASS) {
        kind = Kind.CLASS;
    } else if (classElement.getKind() == ElementKind.ANNOTATION_TYPE) {
        kind = Kind.ANNOTATION;
    } else if (classElement.getKind() == ElementKind.ENUM) {
        kind = Kind.ENUM;
    }
    String comments = AptContext.getContext().getElements().getDocComment(classElement);
    List<String> commentList = Strings.isNullOrEmpty(comments) ? new ArrayList<>() : Arrays.stream(comments.split(NEWLINE_PATTERN)).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    for (TypeMirror interfaceTypeMirrror : classElement.getInterfaces()) {
        TypeRef interfaceType = referenceAdapterFunction.apply(interfaceTypeMirrror);
        if (interfaceType instanceof ClassRef) {
            interfaces.add((ClassRef) interfaceType);
        } else {
            throw new IllegalStateException("Interface: [" + interfaceType + "] not mapped to a class ref.");
        }
    }
    for (TypeParameterElement typeParameter : classElement.getTypeParameters()) {
        TypeParamDef genericType = typeParamAdapterFunction.apply(typeParameter);
        genericTypes.add(genericType);
    }
    TypeDef baseType = new TypeDefBuilder().withComments(commentList).withKind(kind).withModifiers(Types.modifiersToInt(classElement.getModifiers())).withPackageName(getPackageName(classElement)).withName(getClassName(classElement)).withParameters(genericTypes).withExtendsList(superClassType instanceof ClassRef ? (ClassRef) superClassType : null).withImplementsList(interfaces).withOuterTypeName(classElement.getEnclosingElement() instanceof TypeElement ? classElement.getEnclosingElement().toString() : null).build();
    // We will register the base type first and will replace it with the full blown version later.
    context.getDefinitionRepository().registerIfAbsent(baseType);
    List<TypeDef> innerTypes = new ArrayList<TypeDef>();
    for (TypeElement innerElement : ElementFilter.typesIn(classElement.getEnclosedElements())) {
        TypeDef innerType = context.getDefinitionRepository().register(apply(innerElement));
        if (innerType == null) {
            throw new IllegalStateException("Inner type for:" + innerElement + " is null");
        }
        innerType = new TypeDefBuilder(innerType).withOuterTypeName(baseType.getFullyQualifiedName()).build();
        context.getDefinitionRepository().register(innerType);
        innerTypes.add(innerType);
    }
    TypeDefBuilder builder = new TypeDefBuilder(baseType).withInnerTypes(innerTypes);
    for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) {
        builder.addToConstructors(methodAdapterFunction.apply(constructor));
    }
    // Populate Fields
    for (VariableElement variableElement : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
        builder.addToProperties(propertyAdapterFunction.apply(variableElement));
    }
    Set<ExecutableElement> allMethods = new LinkedHashSet<ExecutableElement>();
    allMethods.addAll(ElementFilter.methodsIn(classElement.getEnclosedElements()));
    allMethods.addAll(getInheritedMethods(classElement));
    for (ExecutableElement method : allMethods) {
        builder.addToMethods(methodAdapterFunction.apply(method));
    }
    for (AnnotationMirror annotationMirror : classElement.getAnnotationMirrors()) {
        builder.addToAnnotations(annotationAdapterFunction.apply(annotationMirror));
    }
    // Let's register the full blown definition
    TypeDef result = context.getDefinitionRepository().register(builder.build());
    // Also register other types
    if (context.isDeep()) {
        Set<TypeElement> references = new HashSet<>(context.getReferences());
        references.stream().filter(t -> !t.equals(classElement)).filter(t -> !t.toString().startsWith("sun.") && !t.toString().startsWith("com.sun.")).forEach(t -> {
            String fqcn = t.toString();
            TypeDef existing = context.getDefinitionRepository().getDefinition(fqcn);
            if (existing == null) {
                context.getDefinitionRepository().registerIfAbsent(fqcn, () -> apply(t));
            }
            context.getReferences().remove(t);
        });
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) Apt.getPackageName(io.sundr.adapter.apt.utils.Apt.getPackageName) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ClassRef(io.sundr.model.ClassRef) HashSet(java.util.HashSet) ElementFilter(javax.lang.model.util.ElementFilter) Types(io.sundr.model.utils.Types) LinkedHashSet(java.util.LinkedHashSet) Strings(io.sundr.utils.Strings) ElementKind(javax.lang.model.element.ElementKind) NoType(javax.lang.model.type.NoType) ExecutableElement(javax.lang.model.element.ExecutableElement) SundrException(io.sundr.SundrException) Set(java.util.Set) Element(javax.lang.model.element.Element) Method(io.sundr.model.Method) TypeRef(io.sundr.model.TypeRef) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Property(io.sundr.model.Property) TypeParameterElement(javax.lang.model.element.TypeParameterElement) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Apt.getClassName(io.sundr.adapter.apt.utils.Apt.getClassName) TypeDef(io.sundr.model.TypeDef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) TypeMirror(javax.lang.model.type.TypeMirror) ElementKind(javax.lang.model.element.ElementKind) Kind(io.sundr.model.Kind) SundrException(io.sundr.SundrException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NoType(javax.lang.model.type.NoType) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Aggregations

Method (io.sundr.model.Method)30 TypeDef (io.sundr.model.TypeDef)21 ClassRef (io.sundr.model.ClassRef)20 Property (io.sundr.model.Property)18 TypeRef (io.sundr.model.TypeRef)17 ArrayList (java.util.ArrayList)17 AnnotationRef (io.sundr.model.AnnotationRef)14 TypeDefBuilder (io.sundr.model.TypeDefBuilder)14 MethodBuilder (io.sundr.model.MethodBuilder)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeParamDef (io.sundr.model.TypeParamDef)9 HashSet (java.util.HashSet)9 List (java.util.List)9 PropertyBuilder (io.sundr.model.PropertyBuilder)8 Set (java.util.Set)8 Collectors (java.util.stream.Collectors)8 AttributeKey (io.sundr.model.AttributeKey)7 Statement (io.sundr.model.Statement)7 StringStatement (io.sundr.model.StringStatement)7 Types (io.sundr.model.utils.Types)7