io.micronaut.annotation.processing.JavaAnnotationMetadataBuilder Maven / Gradle / Ivy
/*
* Copyright 2017-2019 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
*
* http://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.core.annotation.AnnotationUtil;
import io.micronaut.core.annotation.AnnotationClassValue;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.value.OptionalValues;
import io.micronaut.inject.annotation.AbstractAnnotationMetadataBuilder;
import io.micronaut.inject.processing.JavaModelUtils;
import io.micronaut.inject.visitor.VisitorContext;
import javax.annotation.Nullable;
import javax.annotation.processing.Filer;
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.DeclaredType;
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 java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Stream;
/**
* 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 static final Map> ANNOTATION_DEFAULTS = new HashMap<>();
private final Elements elementUtils;
private final Messager messager;
private final AnnotationUtils annotationUtils;
private final Types types;
private final ModelUtils modelUtils;
private final Filer filer;
/**
* Default constructor.
*
* @param elements The elementUtils
* @param messager The messager
* @param annotationUtils The annotation utils
* @param types The types
* @param modelUtils The model utils
* @param filer The filer
*/
public JavaAnnotationMetadataBuilder(
Elements elements,
Messager messager,
AnnotationUtils annotationUtils,
Types types,
ModelUtils modelUtils,
Filer filer) {
this.elementUtils = elements;
this.messager = messager;
this.annotationUtils = annotationUtils;
this.types = types;
this.modelUtils = modelUtils;
this.filer = filer;
}
@Override
protected String getAnnotationMemberName(Element member) {
return member.getSimpleName().toString();
}
@Nullable
@Override
protected String getRepeatableName(AnnotationMirror annotationMirror) {
final Element typeElement = annotationMirror.getAnnotationType().asElement();
return getRepeatableNameForType(typeElement);
}
@Nullable
@Override
protected String getRepeatableNameForType(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 (ExecutableElement executableElement : elementValues.keySet()) {
if (executableElement.getSimpleName().toString().equals("value")) {
javax.lang.model.element.AnnotationValue av = elementValues.get(executableElement);
Object value = av.getValue();
if (value instanceof DeclaredType) {
Element element = ((DeclaredType) value).asElement();
return JavaModelUtils.getClassName((TypeElement) element);
}
}
}
}
}
return null;
}
@Override
protected Optional getAnnotationMirror(String annotationName) {
return Optional.ofNullable(elementUtils.getTypeElement(annotationName));
}
@Override
protected VisitorContext createVisitorContext() {
return annotationUtils.newVisitorContext();
}
@Override
protected Element getTypeForAnnotation(AnnotationMirror annotationMirror) {
return annotationMirror.getAnnotationType().asElement();
}
@Override
protected List extends AnnotationMirror> getAnnotationsForType(Element element) {
List extends AnnotationMirror> annotationMirrors = new ArrayList<>(element.getAnnotationMirrors());
annotationMirrors.removeIf(mirror -> getAnnotationTypeName(mirror).equals(AnnotationUtil.KOTLIN_METADATA));
return annotationMirrors;
}
@Override
protected List buildHierarchy(Element element, boolean inheritTypeAnnotations) {
if (element instanceof TypeElement) {
List hierarchy = new ArrayList<>();
hierarchy.add(element);
populateTypeHierarchy(element, hierarchy);
Collections.reverse(hierarchy);
return hierarchy;
} else if (element instanceof ExecutableElement) {
// we have a method
// for methods we merge the data from any overridden interface or abstract methods
// with type level data
ExecutableElement executableElement = (ExecutableElement) element;
// the starting hierarchy is the type and super types of this method
List hierarchy;
if (inheritTypeAnnotations) {
hierarchy = buildHierarchy(executableElement.getEnclosingElement(), false);
} else {
hierarchy = new ArrayList<>();
}
if (hasAnnotation(executableElement, Override.class)) {
hierarchy.addAll(findOverriddenMethods(executableElement));
}
hierarchy.add(element);
return hierarchy;
} else if (element instanceof VariableElement) {
List hierarchy = new ArrayList<>();
VariableElement variable = (VariableElement) element;
Element enclosingElement = variable.getEnclosingElement();
if (enclosingElement instanceof ExecutableElement) {
ExecutableElement executableElement = (ExecutableElement) enclosingElement;
if (hasAnnotation(executableElement, Override.class)) {
int variableIdx = executableElement.getParameters().indexOf(variable);
for (ExecutableElement overridden: findOverriddenMethods(executableElement)) {
hierarchy.add(overridden.getParameters().get(variableIdx));
}
}
}
hierarchy.add(variable);
return hierarchy;
} else {
ArrayList single = new ArrayList<>();
single.add(element);
return single;
}
}
@Override
protected Map extends Element, ?> readAnnotationRawValues(AnnotationMirror annotationMirror) {
return annotationMirror.getElementValues();
}
@Override
protected OptionalValues> getAnnotationValues(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);
Map converted = new LinkedHashMap<>();
for (Map.Entry extends Element, ?> entry : values.entrySet()) {
Element key = entry.getKey();
Object value = entry.getValue();
readAnnotationRawValues(key.getSimpleName().toString(), value, converted);
}
return OptionalValues.of(Object.class, converted);
}
}
return OptionalValues.empty();
}
@Override
protected void readAnnotationRawValues(String memberName, Object annotationValue, Map annotationValues) {
if (memberName != null && annotationValue instanceof javax.lang.model.element.AnnotationValue) {
if (!annotationValues.containsKey(memberName)) {
((javax.lang.model.element.AnnotationValue) annotationValue).accept(new MetadataAnnotationValueVisitor(annotationValues, memberName), this);
}
}
}
@Override
protected Object readAnnotationValue(String memberName, Object annotationValue) {
if (memberName != null && annotationValue instanceof javax.lang.model.element.AnnotationValue) {
HashMap result = new HashMap<>(1);
((javax.lang.model.element.AnnotationValue) annotationValue).accept(new MetadataAnnotationValueVisitor(result, memberName), this);
return result.get(memberName);
}
return null;
}
@Override
protected Map extends Element, ?> readAnnotationDefaultValues(AnnotationMirror annotationMirror) {
final String annotationTypeName = getAnnotationTypeName(annotationMirror);
Element element = annotationMirror.getAnnotationType().asElement();
return readAnnotationDefaultValues(annotationTypeName, element);
}
@Override
protected Map extends Element, ?> readAnnotationDefaultValues(String annotationTypeName, Element element) {
Map> defaults = JavaAnnotationMetadataBuilder.ANNOTATION_DEFAULTS;
if (element instanceof TypeElement) {
TypeElement annotationElement = (TypeElement) element;
String annotationName = annotationElement.getQualifiedName().toString();
if (!defaults.containsKey(annotationName)) {
Map defaultValues = new HashMap<>();
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);
}
);
defaults.put(annotationName, defaultValues);
}
}
return ANNOTATION_DEFAULTS.get(annotationTypeName);
}
private boolean isValidDefaultValue(ExecutableElement executableElement) {
AnnotationValue defaultValue = executableElement.getDefaultValue();
if (defaultValue != null) {
Object v = defaultValue.getValue();
if (v != null) {
if (v instanceof String) {
return StringUtils.isNotEmpty((CharSequence) v);
} else {
return true;
}
}
}
return false;
}
@Override
protected String getAnnotationTypeName(AnnotationMirror annotationMirror) {
return JavaModelUtils.getClassName((TypeElement) annotationMirror.getAnnotationType().asElement());
}
private void populateTypeHierarchy(Element element, List hierarchy) {
while (JavaModelUtils.resolveKind(element, ElementKind.CLASS).isPresent()) {
TypeElement typeElement = (TypeElement) element;
List extends TypeMirror> interfaces = typeElement.getInterfaces();
for (TypeMirror anInterface : interfaces) {
if (anInterface instanceof DeclaredType) {
Element interfaceElement = ((DeclaredType) anInterface).asElement();
hierarchy.add(interfaceElement);
populateTypeHierarchy(interfaceElement, hierarchy);
}
}
TypeMirror superMirror = typeElement.getSuperclass();
if (superMirror instanceof DeclaredType) {
DeclaredType type = (DeclaredType) superMirror;
if (type.toString().equals(Object.class.getName())) {
break;
} else {
element = type.asElement();
hierarchy.add(element);
}
} else {
break;
}
}
}
private List findOverriddenMethods(ExecutableElement executableElement) {
List overridden = new ArrayList<>();
Element enclosingElement = executableElement.getEnclosingElement();
if (enclosingElement instanceof TypeElement) {
TypeElement supertype = (TypeElement) enclosingElement;
while (supertype != null && !supertype.toString().equals(Object.class.getName())) {
Optional result = findOverridden(executableElement, supertype);
if (result.isPresent()) {
ExecutableElement overriddenMethod = result.get();
overridden.add(overriddenMethod);
if (!hasAnnotation(overriddenMethod, Override.class)) {
findOverriddenInterfaceMethod(executableElement, overridden, supertype);
break;
}
} else {
findOverriddenInterfaceMethod(executableElement, overridden, supertype);
}
TypeMirror superclass = supertype.getSuperclass();
if (superclass instanceof DeclaredType) {
supertype = (TypeElement) ((DeclaredType) superclass).asElement();
} else {
break;
}
}
}
return overridden;
}
private void findOverriddenInterfaceMethod(ExecutableElement executableElement, List overridden, TypeElement supertype) {
Optional result;
List extends TypeMirror> interfaces = supertype.getInterfaces();
for (TypeMirror anInterface : interfaces) {
if (anInterface instanceof DeclaredType) {
DeclaredType iElement = (DeclaredType) anInterface;
TypeElement interfaceElement = (TypeElement) iElement.asElement();
result = findOverridden(executableElement, interfaceElement);
if (result.isPresent()) {
overridden.add(result.get());
} else {
findOverriddenInterfaceMethod(executableElement, overridden, interfaceElement);
}
}
}
}
private Optional findOverridden(ExecutableElement executableElement, TypeElement supertype) {
Stream extends Element> elements = supertype.getEnclosedElements().stream();
return elements.filter(el -> el.getKind() == ElementKind.METHOD && el.getEnclosingElement().equals(supertype))
.map(el -> (ExecutableElement) el)
.filter(method -> elementUtils.overrides(executableElement, method, (TypeElement) method.getEnclosingElement()))
.findFirst();
}
/**
* 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy