Search in sources :

Example 11 with Elements

use of javax.lang.model.util.Elements in project spring-boot by spring-projects.

the class ConfigurationMetadataAnnotationProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    this.metadataCollector.processing(roundEnv);
    Elements elementUtils = this.processingEnv.getElementUtils();
    TypeElement annotationType = elementUtils.getTypeElement(configurationPropertiesAnnotation());
    if (annotationType != null) {
        // Is @ConfigurationProperties available
        for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) {
            processElement(element);
        }
    }
    if (roundEnv.processingOver()) {
        try {
            writeMetaData();
        } catch (Exception ex) {
            throw new IllegalStateException("Failed to write metadata", ex);
        }
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) Elements(javax.lang.model.util.Elements) InvalidConfigurationMetadataException(org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException) FileNotFoundException(java.io.FileNotFoundException)

Example 12 with Elements

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

the class AndroidRClassFinder method find.

public IRClass find() throws RClassNotFoundException {
    Elements elementUtils = processingEnv.getElementUtils();
    TypeElement androidRType = elementUtils.getTypeElement("android.R");
    if (androidRType == null) {
        LOGGER.error("The android.R class cannot be found");
        throw new RClassNotFoundException("The android.R class cannot be found");
    }
    LOGGER.info("Found Android class: {}", androidRType.toString());
    return new RClass(androidRType);
}
Also used : RClassNotFoundException(org.androidannotations.internal.exception.RClassNotFoundException) TypeElement(javax.lang.model.element.TypeElement) IRClass(org.androidannotations.rclass.IRClass) Elements(javax.lang.model.util.Elements)

Example 13 with Elements

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

the class ProjectRClassFinder method find.

public IRClass find(AndroidManifest manifest) throws RClassNotFoundException {
    Elements elementUtils = environment.getProcessingEnvironment().getElementUtils();
    String rClass = getRClassPackageName(manifest) + ".R";
    TypeElement rType = elementUtils.getTypeElement(rClass);
    if (rType == null) {
        LOGGER.error("The generated {} class cannot be found", rClass);
        throw new RClassNotFoundException("The generated " + rClass + " class cannot be found");
    }
    LOGGER.info("Found project R class: {}", rType.toString());
    return new RClass(rType);
}
Also used : RClassNotFoundException(org.androidannotations.internal.exception.RClassNotFoundException) TypeElement(javax.lang.model.element.TypeElement) IRClass(org.androidannotations.rclass.IRClass) Elements(javax.lang.model.util.Elements)

Example 14 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)

Example 15 with Elements

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

the class EndpointAnnotationProcessor method findComponentClassProperties.

protected void findComponentClassProperties(PrintWriter writer, RoundEnvironment roundEnv, ComponentModel componentModel, Set<ComponentOption> componentOptions, TypeElement classElement, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    while (true) {
        Metadata componentAnnotation = classElement.getAnnotation(Metadata.class);
        if (componentAnnotation != null && Objects.equals("verifiers", componentAnnotation.label())) {
            componentModel.setVerifiers(componentAnnotation.enums());
        }
        List<ExecutableElement> methods = ElementFilter.methodsIn(classElement.getEnclosedElements());
        for (ExecutableElement method : methods) {
            String methodName = method.getSimpleName().toString();
            boolean deprecated = method.getAnnotation(Deprecated.class) != null;
            Metadata metadata = method.getAnnotation(Metadata.class);
            // must be the setter
            boolean isSetter = methodName.startsWith("set") && method.getParameters().size() == 1 & method.getReturnType().getKind().equals(TypeKind.VOID);
            if (!isSetter) {
                continue;
            }
            // skip unwanted methods as they are inherited from default component and are not intended for end users to configure
            if ("setEndpointClass".equals(methodName) || "setCamelContext".equals(methodName) || "setEndpointHeaderFilterStrategy".equals(methodName) || "setApplicationContext".equals(methodName)) {
                continue;
            }
            // must be a getter/setter pair
            String fieldName = methodName.substring(3);
            fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
            // we usually favor putting the @Metadata annotation on the field instead of the setter, so try to use it if its there
            VariableElement field = findFieldElement(classElement, fieldName);
            if (field != null && metadata == null) {
                metadata = field.getAnnotation(Metadata.class);
            }
            String required = metadata != null ? metadata.required() : null;
            String label = metadata != null ? metadata.label() : null;
            boolean secret = metadata != null && metadata.secret();
            String displayName = metadata != null ? metadata.displayName() : null;
            // we do not yet have default values / notes / as no annotation support yet
            // String defaultValueNote = param.defaultValueNote();
            String defaultValue = metadata != null ? metadata.defaultValue() : null;
            String defaultValueNote = null;
            ExecutableElement setter = method;
            String name = fieldName;
            name = prefix + name;
            TypeMirror fieldType = setter.getParameters().get(0).asType();
            String fieldTypeName = fieldType.toString();
            TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
            String docComment = findJavaDoc(elementUtils, method, fieldName, name, classElement, false);
            if (isNullOrEmpty(docComment)) {
                docComment = metadata != null ? metadata.description() : null;
            }
            if (isNullOrEmpty(docComment)) {
                // apt cannot grab javadoc from camel-core, only from annotations
                if ("setHeaderFilterStrategy".equals(methodName)) {
                    docComment = HEADER_FILTER_STRATEGY_JAVADOC;
                } else {
                    docComment = "";
                }
            }
            // gather enums
            Set<String> enums = new LinkedHashSet<String>();
            boolean isEnum;
            if (metadata != null && !Strings.isNullOrEmpty(metadata.enums())) {
                isEnum = true;
                String[] values = metadata.enums().split(",");
                for (String val : values) {
                    enums.add(val);
                }
            } else {
                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);
                            }
                        }
                    }
                }
            }
            String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
            ComponentOption option = new ComponentOption(name, displayName, fieldTypeName, required, defaultValue, defaultValueNote, docComment.trim(), deprecated, secret, group, label, isEnum, enums);
            componentOptions.add(option);
        }
        // check super classes which may also have fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) ComponentOption(org.apache.camel.tools.apt.model.ComponentOption) ExecutableElement(javax.lang.model.element.ExecutableElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) TypeMirror(javax.lang.model.type.TypeMirror)

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