Search in sources :

Example 6 with PackageElement

use of javax.lang.model.element.PackageElement in project neo4j by neo4j.

the class UserFunctionVisitor method validateName.

private Stream<CompilationMessage> validateName(ExecutableElement method) {
    UserFunction function = method.getAnnotation(UserFunction.class);
    String name = function.name();
    if (!name.isEmpty() && isInRootNamespace(name)) {
        return Stream.of(rootNamespaceError(method, name));
    }
    String value = function.value();
    if (!value.isEmpty() && isInRootNamespace(value)) {
        return Stream.of(rootNamespaceError(method, value));
    }
    PackageElement namespace = elements.getPackageOf(method);
    if (namespace == null) {
        return Stream.of(rootNamespaceError(method));
    }
    return Stream.empty();
}
Also used : UserFunction(org.neo4j.procedure.UserFunction) PackageElement(javax.lang.model.element.PackageElement)

Example 7 with PackageElement

use of javax.lang.model.element.PackageElement in project bazel by bazelbuild.

the class ElementUtils method enclosingPackage.

/**
     * Returns the innermost package element enclosing the given element.
     * The same effect as {@link javax.lang.model.util.Elements#getPackageOf(Element)}.
     * Returns the element itself if it is a package.
     *
     * @param elem the enclosed element of a package
     * @return the innermost package element
     *
     */
public static PackageElement enclosingPackage(final Element elem) {
    Element result = elem;
    while (result != null && result.getKind() != ElementKind.PACKAGE) {
        /*@Nullable*/
        Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (PackageElement) result;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement)

Example 8 with PackageElement

use of javax.lang.model.element.PackageElement in project tiger by google.

the class Utils method getPackage.

public static PackageElement getPackage(TypeElement typeElement) {
    Element result = typeElement.getEnclosingElement();
    ElementKind elementKind = result.getKind();
    while (!elementKind.equals(ElementKind.PACKAGE)) {
        Preconditions.checkState(elementKind.isClass() || elementKind.isInterface(), String.format("Utils.getPackage: unexpected kind: %s for type: %s", elementKind, typeElement));
        result = result.getEnclosingElement();
        elementKind = result.getKind();
    }
    return (PackageElement) result;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement)

Example 9 with PackageElement

use of javax.lang.model.element.PackageElement in project camel by apache.

the class AnnotationProcessorHelper method findTypeElement.

public static TypeElement findTypeElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, String className) {
    if (isNullOrEmpty(className) || "java.lang.Object".equals(className)) {
        return null;
    }
    Set<? extends Element> rootElements = roundEnv.getRootElements();
    for (Element rootElement : rootElements) {
        if (rootElement instanceof TypeElement) {
            TypeElement typeElement = (TypeElement) rootElement;
            String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
            if (className.equals(aRootName)) {
                return typeElement;
            }
        }
    }
    // fallback using package name
    Elements elementUtils = processingEnv.getElementUtils();
    int idx = className.lastIndexOf('.');
    if (idx > 0) {
        String packageName = className.substring(0, idx);
        PackageElement pe = elementUtils.getPackageElement(packageName);
        if (pe != null) {
            List<? extends Element> enclosedElements = getEnclosedElements(pe);
            for (Element rootElement : enclosedElements) {
                if (rootElement instanceof TypeElement) {
                    TypeElement typeElement = (TypeElement) rootElement;
                    String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
                    if (className.equals(aRootName)) {
                        return typeElement;
                    }
                }
            }
        }
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) Elements(javax.lang.model.util.Elements)

Example 10 with PackageElement

use of javax.lang.model.element.PackageElement in project auto by google.

the class BasicAnnotationProcessor method validElements.

/**
   * Returns the valid annotated elements contained in all of the deferred elements. If none are
   * found for a deferred element, defers it again.
   */
private ImmutableSetMultimap<Class<? extends Annotation>, Element> validElements(ImmutableMap<String, Optional<? extends Element>> deferredElements, RoundEnvironment roundEnv) {
    ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> deferredElementsByAnnotationBuilder = ImmutableSetMultimap.builder();
    for (Entry<String, Optional<? extends Element>> deferredTypeElementEntry : deferredElements.entrySet()) {
        Optional<? extends Element> deferredElement = deferredTypeElementEntry.getValue();
        if (deferredElement.isPresent()) {
            findAnnotatedElements(deferredElement.get(), getSupportedAnnotationClasses(), deferredElementsByAnnotationBuilder);
        } else {
            deferredElementNames.add(ElementName.forTypeName(deferredTypeElementEntry.getKey()));
        }
    }
    ImmutableSetMultimap<Class<? extends Annotation>, Element> deferredElementsByAnnotation = deferredElementsByAnnotationBuilder.build();
    ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> validElements = ImmutableSetMultimap.builder();
    Set<ElementName> validElementNames = new LinkedHashSet<ElementName>();
    // Look at the elements we've found and the new elements from this round and validate them.
    for (Class<? extends Annotation> annotationClass : getSupportedAnnotationClasses()) {
        // This should just call roundEnv.getElementsAnnotatedWith(Class) directly, but there is a bug
        // in some versions of eclipse that cause that method to crash.
        TypeElement annotationType = elements.getTypeElement(annotationClass.getCanonicalName());
        Set<? extends Element> elementsAnnotatedWith = (annotationType == null) ? ImmutableSet.<Element>of() : roundEnv.getElementsAnnotatedWith(annotationType);
        for (Element annotatedElement : Sets.union(elementsAnnotatedWith, deferredElementsByAnnotation.get(annotationClass))) {
            if (annotatedElement.getKind().equals(PACKAGE)) {
                PackageElement annotatedPackageElement = (PackageElement) annotatedElement;
                ElementName annotatedPackageName = ElementName.forPackageName(annotatedPackageElement.getQualifiedName().toString());
                boolean validPackage = validElementNames.contains(annotatedPackageName) || (!deferredElementNames.contains(annotatedPackageName) && validateElement(annotatedPackageElement));
                if (validPackage) {
                    validElements.put(annotationClass, annotatedPackageElement);
                    validElementNames.add(annotatedPackageName);
                } else {
                    deferredElementNames.add(annotatedPackageName);
                }
            } else {
                TypeElement enclosingType = getEnclosingType(annotatedElement);
                ElementName enclosingTypeName = ElementName.forTypeName(enclosingType.getQualifiedName().toString());
                boolean validEnclosingType = validElementNames.contains(enclosingTypeName) || (!deferredElementNames.contains(enclosingTypeName) && validateElement(enclosingType));
                if (validEnclosingType) {
                    validElements.put(annotationClass, annotatedElement);
                    validElementNames.add(enclosingTypeName);
                } else {
                    deferredElementNames.add(enclosingTypeName);
                }
            }
        }
    }
    return validElements.build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Optional(com.google.common.base.Optional) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) SuperficialValidation.validateElement(com.google.auto.common.SuperficialValidation.validateElement) Annotation(java.lang.annotation.Annotation) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)133 TypeElement (javax.lang.model.element.TypeElement)92 Element (javax.lang.model.element.Element)51 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8