io.toolisticon.aptk.annotationwrapper.test.annotationonpackage.EmbeddedAnnotationWrapper Maven / Gradle / Ivy
The newest version!
package io.toolisticon.aptk.annotationwrapper.test.annotationonpackage;
import io.toolisticon.aptk.annotationwrapper.test.EmbeddedAnnotation;
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.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import io.toolisticon.aptk.tools.AnnotationUtils;
import io.toolisticon.aptk.tools.TypeMirrorWrapper;
import io.toolisticon.aptk.tools.TypeUtils;
import io.toolisticon.aptk.tools.wrapper.CompileMessageWriter;
import io.toolisticon.aptk.tools.wrapper.ElementWrapper;
/**
* Wrapper class to read attribute values from Annotation EmbeddedAnnotation.
*/
class EmbeddedAnnotationWrapper {
private final Element annotatedElement;
private final AnnotationMirror annotationMirror;
/**
* Private constructor.
* Used to read annotation from Element.
* @param annotatedElement the annotated Element to annotated with this wrapper annotation
*/
private EmbeddedAnnotationWrapper (Element annotatedElement) {
this.annotatedElement = annotatedElement;
this.annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement, EmbeddedAnnotation.class);
}
/**
* Private constructor.
* Mainly used for embedded annotations.
* @param element the element related with the passed annotationMirror
* @param annotationMirror the AnnotationMirror to wrap
*/
private EmbeddedAnnotationWrapper (Element element, AnnotationMirror annotationMirror) {
this.annotatedElement = element;
this.annotationMirror = annotationMirror;
}
/**
* Gets the element on which the wrapped annotation is used.
*/
Element _annotatedElement() {
return this.annotatedElement;
}
/**
* Gets the wrapped AnnotationMirror.
*/
AnnotationMirror _annotationMirror() {
return this.annotationMirror;
}
/**
* Gets the EmbeddedAnnotation.value from wrapped annotation as AnnotationValue.
* @return the attributes AnnotationValue
*/
AnnotationValue valueAsAnnotationValue() {
return AnnotationUtils.getAnnotationValueOfAttributeWithDefaults(annotationMirror, "value");
}
/**
* The Wrapper type for the annotation attribute 'value'.
*/
class EmbeddedAnnotationAttributevalueWrapper{
/**
* Gets the EmbeddedAnnotation.value from wrapped annotation.
* @return the attribute value
*/
long getValue() {
return EmbeddedAnnotationWrapper.this.value();
}
/**
* Writes compiler message and binds them to annotation.
* @return a compiler message builder
*/
CompileMessageWriter.CompileMessageWriterStart compilerMessage() {
return CompileMessageWriter.at(EmbeddedAnnotationWrapper.this.annotatedElement, EmbeddedAnnotationWrapper.this.annotationMirror, valueAsAnnotationValue());
}
}
/**
* Gets the EmbeddedAnnotation.value as wrapped attribute from wrapped annotation.
* @return the attribute value
*/
EmbeddedAnnotationAttributevalueWrapper valueAsAttributeWrapper() {
return new EmbeddedAnnotationAttributevalueWrapper();
}
/**
* Gets the EmbeddedAnnotation.value from wrapped annotation.
* @return the attribute value
*/
long value() {
return (long)valueAsAnnotationValue().getValue();
}
/**
* Checks if passed element is annotated with this wrapper annotation type : EmbeddedAnnotation
* @param element The element to check for wrapped annotation type
* @return true, if passed element is annotated with EmbeddedAnnotation annotation, otherwise false
*/
static boolean isAnnotated(Element element) {
return element != null && element.getAnnotation(EmbeddedAnnotation.class) != null;
}
/**
* Writes compiler message and binds them to annotation.
* @return a compiler message builder
*/
CompileMessageWriter.CompileMessageWriterStart compilerMessage() {
return CompileMessageWriter.at(EmbeddedAnnotationWrapper.this.annotatedElement, EmbeddedAnnotationWrapper.this.annotationMirror);
}
/**
* Gets the AnnotationMirror from passed element for this wrappers annotation type and creates a wrapper instance.
* @param element The element to read the annotations from
* @return The wrapped AnnotationMirror if Element is annotated with this wrappers annotation type, otherwise null.
*/
static EmbeddedAnnotationWrapper wrap(Element element) {
return isAnnotated(element) ? new EmbeddedAnnotationWrapper(element) : null;
}
/**
* Gets the AnnotationMirror from passed element for this wrappers annotation type and creates a wrapper instance.
* @param element The element to read the annotations from
* @return The wrapped AnnotationMirror if Element is annotated with this wrappers annotation type, otherwise null.
*/
static EmbeddedAnnotationWrapper wrap(ElementWrapper element) {
return wrap(element.unwrap());
}
/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param annotationMirror The element annotated with the annotation to wrap
* @return The wrapper instance
*/
static EmbeddedAnnotationWrapper wrap(AnnotationMirror annotationMirror) {
return new EmbeddedAnnotationWrapper(null, annotationMirror);
}
/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param element the element bound to the usage of passed AnnotationMirror
* @param annotationMirror The AnnotationMirror to wrap
* @return The wrapper instance
*/
static EmbeddedAnnotationWrapper wrap(Element element, AnnotationMirror annotationMirror) {
return new EmbeddedAnnotationWrapper(element, annotationMirror);
}
}