Search in sources :

Example 21 with Property

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

the class ClassToTypeDef method getMethods.

private Set<Method> getMethods(Class item, Set<Class> references) {
    Set<Method> methods = new HashSet<Method>();
    for (java.lang.reflect.Method method : item.getDeclaredMethods()) {
        List<AnnotationRef> annotationRefs = new ArrayList<>();
        List<ClassRef> exceptionRefs = new ArrayList<>();
        List<Property> arguments = new ArrayList<Property>();
        List<TypeParamDef> parameters = new ArrayList<TypeParamDef>();
        processMethod(references, method, annotationRefs, exceptionRefs, arguments, parameters);
        Map<AttributeKey, Object> attributes = new HashMap<>();
        if (method.getDefaultValue() != null) {
            attributes.put(Attributeable.DEFAULT_VALUE, method.getDefaultValue());
        }
        methods.add(new MethodBuilder().withName(method.getName()).withDefaultMethod(method.isDefault()).withModifiers(method.getModifiers()).withReturnType(typeToTypeRef.apply(method.getGenericReturnType())).withArguments(arguments).withParameters(parameters).withExceptions(exceptionRefs).withAnnotations(annotationRefs).withAttributes(attributes).build());
    }
    return methods;
}
Also used : TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) MethodBuilder(io.sundr.model.MethodBuilder) AttributeKey(io.sundr.model.AttributeKey) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) HashSet(java.util.HashSet)

Example 22 with Property

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

the class ClassTo method getProperties.

private static Set<Property> getProperties(Class item, Set<Class> references) {
    Set<Property> properties = new HashSet<Property>();
    for (Field field : item.getDeclaredFields()) {
        List<AnnotationRef> annotationRefs = new ArrayList<AnnotationRef>();
        processAnnotatedElement(field, annotationRefs);
        if (field.getGenericType() instanceof Class) {
            references.add((Class) field.getGenericType());
        }
        // If property contains generic bounds, we need to process them too.
        if (field.getGenericType() instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedType) field.getGenericType();
            references.addAll(Stream.of(p.getActualTypeArguments()).filter(t -> t instanceof Class).map(t -> (Class) t).filter(c -> !item.equals(c)).collect(Collectors.toList()));
        }
        properties.add(new PropertyBuilder().withName(field.getName()).withModifiers(field.getModifiers()).withAnnotations(annotationRefs).withTypeRef(TYPEREF.apply(field.getGenericType())).build());
    }
    return properties;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) FunctionFactory(io.sundr.FunctionFactory) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) AnnotationRefBuilder(io.sundr.model.AnnotationRefBuilder) WildcardType(java.lang.reflect.WildcardType) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) Attributeable(io.sundr.model.Attributeable) ArrayList(java.util.ArrayList) AttributeKey(io.sundr.model.AttributeKey) ClassRef(io.sundr.model.ClassRef) HashSet(java.util.HashSet) Map(java.util.Map) WildcardRefBuilder(io.sundr.model.WildcardRefBuilder) VoidRefBuilder(io.sundr.model.VoidRefBuilder) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) TypeVariable(java.lang.reflect.TypeVariable) Set(java.util.Set) PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) Method(io.sundr.model.Method) TypeRef(io.sundr.model.TypeRef) Field(java.lang.reflect.Field) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Collectors(java.util.stream.Collectors) Property(io.sundr.model.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Stream(java.util.stream.Stream) MethodBuilder(io.sundr.model.MethodBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) TypeDef(io.sundr.model.TypeDef) PropertyBuilder(io.sundr.model.PropertyBuilder) Pattern(java.util.regex.Pattern) TypeParamDef(io.sundr.model.TypeParamDef) AnnotatedElement(java.lang.reflect.AnnotatedElement) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) HashSet(java.util.HashSet) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 23 with Property

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

the class ClassTo method getMethods.

