Skip to content

Commit

Permalink
🎨 API 使用优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKETODAY committed Jan 14, 2025
1 parent eceec27 commit b934000
Show file tree
Hide file tree
Showing 25 changed files with 70 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -70,7 +70,7 @@ public abstract class AbstractCachingConfiguration implements ImportAware, BeanF
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableCaching = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableCaching.class.getName()));
importMetadata.getAnnotationAttributes(EnableCaching.class));
if (this.enableCaching == null) {
throw new IllegalArgumentException(
"@EnableCaching is not present on importing class " + importMetadata.getClassName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -79,17 +78,17 @@
*/
public class AnnotationBeanNameGenerator implements BeanNameGenerator {

private static final Logger logger = LoggerFactory.getLogger(AnnotationBeanNameGenerator.class);

/**
* A convenient constant for a default {@code AnnotationBeanNameGenerator} instance,
* as used for component scanning purposes.
*/
public static final AnnotationBeanNameGenerator INSTANCE = new AnnotationBeanNameGenerator();

private static final String COMPONENT_ANNOTATION_CLASSNAME = "infra.stereotype.Component";
private static final String COMPONENT_ANNOTATION_CLASSNAME = Component.class.getName();

private final Map<String, Set<String>> metaAnnotationTypesCache = new ConcurrentHashMap<>();

private static final Logger logger = LoggerFactory.getLogger(AnnotationBeanNameGenerator.class);
private final ConcurrentHashMap<String, Set<String>> metaAnnotationTypesCache = new ConcurrentHashMap<>();

/**
* Set used to track which stereotype annotations have already been checked
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -65,7 +65,7 @@ public void validate(ProblemReporter problemReporter) {
return;
}

Map<String, Object> attributes = configurationClass.metadata.getAnnotationAttributes(Configuration.class.getName());
Map<String, Object> attributes = configurationClass.metadata.getAnnotationAttributes(Configuration.class);
if (attributes != null && (Boolean) attributes.get("proxyBeanMethods") && !metadata.isOverridable()) {
// instance @Bean methods within @Configuration classes must be overridable to accommodate CGLIB
problemReporter.error(new NonOverridableMethodError());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -93,7 +93,7 @@ public boolean shouldSkip(AnnotatedTypeMetadata metadata) {
* @return if the item should be skipped
*/
public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationPhase phase) {
if (metadata == null || !metadata.isAnnotated(Conditional.class.getName())) {
if (metadata == null || !metadata.isAnnotated(Conditional.class)) {
return false;
}

Expand Down Expand Up @@ -153,7 +153,7 @@ List<Condition> collectConditions(@Nullable AnnotatedTypeMetadata metadata) {

@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(Conditional.class, true);
Object values = (attributes != null ? attributes.get("value") : null);
return (List<String[]>) (values != null ? values : Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -335,7 +335,7 @@ protected boolean isOverriddenByExistingDefinition(ComponentMethod componentMeth
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
return true;
}
Map<String, Object> attributes = configClass.metadata.getAnnotationAttributes(Configuration.class.getName());
Map<String, Object> attributes = configClass.metadata.getAnnotationAttributes(Configuration.class);
if ((attributes != null && (Boolean) attributes.get("enforceUniqueMethods"))
|| !bootstrapContext.getRegistry().isBeanDefinitionOverridable(beanName)) {
throw new BeanDefinitionOverrideException(beanName, new ConfigurationClassBeanDefinition(configClass, componentMethod.metadata, beanName),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -258,7 +258,7 @@ else if (configClass.scanned) {
protected final SourceClass doProcessConfigurationClass(
ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter) throws IOException {

if (configClass.metadata.isAnnotated(Component.class.getName())) {
if (configClass.metadata.isAnnotated(Component.class)) {
// Recursively process any member (nested) classes first
processMemberClasses(configClass, sourceClass, filter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -64,7 +64,7 @@ public class MBeanExportConfiguration implements ImportAware, EnvironmentAware,

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class);
this.enableMBeanExport = AnnotationAttributes.fromMap(map);
if (enableMBeanExport == null) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -117,7 +117,7 @@ final class ProfileCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class);
if (attrs != null) {
Environment environment = context.getEnvironment();
for (Object value : attrs.get("value")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -51,7 +51,7 @@ public boolean match(MetadataReader metadataReader, MetadataReaderFactory factor
}

private boolean isConfiguration(MetadataReader metadataReader) {
return metadataReader.getAnnotationMetadata().isAnnotated(Configuration.class.getName());
return metadataReader.getAnnotationMetadata().isAnnotated(Configuration.class);
}

private boolean isAutoConfiguration(MetadataReader metadataReader) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -57,7 +57,7 @@ class AutoConfigurationSorter {
private final UnaryOperator<String> replacementMapper;

AutoConfigurationSorter(MetadataReaderFactory metadataReaderFactory,
AutoConfigurationMetadata autoConfigurationMetadata, UnaryOperator<String> replacementMapper) {
AutoConfigurationMetadata autoConfigurationMetadata, @Nullable UnaryOperator<String> replacementMapper) {
Assert.notNull(metadataReaderFactory, "MetadataReaderFactory required");
this.replacementMapper = replacementMapper;
this.metadataReaderFactory = metadataReaderFactory;
Expand Down Expand Up @@ -238,8 +238,7 @@ private int getOrder() {
return this.autoConfigurationMetadata.getInteger(this.className, "AutoConfigureOrder",
AutoConfigureOrder.DEFAULT_ORDER);
}
Map<String, Object> attributes = getAnnotationMetadata()
.getAnnotationAttributes(AutoConfigureOrder.class.getName());
Map<String, Object> attributes = getAnnotationMetadata().getAnnotationAttributes(AutoConfigureOrder.class);
return (attributes != null) ? (Integer) attributes.get("value") : AutoConfigureOrder.DEFAULT_ORDER;
}

Expand All @@ -248,9 +247,8 @@ private boolean wasProcessed() {
&& this.autoConfigurationMetadata.wasProcessed(this.className));
}

private Set<String> getAnnotationValue(Class<?> annotation) {
Map<String, Object> attributes = getAnnotationMetadata().getAnnotationAttributes(annotation.getName(),
true);
private Set<String> getAnnotationValue(Class<? extends Annotation> annotation) {
Map<String, Object> attributes = getAnnotationMetadata().getAnnotationAttributes(annotation, true);
if (attributes == null) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -138,7 +138,7 @@ private AnnotationMetadata getMetadata(String className) {

@SuppressWarnings("unchecked")
private List<String[]> getConditionClasses(AnnotatedTypeMetadata metadata) {
var attributes = metadata.getAllAnnotationAttributes(Conditional.class.getName(), true);
var attributes = metadata.getAllAnnotationAttributes(Conditional.class, true);
if (attributes != null) {
Object values = attributes.get("value");
if (values != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -56,7 +56,7 @@ final class OnResourceCondition extends InfraCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnResource.class.getName(), true);
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnResource.class, true);
ResourceLoader loader = context.getResourceLoader();
List<String> locations = new ArrayList<>();
collectValues(locations, attributes.get("value"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -604,7 +604,7 @@ protected final String getAnnotationName() {
}

private Set<String> deducedBeanType(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (metadata instanceof MethodMetadata && metadata.isAnnotated(Component.class.getName())) {
if (metadata instanceof MethodMetadata && metadata.isAnnotated(Component.class)) {
return deducedBeanTypeForBeanMethod(context, (MethodMetadata) metadata);
}
return Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,6 +17,7 @@

package infra.context.condition;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -26,6 +27,7 @@
import infra.context.annotation.config.AutoConfigurationMetadata;
import infra.core.Ordered;
import infra.core.type.AnnotatedTypeMetadata;
import infra.lang.Nullable;
import infra.util.MultiValueMap;
import infra.util.StringUtils;

Expand Down Expand Up @@ -107,8 +109,9 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
return ConditionOutcome.match(matchMessage);
}

private List<String> getCandidates(AnnotatedTypeMetadata metadata, Class<?> annotationType) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType.getName(), true);
@Nullable
private List<String> getCandidates(AnnotatedTypeMetadata metadata, Class<? extends Annotation> annotationType) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationType, true);
if (attributes == null) {
return null;
}
Expand Down Expand Up @@ -192,6 +195,7 @@ private ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses,
return outcomes;
}

@Nullable
private ConditionOutcome getOutcome(String candidates) {
try {
if (!candidates.contains(",")) {
Expand All @@ -210,6 +214,7 @@ private ConditionOutcome getOutcome(String candidates) {
return null;
}

@Nullable
private ConditionOutcome getOutcome(String className, ClassLoader classLoader) {
if (ClassNameFilter.MISSING.matches(className, classLoader)) {
return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnClass.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -42,7 +42,7 @@ class OnJavaCondition extends InfraCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnJava.class.getName());
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnJava.class);
ConditionalOnJava.Range range = (ConditionalOnJava.Range) attributes.get("range");
JavaVersion version = (JavaVersion) attributes.get("value");
return getMatchOutcome(range, JVM_VERSION, version);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -36,7 +36,7 @@ class OnThreadingCondition extends InfraCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnThreading.class.getName());
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnThreading.class);
Threading threading = (Threading) attributes.get("value");
return getMatchOutcome(context.getEnvironment(), threading);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,7 +19,6 @@

import java.io.IOException;
import java.util.Set;
import java.util.function.Predicate;

import infra.context.BootstrapContext;
import infra.context.annotation.ClassPathScanningCandidateComponentProvider;
Expand Down Expand Up @@ -76,8 +75,7 @@ private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
if (packagesToScan.isEmpty()) {
packagesToScan.add(ClassUtils.getPackageName(metadata.getClassName()));
}

packagesToScan.removeIf(Predicate.not(StringUtils::hasText));
packagesToScan.removeIf(StringUtils::isBlank);
return packagesToScan;
}

Expand Down Expand Up @@ -117,7 +115,7 @@ private void register(ConfigurationPropertiesBeanRegistrar registrar, MetadataRe

private boolean isComponent(AnnotationMetadata annotationMetadata) {
// MergedAnnotations.from(type, SearchStrategy.TYPE_HIERARCHY).isPresent(Component.class)
return annotationMetadata.isAnnotated(Component.class.getName());
return annotationMetadata.isAnnotated(Component.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -170,8 +170,7 @@ private void registerJacksonHints(ReflectionHints hints, Class<?> clazz) {
}

private void forEachJacksonAnnotation(AnnotatedElement element, Consumer<MergedAnnotation<Annotation>> action) {
MergedAnnotations
.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(JACKSON_ANNOTATION)
.filter(MergedAnnotation::isMetaPresent)
.forEach(action);
Expand Down
Loading

0 comments on commit b934000

Please sign in to comment.