From e5ce7d9e431e54504ddcf321711fe8996ce5ed05 Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Tue, 11 Apr 2023 13:40:56 +0000 Subject: [PATCH] =?UTF-8?q?8284871:=20Use=20covariant=20overrides=20for=20?= =?UTF-8?q?the=20resolveConstantDesc(Lookup)=20method=20in=20sub=E2=80=91i?= =?UTF-8?q?nterfaces=20of=20java.lang.constant.ConstantDesc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: mchung --- .../lang/constant/AsTypeMethodHandleDesc.java | 6 +- .../classes/java/lang/constant/ClassDesc.java | 6 +- .../java/lang/constant/ConstantDesc.java | 8 +- .../constant/DirectMethodHandleDescImpl.java | 6 +- .../lang/constant/DynamicCallSiteDesc.java | 4 +- .../lang/constant/DynamicConstantDesc.java | 4 +- .../java/lang/constant/MethodHandleDesc.java | 5 +- .../java/lang/constant/MethodTypeDesc.java | 13 +++- .../jdk/java/lang/constant/ClassDescTest.java | 22 +++--- .../jdk/java/lang/constant/CondyDescTest.java | 7 +- .../lang/constant/MethodHandleDescTest.java | 76 +++++++++---------- .../lang/constant/MethodTypeDescTest.java | 8 +- .../pkg1/MethodTypeDescriptorAccessTest.java | 6 +- .../methodTypeDesc/ResolveConstantDesc.java | 6 +- .../constant/ReferenceClassDescResolve.java | 6 +- 15 files changed, 100 insertions(+), 83 deletions(-) diff --git a/src/java.base/share/classes/java/lang/constant/AsTypeMethodHandleDesc.java b/src/java.base/share/classes/java/lang/constant/AsTypeMethodHandleDesc.java index b2fe91268d2..32818a9bc00 100644 --- a/src/java.base/share/classes/java/lang/constant/AsTypeMethodHandleDesc.java +++ b/src/java.base/share/classes/java/lang/constant/AsTypeMethodHandleDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -58,8 +58,8 @@ final class AsTypeMethodHandleDesc extends DynamicConstantDesc @Override public MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException { - MethodHandle handle = (MethodHandle) underlying.resolveConstantDesc(lookup); - MethodType methodType = (MethodType) type.resolveConstantDesc(lookup); + MethodHandle handle = underlying.resolveConstantDesc(lookup); + MethodType methodType = type.resolveConstantDesc(lookup); return handle.asType(methodType); } diff --git a/src/java.base/share/classes/java/lang/constant/ClassDesc.java b/src/java.base/share/classes/java/lang/constant/ClassDesc.java index 3f0604b336a..06601c475b3 100644 --- a/src/java.base/share/classes/java/lang/constant/ClassDesc.java +++ b/src/java.base/share/classes/java/lang/constant/ClassDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -24,6 +24,7 @@ */ package java.lang.constant; +import java.lang.invoke.MethodHandles; import java.lang.invoke.TypeDescriptor; import java.util.stream.Stream; @@ -362,6 +363,9 @@ public sealed interface ClassDesc */ String descriptorString(); + @Override + Class resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException; + /** * Compare the specified object with this descriptor for equality. Returns * {@code true} if and only if the specified object is also a diff --git a/src/java.base/share/classes/java/lang/constant/ConstantDesc.java b/src/java.base/share/classes/java/lang/constant/ConstantDesc.java index 2be66b7002c..ee2bc7fce96 100644 --- a/src/java.base/share/classes/java/lang/constant/ConstantDesc.java +++ b/src/java.base/share/classes/java/lang/constant/ConstantDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -99,9 +99,9 @@ public sealed interface ConstantDesc * could not be reflectively resolved in the course of resolution * @throws LinkageError if a linkage error occurs * - * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors - * that are not representable by {@linkplain MethodType}, such as methods with - * more than 255 parameter slots, so attempts to resolve these may result in errors. + * @apiNote Some constant descriptors, such as {@linkplain MethodTypeDesc}, can represent + * a value that is not representable by run-time entities. Attempts to resolve these may + * result in errors. * * @jvms 5.4.3 Resolution * @jvms 5.4.4 Access Control diff --git a/src/java.base/share/classes/java/lang/constant/DirectMethodHandleDescImpl.java b/src/java.base/share/classes/java/lang/constant/DirectMethodHandleDescImpl.java index cb8c8c37e91..77f61d83133 100644 --- a/src/java.base/share/classes/java/lang/constant/DirectMethodHandleDescImpl.java +++ b/src/java.base/share/classes/java/lang/constant/DirectMethodHandleDescImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -152,8 +152,8 @@ final class DirectMethodHandleDescImpl implements DirectMethodHandleDesc { public MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException { - Class resolvedOwner = (Class) owner.resolveConstantDesc(lookup); - MethodType invocationType = (MethodType) this.invocationType().resolveConstantDesc(lookup); + Class resolvedOwner = owner.resolveConstantDesc(lookup); + MethodType invocationType = this.invocationType().resolveConstantDesc(lookup); return switch (kind) { case STATIC, INTERFACE_STATIC -> lookup.findStatic(resolvedOwner, name, invocationType); diff --git a/src/java.base/share/classes/java/lang/constant/DynamicCallSiteDesc.java b/src/java.base/share/classes/java/lang/constant/DynamicCallSiteDesc.java index cf6d09c14b3..41a602ab4b0 100644 --- a/src/java.base/share/classes/java/lang/constant/DynamicCallSiteDesc.java +++ b/src/java.base/share/classes/java/lang/constant/DynamicCallSiteDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -234,7 +234,7 @@ public class DynamicCallSiteDesc { */ public CallSite resolveCallSiteDesc(MethodHandles.Lookup lookup) throws Throwable { assert bootstrapMethod.invocationType().parameterType(1).equals(CD_String); - MethodHandle bsm = (MethodHandle) bootstrapMethod.resolveConstantDesc(lookup); + MethodHandle bsm = bootstrapMethod.resolveConstantDesc(lookup); Object[] args = new Object[bootstrapArgs.length + 3]; args[0] = lookup; args[1] = invocationName; diff --git a/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java b/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java index 82f5bd6cfd6..bf6ae2670b1 100644 --- a/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java +++ b/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -252,7 +252,7 @@ public abstract non-sealed class DynamicConstantDesc @SuppressWarnings("unchecked") public T resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException { try { - MethodHandle bsm = (MethodHandle) bootstrapMethod.resolveConstantDesc(lookup); + MethodHandle bsm = bootstrapMethod.resolveConstantDesc(lookup); if (bsm.type().parameterCount() < 2 || !MethodHandles.Lookup.class.isAssignableFrom(bsm.type().parameterType(0))) { throw new BootstrapMethodError( diff --git a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java index 8c9ee15208e..9e8633d26e0 100644 --- a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java +++ b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -204,6 +204,9 @@ public sealed interface MethodHandleDesc */ MethodTypeDesc invocationType(); + @Override + MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException; + /** * Compares the specified object with this descriptor for equality. Returns * {@code true} if and only if the specified object is also a diff --git a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java index 4750231ce37..175d91bf3f6 100644 --- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java +++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -24,6 +24,7 @@ */ package java.lang.constant; +import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.invoke.TypeDescriptor; import java.util.List; @@ -188,6 +189,16 @@ public sealed interface MethodTypeDesc returnType().displayName()); } + /** + * {@inheritDoc} + * + * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors + * that are not representable by {@linkplain MethodType}, such as methods with + * more than 255 parameter slots, so attempts to resolve these may result in errors. + */ + @Override + MethodType resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException; + /** * Compares the specified object with this descriptor for equality. Returns * {@code true} if and only if the specified object is also a diff --git a/test/jdk/java/lang/constant/ClassDescTest.java b/test/jdk/java/lang/constant/ClassDescTest.java index d5a34568849..4c9493721d9 100644 --- a/test/jdk/java/lang/constant/ClassDescTest.java +++ b/test/jdk/java/lang/constant/ClassDescTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -59,15 +59,15 @@ public class ClassDescTest extends SymbolicDescTest { if (!r.descriptorString().equals("V")) { assertEquals(r, r.arrayType().componentType()); // Commutativity: array -> resolve -> componentType -> toSymbolic - assertEquals(r, ((Class) r.arrayType().resolveConstantDesc(LOOKUP)).getComponentType().describeConstable().orElseThrow()); + assertEquals(r, r.arrayType().resolveConstantDesc(LOOKUP).getComponentType().describeConstable().orElseThrow()); // Commutativity: resolve -> array -> toSymbolic -> component type - assertEquals(r, Array.newInstance(((Class) r.resolveConstantDesc(LOOKUP)), 0).getClass().describeConstable().orElseThrow().componentType()); + assertEquals(r, Array.newInstance(r.resolveConstantDesc(LOOKUP), 0).getClass().describeConstable().orElseThrow().componentType()); } if (r.isArray()) { assertEquals(r, r.componentType().arrayType()); - assertEquals(r, ((Class) r.resolveConstantDesc(LOOKUP)).getComponentType().describeConstable().orElseThrow().arrayType()); - assertEquals(r, Array.newInstance(((Class) r.componentType().resolveConstantDesc(LOOKUP)), 0).getClass().describeConstable().orElseThrow()); + assertEquals(r, r.resolveConstantDesc(LOOKUP).getComponentType().describeConstable().orElseThrow().arrayType()); + assertEquals(r, Array.newInstance(r.componentType().resolveConstantDesc(LOOKUP), 0).getClass().describeConstable().orElseThrow()); } } @@ -88,7 +88,7 @@ public class ClassDescTest extends SymbolicDescTest { && ((f.getModifiers() & Modifier.STATIC) != 0) && ((f.getModifiers() & Modifier.PUBLIC) != 0)) { ClassDesc cr = (ClassDesc) f.get(null); - Class c = (Class)cr.resolveConstantDesc(MethodHandles.lookup()); + Class c = cr.resolveConstantDesc(MethodHandles.lookup()); testClassDesc(cr, c); ++tested; } @@ -106,7 +106,7 @@ public class ClassDescTest extends SymbolicDescTest { for (Primitives p : Primitives.values()) { List descs = List.of(ClassDesc.ofDescriptor(p.descriptor), p.classDesc, - (ClassDesc) p.clazz.describeConstable().orElseThrow()); + p.clazz.describeConstable().orElseThrow()); for (ClassDesc c : descs) { testClassDesc(c, p.clazz); assertTrue(c.isPrimitive()); @@ -115,7 +115,7 @@ public class ClassDescTest extends SymbolicDescTest { descs.forEach(cc -> assertEquals(c, cc)); if (p != Primitives.VOID) { testClassDesc(c.arrayType(), p.arrayClass); - assertEquals(c, ((ClassDesc) p.arrayClass.describeConstable().orElseThrow()).componentType()); + assertEquals(c, p.arrayClass.describeConstable().orElseThrow().componentType()); assertEquals(c, p.classDesc.arrayType().componentType()); } } @@ -228,9 +228,9 @@ public class ClassDescTest extends SymbolicDescTest { assertEquals(a1, ClassDesc.ofDescriptor("[" + d)); assertEquals(a2, ClassDesc.ofDescriptor("[[" + d)); - assertEquals(classToDescriptor((Class) a0.resolveConstantDesc(LOOKUP)), a0.descriptorString()); - assertEquals(classToDescriptor((Class) a1.resolveConstantDesc(LOOKUP)), a1.descriptorString()); - assertEquals(classToDescriptor((Class) a2.resolveConstantDesc(LOOKUP)), a2.descriptorString()); + assertEquals(classToDescriptor(a0.resolveConstantDesc(LOOKUP)), a0.descriptorString()); + assertEquals(classToDescriptor(a1.resolveConstantDesc(LOOKUP)), a1.descriptorString()); + assertEquals(classToDescriptor(a2.resolveConstantDesc(LOOKUP)), a2.descriptorString()); testBadArrayRank(ConstantDescs.CD_int); testBadArrayRank(ConstantDescs.CD_String); diff --git a/test/jdk/java/lang/constant/CondyDescTest.java b/test/jdk/java/lang/constant/CondyDescTest.java index 5c0c4e3996f..08350f7748c 100644 --- a/test/jdk/java/lang/constant/CondyDescTest.java +++ b/test/jdk/java/lang/constant/CondyDescTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -23,7 +23,6 @@ import java.lang.Enum.EnumDesc; import java.lang.constant.MethodTypeDesc; -import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle.VarHandleDesc; @@ -49,7 +48,7 @@ import static org.testng.Assert.assertNull; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; -/** +/* * @test * @compile CondyDescTest.java * @run testng CondyDescTest @@ -99,7 +98,7 @@ public class CondyDescTest extends SymbolicDescTest { DirectMethodHandleDesc format = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, CD_String, "format", MethodTypeDesc.of(CD_String, CD_String, CD_Object.arrayType())); - String s = (String) ((MethodHandle) invoker.resolveConstantDesc(LOOKUP)) + String s = (String) invoker.resolveConstantDesc(LOOKUP) .invoke(LOOKUP, "", String.class, format.resolveConstantDesc(LOOKUP), "%s%s", "moo", "cow"); assertEquals(s, "moocow"); diff --git a/test/jdk/java/lang/constant/MethodHandleDescTest.java b/test/jdk/java/lang/constant/MethodHandleDescTest.java index 92e3084db0b..e8b66daa6b7 100644 --- a/test/jdk/java/lang/constant/MethodHandleDescTest.java +++ b/test/jdk/java/lang/constant/MethodHandleDescTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -90,7 +90,7 @@ public class MethodHandleDescTest extends SymbolicDescTest { DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r; assertEquals(r, MethodHandleDesc.of(rr.kind(), rr.owner(), rr.methodName(), rr.lookupDescriptor())); - assertEquals(r.invocationType().resolveConstantDesc(LOOKUP), ((MethodHandle) r.resolveConstantDesc(LOOKUP)).type()); + assertEquals(r.invocationType().resolveConstantDesc(LOOKUP), r.resolveConstantDesc(LOOKUP).type()); } else { testSymbolicDescForwardOnly(r); @@ -114,7 +114,7 @@ public class MethodHandleDescTest extends SymbolicDescTest { private void testMethodHandleDesc(MethodHandleDesc r, MethodHandle mh) throws ReflectiveOperationException { testMethodHandleDesc(r); - assertMHEquals(((MethodHandle) r.resolveConstantDesc(LOOKUP)), mh); + assertMHEquals(r.resolveConstantDesc(LOOKUP), mh); assertEquals(mh.describeConstable().orElseThrow(), r); // compare extractable properties: refKind, owner, name, type @@ -179,7 +179,7 @@ public class MethodHandleDescTest extends SymbolicDescTest { MethodTypeDesc.of(CD_Integer, CD_int)); MethodHandleDesc takesInteger = mhr.asType(MethodTypeDesc.of(CD_Integer, CD_Integer)); testMethodHandleDesc(takesInteger); - MethodHandle mh1 = (MethodHandle) takesInteger.resolveConstantDesc(LOOKUP); + MethodHandle mh1 = takesInteger.resolveConstantDesc(LOOKUP); assertEquals((Integer) 3, (Integer) mh1.invokeExact((Integer) 3)); assertEquals(takesInteger.toString(), "MethodHandleDesc[STATIC/Integer::valueOf(int)Integer].asType(Integer)Integer"); @@ -191,7 +191,7 @@ public class MethodHandleDescTest extends SymbolicDescTest { MethodHandleDesc takesInt = takesInteger.asType(MethodTypeDesc.of(CD_Integer, CD_int)); testMethodHandleDesc(takesInt); - MethodHandle mh2 = (MethodHandle) takesInt.resolveConstantDesc(LOOKUP); + MethodHandle mh2 = takesInt.resolveConstantDesc(LOOKUP); assertEquals((Integer) 3, (Integer) mh2.invokeExact(3)); try { @@ -241,47 +241,47 @@ public class MethodHandleDescTest extends SymbolicDescTest { for (MethodHandleDesc r : List.of(ctorDesc, staticMethodDesc, staticIMethodDesc, instanceMethodDesc, instanceIMethodDesc)) testMethodHandleDesc(r); - TestHelpers.TestClass instance = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(LOOKUP)).invokeExact(); - TestHelpers.TestClass instance2 = (TestHelpers.TestClass) ((MethodHandle)ctorDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(); + TestHelpers.TestClass instance = (TestHelpers.TestClass) ctorDesc.resolveConstantDesc(LOOKUP).invokeExact(); + TestHelpers.TestClass instance2 = (TestHelpers.TestClass) ctorDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(); TestHelpers.TestInterface instanceI = instance; assertNotSame(instance, instance2); - assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5)); - assertEquals(5, (int) ((MethodHandle)staticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5)); - assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(5)); - assertEquals(0, (int) ((MethodHandle)staticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5)); + assertEquals(5, (int) staticMethodDesc.resolveConstantDesc(LOOKUP).invokeExact(5)); + assertEquals(5, (int) staticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5)); + assertEquals(0, (int) staticIMethodDesc.resolveConstantDesc(LOOKUP).invokeExact(5)); + assertEquals(0, (int) staticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5)); - assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 5)); - assertEquals(5, (int) ((MethodHandle)instanceMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5)); - assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(LOOKUP)).invokeExact(instanceI, 5)); - assertEquals(5, (int) ((MethodHandle)instanceIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instanceI, 5)); + assertEquals(5, (int) instanceMethodDesc.resolveConstantDesc(LOOKUP).invokeExact(instance, 5)); + assertEquals(5, (int) instanceMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5)); + assertEquals(5, (int) instanceIMethodDesc.resolveConstantDesc(LOOKUP).invokeExact(instanceI, 5)); + assertEquals(5, (int) instanceIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instanceI, 5)); try { superMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(-1, (int) ((MethodHandle)superMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5)); + assertEquals(-1, (int) superMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5)); try { superIMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(0, (int) ((MethodHandle)superIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5)); + assertEquals(0, (int) superIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5)); try { privateMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(5, (int) ((MethodHandle)privateMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 5)); + assertEquals(5, (int) privateMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5)); try { privateIMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(instanceI, 5)); - assertEquals(0, (int) ((MethodHandle)privateIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invoke(instanceI, 5)); + assertEquals(0, (int) privateIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP).invokeExact(instanceI, 5)); + assertEquals(0, (int) privateIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invoke(instanceI, 5)); try { privateStaticMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(5, (int) ((MethodHandle)privateStaticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5)); + assertEquals(5, (int) privateStaticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5)); try { privateStaticIMethodDesc.resolveConstantDesc(LOOKUP); fail(); } catch (IllegalAccessException e) { /* expected */ } - assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP)).invokeExact(5)); - assertEquals(0, (int) ((MethodHandle)privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(5)); + assertEquals(0, (int) privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP).invokeExact(5)); + assertEquals(0, (int) privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5)); MethodHandleDesc staticSetterDesc = MethodHandleDesc.ofField(STATIC_SETTER, testClass, "sf", CD_int); MethodHandleDesc staticGetterDesc = MethodHandleDesc.ofField(STATIC_GETTER, testClass, "sf", CD_int); @@ -292,22 +292,22 @@ public class MethodHandleDescTest extends SymbolicDescTest { for (MethodHandleDesc r : List.of(staticSetterDesc, staticGetterDesc, staticGetterIDesc, setterDesc, getterDesc)) testMethodHandleDesc(r); - ((MethodHandle)staticSetterDesc.resolveConstantDesc(LOOKUP)).invokeExact(6); assertEquals(TestHelpers.TestClass.sf, 6); - assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact()); - assertEquals(6, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact()); - ((MethodHandle)staticSetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(7); assertEquals(TestHelpers.TestClass.sf, 7); - assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(LOOKUP)).invokeExact()); - assertEquals(7, (int) ((MethodHandle)staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact()); + staticSetterDesc.resolveConstantDesc(LOOKUP).invokeExact(6); assertEquals(TestHelpers.TestClass.sf, 6); + assertEquals(6, (int) staticGetterDesc.resolveConstantDesc(LOOKUP).invokeExact()); + assertEquals(6, (int) staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact()); + staticSetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(7); assertEquals(TestHelpers.TestClass.sf, 7); + assertEquals(7, (int) staticGetterDesc.resolveConstantDesc(LOOKUP).invokeExact()); + assertEquals(7, (int) staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact()); - assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(LOOKUP)).invokeExact()); - assertEquals(3, (int) ((MethodHandle)staticGetterIDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact()); + assertEquals(3, (int) staticGetterIDesc.resolveConstantDesc(LOOKUP).invokeExact()); + assertEquals(3, (int) staticGetterIDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact()); - ((MethodHandle)setterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance, 6); assertEquals(instance.f, 6); - assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance)); - assertEquals(6, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance)); - ((MethodHandle)setterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance, 7); assertEquals(instance.f, 7); - assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(LOOKUP)).invokeExact(instance)); - assertEquals(7, (int) ((MethodHandle)getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP)).invokeExact(instance)); + setterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance, 6); assertEquals(instance.f, 6); + assertEquals(6, (int) getterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance)); + assertEquals(6, (int) getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance)); + setterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 7); assertEquals(instance.f, 7); + assertEquals(7, (int) getterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance)); + assertEquals(7, (int) getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance)); } private void assertBadArgs(Supplier supplier, String s) { @@ -346,7 +346,7 @@ public class MethodHandleDescTest extends SymbolicDescTest { && ((f.getModifiers() & Modifier.STATIC) != 0) && ((f.getModifiers() & Modifier.PUBLIC) != 0)) { MethodHandleDesc r = (MethodHandleDesc) f.get(null); - MethodHandle m = (MethodHandle)r.resolveConstantDesc(MethodHandles.lookup()); + MethodHandle m = r.resolveConstantDesc(MethodHandles.lookup()); testMethodHandleDesc(r, m); ++tested; } diff --git a/test/jdk/java/lang/constant/MethodTypeDescTest.java b/test/jdk/java/lang/constant/MethodTypeDescTest.java index ee765a95bef..9619b831d3c 100644 --- a/test/jdk/java/lang/constant/MethodTypeDescTest.java +++ b/test/jdk/java/lang/constant/MethodTypeDescTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -100,7 +100,7 @@ public class MethodTypeDescTest extends SymbolicDescTest { ClassDesc rc = ClassDesc.ofDescriptor(r); MethodTypeDesc newDesc = mtDesc.changeReturnType(rc); assertEquals(newDesc, MethodTypeDesc.of(rc, paramTypes)); - testMethodTypeDesc(newDesc, mt.changeReturnType((Class)rc.resolveConstantDesc(LOOKUP))); + testMethodTypeDesc(newDesc, mt.changeReturnType(rc.resolveConstantDesc(LOOKUP))); } // try with null parameter @@ -119,7 +119,7 @@ public class MethodTypeDescTest extends SymbolicDescTest { ps[i] = pc; MethodTypeDesc newDesc = mtDesc.changeParameterType(i, pc); assertEquals(newDesc, MethodTypeDesc.of(returnType, ps)); - testMethodTypeDesc(newDesc, mt.changeParameterType(i, (Class)pc.resolveConstantDesc(LOOKUP))); + testMethodTypeDesc(newDesc, mt.changeParameterType(i, pc.resolveConstantDesc(LOOKUP))); } } @@ -146,7 +146,7 @@ public class MethodTypeDescTest extends SymbolicDescTest { .toArray(ClassDesc[]::new); MethodTypeDesc newDesc = mtDesc.insertParameterTypes(i, p); assertEquals(newDesc, MethodTypeDesc.of(returnType, ps)); - testMethodTypeDesc(newDesc, mt.insertParameterTypes(i, (Class)p.resolveConstantDesc(LOOKUP))); + testMethodTypeDesc(newDesc, mt.insertParameterTypes(i, p.resolveConstantDesc(LOOKUP))); } } diff --git a/test/jdk/java/lang/constant/access_test/pkg1/MethodTypeDescriptorAccessTest.java b/test/jdk/java/lang/constant/access_test/pkg1/MethodTypeDescriptorAccessTest.java index b3bc1905f63..3d252474b19 100644 --- a/test/jdk/java/lang/constant/access_test/pkg1/MethodTypeDescriptorAccessTest.java +++ b/test/jdk/java/lang/constant/access_test/pkg1/MethodTypeDescriptorAccessTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 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 @@ -66,7 +66,7 @@ public class MethodTypeDescriptorAccessTest { private void checkValidAccess(MethodTypeDesc mtd, Lookup lookup) { try { - MethodType mt = (MethodType)mtd.resolveConstantDesc(lookup); + MethodType mt = mtd.resolveConstantDesc(lookup); } catch (ReflectiveOperationException unexpected) { throw new Error("resolveConstantDesc() threw ReflectiveOperationException unexpectedly with cause " + unexpected.getCause() + " for " + mtd); @@ -75,7 +75,7 @@ public class MethodTypeDescriptorAccessTest { private void checkInvalidAccess(MethodTypeDesc mtd, Lookup lookup) { try { - MethodType mt = (MethodType)mtd.resolveConstantDesc(lookup); + MethodType mt = mtd.resolveConstantDesc(lookup); throw new Error("resolveConstantDesc() succeeded unexpectedly " + mtd); } catch (ReflectiveOperationException expected) { if (expected.getClass() != IllegalAccessException.class) { diff --git a/test/jdk/java/lang/constant/methodTypeDesc/ResolveConstantDesc.java b/test/jdk/java/lang/constant/methodTypeDesc/ResolveConstantDesc.java index a4083f28f1c..e02ba995c16 100644 --- a/test/jdk/java/lang/constant/methodTypeDesc/ResolveConstantDesc.java +++ b/test/jdk/java/lang/constant/methodTypeDesc/ResolveConstantDesc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 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 @@ -87,14 +87,14 @@ public class ResolveConstantDesc { // IAE thrown when resolving MethodType using the given Lookup object private static void throwIAE(Lookup lookup, MethodTypeDesc mtd) throws Exception { try { - MethodType mtype = (MethodType)mtd.resolveConstantDesc(lookup); + MethodType mtype = mtd.resolveConstantDesc(lookup); throw new RuntimeException("unexpected IAE not thrown"); } catch (IllegalAccessException e) { } } private static void throwACC(Lookup lookup, MethodTypeDesc mtd) throws Exception { try { - MethodType mtype = (MethodType)mtd.resolveConstantDesc(lookup); + MethodType mtype = mtd.resolveConstantDesc(lookup); throw new RuntimeException("unexpected IAE not thrown"); } catch (AccessControlException e) { Permission perm = e.getPermission(); diff --git a/test/micro/org/openjdk/bench/java/lang/constant/ReferenceClassDescResolve.java b/test/micro/org/openjdk/bench/java/lang/constant/ReferenceClassDescResolve.java index 29351dd9d8e..6d6c49fbb0b 100644 --- a/test/micro/org/openjdk/bench/java/lang/constant/ReferenceClassDescResolve.java +++ b/test/micro/org/openjdk/bench/java/lang/constant/ReferenceClassDescResolve.java @@ -56,16 +56,16 @@ public class ReferenceClassDescResolve { @Benchmark public Class resolveClassOrInterface() throws ReflectiveOperationException { - return (Class) CLASS_OR_INTERFACE.resolveConstantDesc(LOOKUP); + return CLASS_OR_INTERFACE.resolveConstantDesc(LOOKUP); } @Benchmark public Class resolveReferenceArray() throws ReflectiveOperationException { - return (Class) REFERENCE_ARRAY.resolveConstantDesc(LOOKUP); + return REFERENCE_ARRAY.resolveConstantDesc(LOOKUP); } @Benchmark public Class resolvePrimitiveArray() throws ReflectiveOperationException { - return (Class) PRIMITIVE_ARRAY.resolveConstantDesc(LOOKUP); + return PRIMITIVE_ARRAY.resolveConstantDesc(LOOKUP); } }