private static Set<Method> getMethods(Class item, Set<Class> references) {
    Set<Method> methods = new HashSet<Method>();
    for (java.lang.reflect.Method method : item.getDeclaredMethods()) {
        List<AnnotationRef> annotationRefs = new ArrayList<>();
        List<ClassRef> exceptionRefs = new ArrayList<>();
        List<Property> arguments = new ArrayList<Property>();
        List<TypeParamDef> parameters = new ArrayList<TypeParamDef>();
        processMethod(references, method, annotationRefs, exceptionRefs, arguments, parameters);
        Map<AttributeKey, Object> attributes = new HashMap<>();
        if (method.getDefaultValue() != null) {
            attributes.put(Attributeable.DEFAULT_VALUE, String.valueOf(method.getDefaultValue()));
        }
        methods.add(new MethodBuilder().withName(method.getName()).withDefaultMethod(method.isDefault()).withModifiers(method.getModifiers()).withReturnType(TYPEREF.apply(method.getReturnType())).withArguments(arguments).withParameters(parameters).withExceptions(exceptionRefs).withAnnotations(annotationRefs).withAttributes(attributes).build());
    }
    return methods;
}
Also used : TypeParamDef(io.sundr.model.TypeParamDef) ClassRef(io.sundr.model.ClassRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) MethodBuilder(io.sundr.model.MethodBuilder) AttributeKey(io.sundr.model.AttributeKey) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) HashSet(java.util.HashSet)

Example 24 with Property

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

the class TypeDeclarationToTypeDef method apply.

