Replace switch statements with enhanced switch statements for consistency

Closes gh-30283
This commit is contained in:
SW 2023-04-04 23:44:32 +09:00 committed by Sam Brannen
parent f2ae106c32
commit be94e1a2fb
3 changed files with 78 additions and 127 deletions

View File

@ -251,26 +251,17 @@ public class TypeUtils {
}
public static Type getBoxedType(Type type) {
switch (type.getSort()) {
case Type.CHAR:
return Constants.TYPE_CHARACTER;
case Type.BOOLEAN:
return Constants.TYPE_BOOLEAN;
case Type.DOUBLE:
return Constants.TYPE_DOUBLE;
case Type.FLOAT:
return Constants.TYPE_FLOAT;
case Type.LONG:
return Constants.TYPE_LONG;
case Type.INT:
return Constants.TYPE_INTEGER;
case Type.SHORT:
return Constants.TYPE_SHORT;
case Type.BYTE:
return Constants.TYPE_BYTE;
default:
return type;
}
return switch (type.getSort()) {
case Type.CHAR -> Constants.TYPE_CHARACTER;
case Type.BOOLEAN -> Constants.TYPE_BOOLEAN;
case Type.DOUBLE -> Constants.TYPE_DOUBLE;
case Type.FLOAT -> Constants.TYPE_FLOAT;
case Type.LONG -> Constants.TYPE_LONG;
case Type.INT -> Constants.TYPE_INTEGER;
case Type.SHORT -> Constants.TYPE_SHORT;
case Type.BYTE -> Constants.TYPE_BYTE;
default -> type;
};
}
public static Type getUnboxedType(Type type) {
@ -307,13 +298,10 @@ public class TypeUtils {
}
public static boolean isPrimitive(Type type) {
switch (type.getSort()) {
case Type.ARRAY:
case Type.OBJECT:
return false;
default:
return true;
}
return switch (type.getSort()) {
case Type.ARRAY, Type.OBJECT -> false;
default -> true;
};
}
public static String emulateClassGetName(Type type) {
@ -340,16 +328,17 @@ public class TypeUtils {
}
public static int ICONST(int value) {
switch (value) {
case -1: return Constants.ICONST_M1;
case 0: return Constants.ICONST_0;
case 1: return Constants.ICONST_1;
case 2: return Constants.ICONST_2;
case 3: return Constants.ICONST_3;
case 4: return Constants.ICONST_4;
case 5: return Constants.ICONST_5;
}
return -1; // error
return switch (value) {
case -1 -> Constants.ICONST_M1;
case 0 -> Constants.ICONST_0;
case 1 -> Constants.ICONST_1;
case 2 -> Constants.ICONST_2;
case 3 -> Constants.ICONST_3;
case 4 -> Constants.ICONST_4;
case 5 -> Constants.ICONST_5;
default -> -1;
};
// error
}
public static int LCONST(long value) {
@ -385,26 +374,17 @@ public class TypeUtils {
}
public static int NEWARRAY(Type type) {
switch (type.getSort()) {
case Type.BYTE:
return Constants.T_BYTE;
case Type.CHAR:
return Constants.T_CHAR;
case Type.DOUBLE:
return Constants.T_DOUBLE;
case Type.FLOAT:
return Constants.T_FLOAT;
case Type.INT:
return Constants.T_INT;
case Type.LONG:
return Constants.T_LONG;
case Type.SHORT:
return Constants.T_SHORT;
case Type.BOOLEAN:
return Constants.T_BOOLEAN;
default:
return -1; // error
}
return switch (type.getSort()) {
case Type.BYTE -> Constants.T_BYTE;
case Type.CHAR -> Constants.T_CHAR;
case Type.DOUBLE -> Constants.T_DOUBLE;
case Type.FLOAT -> Constants.T_FLOAT;
case Type.INT -> Constants.T_INT;
case Type.LONG -> Constants.T_LONG;
case Type.SHORT -> Constants.T_SHORT;
case Type.BOOLEAN -> Constants.T_BOOLEAN;
default -> -1; // error
};
}
public static String escapeType(String s) {
@ -412,15 +392,14 @@ public class TypeUtils {
for (int i = 0, len = s.length(); i < len; i++) {
char c = s.charAt(i);
switch (c) {
case '$': sb.append("$24"); break;
case '.': sb.append("$2E"); break;
case '[': sb.append("$5B"); break;
case ';': sb.append("$3B"); break;
case '(': sb.append("$28"); break;
case ')': sb.append("$29"); break;
case '/': sb.append("$2F"); break;
default:
sb.append(c);
case '$' -> sb.append("$24");
case '.' -> sb.append("$2E");
case '[' -> sb.append("$5B");
case ';' -> sb.append("$3B");
case '(' -> sb.append("$28");
case ')' -> sb.append("$29");
case '/' -> sb.append("$2F");
default -> sb.append(c);
}
}
return sb.toString();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -104,47 +104,31 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.START_DOCUMENT -> {
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT:
}
case XMLStreamConstants.START_ELEMENT -> {
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
}
case XMLStreamConstants.END_ELEMENT -> {
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
}
case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction((ProcessingInstruction) event);
case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters(event.asCharacters());
case XMLStreamConstants.END_DOCUMENT -> {
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
case XMLStreamConstants.NOTATION_DECLARATION -> handleNotationDeclaration((NotationDeclaration) event);
case XMLStreamConstants.ENTITY_DECLARATION -> handleEntityDeclaration((EntityDeclaration) event);
case XMLStreamConstants.COMMENT -> handleComment((Comment) event);
case XMLStreamConstants.DTD -> handleDtd((DTD) event);
case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference((EntityReference) event);
}
}
if (documentStarted && !documentEnded) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -84,41 +84,29 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
documentStarted = true;
}
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
case XMLStreamConstants.START_ELEMENT -> {
elementDepth++;
handleStartElement();
break;
case XMLStreamConstants.END_ELEMENT:
}
case XMLStreamConstants.END_ELEMENT -> {
elementDepth--;
if (elementDepth >= 0) {
handleEndElement();
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction();
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters();
break;
case XMLStreamConstants.START_DOCUMENT:
}
case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction();
case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters();
case XMLStreamConstants.START_DOCUMENT -> {
handleStartDocument();
documentStarted = true;
break;
case XMLStreamConstants.END_DOCUMENT:
}
case XMLStreamConstants.END_DOCUMENT -> {
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.COMMENT:
handleComment();
break;
case XMLStreamConstants.DTD:
handleDtd();
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference();
break;
}
case XMLStreamConstants.COMMENT -> handleComment();
case XMLStreamConstants.DTD -> handleDtd();
case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference();
}
if (this.reader.hasNext() && elementDepth >= 0) {
eventType = this.reader.next();