Search in sources :

Example 26 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class XmlMetaEntity method parseOneToOne.

private void parseOneToOne(OneToOne oneToOne) {
    XmlMetaSingleAttribute attribute;
    ElementKind elementKind = getElementKind(oneToOne.getAccess());
    String type = getType(oneToOne.getName(), oneToOne.getTargetEntity(), elementKind);
    if (type != null) {
        attribute = new XmlMetaSingleAttribute(this, oneToOne.getName(), type);
        members.add(attribute);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Example 27 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class TypeUtils method getAccessTypeOfIdAnnotation.

private static AccessType getAccessTypeOfIdAnnotation(Element element) {
    AccessType accessType = null;
    final ElementKind kind = element.getKind();
    if (kind == ElementKind.FIELD || kind == ElementKind.METHOD) {
        accessType = kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY;
    }
    return accessType;
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Example 28 with ElementKind

use of javax.lang.model.element.ElementKind in project DeepLinkDispatch by airbnb.

the class DeepLinkProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Set<Element> customAnnotations = new HashSet<>();
    for (Element annotation : annotations) {
        if (annotation.getAnnotation(DEEP_LINK_SPEC_CLASS) != null) {
            customAnnotations.add(annotation);
        }
    }
    Map<Element, String[]> prefixes = new HashMap<>();
    Set<Element> customAnnotatedElements = new HashSet<>();
    for (Element customAnnotation : customAnnotations) {
        ElementKind kind = customAnnotation.getKind();
        if (kind != ElementKind.ANNOTATION_TYPE) {
            error(customAnnotation, "Only annotation types can be annotated with @%s", DEEP_LINK_SPEC_CLASS.getSimpleName());
        }
        String[] prefix = customAnnotation.getAnnotation(DEEP_LINK_SPEC_CLASS).prefix();
        if (Utils.hasEmptyOrNullString(prefix)) {
            error(customAnnotation, "Prefix property cannot have null or empty strings");
        }
        if (prefix.length == 0) {
            error(customAnnotation, "Prefix property cannot be empty");
        }
        prefixes.put(customAnnotation, prefix);
        for (Element customAnnotatedElement : roundEnv.getElementsAnnotatedWith(MoreElements.asType(customAnnotation))) {
            customAnnotatedElements.add(customAnnotatedElement);
        }
    }
    Set<Element> elementsToProcess = new HashSet<>(customAnnotatedElements);
    elementsToProcess.addAll(roundEnv.getElementsAnnotatedWith(DEEP_LINK_CLASS));
    List<DeepLinkAnnotatedElement> deepLinkElements = new ArrayList<>();
    for (Element element : elementsToProcess) {
        ElementKind kind = element.getKind();
        if (kind != ElementKind.METHOD && kind != ElementKind.CLASS) {
            error(element, "Only classes and methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
        }
        if (kind == ElementKind.METHOD) {
            Set<Modifier> methodModifiers = element.getModifiers();
            if (!methodModifiers.contains(Modifier.STATIC)) {
                error(element, "Only static methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
            }
        }
        DeepLink deepLinkAnnotation = element.getAnnotation(DEEP_LINK_CLASS);
        List<String> deepLinks = new ArrayList<>();
        if (deepLinkAnnotation != null) {
            deepLinks.addAll(Arrays.asList(deepLinkAnnotation.value()));
        }
        if (customAnnotatedElements.contains(element)) {
            deepLinks.addAll(enumerateCustomDeepLinks(element, prefixes));
        }
        DeepLinkEntry.Type type = kind == ElementKind.CLASS ? DeepLinkEntry.Type.CLASS : DeepLinkEntry.Type.METHOD;
        for (String deepLink : deepLinks) {
            try {
                deepLinkElements.add(new DeepLinkAnnotatedElement(deepLink, element, type));
            } catch (MalformedURLException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Malformed Deep Link URL " + deepLink);
            }
        }
    }
    Set<? extends Element> deepLinkHandlerElements = roundEnv.getElementsAnnotatedWith(DeepLinkHandler.class);
    for (Element deepLinkHandlerElement : deepLinkHandlerElements) {
        Optional<AnnotationMirror> annotationMirror = getAnnotationMirror(deepLinkHandlerElement, DeepLinkHandler.class);
        if (annotationMirror.isPresent()) {
            Iterable<TypeMirror> klasses = getTypeValue(annotationMirror.get(), "value");
            List<TypeElement> typeElements = FluentIterable.from(klasses).transform(new Function<TypeMirror, TypeElement>() {

                @Override
                public TypeElement apply(TypeMirror klass) {
                    return MoreTypes.asTypeElement(klass);
                }
            }).toList();
            String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkHandlerElement).getQualifiedName().toString();
            try {
                generateDeepLinkDelegate(packageName, typeElements);
            } catch (IOException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
            } catch (RuntimeException e) {
                messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
            }
        }
    }
    Set<? extends Element> deepLinkModuleElements = roundEnv.getElementsAnnotatedWith(DeepLinkModule.class);
    for (Element deepLinkModuleElement : deepLinkModuleElements) {
        String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkModuleElement).getQualifiedName().toString();
        try {
            generateDeepLinkLoader(packageName, deepLinkModuleElement.getSimpleName().toString(), deepLinkElements);
        } catch (IOException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
        } catch (RuntimeException e) {
            messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
        }
    }
    return false;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) ArrayList(java.util.ArrayList) Function(com.google.common.base.Function) TypeMirror(javax.lang.model.type.TypeMirror) Modifier(javax.lang.model.element.Modifier) HashSet(java.util.HashSet) TypeElement(javax.lang.model.element.TypeElement) IOException(java.io.IOException) MoreElements.getAnnotationMirror(com.google.auto.common.MoreElements.getAnnotationMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 29 with ElementKind

use of javax.lang.model.element.ElementKind in project spring-framework by spring-projects.

the class StandardStereotypesProvider method getStereotypes.

@Override
public Set<String> getStereotypes(Element element) {
    Set<String> stereotypes = new LinkedHashSet<>();
    ElementKind kind = element.getKind();
    if (kind != ElementKind.CLASS && kind != ElementKind.INTERFACE) {
        return stereotypes;
    }
    for (AnnotationMirror annotation : this.typeHelper.getAllAnnotationMirrors(element)) {
        String type = this.typeHelper.getType(annotation);
        if (type.startsWith("javax.")) {
            stereotypes.add(type);
        }
    }
    return stereotypes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ElementKind(javax.lang.model.element.ElementKind) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 30 with ElementKind

use of javax.lang.model.element.ElementKind in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method getFreeOrRawAnnotationOfSuperType.

/**
 * Returns a {@link UnderInitialization} annotation (or {@link UnknownInitialization} if rawness
 * is used) that has the supertype of {@code type} as type frame.
 */
protected AnnotationMirror getFreeOrRawAnnotationOfSuperType(TypeMirror type) {
    // Find supertype if possible.
    AnnotationMirror annotation;
    List<? extends TypeMirror> superTypes = types.directSupertypes(type);
    TypeMirror superClass = null;
    for (TypeMirror superType : superTypes) {
        ElementKind kind = types.asElement(superType).getKind();
        if (kind == ElementKind.CLASS) {
            superClass = superType;
            break;
        }
    }
    // Create annotation.
    if (superClass != null) {
        if (useFbc) {
            annotation = createFreeAnnotation(superClass);
        } else {
            annotation = createUnclassifiedAnnotation(superClass);
        }
    } else {
        // Use Object as a valid super-class
        if (useFbc) {
            annotation = createFreeAnnotation(Object.class);
        } else {
            annotation = createUnclassifiedAnnotation(Object.class);
        }
    }
    return annotation;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

ElementKind (javax.lang.model.element.ElementKind)50 TypeElement (javax.lang.model.element.TypeElement)27 Element (javax.lang.model.element.Element)25 ExecutableElement (javax.lang.model.element.ExecutableElement)23 VariableElement (javax.lang.model.element.VariableElement)13 TypeMirror (javax.lang.model.type.TypeMirror)13 PackageElement (javax.lang.model.element.PackageElement)9 ArrayList (java.util.ArrayList)8 AnnotationMirror (javax.lang.model.element.AnnotationMirror)7 LinkedHashSet (java.util.LinkedHashSet)6 ClassName (com.squareup.javapoet.ClassName)5 MethodSpec (com.squareup.javapoet.MethodSpec)5 TypeName (com.squareup.javapoet.TypeName)5 HashSet (java.util.HashSet)5 Modifier (javax.lang.model.element.Modifier)5 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TypeParameterElement (javax.lang.model.element.TypeParameterElement)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3