@Override
public TypeDef apply(TypeDeclaration type) {
    if (type instanceof ClassOrInterfaceDeclaration) {
        ClassOrInterfaceDeclaration decl = (ClassOrInterfaceDeclaration) type;
        Kind kind = decl.isInterface() ? Kind.INTERFACE : Kind.CLASS;
        List<TypeParamDef> parameters = new ArrayList<TypeParamDef>();
        List<ClassRef> extendsList = new ArrayList<ClassRef>();
        List<ClassRef> implementsList = new ArrayList<ClassRef>();
        List<Property> properties = new ArrayList<Property>();
        List<Method> methods = new ArrayList<Method>();
        List<Method> constructors = new ArrayList<Method>();
        List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
        for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
            annotations.add(ANNOTATIONREF.apply(annotationExpr));
        }
        for (TypeParameter typeParameter : decl.getTypeParameters()) {
            parameters.add(typeParameterToTypeParamDef.apply(typeParameter));
        }
        for (ClassOrInterfaceType classOrInterfaceType : decl.getExtends()) {
            extendsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
        }
        for (ClassOrInterfaceType classOrInterfaceType : decl.getImplements()) {
            implementsList.add((ClassRef) classOrInterfaceToTypeRef.apply(classOrInterfaceType));
        }
        for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
            if (bodyDeclaration instanceof FieldDeclaration) {
                FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
                for (VariableDeclarator var : fieldDeclaration.getVariables()) {
                    TypeRef fieldDeclRef = typeToTypeRef.apply(fieldDeclaration.getType());
                    TypeRef typeRef = checkAgainstTypeParamRef(fieldDeclRef, parameters);
                    properties.add(new PropertyBuilder().withName(var.getId().getName()).withTypeRef(typeRef).withModifiers(fieldDeclaration.getModifiers()).addToAttributes(Attributeable.INIT, var.getInit() != null ? var.getInit().toStringWithoutComments() : null).build());
                }
            } else if (bodyDeclaration instanceof MethodDeclaration) {
                MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
                List<Property> arguments = new ArrayList<Property>();
                List<ClassRef> exceptions = new ArrayList<ClassRef>();
                List<AnnotationRef> methodAnnotations = new ArrayList<AnnotationRef>();
                for (AnnotationExpr annotationExpr : methodDeclaration.getAnnotations()) {
                    methodAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                }
                for (ReferenceType referenceType : methodDeclaration.getThrows()) {
                    TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
                    if (exceptionRef instanceof ClassRef) {
                        exceptions.add((ClassRef) exceptionRef);
                    }
                }
                Boolean preferVarArg = false;
                for (Parameter parameter : methodDeclaration.getParameters()) {
                    List<AnnotationRef> paramAnnotations = new ArrayList<AnnotationRef>();
                    for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
                        paramAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                    }
                    TypeRef typeRef = typeToTypeRef.apply(parameter.getType());
                    if (parameter.isVarArgs()) {
                        preferVarArg = true;
                        typeRef = typeRef.withDimensions(typeRef.getDimensions() + 1);
                    }
                    arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(paramAnnotations).build());
                }
                List<TypeParamDef> typeParamDefs = new ArrayList<TypeParamDef>();
                for (TypeParameter typeParameter : methodDeclaration.getTypeParameters()) {
                    typeParamDefs.add(typeParameterToTypeParamDef.apply(typeParameter));
                }
                TypeRef returnType = checkAgainstTypeParamRef(typeToTypeRef.apply(methodDeclaration.getType()), parameters);
                methods.add(new MethodBuilder().withName(methodDeclaration.getName()).withDefaultMethod(methodDeclaration.isDefault()).withModifiers(methodDeclaration.getModifiers()).withParameters(typeParamDefs).withVarArgPreferred(preferVarArg).withReturnType(returnType).withExceptions(exceptions).withArguments(arguments).withAnnotations(methodAnnotations).withBlock(BLOCK.apply(methodDeclaration.getBody())).build());
            } else if (bodyDeclaration instanceof ConstructorDeclaration) {
                ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) bodyDeclaration;
                List<Property> arguments = new ArrayList<Property>();
                List<ClassRef> exceptions = new ArrayList<ClassRef>();
                List<AnnotationRef> ctorAnnotations = new ArrayList<AnnotationRef>();
                for (AnnotationExpr annotationExpr : constructorDeclaration.getAnnotations()) {
                    ctorAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                }
                for (ReferenceType referenceType : constructorDeclaration.getThrows()) {
                    TypeRef exceptionRef = typeToTypeRef.apply(referenceType.getType());
                    exceptions.add((ClassRef) exceptionRef);
                }
                for (Parameter parameter : constructorDeclaration.getParameters()) {
                    List<AnnotationRef> ctorParamAnnotations = new ArrayList<AnnotationRef>();
                    for (AnnotationExpr annotationExpr : parameter.getAnnotations()) {
                        ctorParamAnnotations.add(ANNOTATIONREF.apply(annotationExpr));
                    }
                    TypeRef typeRef = checkAgainstTypeParamRef(typeToTypeRef.apply(parameter.getType()), parameters);
                    arguments.add(new PropertyBuilder().withName(parameter.getId().getName()).withTypeRef(typeRef).withModifiers(parameter.getModifiers()).withAnnotations(ctorParamAnnotations).build());
                }
                constructors.add(new MethodBuilder().withModifiers(constructorDeclaration.getModifiers()).withExceptions(exceptions).withArguments(arguments).withAnnotations(ctorAnnotations).withBlock(BLOCK.apply(constructorDeclaration.getBlock())).build());
            }
        }
        return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withParameters(parameters).withExtendsList(extendsList).withImplementsList(implementsList).withProperties(properties).withMethods(methods).withConstructors(constructors).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
    }
    if (type instanceof AnnotationDeclaration) {
        AnnotationDeclaration decl = (AnnotationDeclaration) type;
        Kind kind = Kind.ANNOTATION;
        List<Method> methods = new ArrayList<Method>();
        for (BodyDeclaration bodyDeclaration : decl.getMembers()) {
            if (bodyDeclaration instanceof AnnotationMemberDeclaration) {
                Map<AttributeKey, Object> attributes = new HashMap<>();
                AnnotationMemberDeclaration annotationMemberDeclaration = (AnnotationMemberDeclaration) bodyDeclaration;
                if (annotationMemberDeclaration.getDefaultValue() != null) {
                    attributes.put(Attributeable.DEFAULT_VALUE, annotationMemberDeclaration.getDefaultValue().toString());
                }
                TypeRef returnType = typeToTypeRef.apply(annotationMemberDeclaration.getType());
                methods.add(new MethodBuilder().withName(annotationMemberDeclaration.getName()).withModifiers(annotationMemberDeclaration.getModifiers()).withReturnType(returnType).withAttributes(attributes).build());
            }
        }
        List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
        for (AnnotationExpr annotationExpr : decl.getAnnotations()) {
            annotations.add(ANNOTATIONREF.apply(annotationExpr));
        }
        return context.getDefinitionRepository().register(new TypeDefBuilder().withKind(kind).withPackageName(PACKAGENAME.apply(type)).withName(decl.getName()).withModifiers(type.getModifiers()).withMethods(methods).withAnnotations(annotations).addToAttributes(TypeDef.ALSO_IMPORT, IMPORTS.apply(type)).build());
    }
    throw new IllegalArgumentException("Unsupported TypeDeclaration:[" + type + "].");
}
Also used : TypeParamDef(io.sundr.model.TypeParamDef) TypeParameter(com.github.javaparser.ast.TypeParameter) ClassRef(io.sundr.model.ClassRef) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeDefBuilder(io.sundr.model.TypeDefBuilder) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ReferenceType(com.github.javaparser.ast.type.ReferenceType) MethodBuilder(io.sundr.model.MethodBuilder) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Kind(io.sundr.model.Kind) ArrayList(java.util.ArrayList) List(java.util.List) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) PropertyBuilder(io.sundr.model.PropertyBuilder) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Method(io.sundr.model.Method) AttributeKey(io.sundr.model.AttributeKey) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.TypeParameter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration)

