Search in sources :

Example 21 with Elements

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

the class CoreEipAnnotationProcessor method processAttribute.

private boolean processAttribute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, VariableElement fieldElement, String fieldName, XmlAttribute attribute, Set<EipOption> eipOptions, String prefix, String modelName) {
    Elements elementUtils = processingEnv.getElementUtils();
    String name = attribute.name();
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        name = fieldName;
    }
    // lets skip some unwanted attributes
    if (skipUnwanted) {
        // we want to skip inheritErrorHandler which is only applicable for the load-balancer
        boolean loadBalancer = "LoadBalanceDefinition".equals(originalClassType.getSimpleName().toString());
        if (!loadBalancer && "inheritErrorHandler".equals(name)) {
            return true;
        }
    }
    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 = attribute.required();
    // metadata may overrule element required
    required = findRequired(fieldElement, required);
    // gather enums
    Set<String> enums = new TreeSet<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);
                }
            }
        }
    }
    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, "attribute", fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, false, null, false);
    eipOptions.add(ep);
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) TreeSet(java.util.TreeSet) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 22 with Elements

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

the class CoreEipAnnotationProcessor method processRoute.

private void processRoute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    // group
    String docComment = findJavaDoc(elementUtils, null, "group", null, classElement, true);
    EipOption ep = new EipOption("group", "Group", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // group
    docComment = findJavaDoc(elementUtils, null, "streamCache", null, classElement, true);
    ep = new EipOption("streamCache", "Stream Cache", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "trace", null, classElement, true);
    ep = new EipOption("trace", "Trace", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "messageHistory", null, classElement, true);
    ep = new EipOption("messageHistory", "Message History", "attribute", "java.lang.String", false, "true", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "handleFault", null, classElement, true);
    ep = new EipOption("handleFault", "Handle Fault", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // delayer
    docComment = findJavaDoc(elementUtils, null, "delayer", null, classElement, true);
    ep = new EipOption("delayer", "Delayer", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // autoStartup
    docComment = findJavaDoc(elementUtils, null, "autoStartup", null, classElement, true);
    ep = new EipOption("autoStartup", "Auto Startup", "attribute", "java.lang.String", false, "true", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // startupOrder
    docComment = findJavaDoc(elementUtils, null, "startupOrder", null, classElement, true);
    ep = new EipOption("startupOrder", "Startup Order", "attribute", "java.lang.Integer", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // errorHandlerRef
    docComment = findJavaDoc(elementUtils, null, "errorHandlerRef", null, classElement, true);
    ep = new EipOption("errorHandlerRef", "Error Handler", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // routePolicyRef
    docComment = findJavaDoc(elementUtils, null, "routePolicyRef", null, classElement, true);
    ep = new EipOption("routePolicyRef", "Route Policy", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // shutdownRoute
    Set<String> enums = new LinkedHashSet<String>();
    enums.add("Default");
    enums.add("Defer");
    docComment = findJavaDoc(elementUtils, null, "shutdownRoute", "Default", classElement, true);
    ep = new EipOption("shutdownRoute", "Shutdown Route", "attribute", "org.apache.camel.ShutdownRoute", false, "", docComment, false, true, enums, false, null, false);
    eipOptions.add(ep);
    // shutdownRunningTask
    enums = new LinkedHashSet<String>();
    enums.add("CompleteCurrentTaskOnly");
    enums.add("CompleteAllTasks");
    docComment = findJavaDoc(elementUtils, null, "shutdownRunningTask", "CompleteCurrentTaskOnly", classElement, true);
    ep = new EipOption("shutdownRunningTask", "Shutdown Running Task", "attribute", "org.apache.camel.ShutdownRunningTask", false, "", docComment, false, true, enums, false, null, false);
    eipOptions.add(ep);
    // inputs
    Set<String> oneOfTypes = new TreeSet<String>();
    oneOfTypes.add("from");
    docComment = findJavaDoc(elementUtils, null, "inputs", null, classElement, true);
    ep = new EipOption("inputs", "Inputs", "element", "java.util.List<org.apache.camel.model.FromDefinition>", true, "", docComment, false, false, null, true, oneOfTypes, false);
    eipOptions.add(ep);
    // outputs
    // gather oneOf which extends any of the output base classes
    oneOfTypes = new TreeSet<String>();
    // find all classes that has that superClassName
    Set<TypeElement> children = new LinkedHashSet<TypeElement>();
    for (String superclass : ONE_OF_OUTPUTS) {
        findTypeElementChildren(processingEnv, roundEnv, children, superclass);
    }
    for (TypeElement child : children) {
        XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
        if (rootElement != null) {
            String childName = rootElement.name();
            oneOfTypes.add(childName);
        }
    }
    // remove some types which are not intended as an output in eips
    oneOfTypes.remove("route");
    docComment = findJavaDoc(elementUtils, null, "outputs", null, classElement, true);
    ep = new EipOption("outputs", "Outputs", "element", "java.util.List<org.apache.camel.model.ProcessorDefinition<?>>", true, "", docComment, false, false, null, true, oneOfTypes, false);
    eipOptions.add(ep);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TreeSet(java.util.TreeSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 23 with Elements

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

the class CoreEipAnnotationProcessor method processRefExpression.

/**
     * Special for processing an @XmlElementRef expression field
     */
private void processRefExpression(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("expression".equals(fieldName)) {
        String kind = "expression";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        // is it used as predicate (check field first and then fallback to its class / original class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        if (!asPredicate) {
            asPredicate = originalClassType.getAnnotation(AsPredicate.class) != null;
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        for (String language : ONE_OF_LANGUAGES) {
            TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
            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);
                }
            }
        }
        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, true, "", docComment, deprecated, false, null, true, 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) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 24 with Elements

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

the class CoreEipAnnotationProcessor method processRefWhenClauses.

/**
     * Special for processing an @XmlElementRef when field
     */
private void processRefWhenClauses(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("whenClauses".equals(fieldName)) {
        String kind = "element";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        // indicate that this element is one of when
        Set<String> oneOfTypes = new HashSet<String>();
        oneOfTypes.add("when");
        // when is predicate
        boolean asPredicate = true;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, false, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
        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) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 25 with Elements

use of javax.lang.model.util.Elements in project dagger by square.

the class ModuleAdapterProcessor method providerMethodsByClass.

/**
   * Returns a map containing all {@code @Provides} methods, indexed by class.
   */
private Map<String, List<ExecutableElement>> providerMethodsByClass(RoundEnvironment env) {
    Elements elementUtils = processingEnv.getElementUtils();
    Types types = processingEnv.getTypeUtils();
    Map<String, List<ExecutableElement>> result = new HashMap<String, List<ExecutableElement>>();
    provides: for (Element providerMethod : findProvidesMethods(env)) {
        switch(providerMethod.getEnclosingElement().getKind()) {
            case CLASS:
                // valid, move along
                break;
            default:
                // TODO(tbroyer): pass annotation information
                error("Unexpected @Provides on " + elementToString(providerMethod), providerMethod);
                continue;
        }
        TypeElement type = (TypeElement) providerMethod.getEnclosingElement();
        Set<Modifier> typeModifiers = type.getModifiers();
        if (typeModifiers.contains(PRIVATE) || typeModifiers.contains(ABSTRACT)) {
            error("Classes declaring @Provides methods must not be private or abstract: " + type.getQualifiedName(), type);
            continue;
        }
        Set<Modifier> methodModifiers = providerMethod.getModifiers();
        if (methodModifiers.contains(PRIVATE) || methodModifiers.contains(ABSTRACT) || methodModifiers.contains(STATIC)) {
            error("@Provides methods must not be private, abstract or static: " + type.getQualifiedName() + "." + providerMethod, providerMethod);
            continue;
        }
        ExecutableElement providerMethodAsExecutable = (ExecutableElement) providerMethod;
        if (!providerMethodAsExecutable.getThrownTypes().isEmpty()) {
            error("@Provides methods must not have a throws clause: " + type.getQualifiedName() + "." + providerMethod, providerMethod);
            continue;
        }
        // Invalidate return types.
        TypeMirror returnType = types.erasure(providerMethodAsExecutable.getReturnType());
        if (!returnType.getKind().equals(TypeKind.ERROR)) {
            // processors is not "invalid" in this way, so ignore).
            for (String invalidTypeName : INVALID_RETURN_TYPES) {
                TypeElement invalidTypeElement = elementUtils.getTypeElement(invalidTypeName);
                if (invalidTypeElement != null && types.isSameType(returnType, types.erasure(invalidTypeElement.asType()))) {
                    error(String.format("@Provides method must not return %s directly: %s.%s", invalidTypeElement, type.getQualifiedName(), providerMethod), providerMethod);
                    // Skip to next provides method.
                    continue provides;
                }
            }
        }
        List<ExecutableElement> methods = result.get(type.getQualifiedName().toString());
        if (methods == null) {
            methods = new ArrayList<ExecutableElement>();
            result.put(type.getQualifiedName().toString(), methods);
        }
        methods.add(providerMethodAsExecutable);
    }
    TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType();
    // should still be registered and a ModuleAdapter should still be written.
    for (Element module : env.getElementsAnnotatedWith(Module.class)) {
        if (!module.getKind().equals(ElementKind.CLASS)) {
            error("Modules must be classes: " + elementToString(module), module);
            continue;
        }
        TypeElement moduleType = (TypeElement) module;
        // Verify that all modules do not extend from non-Object types.
        if (!types.isSameType(moduleType.getSuperclass(), objectType)) {
            error("Modules must not extend from other classes: " + elementToString(module), module);
        }
        String moduleName = moduleType.getQualifiedName().toString();
        if (result.containsKey(moduleName))
            continue;
        result.put(moduleName, new ArrayList<ExecutableElement>());
    }
    return result;
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Util.typeToString(dagger.internal.codegen.Util.typeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) Elements(javax.lang.model.util.Elements) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) ArrayList(java.util.ArrayList)

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