8310849: Pattern matching for instanceof and arrayType cleanup in j.l.invoke and j.l.reflect

Reviewed-by: mchung, darcy
This commit is contained in:
Chen Liang 2023-06-27 16:10:50 +00:00 committed by Mandy Chung
parent 7ce967a10c
commit 2bd4136bdb
34 changed files with 211 additions and 292 deletions

View file

@ -1589,10 +1589,8 @@ public final class Class<T> implements java.io.Serializable,
}
private static Class<?> toClass(Type o) {
if (o instanceof GenericArrayType)
return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
0)
.getClass();
if (o instanceof GenericArrayType gat)
return toClass(gat.getGenericComponentType()).arrayType();
return (Class<?>)o;
}
@ -3013,8 +3011,8 @@ public final class Class<T> implements java.io.Serializable,
// need for a URL connection
if (cl == null) {
return BootLoader.findResourceAsStream(mn, name);
} else if (cl instanceof BuiltinClassLoader) {
return ((BuiltinClassLoader) cl).findResourceAsStream(mn, name);
} else if (cl instanceof BuiltinClassLoader bcl) {
return bcl.findResourceAsStream(mn, name);
} else {
URL url = cl.findResource(mn, name);
return (url != null) ? url.openStream() : null;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -124,8 +124,8 @@ abstract non-sealed class BoundMethodHandle extends MethodHandle {
/*non-public*/
static BoundMethodHandle.SpeciesData speciesDataFor(LambdaForm form) {
Object c = form.names[0].constraint;
if (c instanceof SpeciesData) {
return (SpeciesData) c;
if (c instanceof SpeciesData sd) {
return sd;
}
// if there is no BMH constraint, then use the null constraint
return SPECIALIZER.topSpecies();
@ -153,8 +153,8 @@ abstract non-sealed class BoundMethodHandle extends MethodHandle {
for (int i = 0; i < count; ++i) {
Object theArg = arg(i);
sb.append("\n ").append(prefix).append(i);
if (indentLevel >= 0 && theArg instanceof MethodHandle) {
sb.append(": MethodHandle = {").append(((MethodHandle)theArg).debugString(indentLevel+1));
if (indentLevel >= 0 && theArg instanceof MethodHandle mh) {
sb.append(": MethodHandle = {").append(mh.debugString(indentLevel+1));
sb.append("\n ").append(prefix).append("}");
} else {
sb.append(": ( ").append(theArg).append(" )");

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -315,8 +315,8 @@ abstract sealed class CallSite permits ConstantCallSite, MutableCallSite, Volati
try {
Object binding = BootstrapMethodInvoker.invoke(
CallSite.class, bootstrapMethod, name, type, info, callerClass);
if (binding instanceof CallSite) {
site = (CallSite) binding;
if (binding instanceof CallSite cs) {
site = cs;
} else {
// See the "Linking Exceptions" section for the invokedynamic
// instruction in JVMS 6.5.

View file

@ -265,11 +265,10 @@ abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesDat
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ClassSpecializer.SpeciesData)) {
if (!(obj instanceof ClassSpecializer<?, ?, ?>.SpeciesData that)) {
return false;
}
@SuppressWarnings("rawtypes")
ClassSpecializer.SpeciesData that = (ClassSpecializer.SpeciesData) obj;
return this.outer() == that.outer() && this.key.equals(that.key);
}
@ -656,8 +655,8 @@ abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesDat
for (X x : types) {
String vn = name;
Class<?> vt;
if (x instanceof Class) {
vt = (Class<?>) x;
if (x instanceof Class<?> cl) {
vt = cl;
// make the names friendlier if debugging
assert((vn = vn + "_" + (i++)) != null);
} else {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,9 +27,7 @@ package java.lang.invoke;
import java.security.*;
import java.lang.reflect.*;
import java.lang.invoke.MethodHandleNatives.Constants;
import java.lang.invoke.MethodHandles.Lookup;
import static java.lang.invoke.MethodHandleStatics.*;
/*
* Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
@ -132,11 +130,11 @@ final class InfoFromMemberName implements MethodHandleInfo {
}
private static MemberName convertToMemberName(byte refKind, Member mem) throws IllegalAccessException {
if (mem instanceof Method) {
if (mem instanceof Method mth) {
boolean wantSpecial = (refKind == REF_invokeSpecial);
return new MemberName((Method) mem, wantSpecial);
} else if (mem instanceof Constructor) {
return new MemberName((Constructor) mem);
return new MemberName(mth, wantSpecial);
} else if (mem instanceof Constructor<?> ctor) {
return new MemberName(ctor);
} else if (mem instanceof Field) {
boolean isSetter = (refKind == REF_putField || refKind == REF_putStatic);
return new MemberName((Field) mem, isSetter);

View file

@ -804,8 +804,8 @@ class InvokerBytecodeGenerator {
case SELECT_ALTERNATIVE:
assert lambdaForm.isSelectAlternative(i);
if (PROFILE_GWT) {
assert(name.arguments[0] instanceof Name &&
((Name)name.arguments[0]).refersTo(MethodHandleImpl.class, "profileBoolean"));
assert(name.arguments[0] instanceof Name n &&
n.refersTo(MethodHandleImpl.class, "profileBoolean"));
mv.visitAnnotation(INJECTEDPROFILE_SIG, true);
}
onStack = emitSelectAlternative(name, lambdaForm.names[i+1]);

View file

@ -30,7 +30,6 @@ import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Hidden;
import jdk.internal.vm.annotation.Stable;
import java.lang.reflect.Array;
import java.util.Arrays;
import static java.lang.invoke.MethodHandleStatics.*;
@ -244,7 +243,7 @@ class Invokers {
throw newIllegalArgumentException("need homogeneous rest arguments", restargType);
}
if (argType == Object.class) return Object[].class;
return Array.newInstance(argType, 0).getClass();
return argType.arrayType();
}
public String toString() {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -949,8 +949,8 @@ class LambdaForm {
Object[] arguments = Arrays.copyOf(name.arguments, name.arguments.length, Object[].class);
for (int i = 0; i < arguments.length; i++) {
Object a = arguments[i];
if (a instanceof Name) {
int i2 = ((Name)a).index();
if (a instanceof Name n) {
int i2 = n.index();
assert(names[i2] == a);
a = values[i2];
arguments[i] = a;
@ -1061,7 +1061,7 @@ class LambdaForm {
@Override
public boolean equals(Object obj) {
return obj instanceof LambdaForm && equals((LambdaForm)obj);
return obj instanceof LambdaForm lf && equals(lf);
}
public boolean equals(LambdaForm that) {
if (this.result != that.result) return false;
@ -1362,7 +1362,7 @@ class LambdaForm {
}
Name(MethodType functionType, Object... arguments) {
this(new NamedFunction(functionType), arguments);
assert(arguments[0] instanceof Name && ((Name)arguments[0]).type == L_TYPE);
assert(arguments[0] instanceof Name name && name.type == L_TYPE);
}
Name(MemberName function, Object... arguments) {
this(new NamedFunction(function), arguments);
@ -1524,7 +1524,7 @@ class LambdaForm {
Object c = constraint;
if (c == null)
return s;
if (c instanceof Class) c = ((Class<?>)c).getSimpleName();
if (c instanceof Class<?> cl) c = cl.getSimpleName();
return s + "/" + c;
}
public String exprString() {
@ -1556,8 +1556,8 @@ class LambdaForm {
}
private static boolean typesMatch(BasicType parameterType, Object object) {
if (object instanceof Name) {
return ((Name)object).type == parameterType;
if (object instanceof Name name) {
return name.type == parameterType;
}
switch (parameterType) {
case I_TYPE: return object instanceof Integer;
@ -1608,7 +1608,7 @@ class LambdaForm {
}
@Override
public boolean equals(Object x) {
return x instanceof Name && equals((Name)x);
return x instanceof Name n && equals(n);
}
@Override
public int hashCode() {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -101,10 +101,10 @@ class LambdaFormEditor {
@Override
public boolean equals(Object obj) {
if (obj instanceof TransformKey) {
return equals((TransformKey) obj);
if (obj instanceof TransformKey key) {
return equals(key);
}
return obj instanceof Transform && equals((Transform)obj);
return obj instanceof Transform transform && equals(transform);
}
private boolean equals(TransformKey that) {
@ -354,10 +354,10 @@ class LambdaFormEditor {
@Override
public boolean equals(Object obj) {
if (obj instanceof TransformKey) {
return equals((TransformKey) obj);
if (obj instanceof TransformKey key) {
return equals(key);
}
return obj instanceof Transform && equals((Transform)obj);
return obj instanceof Transform transform && equals(transform);
}
private boolean equals(TransformKey that) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -34,7 +34,7 @@ final class LambdaProxyClassArchive {
*/
static boolean loadedByBuiltinLoader(Class<?> cls) {
ClassLoader cl = cls.getClassLoader();
return (cl == null || (cl instanceof BuiltinClassLoader)) ? true : false;
return cl == null || (cl instanceof BuiltinClassLoader);
}
private static native void addToArchive(Class<?> caller,

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -135,8 +135,8 @@ final class MemberName implements Member, Cloneable {
{
// Get a snapshot of type which doesn't get changed by racing threads.
final Object type = this.type;
if (type instanceof MethodType) {
return (MethodType) type;
if (type instanceof MethodType mt) {
return mt;
}
}
@ -173,8 +173,8 @@ final class MemberName implements Member, Cloneable {
// Get a snapshot of type which doesn't get changed by racing threads.
final Object type = this.type;
if (type instanceof String) {
return (String) type;
if (type instanceof String str) {
return str;
} else {
return getMethodType().toMethodDescriptorString();
}
@ -211,8 +211,8 @@ final class MemberName implements Member, Cloneable {
{
// Get a snapshot of type which doesn't get changed by racing threads.
final Object type = this.type;
if (type instanceof Class<?>) {
return (Class<?>) type;
if (type instanceof Class<?> cl) {
return cl;
}
}
@ -725,7 +725,7 @@ final class MemberName implements Member, Cloneable {
@Override
public boolean equals(Object that) {
return (that instanceof MemberName && this.equals((MemberName)that));
return that instanceof MemberName mn && this.equals(mn);
}
/** Decide if two member names have exactly the same symbolic content.
@ -808,8 +808,8 @@ final class MemberName implements Member, Cloneable {
void checkForTypeAlias(Class<?> refc) {
if (isInvocable()) {
MethodType type;
if (this.type instanceof MethodType)
type = (MethodType) this.type;
if (this.type instanceof MethodType mt)
type = mt;
else
this.type = type = getMethodType();
if (type.erase() == type) return;
@ -817,8 +817,8 @@ final class MemberName implements Member, Cloneable {
throw new LinkageError("bad method type alias: "+type+" not visible from "+refc);
} else {
Class<?> type;
if (this.type instanceof Class<?>)
type = (Class<?>) this.type;
if (this.type instanceof Class<?> cl)
type = cl;
else
this.type = type = getFieldType();
if (VerifyAccess.isTypeVisible(type, refc)) return;
@ -863,8 +863,8 @@ final class MemberName implements Member, Cloneable {
return buf.toString();
}
private static String getName(Object obj) {
if (obj instanceof Class<?>)
return ((Class<?>)obj).getName();
if (obj instanceof Class<?> cl)
return cl.getName();
return String.valueOf(obj);
}
@ -915,8 +915,8 @@ final class MemberName implements Member, Cloneable {
ex = new NoSuchMethodException(message);
else
ex = new NoSuchFieldException(message);
if (resolution instanceof Throwable)
ex.initCause((Throwable) resolution);
if (resolution instanceof Throwable res)
ex.initCause(res);
return ex;
}
@ -992,7 +992,7 @@ final class MemberName implements Member, Cloneable {
if (result.isResolved())
return result;
ReflectiveOperationException ex = result.makeAccessException();
if (ex instanceof IllegalAccessException) throw (IllegalAccessException) ex;
if (ex instanceof IllegalAccessException iae) throw iae;
throw nsmClass.cast(ex);
}
/** Produce a resolved version of the given member.

View file

@ -576,8 +576,8 @@ abstract class MethodHandleImpl {
return;
} else if (av == null) {
throw new NullPointerException("null array reference");
} else if (av instanceof Object[]) {
int len = ((Object[])av).length;
} else if (av instanceof Object[] array) {
int len = array.length;
if (len == n) return;
} else {
int len = java.lang.reflect.Array.getLength(av);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -350,10 +350,10 @@ class MethodHandleNatives {
}
private static String staticArglistForTrace(Object staticArguments) {
if (staticArguments instanceof Object[])
return "BSA="+java.util.Arrays.asList((Object[]) staticArguments);
if (staticArguments instanceof int[])
return "BSA@"+java.util.Arrays.toString((int[]) staticArguments);
if (staticArguments instanceof Object[] array)
return "BSA="+java.util.Arrays.asList(array);
if (staticArguments instanceof int[] array)
return "BSA@"+java.util.Arrays.toString(array);
if (staticArguments == null)
return "BSA0=null";
return "BSA1="+staticArguments;
@ -509,8 +509,8 @@ class MethodHandleNatives {
throw new LinkageError("no such method "+defc.getName()+"."+name+type);
}
private static MethodType fixMethodType(Class<?> callerClass, Object type) {
if (type instanceof MethodType)
return (MethodType) type;
if (type instanceof MethodType mt)
return mt;
else
return MethodType.fromDescriptor((String)type, callerClass.getClassLoader());
}
@ -637,8 +637,8 @@ class MethodHandleNatives {
LinkageError err;
if (ex instanceof IllegalAccessException) {
Throwable cause = ex.getCause();
if (cause instanceof AbstractMethodError) {
return (AbstractMethodError) cause;
if (cause instanceof AbstractMethodError ame) {
return ame;
} else {
err = new IllegalAccessError(ex.getMessage());
}

View file

@ -185,8 +185,8 @@ class MethodHandleStatics {
/** Propagate unchecked exceptions and errors, but wrap anything checked and throw that instead. */
/*non-public*/
static Error uncaughtException(Throwable ex) {
if (ex instanceof Error) throw (Error) ex;
if (ex instanceof RuntimeException) throw (RuntimeException) ex;
if (ex instanceof Error error) throw error;
if (ex instanceof RuntimeException re) throw re;
throw new InternalError("uncaught exception", ex);
}
private static String message(String message, Object obj) {

View file

@ -4254,14 +4254,14 @@ return mh1;
}
MemberName resolved = resolveOrFail(refKind, member);
mh = getDirectMethodForConstant(refKind, defc, resolved);
if (mh instanceof DirectMethodHandle
if (mh instanceof DirectMethodHandle dmh
&& canBeCached(refKind, defc, resolved)) {
MemberName key = mh.internalMemberName();
if (key != null) {
key = key.asNormalOriginal();
}
if (member.equals(key)) { // better safe than sorry
LOOKASIDE_TABLE.put(key, (DirectMethodHandle) mh);
LOOKASIDE_TABLE.put(key, dmh);
}
}
return mh;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -893,14 +893,12 @@ class MethodType
if (this == x) {
return true;
}
if (x instanceof MethodType) {
return equals((MethodType)x);
if (x instanceof MethodType mt) {
return equals(mt);
}
if (x instanceof ConcurrentWeakInternSet.WeakEntry) {
Object o = ((ConcurrentWeakInternSet.WeakEntry)x).get();
if (o instanceof MethodType) {
return equals((MethodType)o);
}
if (x instanceof ConcurrentWeakInternSet.WeakEntry<?> e
&& e.get() instanceof MethodType mt) {
return equals(mt);
}
return false;
}
@ -1483,8 +1481,8 @@ s.writeObject(this.parameterArray());
@Override
public boolean equals(Object obj) {
Object mine = get();
if (obj instanceof WeakEntry) {
Object that = ((WeakEntry) obj).get();
if (obj instanceof WeakEntry<?> we) {
Object that = we.get();
return (that == null || mine == null) ? (this == obj) : mine.equals(that);
}
return (mine == null) ? (obj == null) : mine.equals(obj);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -280,8 +280,8 @@ public final class SerializedLambda implements Serializable {
throw new InvalidObjectException("ReflectiveOperationException during deserialization", roe);
} catch (PrivilegedActionException e) {
Exception cause = e.getException();
if (cause instanceof RuntimeException)
throw (RuntimeException) cause;
if (cause instanceof RuntimeException re)
throw re;
else
throw new RuntimeException("Exception in SerializedLambda.readResolve", e);
}

View file

@ -635,8 +635,8 @@ final class VarHandles {
} else {
throw new AssertionError("Cannot get here");
}
} else if (handle instanceof DelegatingMethodHandle) {
return exceptionTypes(((DelegatingMethodHandle)handle).getTarget());
} else if (handle instanceof DelegatingMethodHandle delegatingMh) {
return exceptionTypes(delegatingMh.getTarget());
} else if (handle instanceof NativeMethodHandle) {
return new Class<?>[0];
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -685,8 +685,8 @@ public class AccessibleObject implements AnnotatedElement {
*/
private boolean isAccessChecked(Class<?> caller, Class<?> targetClass) {
Object cache = accessCheckCache; // read volatile
if (cache instanceof Cache) {
return ((Cache) cache).isCacheFor(caller, targetClass);
if (cache instanceof Cache c) {
return c.isCacheFor(caller, targetClass);
}
return false;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -366,9 +366,9 @@ public interface AnnotatedElement {
T[] result = getDeclaredAnnotationsByType(annotationClass);
if (result.length == 0 && // Neither directly nor indirectly present
this instanceof Class && // the element is a class
this instanceof Class<?> cls && // the element is a class
AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
Class<?> superClass = ((Class<?>) this).getSuperclass();
Class<?> superClass = cls.getSuperclass();
if (superClass != null) {
// Determine if the annotation is associated with the
// superclass

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1052,8 +1052,8 @@ public class Proxy implements java.io.Serializable {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
if (t instanceof RuntimeException re) {
throw re;
} else {
throw new InternalError(t.toString(), t);
}