Search in sources :

Example 6 with TypeElement

use of javax.lang.model.element.TypeElement in project immutables by immutables.

the class Annotations method getAnnotationLines.

static List<CharSequence> getAnnotationLines(Element element, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, ElementType elementType, Function<String, String> importsResolver) {
    List<CharSequence> lines = Lists.newArrayList();
    Set<String> seenAnnotations = new HashSet<>();
    for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
        TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
        if (annotationTypeMatches(element, annotationElement, includeAnnotations, includeAllAnnotations, includeJacksonAnnotations, seenAnnotations) && annotationMatchesTarget(annotationElement, elementType)) {
            lines.add(AnnotationMirrors.toCharSequence(annotation, importsResolver));
        }
    }
    return lines;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 7 with TypeElement

use of javax.lang.model.element.TypeElement in project immutables by immutables.

the class Annotations method annotationTypeMatches.

private static boolean annotationTypeMatches(Element element, TypeElement annotationElement, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, Set<String> seenAnnotations) {
    String qualifiedName = annotationElement.getQualifiedName().toString();
    if (seenAnnotations.contains(qualifiedName) || qualifiedName.startsWith(PREFIX_IMMUTABLES) || qualifiedName.startsWith(PREFIX_JAVA_LANG)) {
        // also skip any we've already seen, since we're recursing.
        return false;
    }
    seenAnnotations.add(qualifiedName);
    if (annotationElement.getSimpleName().contentEquals(NULLABLE_SIMPLE_NAME)) {
        // we expect to propagate nullability separately
        return false;
    }
    if (includeAllAnnotations) {
        return true;
    }
    if (qualifiedName.equals(PREFIX_JACKSON_IGNORE_PROPERTIES)) {
        // but preferred way is to use additionalJsonAnnotations style attribute.
        return true;
    }
    if (includeJacksonAnnotations) {
        if (element.getKind() != ElementKind.METHOD) {
            if (qualifiedName.equals(Proto.JACKSON_DESERIALIZE) || qualifiedName.equals(Proto.JACKSON_SERIALIZE)) {
                return false;
            }
        }
        if (qualifiedName.startsWith(PREFIX_JACKSON) || qualifiedName.startsWith(PREFIX_JACKSON_DATABIND)) {
            return true;
        }
    }
    if (includeAnnotations.contains(qualifiedName)) {
        return true;
    }
    // This block of code can include annotation if it's parent annotation is included
    if (includeJacksonAnnotations || !includeAnnotations.isEmpty()) {
        for (AnnotationMirror parentAnnotation : annotationElement.getAnnotationMirrors()) {
            TypeElement parentElement = (TypeElement) parentAnnotation.getAnnotationType().asElement();
            if (annotationTypeMatches(element, parentElement, includeAnnotations, false, includeJacksonAnnotations, seenAnnotations)) {
                return true;
            }
        }
    }
    return false;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement)

Example 8 with TypeElement

use of javax.lang.model.element.TypeElement in project hibernate-orm by hibernate.

the class TypeUtils method getDefaultAccessForHierarchy.

private static AccessType getDefaultAccessForHierarchy(TypeElement element, Context context) {
    AccessType defaultAccessType = null;
    TypeElement superClass = element;
    do {
        superClass = TypeUtils.getSuperclassTypeElement(superClass);
        if (superClass != null) {
            String fqcn = superClass.getQualifiedName().toString();
            AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo(fqcn);
            if (accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null) {
                return accessTypeInfo.getDefaultAccessType();
            }
            if (TypeUtils.containsAnnotation(superClass, Constants.ENTITY, Constants.MAPPED_SUPERCLASS)) {
                defaultAccessType = getAccessTypeInCaseElementIsRoot(superClass, context);
                if (defaultAccessType != null) {
                    accessTypeInfo = new AccessTypeInformation(fqcn, null, defaultAccessType);
                    context.addAccessTypeInformation(fqcn, accessTypeInfo);
                    // we found an id within the class hierarchy and determined a default access type
                    // there cannot be any super entity classes (otherwise it would be a configuration error)
                    // but there might be mapped super classes
                    // Also note, that if two different class hierarchies with different access types have a common
                    // mapped super class, the access type of the mapped super class will be the one of the last
                    // hierarchy processed. The result is not determined which is odd with the spec
                    setDefaultAccessTypeForMappedSuperclassesInHierarchy(superClass, defaultAccessType, context);
                    // we found a default access type, no need to look further
                    break;
                } else {
                    defaultAccessType = getDefaultAccessForHierarchy(superClass, context);
                }
            }
        }
    } while (superClass != null);
    return defaultAccessType;
}
Also used : TypeElement(javax.lang.model.element.TypeElement)

Example 9 with TypeElement

use of javax.lang.model.element.TypeElement in project hibernate-orm by hibernate.

the class TypeUtils method setDefaultAccessTypeForMappedSuperclassesInHierarchy.

private static void setDefaultAccessTypeForMappedSuperclassesInHierarchy(TypeElement element, AccessType defaultAccessType, Context context) {
    TypeElement superClass = element;
    do {
        superClass = TypeUtils.getSuperclassTypeElement(superClass);
        if (superClass != null) {
            String fqcn = superClass.getQualifiedName().toString();
            if (TypeUtils.containsAnnotation(superClass, Constants.MAPPED_SUPERCLASS)) {
                AccessTypeInformation accessTypeInfo;
                AccessType forcedAccessType = determineAnnotationSpecifiedAccessType(superClass);
                if (forcedAccessType != null) {
                    accessTypeInfo = new AccessTypeInformation(fqcn, null, forcedAccessType);
                } else {
                    accessTypeInfo = new AccessTypeInformation(fqcn, null, defaultAccessType);
                }
                context.addAccessTypeInformation(fqcn, accessTypeInfo);
            }
        }
    } while (superClass != null);
}
Also used : TypeElement(javax.lang.model.element.TypeElement)

Example 10 with TypeElement

use of javax.lang.model.element.TypeElement in project hibernate-orm by hibernate.

the class JPAMetaModelEntityProcessor method process.

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) {
    // see also METAGEN-45
    if (roundEnvironment.processingOver() || annotations.size() == 0) {
        return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
    }
    if (context.isFullyXmlConfigured()) {
        context.logMessage(Diagnostic.Kind.OTHER, "Skipping the processing of annotations since persistence unit is purely xml configured.");
        return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
    }
    Set<? extends Element> elements = roundEnvironment.getRootElements();
    for (Element element : elements) {
        if (isJPAEntity(element)) {
            context.logMessage(Diagnostic.Kind.OTHER, "Processing annotated class " + element.toString());
            handleRootElementAnnotationMirrors(element);
        }
    }
    createMetaModelClasses();
    return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)1564 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)504 TypeMirror (javax.lang.model.type.TypeMirror)421 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)202 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63