Search in sources :

Example 56 with Element

use of javax.lang.model.element.Element 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 57 with Element

use of javax.lang.model.element.Element in project spring-boot by spring-projects.

the class ConfigurationMetadataAnnotationProcessor method processSimpleLombokTypes.

private void processSimpleLombokTypes(String prefix, TypeElement element, ExecutableElement source, TypeElementMembers members, Map<String, Object> fieldValues) {
    for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
        String name = entry.getKey();
        VariableElement field = entry.getValue();
        if (!isLombokField(field, element)) {
            continue;
        }
        TypeMirror returnType = field.asType();
        Element returnTypeElement = this.processingEnv.getTypeUtils().asElement(returnType);
        boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
        boolean isNested = isNested(returnTypeElement, field, element);
        boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
        boolean hasSetter = hasLombokSetter(field, element);
        if (!isExcluded && !isNested && (hasSetter || isCollection)) {
            String dataType = this.typeUtils.getType(returnType);
            String sourceType = this.typeUtils.getQualifiedName(element);
            String description = this.typeUtils.getJavaDoc(field);
            Object defaultValue = fieldValues.get(name);
            boolean deprecated = isDeprecated(field) || isDeprecated(source);
            this.metadataCollector.add(ItemMetadata.newProperty(prefix, name, dataType, sourceType, null, description, defaultValue, (deprecated ? new ItemDeprecation() : null)));
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ItemDeprecation(org.springframework.boot.configurationprocessor.metadata.ItemDeprecation) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 58 with Element

use of javax.lang.model.element.Element in project spring-boot by spring-projects.

the class ConfigurationMetadataAnnotationProcessor method processSimpleTypes.

private void processSimpleTypes(String prefix, TypeElement element, ExecutableElement source, TypeElementMembers members, Map<String, Object> fieldValues) {
    for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters().entrySet()) {
        String name = entry.getKey();
        ExecutableElement getter = entry.getValue();
        TypeMirror returnType = getter.getReturnType();
        ExecutableElement setter = members.getPublicSetter(name, returnType);
        VariableElement field = members.getFields().get(name);
        Element returnTypeElement = this.processingEnv.getTypeUtils().asElement(returnType);
        boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
        boolean isNested = isNested(returnTypeElement, field, element);
        boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
        if (!isExcluded && !isNested && (setter != null || isCollection)) {
            String dataType = this.typeUtils.getType(returnType);
            String sourceType = this.typeUtils.getQualifiedName(element);
            String description = this.typeUtils.getJavaDoc(field);
            Object defaultValue = fieldValues.get(name);
            boolean deprecated = isDeprecated(getter) || isDeprecated(setter) || isDeprecated(source);
            this.metadataCollector.add(ItemMetadata.newProperty(prefix, name, dataType, sourceType, null, description, defaultValue, (deprecated ? getItemDeprecation(getter) : null)));
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 59 with Element

use of javax.lang.model.element.Element in project lombok by rzwitserloot.

the class TestAp method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    roundCounter++;
    log("TestAP in round " + roundCounter);
    boolean foundGetTest = false;
    int annotatedElemCount = 0;
    for (Element annotated : roundEnv.getElementsAnnotatedWith(ExampleAnnotation.class)) {
        annotatedElemCount++;
        for (Element child : annotated.getEnclosedElements()) {
            if (child.getSimpleName().toString().equals("getTest") && child.getKind() == ElementKind.METHOD)
                foundGetTest = true;
            if (child instanceof ExecutableElement) {
                TypeMirror returnType = ((ExecutableElement) child).getReturnType();
                System.out.println("RETURN TYPE for " + child.getSimpleName() + ": " + returnType.getClass() + " -- " + returnType.toString());
            }
        }
    }
    if (foundGetTest)
        log("RESULT: POSITIVE -- found the getTest method");
    else if (annotatedElemCount > 0)
        log("RESULT: NEGATIVE -- found the example class but there's no getTest method in it according to the type mirror.");
    else
        log("RESULT: AMBIVALENT -- The example class is not provided by 'getElementsAnnotatedWith' in this round. Not an issue, unless previously you got a NEGATIVE result.");
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 60 with Element

use of javax.lang.model.element.Element in project lombok by rzwitserloot.

the class LombokProcessor method process.

/** {@inheritDoc} */
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (lombokDisabled)
        return false;
    if (roundEnv.processingOver())
        return false;
    for (Element element : roundEnv.getRootElements()) {
        JCCompilationUnit unit = toUnit(element);
        if (unit == null)
            continue;
        if (roots.containsKey(unit))
            continue;
        roots.put(unit, priorityLevels[0]);
    }
    while (true) {
        for (long prio : priorityLevels) {
            List<JCCompilationUnit> cusForThisRound = new ArrayList<JCCompilationUnit>();
            for (Map.Entry<JCCompilationUnit, Long> entry : roots.entrySet()) {
                Long prioOfCu = entry.getValue();
                if (prioOfCu == null || prioOfCu != prio)
                    continue;
                cusForThisRound.add(entry.getKey());
            }
            transformer.transform(prio, processingEnv.getContext(), cusForThisRound);
        }
        // Step 3: Push up all CUs to the next level. Set level to null if there is no next level.
        Set<Long> newLevels = new HashSet<Long>();
        for (int i = priorityLevels.length - 1; i >= 0; i--) {
            Long curLevel = priorityLevels[i];
            Long nextLevel = (i == priorityLevels.length - 1) ? null : priorityLevels[i + 1];
            List<JCCompilationUnit> cusToAdvance = new ArrayList<JCCompilationUnit>();
            for (Map.Entry<JCCompilationUnit, Long> entry : roots.entrySet()) {
                if (curLevel.equals(entry.getValue())) {
                    cusToAdvance.add(entry.getKey());
                    newLevels.add(nextLevel);
                }
            }
            for (JCCompilationUnit unit : cusToAdvance) {
                roots.put(unit, nextLevel);
            }
        }
        newLevels.remove(null);
        if (newLevels.isEmpty())
            return false;
        newLevels.retainAll(priorityLevelsRequiringResolutionReset);
        if (!newLevels.isEmpty()) {
            // Force a new round to reset resolution. The next round will cause this method (process) to be called again.
            forceNewRound((JavacFiler) processingEnv.getFiler());
            return false;
        }
    // None of the new levels need resolution, so just keep going.
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashSet(java.util.HashSet)

Aggregations

Element (javax.lang.model.element.Element)286 TypeElement (javax.lang.model.element.TypeElement)227 ExecutableElement (javax.lang.model.element.ExecutableElement)148 VariableElement (javax.lang.model.element.VariableElement)96 TypeMirror (javax.lang.model.type.TypeMirror)68 PackageElement (javax.lang.model.element.PackageElement)48 ArrayList (java.util.ArrayList)39 DeclaredType (javax.lang.model.type.DeclaredType)30 IOException (java.io.IOException)29 Map (java.util.Map)26 HashSet (java.util.HashSet)23 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)22 Set (java.util.Set)22 Test (org.junit.Test)21 ElementKind (javax.lang.model.element.ElementKind)20 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Elements (javax.lang.model.util.Elements)18 HashMap (java.util.HashMap)16 LinkedHashMap (java.util.LinkedHashMap)15