/** * Resolve the template for the client annotation. * * @param clientAnnotation client annotation reference * @param templateString template to be applied * @return resolved template contents */ private String resolveTemplate(AnnotationValue<Client> clientAnnotation, String templateString) { String path = clientAnnotation.get("path", String.class).orElse(null); if (StringUtils.isNotEmpty(path)) { return path + templateString; } else { String value = clientAnnotation.getValue(String.class).orElse(null); if (StringUtils.isNotEmpty(value)) { if (value.startsWith("/")) { return value + templateString; } } return templateString; } }
String clientId = clientAnnotation.getValue(String.class).orElse(null); String path = clientAnnotation.get("path", String.class).orElse(null); String clientKey = computeClientKey(clientId, path); clients.remove(clientKey); if (context.hasStereotype(HttpMethodMapping.class) && httpClient != null) { AnnotationValue<HttpMethodMapping> mapping = context.getAnnotation(HttpMethodMapping.class); String uri = mapping.getRequiredValue(String.class); if (StringUtils.isEmpty(uri)) { uri = "/" + context.getMethodName(); String headerName = headerAnnotation.get("name", String.class).orElse(null); String headerValue = headerAnnotation.getValue(String.class).orElse(null); if (StringUtils.isNotEmpty(headerName) && StringUtils.isNotEmpty(headerValue)) { headers.put(headerName, headerValue); .flatMap(versionAnnotation -> versionAnnotation.getValue(String.class)) .filter(StringUtils::isNotEmpty) .ifPresent(version -> { String attributeName = attributeAnnotation.get("name", String.class).orElse(null); Object attributeValue = attributeAnnotation.getValue(Object.class).orElse(null); if (StringUtils.isNotEmpty(attributeName) && attributeValue != null) { attributes.put(attributeName, attributeValue); String serviceId = clientAnnotation.getValue(String.class).orElse(null); Argument<?> errorType = clientAnnotation.get("errorType", Class.class).map((Function<Class, Argument>) Argument::of).orElse(HttpClient.DEFAULT_ERROR_TYPE); request.setAttribute(HttpAttributes.SERVICE_ID, serviceId);
/** * Build the actual {@link AnnotationValue}. * * @return The {@link AnnotationValue} */ public AnnotationValue<T> build() { return new AnnotationValue<>(annotationName, values); }
/** * Gets a list of {@link AnnotationValue} for the given member. * * @param member The member * @param type The type * @param <T> The type * @throws IllegalStateException If no member is available that conforms to the given name and type * @return The result */ public @Nonnull final <T extends Annotation> List<AnnotationValue<T>> getAnnotations(String member, Class<T> type) { AnnotationValue[] values = get(member, AnnotationValue[].class).orElse(null); if (ArrayUtils.isNotEmpty(values)) { List<AnnotationValue<T>> list = new ArrayList<>(values.length); String typeName = type.getName(); for (AnnotationValue value : values) { if (value == null) { continue; } if (value.getAnnotationName().equals(typeName)) { //noinspection unchecked list.add(value); } } return list; } return Collections.emptyList(); }
@Override protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) { try { final String code = annotation.getValue(String.class).orElse(annotation.get("code", String.class).orElse(null)); final HttpStatus status = HttpStatus.valueOf(code); return Collections.singletonList( AnnotationValue.builder(Status.class).value(status).build() ); } catch (IllegalArgumentException e) { // ignore } return Collections.emptyList(); } }
/** * Get the value of the {@code value} member of the annotation. * * @param conversionContext The conversion context * @param <T> The type * @return The result */ public <T> Optional<T> getValue(ArgumentConversionContext<T> conversionContext) { return get(AnnotationMetadata.VALUE_MEMBER, conversionContext); }
@Override protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) { final AnnotationValueBuilder<Async> builder = AnnotationValue.builder(Async.class); annotation.getValue(String.class).ifPresent(builder::value); return Collections.singletonList(builder.build()); } }
/** * @param targetMethod The target method * @param annotationMetadata The annotation metadata * @param transactionManagerName The transaction manager * @return The {@link TransactionAttribute} */ protected TransactionAttribute resolveTransactionAttribute( ExecutableMethod<Object, Object> targetMethod, AnnotationMetadata annotationMetadata, String transactionManagerName) { return transactionDefinitionMap.computeIfAbsent(targetMethod, method -> { AnnotationValue<Transactional> annotation = annotationMetadata.getAnnotation(Transactional.class); if (annotation == null) { throw new IllegalStateException("No declared @Transactional annotation present"); } BindableRuleBasedTransactionAttribute attribute = new BindableRuleBasedTransactionAttribute(); attribute.setReadOnly(annotation.getRequiredValue("readOnly", Boolean.class)); attribute.setTimeout(annotation.getRequiredValue("timeout", Integer.class)); //noinspection unchecked attribute.setRollbackFor(annotation.get("rollbackFor", Class[].class).orElse(ReflectionUtils.EMPTY_CLASS_ARRAY)); //noinspection unchecked attribute.setNoRollbackFor(annotation.get("noRollbackFor", Class[].class).orElse(ReflectionUtils.EMPTY_CLASS_ARRAY)); attribute.setPropagationBehavior(annotation.getRequiredValue("propagation", Propagation.class).value()); attribute.setIsolationLevel(annotation.getRequiredValue("isolation", Isolation.class).value()); attribute.setQualifier(transactionManagerName); return attribute; }); }
Optional<Class> beanType = replacesAnn.getValue(Class.class); Optional<Class> factory = replacesAnn.get("factory", Class.class); if (replacesAnn.contains(NAMED_MEMBER)) { final String qualifier = replacesAnn.get(NAMED_MEMBER, String.class).orElse(null); if (qualifier != null) { final Class type = beanType.orElse(factory.orElse(null));
private boolean matchesPresenceOfEntities(ConditionContext context, AnnotationValue<Requires> annotationValue) { if (annotationValue.contains("entities")) { Optional<AnnotationClassValue[]> classNames = annotationValue.get("entities", AnnotationClassValue[].class); if (classNames.isPresent()) { BeanContext beanContext = context.getBeanContext(); if (beanContext instanceof ApplicationContext) { ApplicationContext applicationContext = (ApplicationContext) beanContext; final AnnotationClassValue[] classValues = classNames.get(); for (AnnotationClassValue<?> classValue : classValues) { final Optional<? extends Class<?>> entityType = classValue.getType(); if (!entityType.isPresent()) { context.fail("Annotation type [" + classValue.getName() + "] not present on classpath"); return false; } else { Environment environment = applicationContext.getEnvironment(); Class annotationType = entityType.get(); if (!environment.scan(annotationType).findFirst().isPresent()) { context.fail("No entities found in packages [" + String.join(", ", environment.getPackages()) + "] for annotation: " + annotationType); return false; } } } } } } return true; }
private String getClientId(AnnotationValue<Client> clientAnn) { String clientId = clientAnn.getValue(String.class).orElse(null); if (clientId == null) { throw new HttpClientException("Either the id or value of the @Client annotation must be specified"); } return clientId; }
@Override protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) { final String[] produces = annotation.get("produces", String[].class).orElse(null); final AnnotationValue<?> readOp = AnnotationValue.builder("io.micronaut.management.endpoint.annotation." + operationName()).build(); List<AnnotationValue<?>> annotationValues = new ArrayList<>(2); annotationValues.add(readOp); if (produces != null) { final AnnotationValue<Produces> producesAnn = AnnotationValue.builder(Produces.class).member("value", produces).build(); annotationValues.add(producesAnn); } return annotationValues; }
@Override protected AnnotationValueBuilder<? extends Annotation> buildAnnotation() { return AnnotationValue.builder(CacheInvalidate.class); } }
/** * Get the value of the {@code value} member of the annotation. * * @param type The type * @param <T> The type * @throws IllegalStateException If no member is available that conforms to the given type * @return The result */ public @Nonnull final <T> T getRequiredValue(Class<T> type) { return getRequiredValue(AnnotationMetadata.VALUE_MEMBER, type); }
io.micronaut.core.annotation.AnnotationValue[] values = (io.micronaut.core.annotation.AnnotationValue[]) value; for (io.micronaut.core.annotation.AnnotationValue av : values) { OptionalValues<Object> aliasForValues = OptionalValues.of(Object.class, av.getValues()); processAnnotationAlias( metadata, AnnotationValue<?> annotationValue = new AnnotationValue(annotationName, annotationValues); VisitorContext visitorContext = createVisitorContext(); for (AnnotationMapper mapper : mappers) { if (o instanceof AnnotationValue) { AnnotationValue av = (AnnotationValue) o; String mappedAnnotationName = av.getAnnotationName(); av.getValues() ); } else { metadata.addAnnotation( mappedAnnotationName, av.getValues() );
/** * Default constructor. * * @param environment The environment * @param target The target */ EnvironmentAnnotationValue(Environment environment, AnnotationValue<A> target) { super(target, AnnotationMetadataSupport.getDefaultValues(target.getAnnotationName()), EnvironmentConvertibleValuesMap.of( environment, target.getValues() )); }
private void addRepeatableInternal(String annotationName, String member, io.micronaut.core.annotation.AnnotationValue annotationValue, Map<String, Map<CharSequence, Object>> allAnnotations) { if (repeated == null) { repeated = new HashMap<>(2); } repeated.put(annotationName, annotationValue.getAnnotationName()); Map<CharSequence, Object> values = allAnnotations.computeIfAbsent(annotationName, s -> new HashMap<>()); Object v = values.get(member); if (v != null) { if (v.getClass().isArray()) { Object[] array = (Object[]) v; List newValues = new ArrayList(array.length + 1); newValues.addAll(Arrays.asList(array)); newValues.add(annotationValue); values.put(member, newValues); } else if (v instanceof Collection) { ((Collection) v).add(annotationValue); } } else { ArrayList<Object> newValues = new ArrayList<>(2); newValues.add(annotationValue); values.put(member, newValues); } }
/** * Returns whether the value of the given member is present. * * @param annotation The annotation class * @param member The annotation member * @return True if the value is true */ default boolean isPresent(@Nonnull String annotation, @Nonnull String member) { ArgumentUtils.requireNonNull("annotation", annotation); ArgumentUtils.requireNonNull("member", member); return findAnnotation(annotation).map(av -> av.contains(member)).orElse(false); }
@Override protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) { List<AnnotationValue<?>> newAnnotations = new ArrayList<>(3); final AnnotationValueBuilder<Bean> beanAnn = AnnotationValue.builder(Bean.class); final Optional<String> destroyMethod = annotation.get("destroyMethod", String.class); destroyMethod.ifPresent(s -> beanAnn.member("preDestroy", s)); newAnnotations.add(beanAnn.build()); newAnnotations.add(AnnotationValue.builder(DefaultScope.class) .value(Singleton.class) .build()); final String beanName = annotation.getValue(String.class).orElse(annotation.get("name", String.class).orElse(null)); if (StringUtils.isNotEmpty(beanName)) { newAnnotations.add(AnnotationValue.builder(Named.class).value(beanName).build()); } return newAnnotations; } }
/** * Get the value of the {@code value} member of the annotation. * * @param member The member * @param type The type * @param <T> The type * @throws IllegalStateException If no member is available that conforms to the given name and type * @return The result */ public @Nonnull final <T> T getRequiredValue(String member, Class<T> type) { return get(member, ConversionContext.of(type)).orElseThrow(() -> new IllegalStateException("No value available for annotation member @" + annotationName + "[" + member + "] of type: " + type)); }