Search in sources :

Example 41 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class CoreEipAnnotationProcessor method processValue.

private void processValue(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, VariableElement fieldElement, String fieldName, XmlValue value, Set<EipOption> eipOptions, String prefix, String modelName) {
    Elements elementUtils = processingEnv.getElementUtils();
    // XmlValue has no name attribute
    String name = fieldName;
    if ("method".equals(modelName) || "tokenize".equals(modelName) || "xtokenize".equals(modelName)) {
        // skip expression attribute on these three languages as they are solely configured using attributes
        if ("expression".equals(name)) {
            return;
        }
    }
    name = prefix + name;
    TypeMirror fieldType = fieldElement.asType();
    String fieldTypeName = fieldType.toString();
    String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
    String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
    boolean required = true;
    // metadata may overrule element required
    required = findRequired(fieldElement, required);
    boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
    String displayName = null;
    Metadata metadata = fieldElement.getAnnotation(Metadata.class);
    if (metadata != null) {
        displayName = metadata.displayName();
    }
    EipOption ep = new EipOption(name, displayName, "value", fieldTypeName, required, defaultValue, docComment, deprecated, false, null, false, null, false);
    eipOptions.add(ep);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 42 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class CoreEipAnnotationProcessor method processElement.

private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (element != null) {
        String kind = "element";
        String name = element.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
        String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
        boolean required = element.required();
        // metadata may overrule element required
        required = findRequired(fieldElement, required);
        // is it used as predicate (check field first and then fallback to its class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        // gather enums
        Set<String> enums = new LinkedHashSet<String>();
        boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
        if (isEnum) {
            TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            if (enumClass != null) {
                // find all the enum constants which has the possible enum value that can be used
                List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
                for (VariableElement var : fields) {
                    if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                        String val = var.toString();
                        enums.add(val);
                    }
                }
            }
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        boolean isOneOf = ONE_OF_TYPE_NAME.equals(fieldTypeName);
        if (isOneOf) {
            // okay its actually an language expression, so favor using that in the eip option
            kind = "expression";
            for (String language : ONE_OF_LANGUAGES) {
                fieldTypeName = language;
                TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
                if (languages != null) {
                    String superClassName = canonicalClassName(languages.toString());
                    // find all classes that has that superClassName
                    Set<TypeElement> children = new LinkedHashSet<TypeElement>();
                    findTypeElementChildren(processingEnv, roundEnv, children, superClassName);
                    for (TypeElement child : children) {
                        XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
                        if (rootElement != null) {
                            String childName = rootElement.name();
                            oneOfTypes.add(childName);
                        }
                    }
                }
            }
        }
        // special for otherwise as we want to indicate that the element is
        if ("otherwise".equals(name)) {
            isOneOf = true;
            oneOfTypes.add("otherwise");
        }
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, isOneOf, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) AsPredicate(org.apache.camel.spi.AsPredicate) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 43 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class CoreEipAnnotationProcessor method processElements.

private void processElements(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElements elements, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (elements != null) {
        String kind = "element";
        String name = fieldName;
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
        boolean required = true;
        required = findRequired(fieldElement, required);
        // gather oneOf of the elements
        Set<String> oneOfTypes = new TreeSet<String>();
        for (XmlElement element : elements.value()) {
            String child = element.name();
            oneOfTypes.add(child);
        }
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, false, false, null, true, oneOfTypes, false);
        eipOptions.add(ep);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet) Metadata(org.apache.camel.spi.Metadata) XmlElement(javax.xml.bind.annotation.XmlElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 44 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class AnnotationProcessorHelper method findTypeElementChildren.

public static void findTypeElementChildren(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, Set<TypeElement> found, String superClassName) {
    Elements elementUtils = processingEnv.getElementUtils();
    int idx = superClassName.lastIndexOf('.');
    if (idx > 0) {
        String packageName = superClassName.substring(0, idx);
        PackageElement pe = elementUtils.getPackageElement(packageName);
        if (pe != null) {
            List<? extends Element> enclosedElements = pe.getEnclosedElements();
            for (Element rootElement : enclosedElements) {
                if (rootElement instanceof TypeElement) {
                    TypeElement typeElement = (TypeElement) rootElement;
                    String aSuperClassName = canonicalClassName(typeElement.getSuperclass().toString());
                    if (superClassName.equals(aSuperClassName)) {
                        found.add(typeElement);
                    }
                }
            }
        }
    }
}
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 45 with Elements

use of javax.lang.model.util.Elements in project auto by google.

the class MoreElementsTest method getLocalAndInheritedMethods_Old.

@Test
public void getLocalAndInheritedMethods_Old() {
    Elements elements = compilation.getElements();
    Types types = compilation.getTypes();
    TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
    TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
    TypeElement childType = elements.getTypeElement(Child.class.getCanonicalName());
    @SuppressWarnings("deprecation") Set<ExecutableElement> childTypeMethods = MoreElements.getLocalAndInheritedMethods(childType, elements);
    Set<ExecutableElement> objectMethods = visibleMethodsFromObject();
    assertThat(childTypeMethods).containsAllIn(objectMethods);
    Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
    assertThat(nonObjectMethods).containsExactly(getMethod(ParentClass.class, "foo"), getMethod(ParentInterface.class, "bar", longMirror), getMethod(Child.class, "bar"), getMethod(Child.class, "baz"), getMethod(Child.class, "buh", intMirror), getMethod(Child.class, "buh", intMirror, intMirror));
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Aggregations

Elements (javax.lang.model.util.Elements)66 TypeElement (javax.lang.model.element.TypeElement)44 TypeMirror (javax.lang.model.type.TypeMirror)28 VariableElement (javax.lang.model.element.VariableElement)19 ExecutableElement (javax.lang.model.element.ExecutableElement)17 Types (javax.lang.model.util.Types)17 Element (javax.lang.model.element.Element)15 LinkedHashSet (java.util.LinkedHashSet)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Test (org.junit.Test)14 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12 Set (java.util.Set)9 TreeSet (java.util.TreeSet)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Optional (java.util.Optional)6 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)6