Search in sources :

Example 1 with Elements

use of javax.lang.model.util.Elements in project buck by facebook.

the class StubJarGeneratingProcessor method generateStubJar.

private void generateStubJar() {
    try {
        Elements elementUtils = processingEnv.getElementUtils();
        StubJar stubJar = new StubJar(classFileVersion, elementUtils, topLevelTypeNames.stream().map(elementUtils::getTypeElement).collect(Collectors.toList()));
        stubJar.writeTo(filesystem, stubJarPath);
    } catch (IOException e) {
        throw new RuntimeException(String.format("Failed to create stub jar: %s. %s", stubJarPath, e.getMessage()), e);
    }
}
Also used : IOException(java.io.IOException) Elements(javax.lang.model.util.Elements)

Example 2 with Elements

use of javax.lang.model.util.Elements in project RoboBinding by RoboBinding.

the class ElementWrapperTest method unsupportedElements.

@DataPoints("unsupportedElements")
public static Element[] unsupportedElements() {
    Elements elements = compilation.getElements();
    PackageElement packageElement = elements.getPackageElement("java.lang");
    TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
    VariableElement variableElement = ElementFilter.fieldsIn(typeElement.getEnclosedElements()).get(0);
    return new Element[] { packageElement, variableElement };
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 3 with Elements

use of javax.lang.model.util.Elements in project RoboBinding by RoboBinding.

the class ElementWrapperTest method supportedElements.

@DataPoints("supportedElements")
public static ElementToWrapped[] supportedElements() {
    Elements elements = compilation.getElements();
    TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
    ExecutableElement methodElement = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
    return new ElementToWrapped[] { a(typeElement).itsWrapped(WrappedTypeElement.class), a(methodElement).itsWrapped(MethodElement.class) };
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 4 with Elements

use of javax.lang.model.util.Elements in project bazel by bazelbuild.

the class InternalUtils method leastUpperBound.

/**
     * Returns the least upper bound of two {@link TypeMirror}s.
     *
     * @param processingEnv The {@link ProcessingEnvironment} to use.
     * @param tm1 A {@link TypeMirror}.
     * @param tm2 A {@link TypeMirror}.
     * @return The least upper bound of {@code tm1} and {@code tm2}.
     */
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually (not done by types.lub).
    if (t1.getKind() == TypeKind.NULL) {
        return t2;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t1;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t2;
        } else if (types.isAssignable(t2, t1)) {
            return t1;
        } else {
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        WildcardType wc1 = (WildcardType) t1;
        Type bound = (Type) wc1.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t1 = bound;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        WildcardType wc2 = (WildcardType) t2;
        Type bound = (Type) wc2.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t2 = bound;
    }
    return types.lub(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Elements(javax.lang.model.util.Elements)

Example 5 with Elements

use of javax.lang.model.util.Elements 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)

Aggregations

Elements (javax.lang.model.util.Elements)146 TypeElement (javax.lang.model.element.TypeElement)90 TypeMirror (javax.lang.model.type.TypeMirror)52 Element (javax.lang.model.element.Element)46 Types (javax.lang.model.util.Types)40 VariableElement (javax.lang.model.element.VariableElement)35 ExecutableElement (javax.lang.model.element.ExecutableElement)32 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)17 PackageElement (javax.lang.model.element.PackageElement)17 LinkedHashSet (java.util.LinkedHashSet)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 Test (org.junit.Test)15 List (java.util.List)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Set (java.util.Set)12 DeclaredType (javax.lang.model.type.DeclaredType)12 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12