@SuppressWarnings( "unchecked" ) @Override protected void process( TypeElement annotationType, Element annotated, AnnotationMirror annotation, Map<? extends ExecutableElement, ? extends AnnotationValue> values ) throws IOException { for ( AnnotationValue o : (List<? extends AnnotationValue>) values.values().iterator().next().getValue() ) { TypeMirror service = (TypeMirror) o.getValue(); addTo( ( (TypeElement) annotated ).getQualifiedName().toString(), "META-INF", "services", service.toString() ); } } }
public DeclaredType extractAnnotationClassParameter(Element element, String annotationName, String methodName) { AnnotationMirror annotationMirror = findAnnotationMirror(element, annotationName); Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) { /* * "methodName" is unset when the default value is used */ if (methodName.equals(entry.getKey().getSimpleName().toString())) { AnnotationValue annotationValue = entry.getValue(); return (DeclaredType) annotationValue.getValue(); } } return null; }
private static Map<String, Object> annotationKeyValues(AnnotationMirror mirror) { Map<String, Object> result = new LinkedHashMap<>(); for (ExecutableElement key : mirror.getElementValues().keySet()) { result.put(key.getSimpleName().toString(), mirror.getElementValues().get(key).getValue()); } return result; } }
private static void qualifierToString(AnnotationMirror qualifier, StringBuilder result) { // TODO: guarantee that element values are sorted by name (if there are multiple) result.append('@'); typeToString(qualifier.getAnnotationType(), result, '$'); result.append('('); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : qualifier.getElementValues().entrySet()) { result.append(entry.getKey().getSimpleName()); result.append('='); result.append(entry.getValue().getValue()); } result.append(")/"); }
public static Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) { assert annotationMirror != null; assert parameterValue != null; Object returnValue = null; for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues() .entrySet() ) { if ( parameterValue.equals( entry.getKey().getSimpleName().toString() ) ) { returnValue = entry.getValue().getValue(); break; } } return returnValue; }
private List<TypeMirror> extractListenerTypeMirrors(Element activityElement) { List<? extends AnnotationMirror> annotationMirrors = activityElement.getAnnotationMirrors(); String annotationName = RoboGuice.class.getName(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationName.equals(annotationMirror.getAnnotationType().toString())) { for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) { if ("value".equals(entry.getKey().getSimpleName().toString())) { AnnotationValue action = entry.getValue(); @SuppressWarnings("unchecked") List<AnnotationValue> elements = (List<AnnotationValue>) action.getValue(); List<TypeMirror> listenerTypeMirrors = new ArrayList<>(elements.size()); for (AnnotationValue annotationValue : elements) { listenerTypeMirrors.add((TypeMirror) annotationValue.getValue()); } return listenerTypeMirrors; } } } } return Collections.emptyList(); }
public List<DeclaredType> extractAnnotationClassArrayParameter(Element element, String annotationName, String methodName) { AnnotationMirror annotationMirror = findAnnotationMirror(element, annotationName); Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) { /* * "methodName" is unset when the default value is used */ if (methodName.equals(entry.getKey().getSimpleName().toString())) { AnnotationValue annotationValue = entry.getValue(); @SuppressWarnings("unchecked") List<AnnotationValue> annotationClassArray = (List<AnnotationValue>) annotationValue.getValue(); List<DeclaredType> result = new ArrayList<>(annotationClassArray.size()); for (AnnotationValue annotationClassValue : annotationClassArray) { result.add((DeclaredType) annotationClassValue.getValue()); } return result; } } return null; }
public static TypeMirror getAnnotationValueAsTypeMirror(AnnotationMirror mirror, String method) { for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().toString().equals(method)) { return (TypeMirror) entry.getValue().getValue(); } } return null; }
@SuppressWarnings("unchecked") public static Set<TypeElement> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) { Set<TypeElement> elements = new HashSet<TypeElement>(); for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().toString().equals(method)) { List<AnnotationValue> values = ((List) entry.getValue().getValue()); for (AnnotationValue value : values) { DeclaredType type = (DeclaredType) value.getValue(); elements.add((TypeElement) type.asElement()); } } } return elements; }
return Collections.emptySet(); Object value = excludedModuleAnnotationValue.getValue(); if (value instanceof List) { List<?> values = (List<?>) value;
@Override protected void process( TypeElement annotationType, Element annotated, AnnotationMirror annotation, Map<? extends ExecutableElement,? extends AnnotationValue> values ) { if ( values.size() != 1 ) { error( annotated, annotation, "Annotation values don't match the expectation" ); return; } String value = (String) values.values().iterator().next().getValue(); if ( StringUtils.isBlank( value ) ) { error( annotated, annotation, "Documentation not available for " + annotated ); } } }
@Override public String classFactoryName() { // use mirror to avoid loading classes not generated yet return Mirrors.findAnnotationMirror(element(), Factory.class) .flatMap(Mirrors::findAnnotationValue) .map(value -> value.getValue().toString()).orElse(null); } }
@Nullable static <T extends Annotation> ClassName getClassParamFromAnnotation( Element annotatedElement, Class<T> annotationClass, String paramName, Types typeUtils) { AnnotationMirror am = getAnnotationMirror(annotatedElement, annotationClass); if (am == null) { return null; } AnnotationValue av = getAnnotationValue(am, paramName); if (av == null) { return null; } else { Object value = av.getValue(); if (value instanceof TypeMirror) { return ClassName.get((TypeElement) typeUtils.asElement((TypeMirror) value)); } // Couldn't resolve R class return null; } }
private static List<String> enumerateCustomDeepLinks(Element element, Map<Element, String[]> prefixesMap) { Set<? extends AnnotationMirror> annotationMirrors = AnnotationMirrors.getAnnotatedAnnotations(element, DEEP_LINK_SPEC_CLASS); final List<String> deepLinks = new ArrayList<>(); for (AnnotationMirror customAnnotation : annotationMirrors) { List<? extends AnnotationValue> suffixes = asAnnotationValues(AnnotationMirrors.getAnnotationValue(customAnnotation, "value")); String[] prefixes = prefixesMap.get(customAnnotation.getAnnotationType().asElement()); for (String prefix : prefixes) { for (AnnotationValue suffix : suffixes) { deepLinks.add(prefix + suffix.getValue()); } } } return deepLinks; }
private void registerSubcommands(List<AnnotationValue> typeMirrors, List<CommandSpec> result, IFactory factory, Map<Element, CommandSpec> commands, Map<TypeMirror, List<CommandSpec>> commandTypes, Map<Element, OptionSpec.Builder> options, Map<Element, PositionalParamSpec.Builder> parameters) { for (AnnotationValue typeMirror : typeMirrors) { Element subcommandElement = processingEnv.getElementUtils().getTypeElement( typeMirror.getValue().toString().replace('$', '.')); logger.fine("Processing subcommand: " + subcommandElement); if (isValidSubcommandHasNameAttribute(subcommandElement)) { CommandSpec commandSpec = buildCommand(subcommandElement, factory, commands, commandTypes, options, parameters); result.add(commandSpec); } } }
@SuppressWarnings("unchecked") private void logErrors(Element element) { AnnotationValue errorsArrayValue = utils.getAnnotationValue(element, ModelGenErrors.class, "value"); List<? extends AnnotationValue> errorsList = (List<? extends AnnotationValue>) errorsArrayValue.getValue(); for (AnnotationValue error : errorsList) { logSingleError(error); } }
private void processConverterAnnotation(ElementValidator validator) { if (annotationOf(Convert.class).isPresent()) { Optional<? extends AnnotationMirror> mirror = Mirrors.findAnnotationMirror(element(), Convert.class); converterType = mirror.map(Mirrors::findAnnotationValue) .filter(Optional::isPresent) .map(Optional::get) .map(value -> value.getValue().toString()).orElse(null); } else if (annotationOf(javax.persistence.Convert.class).isPresent()) { Optional<? extends AnnotationMirror> mirror = Mirrors.findAnnotationMirror(element(), javax.persistence.Convert.class); converterType = mirror.map(m -> Mirrors.findAnnotationValue(m, "converter")) .filter(Optional::isPresent) .map(Optional::get) .map(value -> value.getValue().toString()).orElse(null); } if (converterType != null && cardinality != null) { validator.warning("Cannot specify converter on association field", Convert.class); } }
public static ImmutableList<EventDeclarationModel> getEventDeclarations( Elements elements, TypeElement element, Class<?> annotationType, EnumSet<RunMode> runMode) { final List<AnnotationValue> eventTypes = ProcessorUtils.getAnnotationParameter( elements, element, annotationType, "events", List.class); final List<EventDeclarationModel> eventDeclarations; if (eventTypes != null) { eventDeclarations = new ArrayList<>(); for (AnnotationValue eventType : eventTypes) { final DeclaredType type = (DeclaredType) eventType.getValue(); final TypeName returnType = runMode.contains(RunMode.ABI) ? TypeName.VOID : getReturnType(elements, type.asElement()); final ImmutableList<FieldModel> fields = runMode.contains(RunMode.ABI) ? ImmutableList.of() : FieldsExtractor.extractFields(type.asElement()); eventDeclarations.add( new EventDeclarationModel( ClassName.bestGuess(type.asElement().toString()), returnType, fields, type.asElement())); } } else { eventDeclarations = Collections.emptyList(); } return ImmutableList.copyOf(eventDeclarations); }
@Override public String modelName() { // it's important that the AnnotationMirror is used here since the model name needs to be // known before process() is called if (Mirrors.findAnnotationMirror(element(), Entity.class).isPresent()) { return Mirrors.findAnnotationMirror(element(), Entity.class) .flatMap(mirror -> Mirrors.findAnnotationValue(mirror, "model")) .map(value -> value.getValue().toString()) .filter(name -> !Names.isEmpty(name)) .orElse("default"); } else if (Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class).isPresent()) { Elements elements = processingEnvironment.getElementUtils(); Name packageName = elements.getPackageOf(element()).getQualifiedName(); String[] parts = packageName.toString().split("\\."); return parts[parts.length - 1]; } return ""; }
private void logSingleError(AnnotationValue singleErrorAnnotation) { AnnotationMirror singleErrorMirror = (AnnotationMirror) singleErrorAnnotation.getValue(); TypeMirror errorClass = utils.getTypeMirrorsFromAnnotationValue( utils.getAnnotationValueFromMirror(singleErrorMirror, "specClass")).get(0); String errorMessage = utils.getValuesFromAnnotationValue( utils.getAnnotationValueFromMirror(singleErrorMirror, "message"), String.class).get(0); List<String> errorElementValues = utils.getValuesFromAnnotationValue( utils.getAnnotationValueFromMirror(singleErrorMirror, "element"), String.class); String errorElementName = AptUtils.isEmpty(errorElementValues) ? null : errorElementValues.get(0); Element errorElement = findErrorElement(errorClass, errorElementName); utils.getMessager().printMessage(Diagnostic.Kind.ERROR, errorMessage, errorElement); }