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) { public static Type getBoxedType(Type type) {
switch (type.getSort()) { return switch (type.getSort()) {
case Type.CHAR: case Type.CHAR -> Constants.TYPE_CHARACTER;
return Constants.TYPE_CHARACTER; case Type.BOOLEAN -> Constants.TYPE_BOOLEAN;
case Type.BOOLEAN: case Type.DOUBLE -> Constants.TYPE_DOUBLE;
return Constants.TYPE_BOOLEAN; case Type.FLOAT -> Constants.TYPE_FLOAT;
case Type.DOUBLE: case Type.LONG -> Constants.TYPE_LONG;
return Constants.TYPE_DOUBLE; case Type.INT -> Constants.TYPE_INTEGER;
case Type.FLOAT: case Type.SHORT -> Constants.TYPE_SHORT;
return Constants.TYPE_FLOAT; case Type.BYTE -> Constants.TYPE_BYTE;
case Type.LONG: default -> type;
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;
}
} }
public static Type getUnboxedType(Type type) { public static Type getUnboxedType(Type type) {
@ -307,13 +298,10 @@ public class TypeUtils {
} }
public static boolean isPrimitive(Type type) { public static boolean isPrimitive(Type type) {
switch (type.getSort()) { return switch (type.getSort()) {
case Type.ARRAY: case Type.ARRAY, Type.OBJECT -> false;
case Type.OBJECT: default -> true;
return false; };
default:
return true;
}
} }
public static String emulateClassGetName(Type type) { public static String emulateClassGetName(Type type) {
@ -340,17 +328,18 @@ public class TypeUtils {
} }
public static int ICONST(int value) { public static int ICONST(int value) {
switch (value) { return switch (value) {
case -1: return Constants.ICONST_M1; case -1 -> Constants.ICONST_M1;
case 0: return Constants.ICONST_0; case 0 -> Constants.ICONST_0;
case 1: return Constants.ICONST_1; case 1 -> Constants.ICONST_1;
case 2: return Constants.ICONST_2; case 2 -> Constants.ICONST_2;
case 3: return Constants.ICONST_3; case 3 -> Constants.ICONST_3;
case 4: return Constants.ICONST_4; case 4 -> Constants.ICONST_4;
case 5: return Constants.ICONST_5; case 5 -> Constants.ICONST_5;
} default -> -1;
return -1; // error };
} // error
}
public static int LCONST(long value) { public static int LCONST(long value) {
if (value == 0L) { if (value == 0L) {
@ -385,43 +374,33 @@ public class TypeUtils {
} }
public static int NEWARRAY(Type type) { public static int NEWARRAY(Type type) {
switch (type.getSort()) { return switch (type.getSort()) {
case Type.BYTE: case Type.BYTE -> Constants.T_BYTE;
return Constants.T_BYTE; case Type.CHAR -> Constants.T_CHAR;
case Type.CHAR: case Type.DOUBLE -> Constants.T_DOUBLE;
return Constants.T_CHAR; case Type.FLOAT -> Constants.T_FLOAT;
case Type.DOUBLE: case Type.INT -> Constants.T_INT;
return Constants.T_DOUBLE; case Type.LONG -> Constants.T_LONG;
case Type.FLOAT: case Type.SHORT -> Constants.T_SHORT;
return Constants.T_FLOAT; case Type.BOOLEAN -> Constants.T_BOOLEAN;
case Type.INT: default -> -1; // error
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
}
} }
public static String escapeType(String s) { public static String escapeType(String s) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0, len = s.length(); i < len; i++) { for (int i = 0, len = s.length(); i < len; i++) {
char c = s.charAt(i); char c = s.charAt(i);
switch (c) { switch (c) {
case '$': sb.append("$24"); break; case '$' -> sb.append("$24");
case '.': sb.append("$2E"); break; case '.' -> sb.append("$2E");
case '[': sb.append("$5B"); break; case '[' -> sb.append("$5B");
case ';': sb.append("$3B"); break; case ';' -> sb.append("$3B");
case '(': sb.append("$28"); break; case '(' -> sb.append("$28");
case ')': sb.append("$29"); break; case ')' -> sb.append("$29");
case '/': sb.append("$2F"); break; case '/' -> sb.append("$2F");
default: default -> sb.append(c);
sb.append(c); }
}
} }
return sb.toString(); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -104,47 +104,31 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
documentStarted = true; documentStarted = true;
} }
switch (event.getEventType()) { switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.START_DOCUMENT -> {
handleStartDocument(event); handleStartDocument(event);
documentStarted = true; documentStarted = true;
break; }
case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.START_ELEMENT -> {
elementDepth++; elementDepth++;
handleStartElement(event.asStartElement()); handleStartElement(event.asStartElement());
break; }
case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.END_ELEMENT -> {
elementDepth--; elementDepth--;
if (elementDepth >= 0) { if (elementDepth >= 0) {
handleEndElement(event.asEndElement()); handleEndElement(event.asEndElement());
} }
break; }
case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction((ProcessingInstruction) event);
handleProcessingInstruction((ProcessingInstruction) event); case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters(event.asCharacters());
break; case XMLStreamConstants.END_DOCUMENT -> {
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument(); handleEndDocument();
documentEnded = true; documentEnded = true;
break; }
case XMLStreamConstants.NOTATION_DECLARATION: case XMLStreamConstants.NOTATION_DECLARATION -> handleNotationDeclaration((NotationDeclaration) event);
handleNotationDeclaration((NotationDeclaration) event); case XMLStreamConstants.ENTITY_DECLARATION -> handleEntityDeclaration((EntityDeclaration) event);
break; case XMLStreamConstants.COMMENT -> handleComment((Comment) event);
case XMLStreamConstants.ENTITY_DECLARATION: case XMLStreamConstants.DTD -> handleDtd((DTD) event);
handleEntityDeclaration((EntityDeclaration) event); case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference((EntityReference) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
} }
} }
if (documentStarted && !documentEnded) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,41 +84,29 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
documentStarted = true; documentStarted = true;
} }
switch (eventType) { switch (eventType) {
case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.START_ELEMENT -> {
elementDepth++; elementDepth++;
handleStartElement(); handleStartElement();
break; }
case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.END_ELEMENT -> {
elementDepth--; elementDepth--;
if (elementDepth >= 0) { if (elementDepth >= 0) {
handleEndElement(); handleEndElement();
} }
break; }
case XMLStreamConstants.PROCESSING_INSTRUCTION: case XMLStreamConstants.PROCESSING_INSTRUCTION -> handleProcessingInstruction();
handleProcessingInstruction(); case XMLStreamConstants.CHARACTERS, XMLStreamConstants.SPACE, XMLStreamConstants.CDATA -> handleCharacters();
break; case XMLStreamConstants.START_DOCUMENT -> {
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters();
break;
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(); handleStartDocument();
documentStarted = true; documentStarted = true;
break; }
case XMLStreamConstants.END_DOCUMENT: case XMLStreamConstants.END_DOCUMENT -> {
handleEndDocument(); handleEndDocument();
documentEnded = true; documentEnded = true;
break; }
case XMLStreamConstants.COMMENT: case XMLStreamConstants.COMMENT -> handleComment();
handleComment(); case XMLStreamConstants.DTD -> handleDtd();
break; case XMLStreamConstants.ENTITY_REFERENCE -> handleEntityReference();
case XMLStreamConstants.DTD:
handleDtd();
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference();
break;
} }
if (this.reader.hasNext() && elementDepth >= 0) { if (this.reader.hasNext() && elementDepth >= 0) {
eventType = this.reader.next(); eventType = this.reader.next();