io.micronaut.annotation.processing.JavaAnnotationMetadataBuilder Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.annotation.processing;
import io.micronaut.annotation.processing.visitor.JavaVisitorContext;
import io.micronaut.core.annotation.AnnotationClassValue;
import io.micronaut.core.annotation.AnnotationUtil;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.annotation.AbstractAnnotationMetadataBuilder;
import io.micronaut.inject.processing.JavaModelUtils;
import io.micronaut.inject.visitor.VisitorContext;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.NullType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractAnnotationValueVisitor8;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* A {@link io.micronaut.core.annotation.AnnotationMetadata} for builder for Java to be used at compile time.
*
* @author Graeme Rocher
* @since 1.0
*/
public class JavaAnnotationMetadataBuilder extends AbstractAnnotationMetadataBuilder {
private final Elements elementUtils;
private final Messager messager;
private final ModelUtils modelUtils;
private final JavaNativeElementsHelper nativeElementsHelper;
private final JavaVisitorContext visitorContext;
/**
* Default constructor.
*
* @param elements The elementUtils
* @param messager The messager
* @param annotationUtils The annotation utils
* @param modelUtils The model utils
* @deprecated Not needed
*/
@Deprecated(forRemoval = true, since = "4.3.0")
public JavaAnnotationMetadataBuilder(
Elements elements,
Messager messager,
AnnotationUtils annotationUtils,
ModelUtils modelUtils) {
this(elements, messager, modelUtils, new JavaNativeElementsHelper(elements, modelUtils.getTypeUtils()), annotationUtils.newVisitorContext());
}
/**
* Default constructor.
*
* @param elements The elementUtils
* @param messager The messager
* @param modelUtils The model utils
* @param nativeElementsHelper The native elements helper
* @param visitorContext The visitor context
*/
public JavaAnnotationMetadataBuilder(
Elements elements,
Messager messager,
ModelUtils modelUtils,
JavaNativeElementsHelper nativeElementsHelper,
JavaVisitorContext visitorContext) {
this.elementUtils = elements;
this.messager = messager;
this.modelUtils = modelUtils;
this.nativeElementsHelper = nativeElementsHelper;
this.visitorContext = visitorContext;
}
@Override
protected void addError(@NonNull Element originatingElement, @NonNull String error) {
messager.printMessage(Diagnostic.Kind.ERROR, error, originatingElement);
}
@Override
protected void addWarning(@NonNull Element originatingElement, @NonNull String warning) {
messager.printMessage(Diagnostic.Kind.WARNING, warning, originatingElement);
}
@Override
protected String getAnnotationMemberName(Element member) {
return member.getSimpleName().toString();
}
@Nullable
@Override
protected String getRepeatableName(AnnotationMirror annotationMirror) {
final Element typeElement = annotationMirror.getAnnotationType().asElement();
return getRepeatableContainerNameForType(typeElement);
}
@Nullable
@Override
protected String getRepeatableContainerNameForType(Element annotationType) {
List extends AnnotationMirror> mirrors = annotationType.getAnnotationMirrors();
for (AnnotationMirror mirror : mirrors) {
String name = mirror.getAnnotationType().toString();
if (Repeatable.class.getName().equals(name)) {
Map extends ExecutableElement, ? extends javax.lang.model.element.AnnotationValue> elementValues = mirror.getElementValues();
for (Map.Entry extends ExecutableElement, ? extends javax.lang.model.element.AnnotationValue> entry : elementValues.entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("value")) {
javax.lang.model.element.AnnotationValue av = entry.getValue();
Object value = av.getValue();
if (value instanceof DeclaredType type) {
Element element = type.asElement();
return JavaModelUtils.getClassName((TypeElement) element);
}
}
}
}
}
return null;
}
@Override
protected Optional getAnnotationMirror(String annotationName) {
TypeElement typeElement = elementUtils.getTypeElement(annotationName);
if (typeElement == null) {
// maybe inner class?
typeElement = elementUtils.getTypeElement(annotationName.replace('$', '.'));
}
return Optional.ofNullable(typeElement);
}
@Override
protected VisitorContext getVisitorContext() {
return visitorContext;
}
@NonNull
@Override
protected RetentionPolicy getRetentionPolicy(@NonNull Element annotation) {
final List extends AnnotationMirror> annotationMirrors = annotation.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
final String annotationTypeName = getAnnotationTypeName(annotationMirror);
if (Retention.class.getName().equals(annotationTypeName)) {
final Iterator extends AnnotationValue> i = annotationMirror
.getElementValues().values().iterator();
if (i.hasNext()) {
final AnnotationValue av = i.next();
final String v = av.getValue().toString();
return RetentionPolicy.valueOf(v);
}
break;
}
}
return RetentionPolicy.RUNTIME;
}
@Override
protected Element getTypeForAnnotation(AnnotationMirror annotationMirror) {
return annotationMirror.getAnnotationType().asElement();
}
@Override
protected List extends AnnotationMirror> getAnnotationsForType(Element element) {
var expanded = new ArrayList();
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
boolean repeatable = false;
boolean hasOtherMembers = false;
for (Map.Entry extends ExecutableElement, ? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("value")) {
Object value = entry.getValue().getValue();
if (value instanceof List> list) {
String parentAnnotationName = getAnnotationTypeName(annotation);
for (Object val : list) {
if (val instanceof AnnotationMirror mirror) {
String name = getRepeatableName(mirror);
if (name != null && name.equals(parentAnnotationName)) {
repeatable = true;
expanded.add(mirror);
}
}
}
}
} else {
hasOtherMembers = true;
}
}
if (!repeatable || hasOtherMembers) {
expanded.add(annotation);
}
}
return expanded;
}
@Override
protected boolean isExcludedAnnotation(@NonNull Element element, @NonNull String annotationName) {
if (annotationName.startsWith("java.lang.annotation") && element.getKind() == ElementKind.ANNOTATION_TYPE) {
return false;
} else {
return super.isExcludedAnnotation(element, annotationName);
}
}
@Override
protected List buildHierarchy(Element element, boolean inheritTypeAnnotations, boolean declaredOnly) {
if (declaredOnly) {
var onlyDeclared = new ArrayList(1);
onlyDeclared.add(element);
return onlyDeclared;
}
if (element instanceof TypeElement typeElement) {
var hierarchy = new ArrayList();
if (element.getKind() == ElementKind.ANNOTATION_TYPE) {
hierarchy.add(typeElement);
} else {
nativeElementsHelper.populateTypeHierarchy(typeElement, hierarchy);
}
return (List) hierarchy;
} else if (element instanceof ExecutableElement executableElement) {
// we have a method
// for methods we merge the data from any overridden interface or abstract methods
// with type level data
// the starting hierarchy is the type and super types of this method
List hierarchy;
if (inheritTypeAnnotations) {
hierarchy = buildHierarchy(executableElement.getEnclosingElement(), false, declaredOnly);
} else {
hierarchy = new ArrayList<>();
}
hierarchy.addAll(nativeElementsHelper.findOverriddenMethods(executableElement));
hierarchy.add(element);
return hierarchy;
} else if (element instanceof VariableElement variable) {
var hierarchy = new ArrayList();
Element enclosingElement = variable.getEnclosingElement();
if (enclosingElement instanceof ExecutableElement executableElement) {
int variableIdx = executableElement.getParameters().indexOf(variable);
for (ExecutableElement overridden : nativeElementsHelper.findOverriddenMethods(executableElement)) {
hierarchy.add(overridden.getParameters().get(variableIdx));
}
}
hierarchy.add(variable);
return hierarchy;
} else {
var single = new ArrayList(1);
single.add(element);
return single;
}
}
@Override
protected Map extends Element, ?> readAnnotationRawValues(AnnotationMirror annotationMirror) {
return annotationMirror.getElementValues();
}
@Nullable
@Override
protected Element getAnnotationMember(Element annotationElement, CharSequence member) {
if (annotationElement instanceof TypeElement) {
List extends Element> enclosedElements = annotationElement.getEnclosedElements();
for (Element enclosedElement : enclosedElements) {
if (enclosedElement instanceof ExecutableElement && enclosedElement.getSimpleName().toString().equals(member.toString())) {
return enclosedElement;
}
}
}
return null;
}
@Override
protected String getOriginatingClassName(@NonNull Element orginatingElement) {
TypeElement typeElement = getOriginatingTypeElement(orginatingElement);
if (typeElement != null) {
return JavaModelUtils.getClassName(typeElement);
}
return null;
}
private TypeElement getOriginatingTypeElement(Element element) {
if (element == null) {
return null;
}
if (element instanceof TypeElement typeElement) {
return typeElement;
}
return getOriginatingTypeElement(element.getEnclosingElement());
}
@Override
protected Optional> getAnnotationValues(Element originatingElement, Element member, Class annotationType) {
List extends AnnotationMirror> annotationMirrors = member.getAnnotationMirrors();
String annotationName = annotationType.getName();
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (annotationMirror.getAnnotationType().toString().endsWith(annotationName)) {
Map extends Element, ?> values = readAnnotationRawValues(annotationMirror);
var converted = new LinkedHashMap();
for (Map.Entry extends Element, ?> entry : values.entrySet()) {
Element key = entry.getKey();
Object value = entry.getValue();
readAnnotationRawValues(originatingElement, annotationName, key, key.getSimpleName().toString(), value, converted);
}
return Optional.of(io.micronaut.core.annotation.AnnotationValue.builder(annotationType).members(converted).build());
}
}
return Optional.empty();
}
@Override
protected void readAnnotationRawValues(
Element originatingElement,
String annotationName,
Element member,
String memberName,
Object annotationValue,
Map annotationValues) {
readAnnotationRawValues(originatingElement, annotationName, member, memberName, annotationValue, annotationValues, new HashMap<>());
}
@Override
protected void readAnnotationRawValues(
Element originatingElement,
String annotationName,
Element member,
String memberName,
Object annotationValue,
Map annotationValues,
Map> resolvedDefaults) {
if (memberName != null && annotationValue instanceof javax.lang.model.element.AnnotationValue value && !annotationValues.containsKey(memberName)) {
final MetadataAnnotationValueVisitor resolver = new MetadataAnnotationValueVisitor(originatingElement, (ExecutableElement) member, resolvedDefaults);
value.accept(resolver, this);
Object resolvedValue = resolver.resolvedValue;
if (resolvedValue != null) {
if (isEvaluatedExpression(resolvedValue)) {
resolvedValue = buildEvaluatedExpressionReference(originatingElement, annotationName, memberName, resolvedValue);
}
validateAnnotationValue(originatingElement, annotationName, member, memberName, resolvedValue);
annotationValues.put(memberName, resolvedValue);
}
}
}
@Override
protected boolean isValidationRequired(Element member) {
final List extends AnnotationMirror> annotationMirrors = member.getAnnotationMirrors();
return isValidationRequired(annotationMirrors);
}
private boolean isValidationRequired(List extends AnnotationMirror> annotationMirrors) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
final String annotationName = getAnnotationTypeName(annotationMirror);
if (annotationName.startsWith("jakarta.validation")) {
return true;
} else if (!AnnotationUtil.INTERNAL_ANNOTATION_NAMES.contains(annotationName)) {
final Element element = getAnnotationMirror(annotationName).orElse(null);
if (element != null) {
final List extends AnnotationMirror> childMirrors = element.getAnnotationMirrors()
.stream()
.filter(ann -> !getAnnotationTypeName(ann).equals(annotationName))
.collect(Collectors.toList());
if (isValidationRequired(childMirrors)) {
return true;
}
}
}
}
return false;
}
@Override
protected Object readAnnotationValue(Element originatingElement, Element member, String annotationName, String memberName, Object annotationValue) {
if (memberName != null && annotationValue instanceof javax.lang.model.element.AnnotationValue value) {
final MetadataAnnotationValueVisitor visitor = new MetadataAnnotationValueVisitor(originatingElement, (ExecutableElement) member, new HashMap<>());
value.accept(visitor, this);
return visitor.resolvedValue;
} else if (memberName != null && annotationValue != null && ClassUtils.isJavaLangType(annotationValue.getClass())) {
// only allow basic types
if (isEvaluatedExpression(annotationValue)) {
annotationValue = buildEvaluatedExpressionReference(originatingElement, annotationName, memberName, annotationValue);
}
return annotationValue;
}
return null;
}
@Override
protected Map extends Element, ?> readAnnotationDefaultValues(String annotationTypeName, Element element) {
var defaultValues = new LinkedHashMap();
if (element instanceof TypeElement annotationElement) {
final List extends Element> allMembers = elementUtils.getAllMembers(annotationElement);
allMembers
.stream()
.filter(member -> member.getEnclosingElement().equals(annotationElement))
.filter(ExecutableElement.class::isInstance)
.map(ExecutableElement.class::cast)
.filter(this::isValidDefaultValue)
.forEach(executableElement -> {
final AnnotationValue defaultValue = executableElement.getDefaultValue();
defaultValues.put(executableElement, defaultValue);
}
);
}
return defaultValues;
}
private boolean isValidDefaultValue(ExecutableElement executableElement) {
AnnotationValue defaultValue = executableElement.getDefaultValue();
if (defaultValue != null) {
Object v = defaultValue.getValue();
if (v != null) {
if (v instanceof String string) {
return StringUtils.isNotEmpty(string);
} else {
return true;
}
}
}
return false;
}
@Override
protected String getAnnotationTypeName(AnnotationMirror annotationMirror) {
return JavaModelUtils.getClassName((TypeElement) annotationMirror.getAnnotationType().asElement());
}
@Override
protected String getElementName(Element element) {
if (element instanceof TypeElement typeElement) {
return elementUtils.getBinaryName(typeElement).toString();
}
return element.getSimpleName().toString();
}
/**
* Checks if a method has an annotation.
*
* @param element The method
* @param ann The annotation to look for
* @return Whether if the method has the annotation
*/
@Override
public boolean hasAnnotation(Element element, Class extends Annotation> ann) {
return hasAnnotation(element, ann.getName());
}
/**
* Checks if a method has an annotation.
*
* @param element The method
* @param ann The annotation to look for
* @return Whether if the method has the annotation
*/
@Override
public boolean hasAnnotation(Element element, String ann) {
List extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
if (CollectionUtils.isNotEmpty(annotationMirrors)) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
final DeclaredType annotationType = annotationMirror.getAnnotationType();
if (annotationType.toString().equals(ann)) {
return true;
}
}
}
return false;
}
@Override
protected boolean hasAnnotations(Element element) {
return CollectionUtils.isNotEmpty(element.getAnnotationMirrors());
}
/**
* Clears any caches from the last compilation round.
*/
public static void clearCaches() {
AbstractAnnotationMetadataBuilder.clearCaches();
}
/**
* Checks if a method has an annotation.
*
* @param method The method
* @param ann The annotation to look for
* @return Whether if the method has the annotation
*/
public static boolean hasAnnotation(ExecutableElement method, Class extends Annotation> ann) {
List extends AnnotationMirror> annotationMirrors = method.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (annotationMirror.getAnnotationType().toString().equals(ann.getName())) {
return true;
}
}
return false;
}
/**
* Meta annotation value visitor class.
*/
private class MetadataAnnotationValueVisitor extends AbstractAnnotationValueVisitor8