io.micronaut.sourcegen.model.AnnotationDef Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-sourcegen-model Show documentation
Show all versions of micronaut-sourcegen-model Show documentation
Micronaut SourceGen exposes a language-neutral API for source code generation.
The newest version!
/*
* Copyright 2017-2023 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.sourcegen.model;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.ast.MethodElement;
import io.micronaut.inject.visitor.VisitorContext;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* The annotation definition.
*
* @author Denis Stepanov
* @since 1.0
*/
@Experimental
public final class AnnotationDef {
private final ClassTypeDef type;
private final Map values;
private AnnotationDef(ClassTypeDef type, Map values) {
this.type = type;
this.values = values;
}
public ClassTypeDef getType() {
return type;
}
public Map getValues() {
return values;
}
public static AnnotationDefBuilder builder(ClassTypeDef type) {
return new AnnotationDefBuilder(type);
}
public static AnnotationDefBuilder builder(Class extends Annotation> annotationType) {
return new AnnotationDefBuilder(ClassTypeDef.of(annotationType));
}
/**
* Create an annotation definition from an {@link AnnotationValue}
* annotation.
* Visitor context is required to deduce the types for
* annotation members, as {@link AnnotationValue} does not retain
* such information. The annotation does not need to be present on
* the classpath, but type mirror information must be retrievable.
*
* @param annotation The annotation
* @param context The visitor context
* @return The copy of given annotation
*
* @since 1.0
*/
public static AnnotationDef of(AnnotationValue> annotation, VisitorContext context) {
ClassElement annotationElement = context.getClassElement(annotation.getAnnotationName())
.orElseThrow(() -> new RuntimeException("Could not create class element for " + annotation.getAnnotationName()));
Map fieldTypes = annotationElement.getMethods().stream()
.collect(Collectors.toMap(MethodElement::getName, MethodElement::getReturnType));
// The other way to determine if the annotation is inner would be use the context to get the class element
ClassTypeDef annotationType = ClassTypeDef.of(annotation.getAnnotationName(), annotation.getAnnotationName().contains("$"));
AnnotationDefBuilder builder = AnnotationDef.builder(annotationType);
annotation.getConvertibleValues().asMap().forEach((key, value) ->
copyAnnotationValue(value, fieldTypes.get(key), context)
.ifPresent(copiedValue -> builder.addMember(key, copiedValue))
);
return builder.build();
}
/**
* A helper method for copying annotation members.
*
* @param value The member value to copy
* @param requiredType The required type of the value
* @param context The visitor context
* @return The optional copied value
*/
private static Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy