下面列出了怎么用org.springframework.core.annotation.MergedAnnotations的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException {
MergedAnnotation<ManagedAttribute> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE)
.get(ManagedAttribute.class).withNonMergedAttributes();
if (!ann.isPresent()) {
return null;
}
org.springframework.jmx.export.metadata.ManagedAttribute bean = new org.springframework.jmx.export.metadata.ManagedAttribute();
Map<String, Object> map = ann.asMap();
MutablePropertyValues pvs = new MutablePropertyValues(map);
pvs.removePropertyValue("defaultValue");
PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(pvs);
String defaultValue = (String) map.get("defaultValue");
if (defaultValue.length() > 0) {
bean.setDefaultValue(defaultValue);
}
return bean;
}
private URL[] processUrls(URL[] urls, Class<?> testClass) throws Exception {
MergedAnnotations annotations = MergedAnnotations.from(testClass,
SearchStrategy.TYPE_HIERARCHY);
ClassPathEntryFilter filter = new ClassPathEntryFilter(
annotations.get(ClassPathExclusions.class));
List<URL> processedUrls = new ArrayList<>();
List<URL> additionalUrls = getAdditionalUrls(
annotations.get(ClassPathOverrides.class));
processedUrls.addAll(additionalUrls);
for (URL url : urls) {
if (!filter.isExcluded(url)) {
processedUrls.add(url);
}
}
return processedUrls.toArray(new URL[0]);
}
@Override
protected String[] getProperties(Class<?> testClass) {
String[] properties = MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS)
.get(ReactiveDataNeo4jTest.class)
.getValue("properties", String[].class).orElseGet(() -> new String[0]);
String[] finalProperties = new String[properties.length + 1];
System.arraycopy(properties, 0, finalProperties, 0, properties.length);
finalProperties[finalProperties.length - 1] = "spring.data.neo4j.repositories.type=reactive";
return finalProperties;
}
@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
MergedAnnotation<ManagedResource> ann = MergedAnnotations.from(beanClass, SearchStrategy.EXHAUSTIVE)
.get(ManagedResource.class).withNonMergedAttributes();
if (!ann.isPresent()) {
return null;
}
Class<?> declaringClass = (Class<?>) ann.getSource();
Class<?> target = (declaringClass != null && !declaringClass.isInterface() ? declaringClass : beanClass);
if (!Modifier.isPublic(target.getModifiers())) {
throw new InvalidMetadataException("@ManagedResource class '" + target.getName() + "' must be public");
}
org.springframework.jmx.export.metadata.ManagedResource bean = new org.springframework.jmx.export.metadata.ManagedResource();
Map<String, Object> map = ann.asMap();
List<PropertyValue> list = new ArrayList<>(map.size());
map.forEach((attrName, attrValue) -> {
if (!"value".equals(attrName)) {
Object value = attrValue;
if (this.embeddedValueResolver != null && value instanceof String) {
value = this.embeddedValueResolver.resolveStringValue((String) value);
}
list.add(new PropertyValue(attrName, value));
}
});
PropertyAccessorFactory.forBeanPropertyAccess(bean).setPropertyValues(new MutablePropertyValues(list));
return bean;
}
@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException {
MergedAnnotation<ManagedMetric> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE)
.get(ManagedMetric.class).withNonMergedAttributes();
return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedMetric.class);
}
@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException {
MergedAnnotation<ManagedOperation> ann = MergedAnnotations.from(method, SearchStrategy.EXHAUSTIVE)
.get(ManagedOperation.class).withNonMergedAttributes();
return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedOperation.class);
}
private static List<MergedAnnotation<? extends Annotation>> getRepeatableAnnotations(
AnnotatedElement annotatedElement, Class<? extends Annotation> annotationType,
Class<? extends Annotation> containerAnnotationType) {
return MergedAnnotations.from(annotatedElement, SearchStrategy.EXHAUSTIVE,
RepeatableContainers.of(annotationType, containerAnnotationType), AnnotationFilter.PLAIN)
.stream(annotationType)
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
.map(MergedAnnotation::withNonMergedAttributes)
.collect(Collectors.toList());
}
private void updateConsumesCondition(RequestMappingInfo info, Method method) {
ConsumesRequestCondition condition = info.getConsumesCondition();
if (!condition.isEmpty()) {
for (Parameter parameter : method.getParameters()) {
MergedAnnotation<RequestBody> annot = MergedAnnotations.from(parameter).get(RequestBody.class);
if (annot.isPresent()) {
condition.setBodyRequired(annot.getBoolean("required"));
break;
}
}
}
}
SimpleAnnotationMetadata(String className, int access, @Nullable String enclosingClassName,
@Nullable String superClassName, boolean independentInnerClass, String[] interfaceNames,
String[] memberClassNames, MethodMetadata[] annotatedMethods, MergedAnnotations annotations) {
this.className = className;
this.access = access;
this.enclosingClassName = enclosingClassName;
this.superClassName = superClassName;
this.independentInnerClass = independentInnerClass;
this.interfaceNames = interfaceNames;
this.memberClassNames = memberClassNames;
this.annotatedMethods = annotatedMethods;
this.annotations = annotations;
}
@Override
public void visitEnd() {
String[] memberClassNames = StringUtils.toStringArray(this.memberClassNames);
MethodMetadata[] annotatedMethods = this.annotatedMethods.toArray(new MethodMetadata[0]);
MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
this.metadata = new SimpleAnnotationMetadata(this.className, this.access,
this.enclosingClassName, this.superClassName, this.independentInnerClass,
this.interfaceNames, memberClassNames, annotatedMethods, annotations);
}
public SimpleMethodMetadata(String methodName, int access, String declaringClassName,
String returnTypeName, MergedAnnotations annotations) {
this.methodName = methodName;
this.access = access;
this.declaringClassName = declaringClassName;
this.returnTypeName = returnTypeName;
this.annotations = annotations;
}
/**
* Get the fully qualified class names of all meta-annotation types that
* are <em>present</em> on the given annotation type on the underlying class.
* @param annotationName the fully qualified class name of the meta-annotation
* type to look for
* @return the meta-annotation type names, or an empty set if none found
*/
default Set<String> getMetaAnnotationTypes(String annotationName) {
MergedAnnotation<?> annotation = getAnnotations().get(annotationName, MergedAnnotation::isDirectlyPresent);
if (!annotation.isPresent()) {
return Collections.emptySet();
}
return MergedAnnotations.from(annotation.getType(), SearchStrategy.INHERITED_ANNOTATIONS).stream()
.map(mergedAnnotation -> mergedAnnotation.getType().getName())
.collect(Collectors.toCollection(LinkedHashSet::new));
}
private void updateConsumesCondition(RequestMappingInfo info, Method method) {
ConsumesRequestCondition condition = info.getConsumesCondition();
if (!condition.isEmpty()) {
for (Parameter parameter : method.getParameters()) {
MergedAnnotation<RequestBody> annot = MergedAnnotations.from(parameter).get(RequestBody.class);
if (annot.isPresent()) {
condition.setBodyRequired(annot.getBoolean("required"));
break;
}
}
}
}
@Nullable
private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
MergedAnnotations annotations = MergedAnnotations.from(ao, SearchStrategy.INHERITED_ANNOTATIONS);
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
MergedAnnotation<?> annotation = annotations.get(type);
if (annotation.isPresent()) {
return annotation;
}
}
return null;
}
@Override
protected String[] getProperties(Class<?> testClass) {
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataNeo4jTest.class)
.getValue("properties", String[].class).orElse(null);
}
@Override
public MergedAnnotations getAnnotations() {
return this.mergedAnnotations;
}
@Override
public MergedAnnotations getAnnotations() {
return this.mergedAnnotations;
}
@Override
public MergedAnnotations getAnnotations() {
return this.annotations;
}
@Override
public MergedAnnotations getAnnotations() {
throw new UnsupportedOperationException();
}
@Override
public MergedAnnotations getAnnotations() {
throw new UnsupportedOperationException();
}
@Override
public MergedAnnotations getAnnotations() {
return this.annotations;
}
@Override
@NonNull
public MergedAnnotations getAnnotations() {
throw new UnsupportedOperationException("Not yet supported");
}
/**
* Create a new {@link StandardAnnotationMetadata} wrapper for the given Class,
* providing the option to return any nested annotations or annotation arrays in the
* form of {@link org.springframework.core.annotation.AnnotationAttributes} instead
* of actual {@link Annotation} instances.
* @param introspectedClass the Class to introspect
* @param nestedAnnotationsAsMap return nested annotations and annotation arrays as
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
* with ASM-based {@link AnnotationMetadata} implementations
* @since 3.1.1
* @deprecated since 5.2 in favor of the factory method {@link AnnotationMetadata#introspect(Class)}.
* Use {@link MergedAnnotation#asMap(org.springframework.core.annotation.MergedAnnotation.Adapt...) MergedAnnotation.asMap}
* from {@link #getAnnotations()} rather than {@link #getAnnotationAttributes(String)}
* if {@code nestedAnnotationsAsMap} is {@code false}
*/
@Deprecated
public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) {
super(introspectedClass);
this.mergedAnnotations = MergedAnnotations.from(introspectedClass,
SearchStrategy.DIRECT, RepeatableContainers.none(),
AnnotationFilter.NONE);
this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
}
/**
* Create a new StandardMethodMetadata wrapper for the given Method,
* providing the option to return any nested annotations or annotation arrays in the
* form of {@link org.springframework.core.annotation.AnnotationAttributes} instead
* of actual {@link java.lang.annotation.Annotation} instances.
* @param introspectedMethod the Method to introspect
* @param nestedAnnotationsAsMap return nested annotations and annotation arrays as
* {@link org.springframework.core.annotation.AnnotationAttributes} for compatibility
* with ASM-based {@link AnnotationMetadata} implementations
* @since 3.1.1
* @deprecated since 5.2 in favor of obtaining instances via {@link AnnotationMetadata}
*/
@Deprecated
public StandardMethodMetadata(Method introspectedMethod, boolean nestedAnnotationsAsMap) {
Assert.notNull(introspectedMethod, "Method must not be null");
this.introspectedMethod = introspectedMethod;
this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
this.mergedAnnotations = MergedAnnotations.from(introspectedMethod,
SearchStrategy.DIRECT, RepeatableContainers.none(),
AnnotationFilter.PLAIN);
}
/**
* Return annotation details based on the direct annotations of the
* underlying element.
* @return merged annotations based on the direct annotations
*/
MergedAnnotations getAnnotations();