Use enhanced switch expressions where feasible

Closes gh-28014
This commit is contained in:
a.yazychyan
2022-02-07 20:51:32 +03:00
committed by Sam Brannen
parent 42d114534b
commit c5c926726d
38 changed files with 291 additions and 493 deletions

View File

@@ -287,10 +287,8 @@ public enum SpelMessage {
public String formatMessage(Object... inserts) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code);
switch (this.kind) {
case ERROR:
formattedMessage.append('E');
break;
if (this.kind == Kind.ERROR) {
formattedMessage.append('E');
}
formattedMessage.append(": ");
formattedMessage.append(MessageFormat.format(this.message, inserts));

View File

@@ -115,21 +115,12 @@ public class OpDivide extends Operator {
cf.exitCompilationScope();
CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
switch (targetDesc) {
case 'I':
mv.visitInsn(IDIV);
break;
case 'J':
mv.visitInsn(LDIV);
break;
case 'F':
mv.visitInsn(FDIV);
break;
case 'D':
mv.visitInsn(DDIV);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(IDIV);
case 'J' -> mv.visitInsn(LDIV);
case 'F' -> mv.visitInsn(FDIV);
case 'D' -> mv.visitInsn(DDIV);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
cf.pushDescriptor(this.exitTypeDescriptor);

View File

@@ -184,40 +184,22 @@ public class OpMinus extends Operator {
cf.exitCompilationScope();
CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
switch (targetDesc) {
case 'I':
mv.visitInsn(ISUB);
break;
case 'J':
mv.visitInsn(LSUB);
break;
case 'F':
mv.visitInsn(FSUB);
break;
case 'D':
mv.visitInsn(DSUB);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(ISUB);
case 'J' -> mv.visitInsn(LSUB);
case 'F' -> mv.visitInsn(FSUB);
case 'D' -> mv.visitInsn(DSUB);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
else {
switch (targetDesc) {
case 'I':
mv.visitInsn(INEG);
break;
case 'J':
mv.visitInsn(LNEG);
break;
case 'F':
mv.visitInsn(FNEG);
break;
case 'D':
mv.visitInsn(DNEG);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(INEG);
case 'J' -> mv.visitInsn(LNEG);
case 'F' -> mv.visitInsn(FNEG);
case 'D' -> mv.visitInsn(DNEG);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
cf.pushDescriptor(this.exitTypeDescriptor);

View File

@@ -112,21 +112,12 @@ public class OpModulus extends Operator {
cf.exitCompilationScope();
CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
switch (targetDesc) {
case 'I':
mv.visitInsn(IREM);
break;
case 'J':
mv.visitInsn(LREM);
break;
case 'F':
mv.visitInsn(FREM);
break;
case 'D':
mv.visitInsn(DREM);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(IREM);
case 'J' -> mv.visitInsn(LREM);
case 'F' -> mv.visitInsn(FREM);
case 'D' -> mv.visitInsn(DREM);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
cf.pushDescriptor(this.exitTypeDescriptor);

View File

@@ -140,21 +140,12 @@ public class OpMultiply extends Operator {
cf.exitCompilationScope();
CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
switch (targetDesc) {
case 'I':
mv.visitInsn(IMUL);
break;
case 'J':
mv.visitInsn(LMUL);
break;
case 'F':
mv.visitInsn(FMUL);
break;
case 'D':
mv.visitInsn(DMUL);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(IMUL);
case 'J' -> mv.visitInsn(LMUL);
case 'F' -> mv.visitInsn(FMUL);
case 'D' -> mv.visitInsn(DMUL);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
cf.pushDescriptor(this.exitTypeDescriptor);

View File

@@ -226,21 +226,12 @@ public class OpPlus extends Operator {
cf.exitCompilationScope();
CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
switch (targetDesc) {
case 'I':
mv.visitInsn(IADD);
break;
case 'J':
mv.visitInsn(LADD);
break;
case 'F':
mv.visitInsn(FADD);
break;
case 'D':
mv.visitInsn(DADD);
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
case 'I' -> mv.visitInsn(IADD);
case 'J' -> mv.visitInsn(LADD);
case 'F' -> mv.visitInsn(FADD);
case 'D' -> mv.visitInsn(DADD);
default -> throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
}
}
}

View File

@@ -209,12 +209,12 @@ public class Selection extends SpelNodeImpl {
}
private String prefix() {
switch (this.variant) {
case ALL: return "?[";
case FIRST: return "^[";
case LAST: return "$[";
}
return "";
return switch (this.variant) {
case ALL -> "?[";
case FIRST -> "^[";
case LAST -> "$[";
default -> "";
};
}
}