Search in sources :

Example 6 with ClassElement

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

the class JavaVisitorContext method getClassElements.

@Override
@NonNull
public ClassElement[] getClassElements(@NonNull String aPackage, @NonNull String... stereotypes) {
    ArgumentUtils.requireNonNull("aPackage", aPackage);
    ArgumentUtils.requireNonNull("stereotypes", stereotypes);
    final PackageElement packageElement = elements.getPackageElement(aPackage);
    if (packageElement != null) {
        List<ClassElement> classElements = new ArrayList<>();
        populateClassElements(stereotypes, packageElement, classElements);
        return classElements.toArray(new ClassElement[0]);
    }
    return new ClassElement[0];
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement) PackageElement(javax.lang.model.element.PackageElement) NonNull(io.micronaut.core.annotation.NonNull)

Example 7 with ClassElement

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

the class ModelUtils method findNonPrivateStaticCreators.

private List<ExecutableElement> findNonPrivateStaticCreators(TypeElement classElement, AnnotationUtils annotationUtils) {
    List<? extends Element> enclosedElements = classElement.getEnclosedElements();
    List<ExecutableElement> staticCreators = ElementFilter.methodsIn(enclosedElements).stream().filter(method -> method.getModifiers().contains(STATIC)).filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> typeUtils.isAssignable(typeUtils.erasure(method.getReturnType()), classElement.asType())).filter(method -> {
        final AnnotationMetadata annotationMetadata = annotationUtils.getAnnotationMetadata(method);
        return annotationMetadata.hasStereotype(Creator.class);
    }).collect(Collectors.toList());
    if (staticCreators.isEmpty()) {
        TypeElement companionClass = ElementFilter.typesIn(enclosedElements).stream().filter(type -> type.getSimpleName().toString().equals("Companion")).filter(type -> type.getModifiers().contains(STATIC)).findFirst().orElse(null);
        if (companionClass != null) {
            staticCreators = ElementFilter.methodsIn(companionClass.getEnclosedElements()).stream().filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> method.getReturnType().equals(classElement.asType())).filter(method -> {
                final AnnotationMetadata annotationMetadata = annotationUtils.getAnnotationMetadata(method);
                return annotationMetadata.hasStereotype(Creator.class);
            }).collect(Collectors.toList());
        } else if (classElement.getKind() == ElementKind.ENUM) {
            staticCreators = ElementFilter.methodsIn(classElement.getEnclosedElements()).stream().filter(method -> method.getModifiers().contains(STATIC)).filter(method -> !method.getModifiers().contains(PRIVATE)).filter(method -> method.getReturnType().equals(classElement.asType())).filter(method -> method.getSimpleName().toString().equals("valueOf")).collect(Collectors.toList());
        }
    }
    return staticCreators;
}
Also used : JavaModelUtils(io.micronaut.inject.processing.JavaModelUtils) java.util(java.util) Creator(io.micronaut.core.annotation.Creator) Modifier(javax.lang.model.element.Modifier) ClassElement(io.micronaut.inject.ast.ClassElement) Internal(io.micronaut.core.annotation.Internal) Types(javax.lang.model.util.Types) Elements(javax.lang.model.util.Elements) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationUtil(io.micronaut.core.annotation.AnnotationUtil) Nullable(io.micronaut.core.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NONE(javax.lang.model.type.TypeKind.NONE) ElementFilter(javax.lang.model.util.ElementFilter) NameUtils(io.micronaut.core.naming.NameUtils) javax.lang.model.element(javax.lang.model.element) Creator(io.micronaut.core.annotation.Creator) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 8 with ClassElement

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

the class JavaClassElement method getTypeArguments.

@Override
@NonNull
public Map<String, ClassElement> getTypeArguments() {
    List<? extends TypeParameterElement> typeParameters = classElement.getTypeParameters();
    Iterator<? extends TypeParameterElement> tpi = typeParameters.iterator();
    Map<String, ClassElement> map = new LinkedHashMap<>();
    while (tpi.hasNext()) {
        TypeParameterElement tpe = tpi.next();
        ClassElement classElement = mirrorToClassElement(tpe.asType(), visitorContext, this.genericTypeInfo, visitorContext.getConfiguration().includeTypeLevelAnnotationsInGenericArguments());
        map.put(tpe.toString(), classElement);
    }
    return Collections.unmodifiableMap(map);
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement) ArrayableClassElement(io.micronaut.inject.ast.ArrayableClassElement) LinkedHashMap(java.util.LinkedHashMap) TypeParameterElement(javax.lang.model.element.TypeParameterElement) NonNull(io.micronaut.core.annotation.NonNull)

Example 9 with ClassElement

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

the class JavaClassElement method getTypeArguments.

@NonNull
@Override
public Map<String, ClassElement> getTypeArguments(@NonNull String type) {
    if (StringUtils.isNotEmpty(type)) {
        Map<String, Map<String, TypeMirror>> data = visitorContext.getGenericUtils().buildGenericTypeArgumentElementInfo(classElement, null, getBoundTypeMirrors());
        Map<String, TypeMirror> forType = data.get(type);
        if (forType != null) {
            Map<String, ClassElement> typeArgs = new LinkedHashMap<>(forType.size());
            for (Map.Entry<String, TypeMirror> entry : forType.entrySet()) {
                TypeMirror v = entry.getValue();
                ClassElement ce = v != null ? mirrorToClassElement(v, visitorContext, Collections.emptyMap(), visitorContext.getConfiguration().includeTypeLevelAnnotationsInGenericArguments()) : null;
                if (ce == null) {
                    return Collections.emptyMap();
                } else {
                    typeArgs.put(entry.getKey(), ce);
                }
            }
            return Collections.unmodifiableMap(typeArgs);
        }
    }
    return Collections.emptyMap();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ClassElement(io.micronaut.inject.ast.ClassElement) ArrayableClassElement(io.micronaut.inject.ast.ArrayableClassElement) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) NonNull(io.micronaut.core.annotation.NonNull)

Example 10 with ClassElement

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

the class JavaBeanDefinitionBuilder method createChildBean.

@Override
protected AbstractBeanDefinitionBuilder createChildBean(FieldElement producerField) {
    final ClassElement parentType = getBeanType();
    return new JavaBeanDefinitionBuilder(JavaBeanDefinitionBuilder.this.getOriginatingElement(), producerField.getGenericField().getType(), JavaBeanDefinitionBuilder.this.metadataBuilder, (JavaVisitorContext) JavaBeanDefinitionBuilder.this.visitorContext) {

        @Override
        public Element getProducingElement() {
            return producerField;
        }

        @Override
        public ClassElement getDeclaringElement() {
            return producerField.getDeclaringType();
        }

        @Override
        protected BeanDefinitionWriter createBeanDefinitionWriter() {
            final BeanDefinitionWriter writer = super.createBeanDefinitionWriter();
            final JavaElementFactory elementFactory = ((JavaVisitorContext) visitorContext).getElementFactory();
            final VariableElement variableElement = (VariableElement) producerField.getNativeType();
            writer.visitBeanFactoryField(parentType, elementFactory.newFieldElement(parentType, variableElement, new AnnotationMetadataHierarchy(parentType.getDeclaredMetadata(), producerField.getDeclaredMetadata())));
            return writer;
        }
    };
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement) VariableElement(javax.lang.model.element.VariableElement) BeanDefinitionWriter(io.micronaut.inject.writer.BeanDefinitionWriter) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

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