Skip to content

Use enhanced switch expressions where feasible #28014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,22 @@ public AspectMetadata(Class<?> aspectClass, String aspectName) {
this.ajType = ajType;

switch (this.ajType.getPerClause().getKind()) {
case SINGLETON:
case SINGLETON -> {
this.perClausePointcut = Pointcut.TRUE;
return;
case PERTARGET:
case PERTHIS:
}
case PERTARGET, PERTHIS -> {
AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
ajexp.setLocation(aspectClass.getName());
ajexp.setExpression(findPerClause(aspectClass));
ajexp.setPointcutDeclarationScope(aspectClass);
this.perClausePointcut = ajexp;
return;
case PERTYPEWITHIN:
}
case PERTYPEWITHIN -> {
// Works with a type pattern
this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
return;
default:
throw new AopConfigException(
"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
}
default -> throw new AopConfigException(
"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,18 @@ private void determineAdviceType() {
}
else {
switch (aspectJAnnotation.getAnnotationType()) {
case AtPointcut:
case AtAround:
case AtPointcut, AtAround -> {
this.isBeforeAdvice = false;
this.isAfterAdvice = false;
break;
case AtBefore:
}
case AtBefore -> {
this.isBeforeAdvice = true;
this.isAfterAdvice = false;
break;
case AtAfter:
case AtAfterReturning:
case AtAfterThrowing:
}
case AtAfter, AtAfterReturning, AtAfterThrowing -> {
this.isBeforeAdvice = false;
this.isAfterAdvice = true;
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,36 @@ public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut
AbstractAspectJAdvice springAdvice;

switch (aspectJAnnotation.getAnnotationType()) {
case AtPointcut:
case AtPointcut -> {
if (logger.isDebugEnabled()) {
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
}
return null;
case AtAround:
springAdvice = new AspectJAroundAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtBefore:
springAdvice = new AspectJMethodBeforeAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtAfter:
springAdvice = new AspectJAfterAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtAfterReturning:
}
case AtAround -> springAdvice = new AspectJAroundAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
case AtBefore -> springAdvice = new AspectJMethodBeforeAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
case AtAfter -> springAdvice = new AspectJAfterAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
case AtAfterReturning -> {
springAdvice = new AspectJAfterReturningAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
if (StringUtils.hasText(afterReturningAnnotation.returning())) {
springAdvice.setReturningName(afterReturningAnnotation.returning());
}
break;
case AtAfterThrowing:
}
case AtAfterThrowing -> {
springAdvice = new AspectJAfterThrowingAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
}
break;
default:
throw new UnsupportedOperationException(
"Unsupported advice type on method: " + candidateAdviceMethod);
}
default -> throw new UnsupportedOperationException(
"Unsupported advice type on method: " + candidateAdviceMethod);
}

// Now to configure the advice...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,14 @@ public void setup() {
this.propertyAccessor = new BeanWrapperImpl(this.target);
}
switch (this.customEditor) {
case "stringTrimmer":
this.propertyAccessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
break;
case "numberOnPath":
this.propertyAccessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
break;
case "numberOnNestedPath":
this.propertyAccessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
break;
case "numberOnType":
this.propertyAccessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
break;
case "stringTrimmer" -> this.propertyAccessor.registerCustomEditor(String.class,
new StringTrimmerEditor(false));
case "numberOnPath" -> this.propertyAccessor.registerCustomEditor(int.class, "array.somePath",
new CustomNumberEditor(Integer.class, false));
case "numberOnNestedPath" -> this.propertyAccessor.registerCustomEditor(int.class, "array[0].somePath",
new CustomNumberEditor(Integer.class, false));
case "numberOnType" -> this.propertyAccessor.registerCustomEditor(int.class,
new CustomNumberEditor(Integer.class, false));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,30 +333,28 @@ public final MimeMessage getMimeMessage() {
*/
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {
switch (multipartMode) {
case MULTIPART_MODE_NO:
setMimeMultiparts(null, null);
break;
case MULTIPART_MODE_MIXED:
case MULTIPART_MODE_NO -> setMimeMultiparts(null, null);
case MULTIPART_MODE_MIXED -> {
MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(mixedMultipart);
setMimeMultiparts(mixedMultipart, mixedMultipart);
break;
case MULTIPART_MODE_RELATED:
}
case MULTIPART_MODE_RELATED -> {
MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
mimeMessage.setContent(relatedMultipart);
setMimeMultiparts(relatedMultipart, relatedMultipart);
break;
case MULTIPART_MODE_MIXED_RELATED:
}
case MULTIPART_MODE_MIXED_RELATED -> {
MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
break;
default:
throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");
}
default -> throw new IllegalArgumentException(
"Only multipart modes MIXED_RELATED, RELATED and NO supported");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ public class CachingConfigurationSelector extends AdviceModeImportSelector<Enabl
*/
@Override
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return getProxyImports();
case ASPECTJ:
return getAspectJImports();
default:
return null;
}
return switch (adviceMode) {
case PROXY -> getProxyImports();
case ASPECTJ -> getAspectJImports();
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,32 @@ public static List<TypeFilter> createTypeFiltersFor(AnnotationAttributes filterA

for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
switch (filterType) {
case ANNOTATION:
case ANNOTATION -> {
Assert.isAssignable(Annotation.class, filterClass,
"@ComponentScan ANNOTATION type filter requires an annotation type");
@SuppressWarnings("unchecked")
Class<Annotation> annotationType = (Class<Annotation>) filterClass;
typeFilters.add(new AnnotationTypeFilter(annotationType));
break;
case ASSIGNABLE_TYPE:
typeFilters.add(new AssignableTypeFilter(filterClass));
break;
case CUSTOM:
}
case ASSIGNABLE_TYPE -> typeFilters.add(new AssignableTypeFilter(filterClass));
case CUSTOM -> {
Assert.isAssignable(TypeFilter.class, filterClass,
"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
TypeFilter filter = ParserStrategyUtils.instantiateClass(filterClass, TypeFilter.class,
environment, resourceLoader, registry);
typeFilters.add(filter);
break;
default:
throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
}
default -> throw new IllegalArgumentException(
"Filter type not supported with Class value: " + filterType);
}
}

for (String expression : filterAttributes.getStringArray("pattern")) {
switch (filterType) {
case ASPECTJ:
typeFilters.add(new AspectJTypeFilter(expression, resourceLoader.getClassLoader()));
break;
case REGEX:
typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
break;
default:
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
case ASPECTJ -> typeFilters.add(new AspectJTypeFilter(expression, resourceLoader.getClassLoader()));
case REGEX -> typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
default -> throw new IllegalArgumentException(
"Filter type not supported with String pattern: " + filterType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ private DateTimeFormatter getFormatter(Type type) {
}

private DateTimeFormatter getFallbackFormatter(Type type) {
switch (type) {
case DATE: return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
case TIME: return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
default: return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
}
return switch (type) {
case DATE -> DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
case TIME -> DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
case DATE_TIME -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.AdviceModeImportSelector;
import org.springframework.lang.Nullable;
import org.springframework.lang.NonNull;

/**
* Selects which implementation of {@link AbstractAsyncConfiguration} should
Expand All @@ -43,16 +43,12 @@ public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableA
* respectively.
*/
@Override
@Nullable
@NonNull
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] {ProxyAsyncConfiguration.class.getName()};
case ASPECTJ:
return new String[] {ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
default:
return null;
}
return switch (adviceMode) {
case PROXY -> new String[]{ProxyAsyncConfiguration.class.getName()};
case ASPECTJ -> new String[]{ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
};
}

}
23 changes: 7 additions & 16 deletions spring-core/src/main/java/org/springframework/asm/TypePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,13 @@ public String toString() {
int length = getLength();
StringBuilder result = new StringBuilder(length * 2);
for (int i = 0; i < length; ++i) {
switch (getStep(i)) {
case ARRAY_ELEMENT:
result.append('[');
break;
case INNER_TYPE:
result.append('.');
break;
case WILDCARD_BOUND:
result.append('*');
break;
case TYPE_ARGUMENT:
result.append(getStepArgument(i)).append(';');
break;
default:
throw new AssertionError();
}
switch (getStep(i)) {
case ARRAY_ELEMENT -> result.append('[');
case INNER_TYPE -> result.append('.');
case WILDCARD_BOUND -> result.append('*');
case TYPE_ARGUMENT -> result.append(getStepArgument(i)).append(';');
default -> throw new AssertionError();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refrain from modifying classes under org.springframework.asm, org.springframework.cglib, and org.springframework.objenesis. Those include repackaged forks of the third-party libraries ASM, CGLIB, and Objenesis. Any refactoring to those classes should take place upstream in the originating repository. The Spring Framework will then pick up the changes when syncing with official updates of the forked third-party libraries.


Don't worry about it for this PR though. I'll revert those changes when merging.

}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,13 @@ private static <C, R> R process(C context, AnnotatedElement source,
private static <C, R> R processClass(C context, Class<?> source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {

switch (searchStrategy) {
case DIRECT:
return processElement(context, source, processor);
case INHERITED_ANNOTATIONS:
return processClassInheritedAnnotations(context, source, searchStrategy, processor);
case SUPERCLASS:
return processClassHierarchy(context, source, processor, false, false);
case TYPE_HIERARCHY:
return processClassHierarchy(context, source, processor, true, false);
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
return processClassHierarchy(context, source, processor, true, true);
}
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
return switch (searchStrategy) {
case DIRECT -> processElement(context, source, processor);
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, searchStrategy, processor);
case SUPERCLASS -> processClassHierarchy(context, source, processor, false, false);
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, false);
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processClassHierarchy(context, source, processor, true, true);
};
}

@Nullable
Expand Down Expand Up @@ -238,19 +232,14 @@ private static <C, R> R processClassHierarchy(C context, int[] aggregateIndex, C
private static <C, R> R processMethod(C context, Method source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {

switch (searchStrategy) {
case DIRECT:
case INHERITED_ANNOTATIONS:
return processMethodInheritedAnnotations(context, source, processor);
case SUPERCLASS:
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
processor, source, false);
case TYPE_HIERARCHY:
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
processor, source, true);
}
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
return switch (searchStrategy) {
case DIRECT, INHERITED_ANNOTATIONS -> processMethodInheritedAnnotations(context, source, processor);
case SUPERCLASS -> processMethodHierarchy(context, new int[]{0}, source.getDeclaringClass(),
processor, source, false);
case TYPE_HIERARCHY, TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processMethodHierarchy(context, new int[]{0},
source.getDeclaringClass(),
processor, source, true);
};
}

@Nullable
Expand Down
Loading