Example 25 with Property

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

the class AbstractAdapterTest method testClassWithAnnotationParams.

@Test
public void testClassWithAnnotationParams() {
    T input = getInput(ClassWithAnnotation.class);
    TypeDef typeDef = Adapters.adaptType(input, getContext());
    final List<Property> properties = typeDef.getProperties();
    final Property foo = properties.stream().filter(p -> p.getName().equals("foo")).findFirst().orElseThrow(RuntimeException::new);
    List<AnnotationRef> annotations = foo.getAnnotations();
    assertEquals(1, annotations.size());
    AnnotationRef annotationRef = annotations.get(0);
    assertTrue(annotationRef.toString().contains("SimpleAnnotation"));
    assertEquals("foo", annotationRef.getParameters().get("name"));
    final Method bar = typeDef.getMethods().stream().filter(p -> p.getName().equals("bar")).findFirst().orElseThrow(RuntimeException::new);
    annotations = bar.getAnnotations();
    assertEquals(1, annotations.size());
    annotationRef = annotations.get(0);
    assertEquals("bar", annotationRef.getParameters().get("name"));
    Object params = annotationRef.getParameters().get("values");
    assertArrayEquals(new int[] { 1, 2, 3, 5, 7 }, (int[]) annotationRef.getParameters().get("values"));
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef) Test(org.junit.Test)

Aggregations

Property (io.sundr.model.Property)25 TypeDef (io.sundr.model.TypeDef)18 ClassRef (io.sundr.model.ClassRef)17 ArrayList (java.util.ArrayList)16 Method (io.sundr.model.Method)15 TypeDefBuilder (io.sundr.model.TypeDefBuilder)13 AnnotationRef (io.sundr.model.AnnotationRef)11 TypeRef (io.sundr.model.TypeRef)10 MethodBuilder (io.sundr.model.MethodBuilder)9 HashMap (java.util.HashMap)9 TypeParamDef (io.sundr.model.TypeParamDef)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 AttributeKey (io.sundr.model.AttributeKey)7 PropertyBuilder (io.sundr.model.PropertyBuilder)7 HashSet (java.util.HashSet)7 Map (java.util.Map)7 Kind (io.sundr.model.Kind)6 RichTypeDef (io.sundr.model.RichTypeDef)6 Statement (io.sundr.model.Statement)6