Search in sources :

Example 1 with PropertyBuilder

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

the class ClassToTypeDef method processMethod.

private void processMethod(Set<Class> references, java.lang.reflect.Executable method, List<AnnotationRef> annotationRefs, List<ClassRef> exceptionRefs, List<Property> arguments, List<TypeParamDef> parameters) {
    processAnnotatedElement(method, annotationRefs);
    for (Class exceptionType : method.getExceptionTypes()) {
        exceptionRefs.add((ClassRef) typeToTypeRef.apply(exceptionType));
    }
    for (int i = 1; i <= method.getGenericParameterTypes().length; i++) {
        Type argumentType = method.getGenericParameterTypes()[i - 1];
        arguments.add(new PropertyBuilder().withName(ARGUMENT_PREFIX + i).withTypeRef(typeToTypeRef.apply(argumentType)).build());
        if (argumentType instanceof Class) {
            references.add((Class) argumentType);
        }
    }
    for (Type type : method.getGenericParameterTypes()) {
        TypeParamDef typeParamDef = typeToTypeParamDef.apply(type);
        if (typeParamDef != null) {
            parameters.add(typeParamDef);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeParamDef(io.sundr.model.TypeParamDef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 2 with PropertyBuilder

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

the class ClassToTypeDef method getProperties.

private 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(typeToTypeRef.apply(field.getGenericType())).build());
    }
    return properties;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) AnnotationRefBuilder(io.sundr.model.AnnotationRefBuilder) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) AdapterContext(io.sundr.adapter.api.AdapterContext) 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) TypeVariable(java.lang.reflect.TypeVariable) Set(java.util.Set) 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) Stream(java.util.stream.Stream) MethodBuilder(io.sundr.model.MethodBuilder) ParameterizedType(java.lang.reflect.ParameterizedType) 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 3 with PropertyBuilder

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

the class ClassTo method processMethod.

private static void processMethod(Set<Class> references, java.lang.reflect.Executable method, List<AnnotationRef> annotationRefs, List<ClassRef> exceptionRefs, List<Property> arguments, List<TypeParamDef> parameters) {
    processAnnotatedElement(method, annotationRefs);
    for (Class exceptionType : method.getExceptionTypes()) {
        exceptionRefs.add((ClassRef) TYPEREF.apply(exceptionType));
    }
    for (int i = 1; i <= method.getGenericParameterTypes().length; i++) {
        Type argumentType = method.getGenericParameterTypes()[i - 1];
        arguments.add(new PropertyBuilder().withName(ARGUMENT_PREFIX + i).withTypeRef(TYPEREF.apply(argumentType)).build());
        if (argumentType instanceof Class) {
            references.add((Class) argumentType);
        }
    }
    for (Type type : method.getGenericParameterTypes()) {
        TypeParamDef typeParamDef = TYPEPARAMDEF.apply(type);
        if (typeParamDef != null) {
            parameters.add(typeParamDef);
        }
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeParamDef(io.sundr.model.TypeParamDef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 4 with PropertyBuilder

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

the class VariableElementToProperty method apply.

public Property apply(final VariableElement variableElement) {
    String name = variableElement.getSimpleName().toString();
    TypeRef type = referenceAdapterFunction.apply(variableElement.asType());
    List<AnnotationRef> annotations = new ArrayList<AnnotationRef>();
    for (AnnotationMirror annotationMirror : variableElement.getAnnotationMirrors()) {
        annotations.add(annotationAdapterFunction.apply(annotationMirror));
    }
    String comments = context.getElements().getDocComment(variableElement);
    List<String> commentList = Strings.isNullOrEmpty(comments) ? new ArrayList<>() : Arrays.stream(comments.split(NEWLINE_PATTERN)).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    return new PropertyBuilder().withComments(commentList).withName(name).withTypeRef(type).withAnnotations(annotations).withModifiers(Types.modifiersToInt(variableElement.getModifiers())).build();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList) AnnotationRef(io.sundr.model.AnnotationRef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 5 with PropertyBuilder

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

the class BuildableProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Elements elements = processingEnv.getElementUtils();
    Types types = processingEnv.getTypeUtils();
    Filer filer = processingEnv.getFiler();
    BuilderContext ctx = null;
    // First pass register all buildables
    Set<TypeDef> buildables = new HashSet<>();
    for (TypeElement typeElement : annotations) {
        for (Element element : env.getElementsAnnotatedWith(typeElement)) {
            Buildable buildable = element.getAnnotation(Buildable.class);
            if (buildable == null) {
                continue;
            }
            AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
            ctx = BuilderContextManager.create(elements, types, buildable.validationEnabled(), buildable.generateBuilderPackage(), buildable.builderPackage());
            TypeDef b = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {

                @Override
                public void visit(PropertyBuilder builder) {
                    builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
                    builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
                }
            }).build();
            ctx.getDefinitionRepository().register(b);
            ctx.getBuildableRepository().register(b);
            buildables.add(b);
            for (TypeElement ref : BuilderUtils.getBuildableReferences(ctx, buildable)) {
                TypeDef r = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(ref), aptContext)).addToAttributes(BUILDABLE, buildable).addToAttributes(EDITABLE_ENABLED, buildable.editableEnabled()).addToAttributes(VALIDATION_ENABLED, buildable.validationEnabled()).accept(new Visitor<PropertyBuilder>() {

                    @Override
                    public void visit(PropertyBuilder builder) {
                        builder.addToAttributes(LAZY_COLLECTIONS_INIT_ENABLED, buildable.lazyCollectionInitEnabled());
                        builder.addToAttributes(LAZY_MAP_INIT_ENABLED, buildable.lazyMapInitEnabled());
                    }
                }).build();
                ctx.getDefinitionRepository().register(r);
                ctx.getBuildableRepository().register(r);
                buildables.add(r);
            }
        }
    }
    if (ctx == null) {
        return true;
    }
    generateLocalDependenciesIfNeeded();
    ctx.getDefinitionRepository().updateReferenceMap();
    generateBuildables(ctx, buildables);
    generatePojos(ctx, buildables);
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format("%-120s", "100%: Builder generation complete."));
    return false;
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) AptContext(io.sundr.adapter.apt.AptContext) Visitor(io.sundr.builder.Visitor) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Elements(javax.lang.model.util.Elements) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) BuilderContext(io.sundr.builder.internal.BuilderContext) Filer(javax.annotation.processing.Filer) Buildable(io.sundr.builder.annotations.Buildable) HashSet(java.util.HashSet) PropertyBuilder(io.sundr.model.PropertyBuilder)

Aggregations

PropertyBuilder (io.sundr.model.PropertyBuilder)13 TypeRef (io.sundr.model.TypeRef)9 TypeDefBuilder (io.sundr.model.TypeDefBuilder)8 ClassRef (io.sundr.model.ClassRef)7 TypeDef (io.sundr.model.TypeDef)7 ArrayList (java.util.ArrayList)7 AnnotationRef (io.sundr.model.AnnotationRef)5 Method (io.sundr.model.Method)5 MethodBuilder (io.sundr.model.MethodBuilder)5 Property (io.sundr.model.Property)5 TypeParamDef (io.sundr.model.TypeParamDef)5 HashSet (java.util.HashSet)5 AttributeKey (io.sundr.model.AttributeKey)4 ClassRefBuilder (io.sundr.model.ClassRefBuilder)4 TypedVisitor (io.sundr.builder.TypedVisitor)3 AnnotationRefBuilder (io.sundr.model.AnnotationRefBuilder)3 Attributeable (io.sundr.model.Attributeable)3 Kind (io.sundr.model.Kind)3 Annotation (java.lang.annotation.Annotation)3 ParameterizedType (java.lang.reflect.ParameterizedType)3