Replace if with switch where feasible

See gh-31916
This commit is contained in:
Yanming Zhou
2023-12-28 15:44:55 +08:00
committed by Stéphane Nicoll
parent ea5ef098cf
commit cfa3aa001f
7 changed files with 85 additions and 157 deletions

View File

@@ -182,17 +182,12 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
if (element.hasAttribute(SCOPED_PROXY_ATTRIBUTE)) {
String mode = element.getAttribute(SCOPED_PROXY_ATTRIBUTE);
if ("targetClass".equals(mode)) {
scanner.setScopedProxyMode(ScopedProxyMode.TARGET_CLASS);
}
else if ("interfaces".equals(mode)) {
scanner.setScopedProxyMode(ScopedProxyMode.INTERFACES);
}
else if ("no".equals(mode)) {
scanner.setScopedProxyMode(ScopedProxyMode.NO);
}
else {
throw new IllegalArgumentException("scoped-proxy only supports 'no', 'interfaces' and 'targetClass'");
switch (mode) {
case "targetClass" -> scanner.setScopedProxyMode(ScopedProxyMode.TARGET_CLASS);
case "interfaces" -> scanner.setScopedProxyMode(ScopedProxyMode.INTERFACES);
case "no" -> scanner.setScopedProxyMode(ScopedProxyMode.NO);
default ->
throw new IllegalArgumentException("scoped-proxy only supports 'no', 'interfaces' and 'targetClass'");
}
}
}
@@ -234,28 +229,28 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression);
if ("annotation".equals(filterType)) {
return new AnnotationTypeFilter((Class<Annotation>) ClassUtils.forName(expression, classLoader));
}
else if ("assignable".equals(filterType)) {
return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader));
}
else if ("aspectj".equals(filterType)) {
return new AspectJTypeFilter(expression, classLoader);
}
else if ("regex".equals(filterType)) {
return new RegexPatternTypeFilter(Pattern.compile(expression));
}
else if ("custom".equals(filterType)) {
Class<?> filterClass = ClassUtils.forName(expression, classLoader);
if (!TypeFilter.class.isAssignableFrom(filterClass)) {
throw new IllegalArgumentException(
"Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression);
switch (filterType) {
case "annotation" -> {
return new AnnotationTypeFilter((Class<Annotation>) ClassUtils.forName(expression, classLoader));
}
return (TypeFilter) BeanUtils.instantiateClass(filterClass);
}
else {
throw new IllegalArgumentException("Unsupported filter type: " + filterType);
case "assignable" -> {
return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader));
}
case "aspectj" -> {
return new AspectJTypeFilter(expression, classLoader);
}
case "regex" -> {
return new RegexPatternTypeFilter(Pattern.compile(expression));
}
case "custom" -> {
Class<?> filterClass = ClassUtils.forName(expression, classLoader);
if (!TypeFilter.class.isAssignableFrom(filterClass)) {
throw new IllegalArgumentException(
"Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression);
}
return (TypeFilter) BeanUtils.instantiateClass(filterClass);
}
default -> throw new IllegalArgumentException("Unsupported filter type: " + filterType);
}
}

View File

@@ -61,22 +61,13 @@ public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionPa
return;
}
String prefix = "java.util.concurrent.ThreadPoolExecutor.";
String policyClassName;
if (rejectionPolicy.equals("ABORT")) {
policyClassName = prefix + "AbortPolicy";
}
else if (rejectionPolicy.equals("CALLER_RUNS")) {
policyClassName = prefix + "CallerRunsPolicy";
}
else if (rejectionPolicy.equals("DISCARD")) {
policyClassName = prefix + "DiscardPolicy";
}
else if (rejectionPolicy.equals("DISCARD_OLDEST")) {
policyClassName = prefix + "DiscardOldestPolicy";
}
else {
policyClassName = rejectionPolicy;
}
String policyClassName = switch (rejectionPolicy) {
case "ABORT" -> prefix + "AbortPolicy";
case "CALLER_RUNS" -> prefix + "CallerRunsPolicy";
case "DISCARD" -> prefix + "DiscardPolicy";
case "DISCARD_OLDEST" -> prefix + "DiscardOldestPolicy";
default -> rejectionPolicy;
};
builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName));
}