Skip to content

Commit be94e1a

Browse files
ssang1105sbrannen
authored andcommitted
Replace switch statements with enhanced switch statements for consistency
Closes gh-30283
1 parent f2ae106 commit be94e1a

File tree

3 files changed

+78
-127
lines changed

3 files changed

+78
-127
lines changed

spring-core/src/main/java/org/springframework/cglib/core/TypeUtils.java

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,17 @@ private static String map(String type) {
251251
}
252252

253253
public static Type getBoxedType(Type type) {
254-
switch (type.getSort()) {
255-
case Type.CHAR:
256-
return Constants.TYPE_CHARACTER;
257-
case Type.BOOLEAN:
258-
return Constants.TYPE_BOOLEAN;
259-
case Type.DOUBLE:
260-
return Constants.TYPE_DOUBLE;
261-
case Type.FLOAT:
262-
return Constants.TYPE_FLOAT;
263-
case Type.LONG:
264-
return Constants.TYPE_LONG;
265-
case Type.INT:
266-
return Constants.TYPE_INTEGER;
267-
case Type.SHORT:
268-
return Constants.TYPE_SHORT;
269-
case Type.BYTE:
270-
return Constants.TYPE_BYTE;
271-
default:
272-
return type;
273-
}
254+
return switch (type.getSort()) {
255+
case Type.CHAR -> Constants.TYPE_CHARACTER;
256+
case Type.BOOLEAN -> Constants.TYPE_BOOLEAN;
257+
case Type.DOUBLE -> Constants.TYPE_DOUBLE;
258+
case Type.FLOAT -> Constants.TYPE_FLOAT;
259+
case Type.LONG -> Constants.TYPE_LONG;
260+
case Type.INT -> Constants.TYPE_INTEGER;
261+
case Type.SHORT -> Constants.TYPE_SHORT;
262+
case Type.BYTE -> Constants.TYPE_BYTE;
263+
default -> type;
264+
};
274265
}
275266

276267
public static Type getUnboxedType(Type type) {
@@ -307,13 +298,10 @@ public static Type getComponentType(Type type) {
307298
}
308299

309300
public static boolean isPrimitive(Type type) {
310-
switch (type.getSort()) {
311-
case Type.ARRAY:
312-
case Type.OBJECT:
313-
return false;
314-
default:
315-
return true;
316-
}
301+
return switch (type.getSort()) {
302+
case Type.ARRAY, Type.OBJECT -> false;
303+
default -> true;
304+
};
317305
}
318306

319307
public static String emulateClassGetName(Type type) {
@@ -340,17 +328,18 @@ public static Type[] getTypes(Class[] classes) {
340328
}
341329

342330
public static int ICONST(int value) {
343-
switch (value) {
344-
case -1: return Constants.ICONST_M1;
345-
case 0: return Constants.ICONST_0;
346-
case 1: return Constants.ICONST_1;
347-
case 2: return Constants.ICONST_2;
348-
case 3: return Constants.ICONST_3;
349-
case 4: return Constants.ICONST_4;
350-
case 5: return Constants.ICONST_5;
351-
}
352-
return -1; // error
353-
}
331+
return switch (value) {
332+
case -1 -> Constants.ICONST_M1;
333+
case 0 -> Constants.ICONST_0;
334+
case 1 -> Constants.ICONST_1;
335+
case 2 -> Constants.ICONST_2;
336+
case 3 -> Constants.ICONST_3;
337+
case 4 -> Constants.ICONST_4;
338+
case 5 -> Constants.ICONST_5;
339+
default -> -1;
340+
};
341+
// error
342+
}
354343

355344
public static int LCONST(long value) {
356345
if (value == 0L) {
@@ -385,43 +374,33 @@ public static int DCONST(double value) {
385374
}
386375

387376
public static int NEWARRAY(Type type) {
388-
switch (type.getSort()) {
389-
case Type.BYTE:
390-
return Constants.T_BYTE;
391-
case Type.CHAR:
392-
return Constants.T_CHAR;
393-
case Type.DOUBLE:
394-
return Constants.T_DOUBLE;
395-
case Type.FLOAT:
396-
return Constants.T_FLOAT;
397-
case Type.INT:
398-
return Constants.T_INT;
399-
case Type.LONG:
400-
return Constants.T_LONG;
401-
case Type.SHORT:
402-
return Constants.T_SHORT;
403-
case Type.BOOLEAN:
404-
return Constants.T_BOOLEAN;
405-
default:
406-
return -1; // error
407-
}
377+
return switch (type.getSort()) {
378+
case Type.BYTE -> Constants.T_BYTE;
379+
case Type.CHAR -> Constants.T_CHAR;
380+
case Type.DOUBLE -> Constants.T_DOUBLE;
381+
case Type.FLOAT -> Constants.T_FLOAT;
382+
case Type.INT -> Constants.T_INT;
383+
case Type.LONG -> Constants.T_LONG;
384+
case Type.SHORT -> Constants.T_SHORT;
385+
case Type.BOOLEAN -> Constants.T_BOOLEAN;
386+
default -> -1; // error
387+
};
408388
}
409389

410390
public static String escapeType(String s) {
411391
StringBuilder sb = new StringBuilder();
412392
for (int i = 0, len = s.length(); i < len; i++) {
413393
char c = s.charAt(i);
414-
switch (c) {
415-
case '$': sb.append("$24"); break;
416-
case '.': sb.append("$2E"); break;
417-
case '[': sb.append("$5B"); break;
418-
case ';': sb.append("$3B"); break;
419-
case '(': sb.append("$28"); break;
420-
case ')': sb.append("$29"); break;
421-
case '/': sb.append("$2F"); break;
422-
default:
423-
sb.append(c);
424-
}
394+
switch (c) {
395+
case '$' -> sb.append("$24");
396+
case '.' -> sb.append("$2E");
397+
case '[' -> sb.append("$5B");
398+
case ';' -> sb.append("$3B");
399+
case '(' -> sb.append("$28");
400+
case ')' -> sb.append("$29");
401+
case '/' -> sb.append("$2F");
402+
default -> sb.append(c);
403+
}
425404
}
426405
return sb.toString();
427406
}

spring-core/src/main/java/org/springframework/util/xml/StaxEventXMLReader.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,47 +104,31 @@ protected void parseInternal() throws SAXException, XMLStreamException {
104104
documentStarted = true;
105105
}
106106
switch (event.getEventType()) {
107-
case XMLStreamConstants.START_DOCUMENT:
107+
case XMLStreamConstants.START_DOCUMENT -> {
108108
handleStartDocument(event);
109109
documentStarted = true;
110-
break;
111-
case XMLStreamConstants.START_ELEMENT:
110+
}
111+
case XMLStreamConstants.START_ELEMENT -> {
112112
elementDepth++;
113113
handleStartElement(event.asStartElement());
114-
break;
115-
case XMLStreamConstants.END_ELEMENT:
114+
}
115+
case XMLStreamConstants.END_ELEMENT -> {
116116
elementDepth--;
117117
if (elementDepth >= 0) {
118118
handleEndElement(event.asEndElement());
119119
}
120-
break;
121-
case XMLStreamConstants.PROCESSING_INSTRUCTION:
122-
handleProcessingInstruction((ProcessingInstruction) event);
123-
break;
124-
case XMLStreamConstants.CHARACTERS:
125-
case XMLStreamConstants.SPACE:
126-
case XMLStreamConstants.CDATA:
127-
handleCharacters(event.asCharacters());
128-
break;
129-
case XMLStreamConstants.END_DOCUMENT:
120+
}
121+
case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction((ProcessingInstruction) event);
122+
case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters(event.asCharacters());
123+
case XMLStreamConstants.END_DOCUMENT -> {
130124
handleEndDocument();
131125
documentEnded = true;
132-
break;
133-
case XMLStreamConstants.NOTATION_DECLARATION:
134-
handleNotationDeclaration((NotationDeclaration) event);
135-
break;
136-
case XMLStreamConstants.ENTITY_DECLARATION:
137-
handleEntityDeclaration((EntityDeclaration) event);
138-
break;
139-
case XMLStreamConstants.COMMENT:
140-
handleComment((Comment) event);
141-
break;
142-
case XMLStreamConstants.DTD:
143-
handleDtd((DTD) event);
144-
break;
145-
case XMLStreamConstants.ENTITY_REFERENCE:
146-
handleEntityReference((EntityReference) event);
147-
break;
126+
}
127+
case XMLStreamConstants.NOTATION_DECLARATION -> handleNotationDeclaration((NotationDeclaration) event);
128+
case XMLStreamConstants.ENTITY_DECLARATION -> handleEntityDeclaration((EntityDeclaration) event);
129+
case XMLStreamConstants.COMMENT -> handleComment((Comment) event);
130+
case XMLStreamConstants.DTD -> handleDtd((DTD) event);
131+
case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference((EntityReference) event);
148132
}
149133
}
150134
if (documentStarted && !documentEnded) {

spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,41 +84,29 @@ protected void parseInternal() throws SAXException, XMLStreamException {
8484
documentStarted = true;
8585
}
8686
switch (eventType) {
87-
case XMLStreamConstants.START_ELEMENT:
87+
case XMLStreamConstants.START_ELEMENT -> {
8888
elementDepth++;
8989
handleStartElement();
90-
break;
91-
case XMLStreamConstants.END_ELEMENT:
90+
}
91+
case XMLStreamConstants.END_ELEMENT -> {
9292
elementDepth--;
9393
if (elementDepth >= 0) {
9494
handleEndElement();
9595
}
96-
break;
97-
case XMLStreamConstants.PROCESSING_INSTRUCTION:
98-
handleProcessingInstruction();
99-
break;
100-
case XMLStreamConstants.CHARACTERS:
101-
case XMLStreamConstants.SPACE:
102-
case XMLStreamConstants.CDATA:
103-
handleCharacters();
104-
break;
105-
case XMLStreamConstants.START_DOCUMENT:
96+
}
97+
case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction();
98+
case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters();
99+
case XMLStreamConstants.START_DOCUMENT -> {
106100
handleStartDocument();
107101
documentStarted = true;
108-
break;
109-
case XMLStreamConstants.END_DOCUMENT:
102+
}
103+
case XMLStreamConstants.END_DOCUMENT -> {
110104
handleEndDocument();
111105
documentEnded = true;
112-
break;
113-
case XMLStreamConstants.COMMENT:
114-
handleComment();
115-
break;
116-
case XMLStreamConstants.DTD:
117-
handleDtd();
118-
break;
119-
case XMLStreamConstants.ENTITY_REFERENCE:
120-
handleEntityReference();
121-
break;
106+
}
107+
case XMLStreamConstants.COMMENT -> handleComment();
108+
case XMLStreamConstants.DTD -> handleDtd();
109+
case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference();
122110
}
123111
if (this.reader.hasNext() && elementDepth >= 0) {
124112
eventType = this.reader.next();

0 commit comments

Comments
 (0)