br.com.objectos.code.AnnotationValueWrapper Maven / Gradle / Ivy
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.code;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import javax.lang.model.util.SimpleTypeVisitor6;
/**
* @author [email protected] (Marcio Endo)
*/
public class AnnotationValueWrapper {
private static final Set ERROR_STRING_SET = ImmutableSet.of("", "");
private final ProcessingEnvironmentWrapper processingEnv;
private final ExecutableElement element;
private final AnnotationValue value;
private AnnotationValueWrapper(ProcessingEnvironmentWrapper processingEnv,
ExecutableElement element,
AnnotationValue value) {
this.processingEnv = processingEnv;
this.element = element;
this.value = value;
}
@SuppressWarnings("unchecked")
public static Stream wrapAll(
ProcessingEnvironmentWrapper processingEnv, AnnotationMirror annotation) {
Map extends ExecutableElement, ? extends AnnotationValue> valueMap;
valueMap = processingEnv.getElementValuesWithDefaults(annotation);
Set> set = valueMap.entrySet();
Set> entrySet;
entrySet = (Set>) set;
return entrySet.stream()
.map(input -> {
ExecutableElement element = input.getKey();
AnnotationValue value = input.getValue();
return wrapperOf(processingEnv, element, value);
});
}
public static AnnotationValueWrapper wrapperOf(
ProcessingEnvironmentWrapper processingEnv, ExecutableElement element, AnnotationValue value) {
return new AnnotationValueWrapper(processingEnv, element, value);
}
AnnotationValueInfo toAnnotationValueInfo() {
String name = element.getSimpleName().toString();
return value.accept(new Visitor(name), name);
}
private class Visitor
extends SimpleAnnotationValueVisitor6
implements Function {
String name;
AnnotationValueKind kind;
public Visitor(String name) {
this.name = name;
}
@Override
public AnnotationValueInfo apply(AnnotationValue input) {
return input.accept(this, name);
}
@Override
public AnnotationValueInfo visitAnnotation(AnnotationMirror type, String p) {
kind = AnnotationValueKind.ANNOTATION;
AnnotationMirrorWrapper wrapper = AnnotationMirrorWrapper.wrapperOf(processingEnv, type);
AnnotationInfo value = wrapper.toAnnotationInfo();
return AnnotationValueInfo.newPojo()
.name(p)
.kind(kind)
.value(value)
.build();
}
@Override
public AnnotationValueInfo visitArray(List extends AnnotationValue> list, String p) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy