Skip to content

Commit d43c8a7

Browse files
committed
8268124: Update java.lang to use switch expressions
Reviewed-by: naoto, darcy, mchung, iris, lancea, dfuchs
1 parent a187fcc commit d43c8a7

22 files changed

+422
-552
lines changed

src/java.base/share/classes/java/lang/CharacterData.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,15 @@ static final CharacterData of(int ch) {
7272
if (ch >>> 8 == 0) { // fast-path
7373
return CharacterDataLatin1.instance;
7474
} else {
75-
switch(ch >>> 16) { //plane 00-16
76-
case(0):
77-
return CharacterData00.instance;
78-
case(1):
79-
return CharacterData01.instance;
80-
case(2):
81-
return CharacterData02.instance;
82-
case(3):
83-
return CharacterData03.instance;
84-
case(14):
85-
return CharacterData0E.instance;
86-
case(15): // Private Use
87-
case(16): // Private Use
88-
return CharacterDataPrivateUse.instance;
89-
default:
90-
return CharacterDataUndefined.instance;
91-
}
75+
return switch (ch >>> 16) { //plane 00-16
76+
case 0 -> CharacterData00.instance;
77+
case 1 -> CharacterData01.instance;
78+
case 2 -> CharacterData02.instance;
79+
case 3 -> CharacterData03.instance;
80+
case 14 -> CharacterData0E.instance;
81+
case 15, 16 -> CharacterDataPrivateUse.instance; // Both cases Private Use
82+
default -> CharacterDataUndefined.instance;
83+
};
9284
}
9385
}
9486
}

src/java.base/share/classes/java/lang/ConditionalSpecialCasing.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,14 @@ private static char[] lookUpTable(String src, int index, Locale locale, boolean
170170
}
171171

172172
private static boolean isConditionMet(String src, int index, Locale locale, int condition) {
173-
switch (condition) {
174-
case FINAL_CASED:
175-
return isFinalCased(src, index, locale);
176-
177-
case AFTER_SOFT_DOTTED:
178-
return isAfterSoftDotted(src, index);
179-
180-
case MORE_ABOVE:
181-
return isMoreAbove(src, index);
182-
183-
case AFTER_I:
184-
return isAfterI(src, index);
185-
186-
case NOT_BEFORE_DOT:
187-
return !isBeforeDot(src, index);
188-
189-
default:
190-
return true;
191-
}
173+
return switch (condition) {
174+
case FINAL_CASED -> isFinalCased(src, index, locale);
175+
case AFTER_SOFT_DOTTED -> isAfterSoftDotted(src, index);
176+
case MORE_ABOVE -> isMoreAbove(src, index);
177+
case AFTER_I -> isAfterI(src, index);
178+
case NOT_BEFORE_DOT -> !isBeforeDot(src, index);
179+
default -> true;
180+
};
192181
}
193182

194183
/**

src/java.base/share/classes/java/lang/Long.java

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,38 +213,27 @@ public static String toUnsignedString(long i, int radix) {
213213
if (i >= 0)
214214
return toString(i, radix);
215215
else {
216-
switch (radix) {
217-
case 2:
218-
return toBinaryString(i);
219-
220-
case 4:
221-
return toUnsignedString0(i, 2);
222-
223-
case 8:
224-
return toOctalString(i);
225-
226-
case 10:
227-
/*
228-
* We can get the effect of an unsigned division by 10
229-
* on a long value by first shifting right, yielding a
230-
* positive value, and then dividing by 5. This
231-
* allows the last digit and preceding digits to be
232-
* isolated more quickly than by an initial conversion
233-
* to BigInteger.
234-
*/
235-
long quot = (i >>> 1) / 5;
236-
long rem = i - quot * 10;
237-
return toString(quot) + rem;
238-
239-
case 16:
240-
return toHexString(i);
241-
242-
case 32:
243-
return toUnsignedString0(i, 5);
244-
245-
default:
246-
return toUnsignedBigInteger(i).toString(radix);
247-
}
216+
return switch (radix) {
217+
case 2 -> toBinaryString(i);
218+
case 4 -> toUnsignedString0(i, 2);
219+
case 8 -> toOctalString(i);
220+
case 10 -> {
221+
/*
222+
* We can get the effect of an unsigned division by 10
223+
* on a long value by first shifting right, yielding a
224+
* positive value, and then dividing by 5. This
225+
* allows the last digit and preceding digits to be
226+
* isolated more quickly than by an initial conversion
227+
* to BigInteger.
228+
*/
229+
long quot = (i >>> 1) / 5;
230+
long rem = i - quot * 10;
231+
yield toString(quot) + rem;
232+
}
233+
case 16 -> toHexString(i);
234+
case 32 -> toUnsignedString0(i, 5);
235+
default -> toUnsignedBigInteger(i).toString(radix);
236+
};
248237
}
249238
}
250239

