Search in sources :

Example 16 with ClassElement

use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.

the class AbstractBeanDefinitionBuilder method resolveTypeArguments.

@Nullable
private Map<String, ClassElement> resolveTypeArguments(Map<String, ClassElement> typeArguments, ClassElement... types) {
    Map<String, ClassElement> resolvedTypes = null;
    if (typeArguments.size() == types.length) {
        resolvedTypes = new LinkedHashMap<>(typeArguments.size());
        final Iterator<String> i = typeArguments.keySet().iterator();
        for (ClassElement type : types) {
            final String variable = i.next();
            resolvedTypes.put(variable, type);
        }
    }
    return resolvedTypes;
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement) Nullable(io.micronaut.core.annotation.Nullable)

Example 17 with ClassElement

use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.

the class AbstractBeanDefinitionBuilder method produceBeans.

@Override
public <E extends MemberElement> BeanElementBuilder produceBeans(ElementQuery<E> methodsOrFields, Consumer<BeanElementBuilder> childBeanBuilder) {
    methodsOrFields = methodsOrFields.onlyConcrete().onlyInstance().modifiers((modifiers) -> modifiers.contains(ElementModifier.PUBLIC));
    final List<E> enclosedElements = this.beanType.getEnclosedElements(methodsOrFields);
    for (E enclosedElement : enclosedElements) {
        if (enclosedElement instanceof FieldElement) {
            FieldElement fe = (FieldElement) enclosedElement;
            final ClassElement type = fe.getGenericField().getType();
            if (type.isPublic() && !type.isPrimitive()) {
                return addChildBean(fe, childBeanBuilder);
            }
        }
        if (enclosedElement instanceof MethodElement && !(enclosedElement instanceof ConstructorElement)) {
            MethodElement me = (MethodElement) enclosedElement;
            final ClassElement type = me.getGenericReturnType().getType();
            if (type.isPublic() && !type.isPrimitive()) {
                return addChildBean(me, childBeanBuilder);
            }
        }
    }
    return this;
}
Also used : ElementQuery(io.micronaut.inject.ast.ElementQuery) Arrays(java.util.Arrays) ArrayUtils(io.micronaut.core.util.ArrayUtils) FieldElement(io.micronaut.inject.ast.FieldElement) BeanFieldElement(io.micronaut.inject.ast.beans.BeanFieldElement) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) Internal(io.micronaut.core.annotation.Internal) ConstructorElement(io.micronaut.inject.ast.ConstructorElement) BeanMethodElement(io.micronaut.inject.ast.beans.BeanMethodElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) PreDestroy(javax.annotation.PreDestroy) ElementFactory(io.micronaut.inject.ast.ElementFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) ElementModifier(io.micronaut.inject.ast.ElementModifier) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) Property(io.micronaut.context.annotation.Property) Element(io.micronaut.inject.ast.Element) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Set(java.util.Set) IOException(java.io.IOException) Value(io.micronaut.context.annotation.Value) BeanParameterElement(io.micronaut.inject.ast.beans.BeanParameterElement) MemberElement(io.micronaut.inject.ast.MemberElement) Bean(io.micronaut.context.annotation.Bean) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) VisitorContext(io.micronaut.inject.visitor.VisitorContext) List(java.util.List) BeanConstructorElement(io.micronaut.inject.ast.beans.BeanConstructorElement) AnnotationValueBuilder(io.micronaut.core.annotation.AnnotationValueBuilder) BeanElementBuilder(io.micronaut.inject.ast.beans.BeanElementBuilder) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) MethodElement(io.micronaut.inject.ast.MethodElement) ConfigurationMetadataBuilder(io.micronaut.inject.configuration.ConfigurationMetadataBuilder) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Comparator(java.util.Comparator) Collections(java.util.Collections) MutableAnnotationMetadata(io.micronaut.inject.annotation.MutableAnnotationMetadata) ConstructorElement(io.micronaut.inject.ast.ConstructorElement) BeanConstructorElement(io.micronaut.inject.ast.beans.BeanConstructorElement) FieldElement(io.micronaut.inject.ast.FieldElement) BeanFieldElement(io.micronaut.inject.ast.beans.BeanFieldElement) BeanMethodElement(io.micronaut.inject.ast.beans.BeanMethodElement) MethodElement(io.micronaut.inject.ast.MethodElement) ClassElement(io.micronaut.inject.ast.ClassElement)

