mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
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:
parent
7ce967a10c
commit
2bd4136bdb
34 changed files with 211 additions and 292 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 2015, 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
|
||||
|
@ -53,7 +53,7 @@ public class BytecodeDescriptor {
|
|||
int start, int end, ClassLoader loader) {
|
||||
String str = bytecodeSignature;
|
||||
int[] i = {start};
|
||||
ArrayList<Class<?>> ptypes = new ArrayList<Class<?>>();
|
||||
var ptypes = new ArrayList<Class<?>>();
|
||||
if (i[0] < end && str.charAt(i[0]) == '(') {
|
||||
++i[0]; // skip '('
|
||||
while (i[0] < end && str.charAt(i[0]) != ')') {
|
||||
|
@ -97,7 +97,7 @@ public class BytecodeDescriptor {
|
|||
} else if (c == '[') {
|
||||
Class<?> t = parseSig(str, i, end, loader);
|
||||
if (t != null)
|
||||
t = java.lang.reflect.Array.newInstance(t, 0).getClass();
|
||||
t = t.arrayType();
|
||||
return t;
|
||||
} else {
|
||||
return Wrapper.forBasicType(c).primitiveType();
|
||||
|
@ -114,10 +114,10 @@ public class BytecodeDescriptor {
|
|||
}
|
||||
|
||||
public static String unparse(Object type) {
|
||||
if (type instanceof Class<?>)
|
||||
return unparse((Class<?>) type);
|
||||
if (type instanceof MethodType)
|
||||
return ((MethodType) type).toMethodDescriptorString();
|
||||
if (type instanceof Class<?> cl)
|
||||
return unparse(cl);
|
||||
if (type instanceof MethodType mt)
|
||||
return mt.toMethodDescriptorString();
|
||||
return (String) type;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2007, 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
|
||||
|
@ -354,8 +354,8 @@ public class BytecodeName {
|
|||
Object[] components0 = components;
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
Object c = components[i];
|
||||
if (c instanceof String) {
|
||||
String mc = toBytecodeName((String) c);
|
||||
if (c instanceof String s) {
|
||||
String mc = toBytecodeName(s);
|
||||
if (i == 0 && components.length == 1)
|
||||
return mc; // usual case
|
||||
if ((Object)mc != c) {
|
||||
|
@ -376,8 +376,8 @@ public class BytecodeName {
|
|||
}
|
||||
int slen = 0;
|
||||
for (Object c : components) {
|
||||
if (c instanceof String)
|
||||
slen += String.valueOf(c).length();
|
||||
if (c instanceof String s)
|
||||
slen += s.length();
|
||||
else
|
||||
slen += 1;
|
||||
}
|
||||
|
@ -408,9 +408,8 @@ public class BytecodeName {
|
|||
public static String toDisplayName(String s) {
|
||||
Object[] components = parseBytecodeName(s);
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
if (!(components[i] instanceof String))
|
||||
if (!(components[i] instanceof String sn))
|
||||
continue;
|
||||
String sn = (String) components[i];
|
||||
// note that the name is already demangled!
|
||||
//sn = toSourceName(sn);
|
||||
if (!isJavaIdent(sn) || sn.indexOf('$') >=0 ) {
|
||||
|
|
|
@ -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
|
||||
|
@ -76,8 +76,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static int unboxInteger(Object x, boolean cast) {
|
||||
if (x instanceof Integer)
|
||||
return (Integer) x;
|
||||
if (x instanceof Integer i)
|
||||
return i;
|
||||
return primitiveConversion(Wrapper.INT, x, cast).intValue();
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static byte unboxByte(Object x, boolean cast) {
|
||||
if (x instanceof Byte)
|
||||
return (Byte) x;
|
||||
if (x instanceof Byte b)
|
||||
return b;
|
||||
return primitiveConversion(Wrapper.BYTE, x, cast).byteValue();
|
||||
}
|
||||
|
||||
|
@ -94,8 +94,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static short unboxShort(Object x, boolean cast) {
|
||||
if (x instanceof Short)
|
||||
return (Short) x;
|
||||
if (x instanceof Short s)
|
||||
return s;
|
||||
return primitiveConversion(Wrapper.SHORT, x, cast).shortValue();
|
||||
}
|
||||
|
||||
|
@ -103,8 +103,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static boolean unboxBoolean(Object x, boolean cast) {
|
||||
if (x instanceof Boolean)
|
||||
return (Boolean) x;
|
||||
if (x instanceof Boolean b)
|
||||
return b;
|
||||
return (primitiveConversion(Wrapper.BOOLEAN, x, cast).intValue() & 1) != 0;
|
||||
}
|
||||
|
||||
|
@ -112,8 +112,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static char unboxCharacter(Object x, boolean cast) {
|
||||
if (x instanceof Character)
|
||||
return (Character) x;
|
||||
if (x instanceof Character c)
|
||||
return c;
|
||||
return (char) primitiveConversion(Wrapper.CHAR, x, cast).intValue();
|
||||
}
|
||||
|
||||
|
@ -121,8 +121,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static long unboxLong(Object x, boolean cast) {
|
||||
if (x instanceof Long)
|
||||
return (Long) x;
|
||||
if (x instanceof Long l)
|
||||
return l;
|
||||
return primitiveConversion(Wrapper.LONG, x, cast).longValue();
|
||||
}
|
||||
|
||||
|
@ -130,8 +130,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static float unboxFloat(Object x, boolean cast) {
|
||||
if (x instanceof Float)
|
||||
return (Float) x;
|
||||
if (x instanceof Float f)
|
||||
return f;
|
||||
return primitiveConversion(Wrapper.FLOAT, x, cast).floatValue();
|
||||
}
|
||||
|
||||
|
@ -139,8 +139,8 @@ public class ValueConversions {
|
|||
return x;
|
||||
}
|
||||
static double unboxDouble(Object x, boolean cast) {
|
||||
if (x instanceof Double)
|
||||
return (Double) x;
|
||||
if (x instanceof Double d)
|
||||
return d;
|
||||
return primitiveConversion(Wrapper.DOUBLE, x, cast).doubleValue();
|
||||
}
|
||||
|
||||
|
@ -236,12 +236,12 @@ public class ValueConversions {
|
|||
if (!cast) return null;
|
||||
return ZERO_INT;
|
||||
}
|
||||
if (x instanceof Number) {
|
||||
res = (Number) x;
|
||||
} else if (x instanceof Boolean) {
|
||||
res = ((boolean)x ? ONE_INT : ZERO_INT);
|
||||
} else if (x instanceof Character) {
|
||||
res = (int)(char)x;
|
||||
if (x instanceof Number n) {
|
||||
res = n;
|
||||
} else if (x instanceof Boolean b) {
|
||||
res = b ? ONE_INT : ZERO_INT;
|
||||
} else if (x instanceof Character c) {
|
||||
res = (int) c;
|
||||
} else {
|
||||
// this will fail with the required ClassCastException:
|
||||
res = (Number) x;
|
||||
|
@ -259,16 +259,16 @@ public class ValueConversions {
|
|||
* Byte, Short, Character, or Integer.
|
||||
*/
|
||||
public static int widenSubword(Object x) {
|
||||
if (x instanceof Integer)
|
||||
return (int) x;
|
||||
else if (x instanceof Boolean)
|
||||
return fromBoolean((boolean) x);
|
||||
else if (x instanceof Character)
|
||||
return (char) x;
|
||||
else if (x instanceof Short)
|
||||
return (short) x;
|
||||
else if (x instanceof Byte)
|
||||
return (byte) x;
|
||||
if (x instanceof Integer i)
|
||||
return i;
|
||||
else if (x instanceof Boolean b)
|
||||
return fromBoolean(b);
|
||||
else if (x instanceof Character c)
|
||||
return c;
|
||||
else if (x instanceof Short s)
|
||||
return s;
|
||||
else if (x instanceof Byte b)
|
||||
return b;
|
||||
else
|
||||
// Fail with a ClassCastException.
|
||||
return (int) x;
|
||||
|
|
|
@ -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
|
||||
|
@ -580,9 +580,9 @@ public enum Wrapper {
|
|||
}
|
||||
|
||||
private static Number numberValue(Object x) {
|
||||
if (x instanceof Number) return (Number)x;
|
||||
if (x instanceof Character) return (int)(Character)x;
|
||||
if (x instanceof Boolean) return (Boolean)x ? 1 : 0;
|
||||
if (x instanceof Number n) return n;
|
||||
if (x instanceof Character c) return (int) c;
|
||||
if (x instanceof Boolean b) return b ? 1 : 0;
|
||||
// Remaining allowed case of void: Must be a null reference.
|
||||
return (Number)x;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue