下面列出了org.springframework.core.type.AnnotatedTypeMetadata#getAllAnnotationAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Set<String> retrieveAnnotatedProfiles(AnnotatedTypeMetadata metadata, String annotationType) {
if (!metadata.isAnnotated(annotationType)) {
return Collections.emptySet();
}
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType);
if (attributes == null) {
return Collections.emptySet();
}
Set<String> profiles = Sets.newHashSet();
List<?> values = attributes.get("value");
if (values != null) {
for (Object value : values) {
if (value instanceof String[]) {
Collections.addAll(profiles, (String[]) value);
} else {
profiles.add((String) value);
}
}
}
return profiles;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 获取 ConditionalOnSystemProperty 所有的属性方法值
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
// 获取 ConditionalOnSystemProperty#name() 方法值(单值)
String propertyName = (String) attributes.getFirst("name");
// 获取 ConditionalOnSystemProperty#value() 方法值(单值)
String propertyValue = (String) attributes.getFirst("value");
// 获取 系统属性值
String systemPropertyValue = System.getProperty(propertyName);
// 比较 系统属性值 与 ConditionalOnSystemProperty#value() 方法值 是否相等
if (Objects.equals(systemPropertyValue, propertyValue)) {
System.out.printf("系统属性[名称 : %s] 找到匹配值 : %s\n",propertyName,propertyValue);
return true;
}
return false;
}
private boolean isComponentEnable(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean isImporter = metadata.isAnnotated(ConditionalOnImporterComponent.class.getName());
MultiValueMap<String, Object> map = null;
if (isImporter) {
map = metadata.getAllAnnotationAttributes(ConditionalOnImporterComponent.class
.getName());
} else {
map = metadata.getAllAnnotationAttributes(ConditionalOnExporterComponent.class
.getName());
}
String importerExporterName = getFirstAnnotationValueFromMetadata(map, "value");
MonitorComponent monitorComponent = getFirstAnnotationValueFromMetadata(map, "type");
List<String> activeComponents = getActivatedComponentsFromConfiguration(context,
isImporter, monitorComponent);
boolean active = activeComponents.contains(importerExporterName);
LOGGER.info("gateway {} {}:{} active:{}", monitorComponent.name().toLowerCase(),
(isImporter ? "importer" : "exporter"), importerExporterName, active);
return active;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
MultiValueMap<String, Object> map = metadata
.getAllAnnotationAttributes(ConditionalOnMonitorComponent.class.getName());
List<Object> value = map.get("value");
if (CollectionUtils.isEmpty(value)) {
return false;
}
String componentName = (String) value.get(0);
List<String> activeComponents = getComponents(context);
boolean active = activeComponents.contains(componentName);
active = active ? isActiveByZoneInfo(env, componentName) : false;
LOGGER.info("monitor component:{}, active:{}", componentName, active);
return active;
}
/**
* Get annotation delegate alias value.
*
* @param metadata
* @return
*/
@SuppressWarnings("rawtypes")
private String[] getAnnotationDelegateAliasValue(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> annotationPropertyValues = metadata
.getAllAnnotationAttributes(PrototypeAlias.class.getName());
if (!CollectionUtils.isEmpty(annotationPropertyValues)) {
/**
* See:{@link DelegateAlias}
*/
Object values = annotationPropertyValues.get("value");
if (nonNull(values) && values instanceof List) {
List _values = ((List) values);
if (!isEmpty(_values)) {
return (String[]) _values.get(0);
}
}
}
return null;
}
@Override
protected boolean matches(ProjectDescription description, ConditionContext context,
AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(ConditionalOnBuildSystem.class.getName());
String buildSystemId = (String) attributes.getFirst("value");
String dialect = (String) attributes.getFirst("dialect");
BuildSystem buildSystem = description.getBuildSystem();
if (buildSystem.id().equals(buildSystemId)) {
if (StringUtils.hasText(dialect)) {
return dialect.equals(buildSystem.dialect());
}
return true;
}
return false;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
return true;
}
}
return false;
}
return true;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
return true;
}
}
return false;
}
return true;
}
@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(Conditional.class.getName(), true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}
private Set<String> retrieveAnnotatedProfiles(AnnotatedTypeMetadata metadata, String annotationType) {
if (!metadata.isAnnotated(annotationType)) {
return Collections.emptySet();
}
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType);
if (attributes == null) {
return Collections.emptySet();
}
Set<String> profiles = Sets.newHashSet();
List<?> values = attributes.get("value");
if (values != null) {
for (Object value : values) {
if (value instanceof String[]) {
Collections.addAll(profiles, (String[]) value);
}
else {
profiles.add((String) value);
}
}
}
return profiles;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(ConditionOnPropertyExists.class.getName());
if (attrs != null) {
Object value = attrs.get("value");
return value != null && null != env && env.getProperty(value.toString()) != null;
}
return false;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(ConditionalOnClass.class.getName(), true);
String className = String.valueOf(attributes.get(AnnotationUtils.VALUE).get(0));
return ClassUtils.isPresent(className, context.getClassLoader());
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
ConditionalOnMissingAmazonClient.class.getName(), true);
for (Object amazonClientClass : attributes.get("value")) {
if (isAmazonClientMissing(context, (String) amazonClientClass)) {
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}
@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}
@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}
@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}