Example 18 with ClassElement

use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.

the class AbstractClassFileWriter method buildArgumentWithGenerics.

/**
 * Builds generic type arguments recursively.
 *
 * @param generatorAdapter   The generator adapter to use
 * @param type               The type that declares the generics
 * @param annotationMetadata The annotation metadata reference
 * @param generics           The generics
 * @since 3.0.0
 */
protected static void buildArgumentWithGenerics(GeneratorAdapter generatorAdapter, Type type, AnnotationMetadataReference annotationMetadata, ClassElement[] generics) {
    // 1st argument: the type
    generatorAdapter.push(type);
    // 2nd argument: the annotation metadata
    AnnotationMetadataWriter.pushAnnotationMetadataReference(generatorAdapter, annotationMetadata);
    // 3rd argument, the generics
    pushNewArray(generatorAdapter, Class.class, generics.length);
    final int len = generics.length;
    for (int i = 0; i < len; i++) {
        ClassElement generic = generics[i];
        pushStoreInArray(generatorAdapter, i, len, () -> generatorAdapter.push(getTypeReference(generic)));
    }
    // Argument.create( .. )
    invokeInterfaceStaticMethod(generatorAdapter, Argument.class, METHOD_CREATE_ARGUMENT_WITH_ANNOTATION_METADATA_CLASS_GENERICS);
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement)

Example 19 with ClassElement

use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.

the class AbstractClassFileWriter method invokeMethod.

/**
 * Invokes the given method.
 *
 * @param generatorAdapter The generator adapter
 * @param method           The method to invoke
 * @return The return type
 */
@NonNull
protected ClassElement invokeMethod(@NonNull GeneratorAdapter generatorAdapter, @NonNull MethodElement method) {
    ClassElement returnType = method.getReturnType();
    Method targetMethod = new Method(method.getName(), getMethodDescriptor(returnType, Arrays.asList(method.getParameters())));
    ClassElement declaringElement = method.getDeclaringType();
    Type declaringType = JavaModelUtils.getTypeReference(declaringElement);
    if (method.isStatic()) {
        generatorAdapter.invokeStatic(declaringType, targetMethod);
    } else if (declaringElement.isInterface()) {
        generatorAdapter.invokeInterface(declaringType, targetMethod);
    } else {
        generatorAdapter.invokeVirtual(declaringType, targetMethod);
    }
    return returnType;
}
Also used : Type(org.objectweb.asm.Type) ClassElement(io.micronaut.inject.ast.ClassElement) Method(org.objectweb.asm.commons.Method) NonNull(io.micronaut.core.annotation.NonNull)

Example 20 with ClassElement

use of io.micronaut.inject.ast.ClassElement in project micronaut-core by micronaut-projects.

the class AbstractClassFileWriter method getTypes.

/**
 * @param types The types
 * @return An array with the {@link Type} of the objects
 */
protected Type[] getTypes(Collection<ClassElement> types) {
    Type[] converted = new Type[types.size()];
    Iterator<ClassElement> iter = types.iterator();
    for (int i = 0; i < converted.length; i++) {
        ClassElement type = iter.next();
        converted[i] = JavaModelUtils.getTypeReference(type);
    }
    return converted;
}
Also used : Type(org.objectweb.asm.Type) ClassElement(io.micronaut.inject.ast.ClassElement)

Aggregations

ClassElement (io.micronaut.inject.ast.ClassElement)55 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)15 NonNull (io.micronaut.core.annotation.NonNull)15 ParameterElement (io.micronaut.inject.ast.ParameterElement)14 Map (java.util.Map)13 MethodElement (io.micronaut.inject.ast.MethodElement)12 Type (org.objectweb.asm.Type)12 LinkedHashMap (java.util.LinkedHashMap)10 DefaultAnnotationMetadata (io.micronaut.inject.annotation.DefaultAnnotationMetadata)9 ArrayList (java.util.ArrayList)9 List (java.util.List)8 HashMap (java.util.HashMap)7 Internal (io.micronaut.core.annotation.Internal)6 Argument (io.micronaut.core.type.Argument)6 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)6 MutableAnnotationMetadata (io.micronaut.inject.annotation.MutableAnnotationMetadata)6 Collections (java.util.Collections)6 Set (java.util.Set)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 NameUtils (io.micronaut.core.naming.NameUtils)5