Skip to content

Commit c5c9267

Browse files
a.yazychyansbrannen
authored andcommitted
Use enhanced switch expressions where feasible
Closes gh-28014
1 parent 42d1145 commit c5c9267

File tree

38 files changed

+291
-493
lines changed

38 files changed

+291
-493
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectMetadata.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,22 @@ public AspectMetadata(Class<?> aspectClass, String aspectName) {
101101
this.ajType = ajType;
102102

103103
switch (this.ajType.getPerClause().getKind()) {
104-
case SINGLETON:
104+
case SINGLETON -> {
105105
this.perClausePointcut = Pointcut.TRUE;
106-
return;
107-
case PERTARGET:
108-
case PERTHIS:
106+
}
107+
case PERTARGET, PERTHIS -> {
109108
AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
110109
ajexp.setLocation(aspectClass.getName());
111110
ajexp.setExpression(findPerClause(aspectClass));
112111
ajexp.setPointcutDeclarationScope(aspectClass);
113112
this.perClausePointcut = ajexp;
114-
return;
115-
case PERTYPEWITHIN:
113+
}
114+
case PERTYPEWITHIN -> {
116115
// Works with a type pattern
117116
this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
118-
return;
119-
default:
120-
throw new AopConfigException(
121-
"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
117+
}
118+
default -> throw new AopConfigException(
119+
"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
122120
}
123121
}
124122

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,18 @@ private void determineAdviceType() {
220220
}
221221
else {
222222
switch (aspectJAnnotation.getAnnotationType()) {
223-
case AtPointcut:
224-
case AtAround:
223+
case AtPointcut, AtAround -> {
225224
this.isBeforeAdvice = false;
226225
this.isAfterAdvice = false;
227-
break;
228-
case AtBefore:
226+
}
227+
case AtBefore -> {
229228
this.isBeforeAdvice = true;
230229
this.isAfterAdvice = false;
231-
break;
232-
case AtAfter:
233-
case AtAfterReturning:
234-
case AtAfterThrowing:
230+
}
231+
case AtAfter, AtAfterReturning, AtAfterThrowing -> {
235232
this.isBeforeAdvice = false;
236233
this.isAfterAdvice = true;
237-
break;
234+
}
238235
}
239236
}
240237
}

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -261,42 +261,36 @@ public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut
261261
AbstractAspectJAdvice springAdvice;
262262

263263
switch (aspectJAnnotation.getAnnotationType()) {
264-
case AtPointcut:
264+
case AtPointcut -> {
265265
if (logger.isDebugEnabled()) {
266266
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
267267
}
268268
return null;
269-
case AtAround:
270-
springAdvice = new AspectJAroundAdvice(
271-
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
272-
break;
273-
case AtBefore:
274-
springAdvice = new AspectJMethodBeforeAdvice(
275-
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
276-
break;
277-
case AtAfter:
278-
springAdvice = new AspectJAfterAdvice(
279-
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
280-
break;
281-
case AtAfterReturning:
269+
}
270+
case AtAround -> springAdvice = new AspectJAroundAdvice(
271+
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
272+
case AtBefore -> springAdvice = new AspectJMethodBeforeAdvice(
273+
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
274+
case AtAfter -> springAdvice = new AspectJAfterAdvice(
275+
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
276+
case AtAfterReturning -> {
282277
springAdvice = new AspectJAfterReturningAdvice(
283278
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
284279
AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
285280
if (StringUtils.hasText(afterReturningAnnotation.returning())) {
286281
springAdvice.setReturningName(afterReturningAnnotation.returning());
287282
}
288-
break;
289-
case AtAfterThrowing:
283+
}
284+
case AtAfterThrowing -> {
290285
springAdvice = new AspectJAfterThrowingAdvice(
291286
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
292287
AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
293288
if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
294289
springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
295290
}
296-
break;
297-
default:
298-
throw new UnsupportedOperationException(
299-
"Unsupported advice type on method: " + candidateAdviceMethod);
291+
}
292+
default -> throw new UnsupportedOperationException(
293+
"Unsupported advice type on method: " + candidateAdviceMethod);
300294
}
301295

302296
// Now to configure the advice...

spring-beans/src/jmh/java/org/springframework/beans/AbstractPropertyAccessorBenchmark.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,14 @@ public void setup() {
6161
this.propertyAccessor = new BeanWrapperImpl(this.target);
6262
}
6363
switch (this.customEditor) {
64-
case "stringTrimmer":
65-
this.propertyAccessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
66-
break;
67-
case "numberOnPath":
68-
this.propertyAccessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
69-
break;
70-
case "numberOnNestedPath":
71-
this.propertyAccessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
72-
break;
73-
case "numberOnType":
74-
this.propertyAccessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
75-
break;
64+
case "stringTrimmer" -> this.propertyAccessor.registerCustomEditor(String.class,
65+
new StringTrimmerEditor(false));
66+
case "numberOnPath" -> this.propertyAccessor.registerCustomEditor(int.class, "array.somePath",
67+
new CustomNumberEditor(Integer.class, false));
68+
case "numberOnNestedPath" -> this.propertyAccessor.registerCustomEditor(int.class, "array[0].somePath",
69+
new CustomNumberEditor(Integer.class, false));
70+
case "numberOnType" -> this.propertyAccessor.registerCustomEditor(int.class,
71+
new CustomNumberEditor(Integer.class, false));
7672
}
7773

7874
}

spring-context-support/src/main/java/org/springframework/mail/javamail/MimeMessageHelper.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,30 +333,28 @@ public final MimeMessage getMimeMessage() {
333333
*/
334334
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {
335335
switch (multipartMode) {
336-
case MULTIPART_MODE_NO:
337-
setMimeMultiparts(null, null);
338-
break;
339-
case MULTIPART_MODE_MIXED:
336+
case MULTIPART_MODE_NO -> setMimeMultiparts(null, null);
337+
case MULTIPART_MODE_MIXED -> {
340338
MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
341339
mimeMessage.setContent(mixedMultipart);
342340
setMimeMultiparts(mixedMultipart, mixedMultipart);
343-
break;
344-
case MULTIPART_MODE_RELATED:
341+
}
342+
case MULTIPART_MODE_RELATED -> {
345343
MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
346344
mimeMessage.setContent(relatedMultipart);
347345
setMimeMultiparts(relatedMultipart, relatedMultipart);
348-
break;
349-
case MULTIPART_MODE_MIXED_RELATED:
346+
}
347+
case MULTIPART_MODE_MIXED_RELATED -> {
350348
MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
351349
mimeMessage.setContent(rootMixedMultipart);
352350
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
353351
MimeBodyPart relatedBodyPart = new MimeBodyPart();
354352
relatedBodyPart.setContent(nestedRelatedMultipart);
355353
rootMixedMultipart.addBodyPart(relatedBodyPart);
356354
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
357-
break;
358-
default:
359-
throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");
355+
}
356+
default -> throw new IllegalArgumentException(
357+
"Only multipart modes MIXED_RELATED, RELATED and NO supported");
360358
}
361359
}
362360

spring-context/src/main/java/org/springframework/cache/annotation/CachingConfigurationSelector.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,10 @@ public class CachingConfigurationSelector extends AdviceModeImportSelector<Enabl
6868
*/
6969
@Override
7070
public String[] selectImports(AdviceMode adviceMode) {
71-
switch (adviceMode) {
72-
case PROXY:
73-
return getProxyImports();
74-
case ASPECTJ:
75-
return getAspectJImports();
76-
default:
77-
return null;
78-
}
71+
return switch (adviceMode) {
72+
case PROXY -> getProxyImports();
73+
case ASPECTJ -> getAspectJImports();
74+
};
7975
}
8076

8177
/**

spring-context/src/main/java/org/springframework/context/annotation/TypeFilterUtils.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,32 @@ public static List<TypeFilter> createTypeFiltersFor(AnnotationAttributes filterA
7878

7979
for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
8080
switch (filterType) {
81-
case ANNOTATION:
81+
case ANNOTATION -> {
8282
Assert.isAssignable(Annotation.class, filterClass,
8383
"@ComponentScan ANNOTATION type filter requires an annotation type");
8484
@SuppressWarnings("unchecked")
8585
Class<Annotation> annotationType = (Class<Annotation>) filterClass;
8686
typeFilters.add(new AnnotationTypeFilter(annotationType));
87-
break;
88-
case ASSIGNABLE_TYPE:
89-
typeFilters.add(new AssignableTypeFilter(filterClass));
90-
break;
91-
case CUSTOM:
87+
}
88+
case ASSIGNABLE_TYPE -> typeFilters.add(new AssignableTypeFilter(filterClass));
89+
case CUSTOM -> {
9290
Assert.isAssignable(TypeFilter.class, filterClass,
9391
"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
9492
TypeFilter filter = ParserStrategyUtils.instantiateClass(filterClass, TypeFilter.class,
9593
environment, resourceLoader, registry);
9694
typeFilters.add(filter);
97-
break;
98-
default:
99-
throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
95+
}
96+
default -> throw new IllegalArgumentException(
97+
"Filter type not supported with Class value: " + filterType);
10098
}
10199
}
102100

103101
for (String expression : filterAttributes.getStringArray("pattern")) {
104102
switch (filterType) {
105-
case ASPECTJ:
106-
typeFilters.add(new AspectJTypeFilter(expression, resourceLoader.getClassLoader()));
107-
break;
108-
case REGEX:
109-
typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
110-
break;
111-
default:
112-
throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
103+
case ASPECTJ -> typeFilters.add(new AspectJTypeFilter(expression, resourceLoader.getClassLoader()));
104+
case REGEX -> typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
105+
default -> throw new IllegalArgumentException(
106+
"Filter type not supported with String pattern: " + filterType);
113107
}
114108
}
115109

spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ private DateTimeFormatter getFormatter(Type type) {
209209
}
210210

211211
private DateTimeFormatter getFallbackFormatter(Type type) {
212-
switch (type) {
213-
case DATE: return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
214-
case TIME: return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
215-
default: return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
216-
}
212+
return switch (type) {
213+
case DATE -> DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
214+
case TIME -> DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
215+
case DATE_TIME -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
216+
};
217217
}
218218

219219
}

spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurationSelector.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import org.springframework.context.annotation.AdviceMode;
2020
import org.springframework.context.annotation.AdviceModeImportSelector;
21-
import org.springframework.lang.Nullable;
21+
import org.springframework.lang.NonNull;
2222

2323
/**
2424
* Selects which implementation of {@link AbstractAsyncConfiguration} should
@@ -43,16 +43,12 @@ public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableA
4343
* respectively.
4444
*/
4545
@Override
46-
@Nullable
46+
@NonNull
4747
public String[] selectImports(AdviceMode adviceMode) {
48-
switch (adviceMode) {
49-
case PROXY:
50-
return new String[] {ProxyAsyncConfiguration.class.getName()};
51-
case ASPECTJ:
52-
return new String[] {ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
53-
default:
54-
return null;
55-
}
48+
return switch (adviceMode) {
49+
case PROXY -> new String[]{ProxyAsyncConfiguration.class.getName()};
50+
case ASPECTJ -> new String[]{ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
51+
};
5652
}
5753

5854
}

spring-core/src/main/java/org/springframework/asm/TypePath.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,13 @@ public String toString() {
163163
int length = getLength();
164164
StringBuilder result = new StringBuilder(length * 2);
165165
for (int i = 0; i < length; ++i) {
166-
switch (getStep(i)) {
167-
case ARRAY_ELEMENT:
168-
result.append('[');
169-
break;
170-
case INNER_TYPE:
171-
result.append('.');
172-
break;
173-
case WILDCARD_BOUND:
174-
result.append('*');
175-
break;
176-
case TYPE_ARGUMENT:
177-
result.append(getStepArgument(i)).append(';');
178-
break;
179-
default:
180-
throw new AssertionError();
181-
}
166+
switch (getStep(i)) {
167+
case ARRAY_ELEMENT -> result.append('[');
168+
case INNER_TYPE -> result.append('.');
169+
case WILDCARD_BOUND -> result.append('*');
170+
case TYPE_ARGUMENT -> result.append(getStepArgument(i)).append(';');
171+
default -> throw new AssertionError();
172+
}
182173
}
183174
return result.toString();
184175
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,13 @@ private static <C, R> R process(C context, AnnotatedElement source,
9595
private static <C, R> R processClass(C context, Class<?> source,
9696
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
9797

98-
switch (searchStrategy) {
99-
case DIRECT:
100-
return processElement(context, source, processor);
101-
case INHERITED_ANNOTATIONS:
102-
return processClassInheritedAnnotations(context, source, searchStrategy, processor);
103-
case SUPERCLASS:
104-
return processClassHierarchy(context, source, processor, false, false);
105-
case TYPE_HIERARCHY:
106-
return processClassHierarchy(context, source, processor, true, false);
107-
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
108-
return processClassHierarchy(context, source, processor, true, true);
109-
}
110-
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
98+
return switch (searchStrategy) {
99+
case DIRECT -> processElement(context, source, processor);
100+
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, searchStrategy, processor);
101+
case SUPERCLASS -> processClassHierarchy(context, source, processor, false, false);
102+
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, false);
103+
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processClassHierarchy(context, source, processor, true, true);
104+
};
111105
}
112106

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

241-
switch (searchStrategy) {
242-
case DIRECT:
243-
case INHERITED_ANNOTATIONS:
244-
return processMethodInheritedAnnotations(context, source, processor);
245-
case SUPERCLASS:
246-
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
247-
processor, source, false);
248-
case TYPE_HIERARCHY:
249-
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
250-
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
251-
processor, source, true);
252-
}
253-
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
235+
return switch (searchStrategy) {
236+
case DIRECT, INHERITED_ANNOTATIONS -> processMethodInheritedAnnotations(context, source, processor);
237+
case SUPERCLASS -> processMethodHierarchy(context, new int[]{0}, source.getDeclaringClass(),
238+
processor, source, false);
239+
case TYPE_HIERARCHY, TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processMethodHierarchy(context, new int[]{0},
240+
source.getDeclaringClass(),
241+
processor, source, true);
242+
};
254243
}
255244

256245
@Nullable

0 commit comments

Comments
 (0)