src/java.base/share/classes/java/lang/Math.java

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,29 +1926,25 @@ public static float fma(float a, float b, float c) {
19261926
public static double ulp(double d) {
19271927
int exp = getExponent(d);
19281928

1929-
switch(exp) {
1930-
case Double.MAX_EXPONENT + 1: // NaN or infinity
1931-
return Math.abs(d);
1932-
1933-
case Double.MIN_EXPONENT - 1: // zero or subnormal
1934-
return Double.MIN_VALUE;
1935-
1936-
default:
1937-
assert exp <= Double.MAX_EXPONENT && exp >= Double.MIN_EXPONENT;
1938-
1939-
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1940-
exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
1941-
if (exp >= Double.MIN_EXPONENT) {
1942-
return powerOfTwoD(exp);
1943-
}
1944-
else {
1945-
// return a subnormal result; left shift integer
1946-
// representation of Double.MIN_VALUE appropriate
1947-
// number of positions
1948-
return Double.longBitsToDouble(1L <<
1949-
(exp - (Double.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
1929+
return switch(exp) {
1930+
case Double.MAX_EXPONENT + 1 -> Math.abs(d); // NaN or infinity
1931+
case Double.MIN_EXPONENT - 1 -> Double.MIN_VALUE; // zero or subnormal
1932+
default -> {
1933+
assert exp <= Double.MAX_EXPONENT && exp >= Double.MIN_EXPONENT;
1934+
1935+
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1936+
exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH - 1);
1937+
if (exp >= Double.MIN_EXPONENT) {
1938+
yield powerOfTwoD(exp);
1939+
} else {
1940+
// return a subnormal result; left shift integer
1941+
// representation of Double.MIN_VALUE appropriate
1942+
// number of positions
1943+
yield Double.longBitsToDouble(1L <<
1944+
(exp - (Double.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH - 1))));
1945+
}
19501946
}
1951-
}
1947+
};
19521948
}
19531949

19541950
/**
@@ -1977,28 +1973,25 @@ public static double ulp(double d) {
19771973
public static float ulp(float f) {
19781974
int exp = getExponent(f);
19791975

1980-
switch(exp) {
1981-
case Float.MAX_EXPONENT+1: // NaN or infinity
1982-
return Math.abs(f);
1976+
return switch(exp) {
1977+
case Float.MAX_EXPONENT + 1 -> Math.abs(f); // NaN or infinity
1978+
case Float.MIN_EXPONENT - 1 -> Float.MIN_VALUE; // zero or subnormal
1979+
default -> {
1980+
assert exp <= Float.MAX_EXPONENT && exp >= Float.MIN_EXPONENT;
19831981

1984-
case Float.MIN_EXPONENT-1: // zero or subnormal
1985-
return Float.MIN_VALUE;
1986-
1987-
default:
1988-
assert exp <= Float.MAX_EXPONENT && exp >= Float.MIN_EXPONENT;
1989-
1990-
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1991-
exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
1992-
if (exp >= Float.MIN_EXPONENT) {
1993-
return powerOfTwoF(exp);
1994-
} else {
1995-
// return a subnormal result; left shift integer
1996-
// representation of FloatConsts.MIN_VALUE appropriate
1997-
// number of positions
1998-
return Float.intBitsToFloat(1 <<
1999-
(exp - (Float.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
1982+
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1983+
exp = exp - (FloatConsts.SIGNIFICAND_WIDTH - 1);
1984+
if (exp >= Float.MIN_EXPONENT) {
1985+
yield powerOfTwoF(exp);
1986+
} else {
1987+
// return a subnormal result; left shift integer
1988+
// representation of FloatConsts.MIN_VALUE appropriate
1989+
// number of positions
1990+
yield Float.intBitsToFloat(1 <<
1991+
(exp - (Float.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH - 1))));
1992+
}
20001993
}
2001-
}
1994+
};
20021995
}
20031996

20041997
/**

src/java.base/share/classes/java/lang/constant/DirectMethodHandleDescImpl.java

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ final class DirectMethodHandleDescImpl implements DirectMethodHandleDesc {
7272
requireNonNull(type);
7373

7474
switch (kind) {
75-
case CONSTRUCTOR: validateConstructor(type); break;
76-
case GETTER: validateFieldType(type, false, true); break;
77-
case SETTER: validateFieldType(type, true, true); break;
78-
case STATIC_GETTER: validateFieldType(type, false, false); break;
79-
case STATIC_SETTER: validateFieldType(type, true, false); break;
75+
case CONSTRUCTOR -> validateConstructor(type);
76+
case GETTER -> validateFieldType(type, false, true);
77+
case SETTER -> validateFieldType(type, true, true);
78+
case STATIC_GETTER -> validateFieldType(type, false, false);
79+
case STATIC_SETTER -> validateFieldType(type, true, false);
8080
}
8181

8282
this.kind = kind;
@@ -134,57 +134,40 @@ public MethodTypeDesc invocationType() {
134134

135135
@Override
136136
public String lookupDescriptor() {
137-
switch (kind) {
138-
case VIRTUAL:
139-
case SPECIAL:
140-
case INTERFACE_VIRTUAL:
141-
case INTERFACE_SPECIAL:
142-
return invocationType.dropParameterTypes(0, 1).descriptorString();
143-
case STATIC:
144-
case INTERFACE_STATIC:
145-
return invocationType.descriptorString();
146-
case CONSTRUCTOR:
147-
return invocationType.changeReturnType(CD_void).descriptorString();
148-
case GETTER:
149-
case STATIC_GETTER:
150-
return invocationType.returnType().descriptorString();
151-
case SETTER:
152-
return invocationType.parameterType(1).descriptorString();
153-
case STATIC_SETTER:
154-
return invocationType.parameterType(0).descriptorString();
155-
default:
156-
throw new IllegalStateException(kind.toString());
157-
}
137+
return switch (kind) {
138+
case VIRTUAL,
139+
SPECIAL,
140+
INTERFACE_VIRTUAL,
141+
INTERFACE_SPECIAL -> invocationType.dropParameterTypes(0, 1).descriptorString();
142+
case STATIC,
143+
INTERFACE_STATIC -> invocationType.descriptorString();
144+
case CONSTRUCTOR -> invocationType.changeReturnType(CD_void).descriptorString();
145+
case GETTER,
146+
STATIC_GETTER -> invocationType.returnType().descriptorString();
147+
case SETTER -> invocationType.parameterType(1).descriptorString();
148+
case STATIC_SETTER -> invocationType.parameterType(0).descriptorString();
149+
default -> throw new IllegalStateException(kind.toString());
150+
};
158151
}
159152

160153
public MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup)
161154
throws ReflectiveOperationException {
162155
Class<?> resolvedOwner = (Class<?>) owner.resolveConstantDesc(lookup);
163156
MethodType invocationType = (MethodType) this.invocationType().resolveConstantDesc(lookup);
164-
switch (kind) {
165-
case STATIC:
166-
case INTERFACE_STATIC:
167-
return lookup.findStatic(resolvedOwner, name, invocationType);
168-
case INTERFACE_VIRTUAL:
169-
case VIRTUAL:
170-
return lookup.findVirtual(resolvedOwner, name, invocationType.dropParameterTypes(0, 1));
171-
case SPECIAL:
172-
case INTERFACE_SPECIAL:
173-
return lookup.findSpecial(resolvedOwner, name, invocationType.dropParameterTypes(0, 1),
174-
lookup.lookupClass());
175-
case CONSTRUCTOR:
176-
return lookup.findConstructor(resolvedOwner, invocationType.changeReturnType(void.class));
177-
case GETTER:
178-
return lookup.findGetter(resolvedOwner, name, invocationType.returnType());
179-
case STATIC_GETTER:
180-
return lookup.findStaticGetter(resolvedOwner, name, invocationType.returnType());
181-
case SETTER:
182-
return lookup.findSetter(resolvedOwner, name, invocationType.parameterType(1));
183-
case STATIC_SETTER:
184-
return lookup.findStaticSetter(resolvedOwner, name, invocationType.parameterType(0));
185-
default:
186-
throw new IllegalStateException(kind.name());
187-
}
157+
return switch (kind) {
158+
case STATIC,
159+
INTERFACE_STATIC -> lookup.findStatic(resolvedOwner, name, invocationType);
160+
case VIRTUAL,
161+
INTERFACE_VIRTUAL -> lookup.findVirtual(resolvedOwner, name, invocationType.dropParameterTypes(0, 1));
162+
case SPECIAL,
163+
INTERFACE_SPECIAL -> lookup.findSpecial(resolvedOwner, name, invocationType.dropParameterTypes(0, 1), lookup.lookupClass());
164+
case CONSTRUCTOR -> lookup.findConstructor(resolvedOwner, invocationType.changeReturnType(void.class));
165+
case GETTER -> lookup.findGetter(resolvedOwner, name, invocationType.returnType());
166+
case STATIC_GETTER -> lookup.findStaticGetter(resolvedOwner, name, invocationType.returnType());
167+
case SETTER -> lookup.findSetter(resolvedOwner, name, invocationType.parameterType(1));
168+
case STATIC_SETTER -> lookup.findStaticSetter(resolvedOwner, name, invocationType.parameterType(0));
169+
default -> throw new IllegalStateException(kind.name());
170+
};
188171
}
189172

190173
/**

src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,13 @@ static DirectMethodHandleDesc ofField(DirectMethodHandleDesc.Kind kind,
156156
ClassDesc owner,
157157
String fieldName,
158158
ClassDesc fieldType) {
159-
MethodTypeDesc mtr;
160-
switch (kind) {
161-
case GETTER: mtr = MethodTypeDesc.of(fieldType, owner); break;
162-
case SETTER: mtr = MethodTypeDesc.of(CD_void, owner, fieldType); break;
163-
case STATIC_GETTER: mtr = MethodTypeDesc.of(fieldType); break;
164-
case STATIC_SETTER: mtr = MethodTypeDesc.of(CD_void, fieldType); break;
165-
default:
166-
throw new IllegalArgumentException(kind.toString());
167-
}
159+
MethodTypeDesc mtr = switch (kind) {
160+
case GETTER -> MethodTypeDesc.of(fieldType, owner);
161+
case SETTER -> MethodTypeDesc.of(CD_void, owner, fieldType);
162+
case STATIC_GETTER -> MethodTypeDesc.of(fieldType);
163+
case STATIC_SETTER -> MethodTypeDesc.of(CD_void, fieldType);
164+
default -> throw new IllegalArgumentException(kind.toString());
165+
};
168166
return new DirectMethodHandleDescImpl(kind, owner, fieldName, mtr);
169167
}
170168

src/java.base/share/classes/java/lang/invoke/BoundMethodHandle.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,14 @@ abstract class BoundMethodHandle extends MethodHandle {
6464
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
6565
// for some type signatures, there exist pre-defined concrete BMH classes
6666
try {
67-
switch (xtype) {
68-
case L_TYPE:
69-
return bindSingle(type, form, x); // Use known fast path.
70-
case I_TYPE:
71-
return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(I_TYPE_NUM).factory().invokeBasic(type, form, ValueConversions.widenSubword(x));
72-
case J_TYPE:
73-
return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(J_TYPE_NUM).factory().invokeBasic(type, form, (long) x);
74-
case F_TYPE:
75-
return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(F_TYPE_NUM).factory().invokeBasic(type, form, (float) x);
76-
case D_TYPE:
77-
return (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(D_TYPE_NUM).factory().invokeBasic(type, form, (double) x);
78-
default : throw newInternalError("unexpected xtype: " + xtype);
79-
}
67+
return switch (xtype) {
68+
case L_TYPE -> bindSingle(type, form, x); // Use known fast path.
69+
case I_TYPE -> (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(I_TYPE_NUM).factory().invokeBasic(type, form, ValueConversions.widenSubword(x));
70+
case J_TYPE -> (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(J_TYPE_NUM).factory().invokeBasic(type, form, (long) x);
71+
case F_TYPE -> (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(F_TYPE_NUM).factory().invokeBasic(type, form, (float) x);
72+
case D_TYPE -> (BoundMethodHandle) SPECIALIZER.topSpecies().extendWith(D_TYPE_NUM).factory().invokeBasic(type, form, (double) x);
73+
default -> throw newInternalError("unexpected xtype: " + xtype);
74+
};
8075
} catch (Throwable t) {
8176
throw uncaughtException(t);
8277
}

src/java.base/share/classes/java/lang/invoke/ClassSpecializer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -853,14 +853,14 @@ public void emitFieldInsn(int asmop, MethodVisitor mv) {
853853
}
854854

855855
private int typeLoadOp(char t) {
856-
switch (t) {
857-
case 'L': return ALOAD;
858-
case 'I': return ILOAD;
859-
case 'J': return LLOAD;
860-
case 'F': return FLOAD;
861-
case 'D': return DLOAD;
862-
default : throw newInternalError("unrecognized type " + t);
863-
}
856+
return switch (t) {
857+
case 'L' -> ALOAD;
858+
case 'I' -> ILOAD;
859+
case 'J' -> LLOAD;
860+
case 'F' -> FLOAD;
861+
case 'D' -> DLOAD;
862+
default -> throw newInternalError("unrecognized type " + t);
863+
};
864864
}
865865

866866
private void emitIntConstant(int con, MethodVisitor mv) {

0 commit comments

Comments
 (0)