Search in sources :

Example 1 with ClassElement

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

the class ClientTypesRule method validate.

@Override
public RouteValidationResult validate(List<UriMatchTemplate> templates, ParameterElement[] parameters, MethodElement method) {
    String[] errors = new String[] {};
    if (method.hasAnnotation(Client.class)) {
        final Stream.Builder<ClassElement> builder = Stream.<ClassElement>builder().add(method.getReturnType());
        for (ParameterElement param : method.getParameters()) {
            builder.add(param.getType());
        }
        errors = builder.build().filter(type -> {
            for (Class<?> clazz : SERVER_TYPES) {
                if (type.isAssignable(clazz)) {
                    return true;
                }
            }
            return false;
        }).map(type -> "The type [" + type + "] must not be used in declarative client methods. The type is specific to server based usages.").toArray(String[]::new);
    }
    return new RouteValidationResult(errors);
}
Also used : List(java.util.List) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult) Stream(java.util.stream.Stream) MultipartBody(io.micronaut.http.server.multipart.MultipartBody) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) MethodElement(io.micronaut.inject.ast.MethodElement) UriMatchTemplate(io.micronaut.http.uri.UriMatchTemplate) ClassElement(io.micronaut.inject.ast.ClassElement) Client(io.micronaut.http.client.annotation.Client) SystemFile(io.micronaut.http.server.types.files.SystemFile) ParameterElement(io.micronaut.inject.ast.ParameterElement) Stream(java.util.stream.Stream) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) RouteValidationResult(io.micronaut.validation.routes.RouteValidationResult)

Example 2 with ClassElement

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

the class WebSocketVisitor method isInvalidParameter.

private boolean isInvalidParameter(List<String> variables, ParameterElement parameter, String... validTypes) {
    String parameterName = parameter.getName();
    ClassElement parameterType = parameter.getType();
    return !parameter.hasStereotype(Bindable.class) && !variables.contains(parameterName) && Arrays.stream(validTypes).noneMatch(parameterType::isAssignable);
}
Also used : Bindable(io.micronaut.core.bind.annotation.Bindable) ClassElement(io.micronaut.inject.ast.ClassElement)

Example 3 with ClassElement

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

the class AbstractGroovyElement method getGenericElement.

/**
 * Get a generic element for the given element and data.
 * @param sourceUnit The source unit
 * @param type The type
 * @param rawElement A raw element to fall back to
 * @param genericsSpec The generics spec
 * @return The element, never null.
 */
@NonNull
protected ClassElement getGenericElement(@NonNull SourceUnit sourceUnit, @NonNull ClassNode type, @NonNull ClassElement rawElement, @NonNull Map<String, ClassNode> genericsSpec) {
    if (CollectionUtils.isNotEmpty(genericsSpec)) {
        ClassElement classNode = resolveGenericType(genericsSpec, type);
        if (classNode != null) {
            return classNode;
        } else {
            GenericsType[] genericsTypes = type.getGenericsTypes();
            GenericsType[] redirectTypes = type.redirect().getGenericsTypes();
            if (genericsTypes != null && redirectTypes != null) {
                genericsSpec = alignNewGenericsInfo(genericsTypes, redirectTypes, genericsSpec);
                return new GroovyClassElement(visitorContext, type, annotationMetadata, Collections.singletonMap(type.getName(), genericsSpec), 0);
            }
        }
    }
    return rawElement;
}
Also used : GenericsType(org.codehaus.groovy.ast.GenericsType) ClassElement(io.micronaut.inject.ast.ClassElement) NonNull(io.micronaut.core.annotation.NonNull)

Example 4 with ClassElement

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

the class GroovyVisitorContext method getClassElement.

@Override
public Optional<ClassElement> getClassElement(Class<?> type) {
    final ClassNode classNode = ClassHelper.makeCached(type);
    final AnnotationMetadata annotationMetadata = AstAnnotationUtils.getAnnotationMetadata(sourceUnit, compilationUnit, classNode);
    final ClassElement classElement = groovyElementFactory.newClassElement(classNode, annotationMetadata);
    return Optional.of(classElement);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ClassElement(io.micronaut.inject.ast.ClassElement) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 5 with ClassElement

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

the class JavaVisitorContext method populateClassElements.

private void populateClassElements(@NonNull String[] stereotypes, PackageElement packageElement, List<ClassElement> classElements) {
    final List<? extends Element> enclosedElements = packageElement.getEnclosedElements();
    boolean includeAll = Arrays.equals(stereotypes, new String[] { "*" });
    for (Element enclosedElement : enclosedElements) {
        if (enclosedElement instanceof TypeElement) {
            final AnnotationMetadata annotationMetadata = annotationUtils.getAnnotationMetadata(enclosedElement);
            if (includeAll || Arrays.stream(stereotypes).anyMatch(annotationMetadata::hasStereotype)) {
                JavaClassElement classElement = elementFactory.newClassElement((TypeElement) enclosedElement, annotationMetadata);
                if (!classElement.isAbstract()) {
                    classElements.add(classElement);
                }
            }
        } else if (enclosedElement instanceof PackageElement) {
            populateClassElements(stereotypes, (PackageElement) enclosedElement, classElements);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) ClassElement(io.micronaut.inject.ast.ClassElement) TypeElement(javax.lang.model.element.TypeElement) BeanElement(io.micronaut.inject.ast.beans.BeanElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

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