Skip to content

Commit 2bd4136

Browse files
liachMandy Chung
authored andcommitted
8310849: Pattern matching for instanceof and arrayType cleanup in j.l.invoke and j.l.reflect
Reviewed-by: mchung, darcy
1 parent 7ce967a commit 2bd4136

34 files changed

+211
-292
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,8 @@ boolean isPartial() {
15891589
}
15901590

15911591
private static Class<?> toClass(Type o) {
1592-
if (o instanceof GenericArrayType)
1593-
return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
1594-
0)
1595-
.getClass();
1592+
if (o instanceof GenericArrayType gat)
1593+
return toClass(gat.getGenericComponentType()).arrayType();
15961594
return (Class<?>)o;
15971595
}
15981596

@@ -3013,8 +3011,8 @@ public InputStream getResourceAsStream(String name) {
30133011
// need for a URL connection
30143012
if (cl == null) {
30153013
return BootLoader.findResourceAsStream(mn, name);
3016-
} else if (cl instanceof BuiltinClassLoader) {
3017-
return ((BuiltinClassLoader) cl).findResourceAsStream(mn, name);
3014+
} else if (cl instanceof BuiltinClassLoader bcl) {
3015+
return bcl.findResourceAsStream(mn, name);
30183016
} else {
30193017
URL url = cl.findResource(mn, name);
30203018
return (url != null) ? url.openStream() : null;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -124,8 +124,8 @@ static BoundMethodHandle makeReinvoker(MethodHandle target) {
124124
/*non-public*/
125125
static BoundMethodHandle.SpeciesData speciesDataFor(LambdaForm form) {
126126
Object c = form.names[0].constraint;
127-
if (c instanceof SpeciesData) {
128-
return (SpeciesData) c;
127+
if (c instanceof SpeciesData sd) {
128+
return sd;
129129
}
130130
// if there is no BMH constraint, then use the null constraint
131131
return SPECIALIZER.topSpecies();
@@ -153,8 +153,8 @@ final String internalValues(int indentLevel) {
153153
for (int i = 0; i < count; ++i) {
154154
Object theArg = arg(i);
155155
sb.append("\n ").append(prefix).append(i);
156-
if (indentLevel >= 0 && theArg instanceof MethodHandle) {
157-
sb.append(": MethodHandle = {").append(((MethodHandle)theArg).debugString(indentLevel+1));
156+
if (indentLevel >= 0 && theArg instanceof MethodHandle mh) {
157+
sb.append(": MethodHandle = {").append(mh.debugString(indentLevel+1));
158158
sb.append("\n ").append(prefix).append("}");
159159
} else {
160160
sb.append(": ( ").append(theArg).append(" )");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -315,8 +315,8 @@ static CallSite makeSite(MethodHandle bootstrapMethod,
315315
try {
316316
Object binding = BootstrapMethodInvoker.invoke(
317317
CallSite.class, bootstrapMethod, name, type, info, callerClass);
318-
if (binding instanceof CallSite) {
319-
site = (CallSite) binding;
318+
if (binding instanceof CallSite cs) {
319+
site = cs;
320320
} else {
321321
// See the "Linking Exceptions" section for the invokedynamic
322322
// instruction in JVMS 6.5.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,10 @@ public int hashCode() {
265265

266266
@Override
267267
public boolean equals(Object obj) {
268-
if (!(obj instanceof ClassSpecializer.SpeciesData)) {
268+
if (!(obj instanceof ClassSpecializer<?, ?, ?>.SpeciesData that)) {
269269
return false;
270270
}
271-
@SuppressWarnings("rawtypes")
272-
ClassSpecializer.SpeciesData that = (ClassSpecializer.SpeciesData) obj;
271+
273272
return this.outer() == that.outer() && this.key.equals(that.key);
274273
}
275274

@@ -656,8 +655,8 @@ <X> List<Var> fromTypes(List<X> types) {
656655
for (X x : types) {
657656
String vn = name;
658657
Class<?> vt;
659-
if (x instanceof Class) {
660-
vt = (Class<?>) x;
658+
if (x instanceof Class<?> cl) {
659+
vt = cl;
661660
// make the names friendlier if debugging
662661
assert((vn = vn + "_" + (i++)) != null);
663662
} else {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,9 +27,7 @@
2727

2828
import java.security.*;
2929
import java.lang.reflect.*;
30-
import java.lang.invoke.MethodHandleNatives.Constants;
3130
import java.lang.invoke.MethodHandles.Lookup;
32-
import static java.lang.invoke.MethodHandleStatics.*;
3331

3432
/*
3533
* Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
@@ -132,11 +130,11 @@ private Member reflectUnchecked() throws ReflectiveOperationException {
132130
}
133131

134132
private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException {
135-
if (mem instanceof Method) {
133+
if (mem instanceof Method mth) {
136134
boolean wantSpecial = (refKind == REF_invokeSpecial);
137-
return new MemberName((Method) mem, wantSpecial);
138-
} else if (mem instanceof Constructor) {
139-
return new MemberName((Constructor) mem);
135+
return new MemberName(mth, wantSpecial);
136+
} else if (mem instanceof Constructor<?> ctor) {
137+
return new MemberName(ctor);
140138
} else if (mem instanceof Field) {
141139
boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic);
142140
return new MemberName((Field) mem, isSetter);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,8 @@ void addMethod() {
804804
case SELECT_ALTERNATIVE:
805805
assert lambdaForm.isSelectAlternative(i);
806806
if (PROFILE_GWT) {
807-
assert(name.arguments[0] instanceof Name &&
808-
((Name)name.arguments[0]).refersTo(MethodHandleImpl.class, "profileBoolean"));
807+
assert(name.arguments[0] instanceof Name n &&
808+
n.refersTo(MethodHandleImpl.class, "profileBoolean"));
809809
mv.visitAnnotation(INJECTEDPROFILE_SIG, true);
810810
}
811811
onStack = emitSelectAlternative(name, lambdaForm.names[i+1]);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import jdk.internal.vm.annotation.Hidden;
3131
import jdk.internal.vm.annotation.Stable;
3232

33-
import java.lang.reflect.Array;
3433
import java.util.Arrays;
3534

3635
import static java.lang.invoke.MethodHandleStatics.*;
@@ -244,7 +243,7 @@ private static Class<?> impliedRestargType(MethodType restargType, int fromPos)
244243
throw newIllegalArgumentException("need homogeneous rest arguments", restargType);
245244
}
246245
if (argType == Object.class) return Object[].class;
247-
return Array.newInstance(argType, 0).getClass();
246+
return argType.arrayType();
248247
}
249248

250249
public String toString() {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -949,8 +949,8 @@ Object interpretName(Name name, Object[] values) throws Throwable {
949949
Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
950950
for (int i = 0; i < arguments.length; i++) {
951951
Object a = arguments[i];
952-
if (a instanceof Name) {
953-
int i2 = ((Name)a).index();
952+
if (a instanceof Name n) {
953+
int i2 = n.index();
954954
assert(names[i2] == a);
955955
a = values[i2];
956956
arguments[i] = a;
@@ -1061,7 +1061,7 @@ String debugString(int indentLevel) {
10611061

10621062
@Override
10631063
public boolean equals(Object obj) {
1064-
return obj instanceof LambdaForm && equals((LambdaForm)obj);
1064+
return obj instanceof LambdaForm lf && equals(lf);
10651065
}
10661066
public boolean equals(LambdaForm that) {
10671067
if (this.result != that.result) return false;
@@ -1362,7 +1362,7 @@ private Name(Name that, Object constraint) {
13621362
}
13631363
Name(MethodType functionType, Object... arguments) {
13641364
this(new NamedFunction(functionType), arguments);
1365-
assert(arguments[0] instanceof Name && ((Name)arguments[0]).type == L_TYPE);
1365+
assert(arguments[0] instanceof Name name && name.type == L_TYPE);
13661366
}
13671367
Name(MemberName function, Object... arguments) {
13681368
this(new NamedFunction(function), arguments);
@@ -1524,7 +1524,7 @@ public String paramString() {
15241524
Object c = constraint;
15251525
if (c == null)
15261526
return s;
1527-
if (c instanceof Class) c = ((Class<?>)c).getSimpleName();
1527+
if (c instanceof Class<?> cl) c = cl.getSimpleName();
15281528
return s + "/" + c;
15291529
}
15301530
public String exprString() {
@@ -1556,8 +1556,8 @@ private boolean typesMatch(NamedFunction function, Object ... arguments) {
15561556
}
15571557

15581558
private static boolean typesMatch(BasicType parameterType, Object object) {
1559-
if (object instanceof Name) {
1560-
return ((Name)object).type == parameterType;
1559+
if (object instanceof Name name) {
1560+
return name.type == parameterType;
15611561
}
15621562
switch (parameterType) {
15631563
case I_TYPE: return object instanceof Integer;
@@ -1608,7 +1608,7 @@ public boolean equals(Name that) {
16081608
}
16091609
@Override
16101610
public boolean equals(Object x) {
1611-
return x instanceof Name && equals((Name)x);
1611+
return x instanceof Name n && equals(n);
16121612
}
16131613
@Override
16141614
public int hashCode() {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -101,10 +101,10 @@ private Transform(long packedBytes, byte[] fullBytes, LambdaForm result) {
101101

102102
@Override
103103
public boolean equals(Object obj) {
104-
if (obj instanceof TransformKey) {
105-
return equals((TransformKey) obj);
104+
if (obj instanceof TransformKey key) {
105+
return equals(key);
106106
}
107-
return obj instanceof Transform && equals((Transform)obj);
107+
return obj instanceof Transform transform && equals(transform);
108108
}
109109

110110
private boolean equals(TransformKey that) {
@@ -354,10 +354,10 @@ public String toString() {
354354

355355
@Override
356356
public boolean equals(Object obj) {
357-
if (obj instanceof TransformKey) {
358-
return equals((TransformKey) obj);
357+
if (obj instanceof TransformKey key) {
358+
return equals(key);
359359
}
360-
return obj instanceof Transform && equals((Transform)obj);
360+
return obj instanceof Transform transform && equals(transform);
361361
}
362362

363363
private boolean equals(TransformKey that) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,7 +34,7 @@ final class LambdaProxyClassArchive {
3434
*/
3535
static boolean loadedByBuiltinLoader(Class<?> cls) {
3636
ClassLoader cl = cls.getClassLoader();
37-
return (cl == null || (cl instanceof BuiltinClassLoader)) ? true : false;
37+
return cl == null || (cl instanceof BuiltinClassLoader);
3838
}
3939

4040
private static native void addToArchive(Class<?> caller,

0 commit comments

Comments
 (0)