下面列出了org.springframework.core.annotation.AnnotationUtils#synthesizeAnnotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Annotation synthesizeWithMethodParameterNameAsFallbackValue(
Annotation parameterAnnotation, Method method, int parameterIndex) {
Map<String, Object> annotationAttributes = AnnotationUtils
.getAnnotationAttributes(parameterAnnotation);
Object defaultValue = AnnotationUtils.getDefaultValue(parameterAnnotation);
if (defaultValue instanceof String
&& defaultValue.equals(annotationAttributes.get(AnnotationUtils.VALUE))) {
Type[] parameterTypes = method.getGenericParameterTypes();
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
if (shouldAddParameterName(parameterIndex, parameterTypes, parameterNames)) {
annotationAttributes.put(AnnotationUtils.VALUE,
parameterNames[parameterIndex]);
}
}
return AnnotationUtils.synthesizeAnnotation(annotationAttributes,
parameterAnnotation.annotationType(), null);
}
private Annotation synthesizeWithMethodParameterNameAsFallbackValue(
Annotation parameterAnnotation, Method method, int parameterIndex) {
Map<String, Object> annotationAttributes = AnnotationUtils
.getAnnotationAttributes(parameterAnnotation);
Object defaultValue = AnnotationUtils.getDefaultValue(parameterAnnotation);
if (defaultValue instanceof String
&& defaultValue.equals(annotationAttributes.get(AnnotationUtils.VALUE))) {
Type[] parameterTypes = method.getGenericParameterTypes();
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
if (shouldAddParameterName(parameterIndex, parameterTypes, parameterNames)) {
annotationAttributes.put(AnnotationUtils.VALUE,
parameterNames[parameterIndex]);
}
}
return AnnotationUtils.synthesizeAnnotation(annotationAttributes,
parameterAnnotation.annotationType(), null);
}
private Annotation synthesizeWithMethodParameterNameAsFallbackValue(
Annotation parameterAnnotation, Method method, int parameterIndex) {
Map<String, Object> annotationAttributes = AnnotationUtils
.getAnnotationAttributes(parameterAnnotation);
Object defaultValue = AnnotationUtils.getDefaultValue(parameterAnnotation);
if (defaultValue instanceof String
&& defaultValue.equals(annotationAttributes.get(AnnotationUtils.VALUE))) {
Type[] parameterTypes = method.getGenericParameterTypes();
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
if (shouldAddParameterName(parameterIndex, parameterTypes, parameterNames)) {
annotationAttributes.put(AnnotationUtils.VALUE,
parameterNames[parameterIndex]);
}
}
return AnnotationUtils.synthesizeAnnotation(annotationAttributes,
parameterAnnotation.annotationType(), null);
}
@Test
public void synthesizeLogIn() {
// given
Map<String, Object> attributes = new HashMap<>();
attributes.put("level", ERROR);
attributes.put("ifEnabled", WARN);
attributes.put("verbose", INFO);
attributes.put("printer", "printer");
attributes.put("logger", "logger");
Log log = AnnotationUtils.synthesizeAnnotation(attributes, Log.class, null);
// when
Log.in logIn = annotationExtractor.synthesizeLogIn(log);
// then
assertThat(logIn.level(), is(ERROR));
assertThat(logIn.ifEnabled(), is(WARN));
assertThat(logIn.verbose(), is(INFO));
assertThat(logIn.printer(), is("printer"));
assertThat(logIn.logger(), is("logger"));
}
@Test
public void synthesizeLogOut() {
// given
Map<String, Object> attributes = new HashMap<>();
attributes.put("level", ERROR);
attributes.put("ifEnabled", WARN);
attributes.put("verbose", INFO);
attributes.put("printer", "printer");
attributes.put("logger", "logger");
Log log = AnnotationUtils.synthesizeAnnotation(attributes, Log.class, null);
// when
Log.out logOut = annotationExtractor.synthesizeLogOut(log);
// then
assertThat(logOut.level(), is(ERROR));
assertThat(logOut.ifEnabled(), is(WARN));
assertThat(logOut.verbose(), is(INFO));
assertThat(logOut.printer(), is("printer"));
assertThat(logOut.logger(), is("logger"));
}
/**
* Overrides the default {@link StreamListenerAnnotationBeanPostProcessor}.
*/
@Bean(name = STREAM_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_NAME)
public static StreamListenerAnnotationBeanPostProcessor streamListenerAnnotationBeanPostProcessor() {
return new StreamListenerAnnotationBeanPostProcessor() {
@Override
protected StreamListener postProcessAnnotation(
StreamListener originalAnnotation, Method annotatedMethod) {
Map<String, Object> attributes = new HashMap<>(
AnnotationUtils.getAnnotationAttributes(originalAnnotation));
attributes.put("condition",
"headers['type']=='" + originalAnnotation.condition() + "'");
return AnnotationUtils.synthesizeAnnotation(attributes,
StreamListener.class, annotatedMethod);
}
};
}
protected Annotation synthesizeWithMethodParameterNameAsFallbackValue(Annotation parameterAnnotation, Method method,
int parameterIndex) {
Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(parameterAnnotation);
Object defaultValue = AnnotationUtils.getDefaultValue(parameterAnnotation);
if (defaultValue instanceof String && defaultValue.equals(annotationAttributes.get(AnnotationUtils.VALUE))) {
Type[] parameterTypes = method.getGenericParameterTypes();
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
if (shouldAddParameterName(parameterIndex, parameterTypes, parameterNames)) {
annotationAttributes.put(AnnotationUtils.VALUE, parameterNames[parameterIndex]);
}
}
return AnnotationUtils.synthesizeAnnotation(annotationAttributes, parameterAnnotation.annotationType(), null);
}
@Override
public Annotation[] getParameterAnnotations() {
Annotation[][] annotationArray = this.interfaceMethod.getParameterAnnotations();
if (this.getParameterIndex() >= 0 && this.getParameterIndex() < annotationArray.length) {
this.parameterAnnotations = annotationArray[this.getParameterIndex()];
for (int i = 0; i < this.parameterAnnotations.length; i++) {
this.parameterAnnotations[i] =
AnnotationUtils.synthesizeAnnotation(this.parameterAnnotations[i], null);
}
} else {
this.parameterAnnotations = new Annotation[0];
}
return this.parameterAnnotations;
}
@Override
public Annotation[] getParameterAnnotations() {
Annotation[][] annotationArray = this.interfaceMethod.getParameterAnnotations();
if (this.getParameterIndex() >= 0 && this.getParameterIndex() < annotationArray.length) {
this.parameterAnnotations = annotationArray[this.getParameterIndex()];
for (int i = 0; i < this.parameterAnnotations.length; i++) {
this.parameterAnnotations[i] =
AnnotationUtils.synthesizeAnnotation(this.parameterAnnotations[i], null);
}
} else {
this.parameterAnnotations = new Annotation[0];
}
return this.parameterAnnotations;
}
@Test(expected = AnnotationUsageException.class)
public void validateLevels() {
// given
PrinterResolver printerResolver = new AliasedPrinterResolver(emptyMap(), emptyMap());
LogValidator<Log> logValidator = new LogValidator<>(printerResolver);
Log log = AnnotationUtils.synthesizeAnnotation(singletonMap("ifEnabled", DEBUG), Log.class, null);
// when
logValidator.validate(method, log);
// then expected exception
}
@Test(expected = AnnotationUsageException.class)
public void validatePrinter() {
// given
PrinterResolver printerResolver = new AliasedPrinterResolver(emptyMap(), emptyMap());
LogValidator<Log> logValidator = new LogValidator<>(printerResolver);
Log log = AnnotationUtils.synthesizeAnnotation(singletonMap("printer", "unknown"), Log.class, null);
// when
logValidator.validate(method, log);
// then expected exception
}
@Test(expected = AnnotationUsageException.class)
public void validateEmptyWithoutParameters() {
// given
Mdc mdc = AnnotationUtils.synthesizeAnnotation(Mdc.class);
Collection<Mdc> target = singletonList(mdc);
// when
methodMdcsValidator.validate(method, target);
// then expected exception
}
@Test(expected = AnnotationUsageException.class)
public void validateDuplicate() {
// given
Mdc mdc = AnnotationUtils.synthesizeAnnotation(singletonMap("key", "key"), Mdc.class, null);
Mdc mdc1 = AnnotationUtils.synthesizeAnnotation(singletonMap("key", "key"), Mdc.class, null);
Collection<Mdc> target = asList(mdc, mdc1);
// when
mergedMdcsValidator.validate(method, target);
// then expected exception
}
@Test(expected = AnnotationUsageException.class)
public void validateDuplicate() {
// given
Mdc mdc = AnnotationUtils.synthesizeAnnotation(singletonMap("key", "key"), Mdc.class, null);
Mdc mdc1 = AnnotationUtils.synthesizeAnnotation(singletonMap("key", "key"), Mdc.class, null);
Collection<Mdc> target = asList(mdc, mdc1);
// when
mdcsValidator.validate(method, target);
// then expected exception
}
@Test(expected = AnnotationUsageException.class)
public void validateEmptyDuplicate() {
// given
Mdc mdc = AnnotationUtils.synthesizeAnnotation(singletonMap("key", ""), Mdc.class, null);
Mdc mdc1 = AnnotationUtils.synthesizeAnnotation(singletonMap("key", ""), Mdc.class, null);
Collection<Mdc> target = asList(mdc, mdc1);
// when
mdcsValidator.validate(method, target);
// then expected exception
}
private static FlywayTest copyAnnotation(FlywayTest annotation, boolean invokeCleanDB, boolean invokeBaselineDB, boolean invokeMigrateDB) {
Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
attributes.put("invokeCleanDB", invokeCleanDB);
attributes.put("invokeBaselineDB", invokeBaselineDB);
attributes.put("invokeMigrateDB", invokeMigrateDB);
return AnnotationUtils.synthesizeAnnotation(attributes, FlywayTest.class, null);
}
private Class<?>[] collectClasses(AnnotationAttributes attrs, String className) {
EnableBinding enableBinding = AnnotationUtils.synthesizeAnnotation(attrs,
EnableBinding.class, ClassUtils.resolveClassName(className, null));
return enableBinding.value();
}
/**
* Synthesize the merged {@link #getAnnotationAttributes AnnotationAttributes}
* in this descriptor back into an annotation of the target
* {@linkplain #getAnnotationType annotation type}.
* @since 4.2
* @see #getAnnotationAttributes()
* @see #getAnnotationType()
* @see AnnotationUtils#synthesizeAnnotation(java.util.Map, Class, java.lang.reflect.AnnotatedElement)
*/
@SuppressWarnings("unchecked")
public T synthesizeAnnotation() {
return AnnotationUtils.synthesizeAnnotation(
getAnnotationAttributes(), (Class<T>) getAnnotationType(), getRootDeclaringClass());
}
/**
* Synthesize the merged {@link #getAnnotationAttributes AnnotationAttributes}
* in this descriptor back into an annotation of the target
* {@linkplain #getAnnotationType annotation type}.
* @since 4.2
* @see #getAnnotationAttributes()
* @see #getAnnotationType()
* @see AnnotationUtils#synthesizeAnnotation(java.util.Map, Class, java.lang.reflect.AnnotatedElement)
*/
@SuppressWarnings("unchecked")
public T synthesizeAnnotation() {
return AnnotationUtils.synthesizeAnnotation(
getAnnotationAttributes(), (Class<T>) getAnnotationType(), getRootDeclaringClass());
}
/**
* Synthesize the merged {@link #getAnnotationAttributes AnnotationAttributes}
* in this descriptor back into an annotation of the target
* {@linkplain #getAnnotationType annotation type}.
* @since 4.2
* @see #getAnnotationAttributes()
* @see #getAnnotationType()
* @see AnnotationUtils#synthesizeAnnotation(java.util.Map, Class, java.lang.reflect.AnnotatedElement)
*/
@SuppressWarnings("unchecked")
public T synthesizeAnnotation() {
return AnnotationUtils.synthesizeAnnotation(getAnnotationAttributes(), (Class<T>) getAnnotationType(),
getRootDeclaringClass());
}