No testng in constant

This commit is contained in:
Chen Liang 2026-01-23 14:29:19 -06:00
parent e08fb3a914
commit 72e0b3a9c9
13 changed files with 329 additions and 536 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -31,23 +31,17 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @bug 8215510 8283075 8338544
* @compile ClassDescTest.java
* @run testng ClassDescTest
* @run junit ClassDescTest
* @summary unit tests for java.lang.constant.ClassDesc
*/
@Test
public class ClassDescTest extends SymbolicDescTest {
private void testClassDesc(ClassDesc r) throws ReflectiveOperationException {
@ -73,7 +67,7 @@ public class ClassDescTest extends SymbolicDescTest {
}
if (!r.isClassOrInterface()) {
assertEquals(r.packageName(), "");
assertEquals("", r.packageName());
}
}
@ -97,18 +91,19 @@ public class ClassDescTest extends SymbolicDescTest {
private void testClassDesc(ClassDesc r, Class<?> c) throws ReflectiveOperationException {
testClassDesc(r);
assertEquals(r.resolveConstantDesc(LOOKUP), c);
assertEquals(c.describeConstable().orElseThrow(), r);
assertEquals(ClassDesc.ofDescriptor(c.descriptorString()), r);
assertEquals(c, r.resolveConstantDesc(LOOKUP));
assertEquals(r, c.describeConstable().orElseThrow());
assertEquals(r, ClassDesc.ofDescriptor(c.descriptorString()));
if (r.isArray()) {
testClassDesc(r.componentType(), c.componentType());
}
if (r.isClassOrInterface()) {
assertEquals(r.packageName(), c.getPackageName());
assertEquals(c.getPackageName(), r.packageName());
}
assertEquals(r.displayName(), classDisplayName(c));
assertEquals(classDisplayName(c), r.displayName());
}
@Test
public void testSymbolicDescsConstants() throws ReflectiveOperationException {
int tested = 0;
Field[] fields = ConstantDescs.class.getDeclaredFields();
@ -132,6 +127,7 @@ public class ClassDescTest extends SymbolicDescTest {
assertTrue(tested > 0);
}
@Test
public void testPrimitiveClassDesc() throws ReflectiveOperationException {
for (Primitives p : Primitives.values()) {
List<ClassDesc> descs = List.of(ClassDesc.ofDescriptor(p.descriptor),
@ -140,26 +136,27 @@ public class ClassDescTest extends SymbolicDescTest {
for (ClassDesc c : descs) {
testClassDesc(c, p.clazz);
assertTrue(c.isPrimitive());
assertEquals(p.descriptor, c.descriptorString());
assertEquals(p.name, c.displayName());
descs.forEach(cc -> assertEquals(c, cc));
assertEquals(c.descriptorString(), p.descriptor);
assertEquals(c.displayName(), p.name);
descs.forEach(cc -> assertEquals(cc, c));
if (p != Primitives.VOID) {
testClassDesc(c.arrayType(), p.arrayClass);
assertEquals(c, p.arrayClass.describeConstable().orElseThrow().componentType());
assertEquals(c, p.classDesc.arrayType().componentType());
assertEquals(p.arrayClass.describeConstable().orElseThrow().componentType(), c);
assertEquals(p.classDesc.arrayType().componentType(), c);
}
}
for (Primitives other : Primitives.values()) {
ClassDesc otherDescr = ClassDesc.ofDescriptor(other.descriptor);
if (p != other)
descs.forEach(c -> assertNotEquals(c, otherDescr));
descs.forEach(c -> assertNotEquals(otherDescr, c));
else
descs.forEach(c -> assertEquals(c, otherDescr));
descs.forEach(c -> assertEquals(otherDescr, c));
}
}
}
@Test
public void testSimpleClassDesc() throws ReflectiveOperationException {
List<ClassDesc> stringClassDescs = Arrays.asList(ClassDesc.ofDescriptor("Ljava/lang/String;"),
@ -175,22 +172,23 @@ public class ClassDescTest extends SymbolicDescTest {
assertEquals("String", r.displayName());
testClassDesc(r.arrayType(), String[].class);
testClassDesc(r.arrayType(3), String[][][].class);
stringClassDescs.forEach(rr -> assertEquals(r, rr));
stringClassDescs.forEach(rr -> assertEquals(rr, r));
}
testClassDesc(ClassDesc.of("java.lang.String").arrayType(), String[].class);
testClassDesc(ClassDesc.of("java.util.Map").nested("Entry"), Map.Entry.class);
assertEquals(ClassDesc.of("java.lang.String"), ClassDesc.ofDescriptor("Ljava/lang/String;"));
assertEquals(ClassDesc.of("java.lang.String"), ClassDesc.ofInternalName("java/lang/String"));
assertEquals(ClassDesc.ofDescriptor("Ljava/lang/String;"), ClassDesc.of("java.lang.String"));
assertEquals(ClassDesc.ofInternalName("java/lang/String"), ClassDesc.of("java.lang.String"));
ClassDesc thisClassDesc = ClassDesc.ofDescriptor("LClassDescTest;");
assertEquals(thisClassDesc, ClassDesc.of("", "ClassDescTest"));
assertEquals(thisClassDesc, ClassDesc.of("ClassDescTest"));
assertEquals(thisClassDesc.displayName(), "ClassDescTest");
assertEquals(ClassDesc.of("", "ClassDescTest"), thisClassDesc);
assertEquals(ClassDesc.of("ClassDescTest"), thisClassDesc);
assertEquals("ClassDescTest", thisClassDesc.displayName());
testClassDesc(thisClassDesc, ClassDescTest.class);
}
@Test
public void testPackageName() {
assertEquals("com.foo", ClassDesc.of("com.foo.Bar").packageName());
assertEquals("com.foo", ClassDesc.of("com.foo.Bar").nested("Baz").packageName());
@ -205,33 +203,19 @@ public class ClassDescTest extends SymbolicDescTest {
}
private void testBadArrayRank(ClassDesc cr) {
try {
cr.arrayType(-1);
fail("");
} catch (IllegalArgumentException e) {
// good
}
try {
cr.arrayType(0);
fail("");
} catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalArgumentException.class, () -> cr.arrayType(-1));
assertThrows(IllegalArgumentException.class, () -> cr.arrayType(0));
}
private void testArrayRankOverflow() {
ClassDesc TwoDArrayDesc =
String.class.describeConstable().get().arrayType().arrayType();
try {
TwoDArrayDesc.arrayType(Integer.MAX_VALUE);
fail("");
} catch (IllegalArgumentException iae) {
// Expected
}
assertThrows(IllegalArgumentException.class, () -> TwoDArrayDesc.arrayType(Integer.MAX_VALUE));
}
@Test
public void testArrayClassDesc() throws ReflectiveOperationException {
for (String d : basicDescs) {
ClassDesc a0 = ClassDesc.ofDescriptor(d);
@ -246,36 +230,32 @@ public class ClassDescTest extends SymbolicDescTest {
assertTrue(a2.isArray());
assertFalse(a1.isPrimitive());
assertFalse(a2.isPrimitive());
assertEquals(a0.descriptorString(), d);
assertEquals(a1.descriptorString(), "[" + a0.descriptorString());
assertEquals(a2.descriptorString(), "[[" + a0.descriptorString());
assertEquals(d, a0.descriptorString());
assertEquals("[" + a0.descriptorString(), a1.descriptorString());
assertEquals("[[" + a0.descriptorString(), a2.descriptorString());
assertNull(a0.componentType());
assertEquals(a0, a1.componentType());
assertEquals(a1, a2.componentType());
assertEquals(a1.componentType(), a0);
assertEquals(a2.componentType(), a1);
assertNotEquals(a0, a1);
assertNotEquals(a1, a2);
assertNotEquals(a1, a0);
assertNotEquals(a2, a1);
assertEquals(a1, ClassDesc.ofDescriptor("[" + d));
assertEquals(a2, ClassDesc.ofDescriptor("[[" + d));
assertEquals(classToDescriptor(a0.resolveConstantDesc(LOOKUP)), a0.descriptorString());
assertEquals(classToDescriptor(a1.resolveConstantDesc(LOOKUP)), a1.descriptorString());
assertEquals(classToDescriptor(a2.resolveConstantDesc(LOOKUP)), a2.descriptorString());
assertEquals(ClassDesc.ofDescriptor("[" + d), a1);
assertEquals(ClassDesc.ofDescriptor("[[" + d), a2);
assertEquals(a0.descriptorString(), classToDescriptor(a0.resolveConstantDesc(LOOKUP)));
assertEquals(a1.descriptorString(), classToDescriptor(a1.resolveConstantDesc(LOOKUP)));
assertEquals(a2.descriptorString(), classToDescriptor(a2.resolveConstantDesc(LOOKUP)));
testBadArrayRank(ConstantDescs.CD_int);
testBadArrayRank(ConstantDescs.CD_String);
testBadArrayRank(ClassDesc.of("Bar"));
testArrayRankOverflow();
}
try {
ConstantDescs.CD_void.arrayType();
fail("Should throw IAE");
} catch (IllegalArgumentException iae) {
// Expected
}
assertThrows(IllegalArgumentException.class, () -> ConstantDescs.CD_void.arrayType());
}
@Test
public void testBadClassDescs() {
List<String> badDescriptors = List.of("II", "I;", "Q", "L", "",
"java.lang.String", "[]", "Ljava/lang/String",
@ -283,35 +263,19 @@ public class ClassDescTest extends SymbolicDescTest {
"La//b;", "L/a;", "La/;");
for (String d : badDescriptors) {
try {
ClassDesc constant = ClassDesc.ofDescriptor(d);
fail(d);
}
catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalArgumentException.class, () -> ClassDesc.ofDescriptor(d), d);
}
List<String> badBinaryNames = List.of("I;", "[]", "Ljava/lang/String",
"Ljava.lang.String;", "java/lang/String", "");
for (String d : badBinaryNames) {
try {
ClassDesc constant = ClassDesc.of(d);
fail(d);
} catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalArgumentException.class, () -> ClassDesc.of(d), d);
}
List<String> badInternalNames = List.of("I;", "[]", "[Ljava/lang/String;",
"Ljava.lang.String;", "java.lang.String", "");
for (String d : badInternalNames) {
try {
ClassDesc constant = ClassDesc.ofInternalName(d);
fail(d);
} catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalArgumentException.class, () -> ClassDesc.ofInternalName(d), d);
}
for (Primitives p : Primitives.values()) {
@ -321,80 +285,47 @@ public class ClassDescTest extends SymbolicDescTest {
ClassDesc stringDesc = ClassDesc.ofDescriptor("Ljava/lang/String;");
ClassDesc stringArrDesc = stringDesc.arrayType(255);
try {
ClassDesc arrGreaterThan255 = stringArrDesc.arrayType();
fail("can't create an array type descriptor with more than 255 dimensions");
} catch (IllegalStateException e) {
// good
}
String descWith255ArrayDims = new String(new char[255]).replace('\0', '[');
try {
ClassDesc arrGreaterThan255 = ClassDesc.ofDescriptor(descWith255ArrayDims + "[Ljava/lang/String;");
fail("can't create an array type descriptor with more than 255 dimensions");
} catch (IllegalArgumentException e) {
// good
}
try {
ClassDesc arrWith255Dims = ClassDesc.ofDescriptor(descWith255ArrayDims + "Ljava/lang/String;");
arrWith255Dims.arrayType(1);
fail("can't create an array type descriptor with more than 255 dimensions");
} catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalStateException.class, () -> stringArrDesc.arrayType(),
"can't create an array type descriptor with more than 255 dimensions");
String descWith255ArrayDims = "[".repeat(255);
assertThrows(IllegalArgumentException.class, () -> ClassDesc.ofDescriptor(descWith255ArrayDims + "[Ljava/lang/String;"),
"can't create an array type descriptor with more than 255 dimensions");
assertThrows(IllegalArgumentException.class, () -> ClassDesc.ofDescriptor(descWith255ArrayDims + "Ljava/lang/String;"),
"can't create an array type descriptor with more than 255 dimensions");
}
private void testBadNestedClasses(ClassDesc cr, String firstNestedName, String... moreNestedNames) {
try {
cr.nested(firstNestedName, moreNestedNames);
fail("");
} catch (IllegalStateException e) {
// good
}
assertThrows(IllegalStateException.class, () -> cr.nested(firstNestedName, moreNestedNames));
}
@Test
public void testLangClasses() {
Double d = 1.0;
assertEquals(d.resolveConstantDesc(LOOKUP), d);
assertEquals(d.describeConstable().get(), d);
assertEquals(d, d.resolveConstantDesc(LOOKUP));
assertEquals(d, d.describeConstable().get());
Integer i = 1;
assertEquals(i.resolveConstantDesc(LOOKUP), i);
assertEquals(i.describeConstable().get(), i);
assertEquals(i, i.resolveConstantDesc(LOOKUP));
assertEquals(i, i.describeConstable().get());
Float f = 1.0f;
assertEquals(f.resolveConstantDesc(LOOKUP), f);
assertEquals(f.describeConstable().get(), f);
assertEquals(f, f.resolveConstantDesc(LOOKUP));
assertEquals(f, f.describeConstable().get());
Long l = 1L;
assertEquals(l.resolveConstantDesc(LOOKUP), l);
assertEquals(l.describeConstable().get(), l);
assertEquals(l, l.resolveConstantDesc(LOOKUP));
assertEquals(l, l.describeConstable().get());
String s = "";
assertEquals(s.resolveConstantDesc(LOOKUP), s);
assertEquals(s.describeConstable().get(), s);
assertEquals(s, s.resolveConstantDesc(LOOKUP));
assertEquals(s, s.describeConstable().get());
}
@Test
public void testNullNestedClasses() {
ClassDesc cd = ClassDesc.of("Bar");
try {
cd.nested(null);
fail("");
} catch (NullPointerException e) {
// good
}
try {
cd.nested("good", null);
fail("");
} catch (NullPointerException e) {
// good
}
try {
cd.nested("good", "goodToo", null);
fail("");
} catch (NullPointerException e) {
// good
}
assertThrows(NullPointerException.class, () -> cd.nested(null));
assertThrows(NullPointerException.class, () -> cd.nested("good", null));
assertThrows(NullPointerException.class, () -> cd.nested("good", "goodToo", null));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -33,52 +33,50 @@ import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodHandleDesc;
import org.testng.annotations.Test;
import static java.lang.constant.ConstantDescs.CD_MethodHandle;
import static java.lang.constant.ConstantDescs.CD_Object;
import static java.lang.constant.ConstantDescs.CD_String;
import static java.lang.constant.ConstantDescs.CD_VarHandle;
import static java.lang.constant.ConstantDescs.CD_int;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/*
* @test
* @compile CondyDescTest.java
* @run testng CondyDescTest
* @run junit CondyDescTest
* @summary unit tests for java.lang.constant.CondyDescTest
*/
@Test
public class CondyDescTest extends SymbolicDescTest {
private final static ConstantDesc[] EMPTY_ARGS = new ConstantDesc[0];
private final static ClassDesc CD_ConstantBootstraps = ClassDesc.of("java.lang.invoke.ConstantBootstraps");
private static<T> void testDCR(DynamicConstantDesc<T> r, T c) throws ReflectiveOperationException {
assertEquals(r, DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()));
assertEquals(r.resolveConstantDesc(LOOKUP), c);
assertEquals(DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()), r);
assertEquals(c, r.resolveConstantDesc(LOOKUP));
}
private void testVarHandleDesc(DynamicConstantDesc<VarHandle> r, VarHandle vh) throws ReflectiveOperationException {
testSymbolicDesc(r);
assertEquals(vh.describeConstable().orElseThrow(), r);
assertEquals(r, vh.describeConstable().orElseThrow());
}
private static<E extends Enum<E>> void testEnumDesc(EnumDesc<E> r, E e) throws ReflectiveOperationException {
testSymbolicDesc(r);
assertEquals(r, EnumDesc.of(r.constantType(), r.constantName()));
assertEquals(r.resolveConstantDesc(LOOKUP), e);
assertEquals(EnumDesc.of(r.constantType(), r.constantName()), r);
assertEquals(e, r.resolveConstantDesc(LOOKUP));
}
@Test
public void testNullConstant() throws ReflectiveOperationException {
DynamicConstantDesc<?> r = (DynamicConstantDesc<?>) ConstantDescs.NULL;
assertEquals(r, DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()));
assertEquals(DynamicConstantDesc.ofNamed(r.bootstrapMethod(), r.constantName(), r.constantType(), r.bootstrapArgs()), r);
assertNull(r.resolveConstantDesc(LOOKUP));
}
@ -86,6 +84,7 @@ public class CondyDescTest extends SymbolicDescTest {
return a + b;
}
@Test
public void testDynamicConstant() throws ReflectiveOperationException {
DirectMethodHandleDesc bsmDesc = ConstantDescs.ofConstantBootstrap(ClassDesc.of("CondyDescTest"), "concatBSM",
CD_String, CD_String, CD_String);
@ -93,6 +92,7 @@ public class CondyDescTest extends SymbolicDescTest {
testDCR(r, "foobar");
}
@Test
public void testNested() throws Throwable {
DirectMethodHandleDesc invoker = ConstantDescs.ofConstantBootstrap(CD_ConstantBootstraps, "invoke", CD_Object, CD_MethodHandle, CD_Object.arrayType());
DirectMethodHandleDesc format = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, CD_String, "format",
@ -101,7 +101,7 @@ public class CondyDescTest extends SymbolicDescTest {
String s = (String) invoker.resolveConstantDesc(LOOKUP)
.invoke(LOOKUP, "", String.class,
format.resolveConstantDesc(LOOKUP), "%s%s", "moo", "cow");
assertEquals(s, "moocow");
assertEquals("moocow", s);
DynamicConstantDesc<String> desc = DynamicConstantDesc.of(invoker, format, "%s%s", "moo", "cow");
testDCR(desc, "moocow");
@ -112,6 +112,7 @@ public class CondyDescTest extends SymbolicDescTest {
enum MyEnum { A, B, C }
@Test
public void testEnumDesc() throws ReflectiveOperationException {
ClassDesc enumClass = ClassDesc.of("CondyDescTest").nested("MyEnum");
@ -131,6 +132,7 @@ public class CondyDescTest extends SymbolicDescTest {
int f;
}
@Test
public void testVarHandles() throws ReflectiveOperationException {
ClassDesc testClass = ClassDesc.of("CondyDescTest").nested("MyClass");
MyClass instance = new MyClass();
@ -140,20 +142,20 @@ public class CondyDescTest extends SymbolicDescTest {
VarHandle varHandle = LOOKUP.findStaticVarHandle(MyClass.class, "sf", int.class);
testVarHandleDesc(vhc, varHandle);
assertEquals(varHandle.varType(), int.class);
assertEquals(int.class, varHandle.varType());
varHandle.set(8);
assertEquals(8, (int) varHandle.get());
assertEquals(MyClass.sf, 8);
assertEquals(8, MyClass.sf);
// static varHandle
vhc = VarHandleDesc.ofField(testClass, "f", CD_int);
varHandle = LOOKUP.findVarHandle(MyClass.class, "f", int.class);
testVarHandleDesc(vhc, varHandle);
assertEquals(varHandle.varType(), int.class);
assertEquals(int.class, varHandle.varType());
varHandle.set(instance, 9);
assertEquals(9, (int) varHandle.get(instance));
assertEquals(instance.f, 9);
assertEquals(9, instance.f);
vhc = VarHandleDesc.ofArray(CD_int.arrayType());
varHandle = MethodHandles.arrayElementVarHandle(int[].class);
@ -205,16 +207,17 @@ public class CondyDescTest extends SymbolicDescTest {
assertNotSame(canonical, nonCanonical);
assertTrue(clazz.isAssignableFrom(canonical.getClass()));
assertFalse(clazz.isAssignableFrom(nonCanonical.getClass()));
assertEquals(prototype, canonical);
assertEquals(canonical, prototype);
assertEquals(prototype, canonical);
if (prototype instanceof DynamicConstantDesc) {
assertEquals(canonical, nonCanonical);
assertEquals(nonCanonical, canonical);
assertEquals(prototype, nonCanonical);
assertEquals(canonical, nonCanonical);
assertEquals(nonCanonical, prototype);
assertEquals(prototype, nonCanonical);
}
}
@Test
public void testLifting() {
DynamicConstantDesc<Object> unliftedNull = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_NULL_CONSTANT, "_", CD_Object, EMPTY_ARGS);
assertEquals(ConstantDescs.NULL, unliftedNull);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2026, 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
@ -21,9 +21,6 @@
* questions.
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
import java.lang.constant.ConstantDescs;
@ -47,18 +44,20 @@ import java.util.Set;
import java.util.stream.Stream;
import static java.lang.constant.ConstantDescs.*;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
/*
* @test
* @compile ConstantDescsTest.java
* @run testng ConstantDescsTest
* @run junit ConstantDescsTest
* @summary unit tests for java.lang.constant.ConstantDescs
*/
public class ConstantDescsTest {
@DataProvider(name = "validateFields")
public Object[][] knownFieldsData() {
public static Object[][] knownFieldsData() {
return new Object[][]{
{CD_Object, Object.class},
{CD_String, String.class},
@ -117,10 +116,11 @@ public class ConstantDescsTest {
* constants.
* @throws ReflectiveOperationException if the test fails
*/
@Test(dataProvider = "validateFields")
@ParameterizedTest
@MethodSource("knownFieldsData")
public void validateFields(ConstantDesc desc, Object value) throws ReflectiveOperationException {
// Use a minimally-trusted lookup
assertEquals(desc.resolveConstantDesc(MethodHandles.publicLookup()), value);
assertEquals(value, desc.resolveConstantDesc(MethodHandles.publicLookup()));
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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,21 +23,19 @@
/*
* @test
* @run testng DescribeResolveTest
* @run junit DescribeResolveTest
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.constant.Constable;
import java.lang.constant.ConstantDesc;
import java.lang.invoke.MethodHandles;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class DescribeResolveTest {
@DataProvider
public static Object[][] constables() {
return new Object[][]{
{ true },
@ -48,7 +46,8 @@ public class DescribeResolveTest {
};
}
@Test(dataProvider = "constables")
@ParameterizedTest
@MethodSource("constables")
public void testDescribeResolve(Constable constable) throws ReflectiveOperationException {
ConstantDesc desc = constable.describeConstable().orElseThrow();
Object resolved = desc.resolveConstantDesc(MethodHandles.lookup());

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, 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
@ -21,109 +21,71 @@
* questions.
*/
import java.lang.invoke.MethodType;
import java.lang.constant.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.CD_void;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;
/**
/*
* @test
* @compile DynamicCallSiteDescTest.java
* @run testng DynamicCallSiteDescTest
* @run junit DynamicCallSiteDescTest
* @summary unit tests for java.lang.constant.DynamicCallSiteDesc
*/
@Test
public class DynamicCallSiteDescTest extends SymbolicDescTest {
/* note there is no unit test for method resolveCallSiteDesc as it is being tested in another test in this
* suite, IndyDescTest
*/
@Test
public void testOf() throws ReflectiveOperationException {
DirectMethodHandleDesc dmh = ConstantDescs.ofCallsiteBootstrap(
ClassDesc.of("BootstrapAndTarget"),
"bootstrap",
ClassDesc.of("java.lang.invoke.CallSite")
);
try {
DynamicCallSiteDesc.of(
dmh,
"",
MethodTypeDesc.ofDescriptor("()I")
);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException iae) {
// good
}
assertThrows(IllegalArgumentException.class, () -> DynamicCallSiteDesc.of(
dmh,
"",
MethodTypeDesc.ofDescriptor("()I")
));
try {
DynamicCallSiteDesc.of(
null,
"getTarget",
MethodTypeDesc.ofDescriptor("()I")
);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> DynamicCallSiteDesc.of(
null,
"getTarget",
MethodTypeDesc.ofDescriptor("()I")
));
try {
DynamicCallSiteDesc.of(
dmh,
null,
MethodTypeDesc.ofDescriptor("()I")
);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> DynamicCallSiteDesc.of(
dmh,
null,
MethodTypeDesc.ofDescriptor("()I")
));
try {
DynamicCallSiteDesc.of(
dmh,
"getTarget",
null
);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> DynamicCallSiteDesc.of(
dmh,
"getTarget",
null
));
try {
DynamicCallSiteDesc.of(
dmh,
"getTarget",
MethodTypeDesc.ofDescriptor("()I"),
null
);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
try {
DynamicCallSiteDesc.of(
dmh,
"getTarget",
MethodTypeDesc.ofDescriptor("()I"),
new ConstantDesc[]{ null }
);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> DynamicCallSiteDesc.of(
dmh,
"getTarget",
MethodTypeDesc.ofDescriptor("()I"),
null
));
assertThrows(NullPointerException.class, () -> DynamicCallSiteDesc.of(
dmh,
"getTarget",
MethodTypeDesc.ofDescriptor("()I"),
new ConstantDesc[]{ null }
));
}
@Test
public void testWithArgs() throws ReflectiveOperationException {
DynamicCallSiteDesc desc = DynamicCallSiteDesc.of(ConstantDescs.ofCallsiteBootstrap(
ClassDesc.of("BootstrapAndTarget"),
@ -134,21 +96,11 @@ public class DynamicCallSiteDescTest extends SymbolicDescTest {
MethodTypeDesc.ofDescriptor("()I")
);
try {
desc.withArgs(null);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
try {
desc.withArgs(new ConstantDesc[]{ null });
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> desc.withArgs(null));
assertThrows(NullPointerException.class, () -> desc.withArgs(new ConstantDesc[]{ null }));
}
@Test
public void testWithNameAndType() throws ReflectiveOperationException {
DynamicCallSiteDesc desc = DynamicCallSiteDesc.of(ConstantDescs.ofCallsiteBootstrap(
ClassDesc.of("BootstrapAndTarget"),
@ -159,21 +111,11 @@ public class DynamicCallSiteDescTest extends SymbolicDescTest {
MethodTypeDesc.ofDescriptor("()I")
);
try {
desc.withNameAndType(null, MethodTypeDesc.ofDescriptor("()I"));
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
try {
desc.withNameAndType("bootstrap", null);
fail("NullPointerException expected");
} catch (NullPointerException npe) {
// good
}
assertThrows(NullPointerException.class, () -> desc.withNameAndType(null, MethodTypeDesc.ofDescriptor("()I")));
assertThrows(NullPointerException.class, () -> desc.withNameAndType("bootstrap", null));
}
@Test
public void testAccessorsAndFactories() throws ReflectiveOperationException {
DynamicCallSiteDesc desc = DynamicCallSiteDesc.of(ConstantDescs.ofCallsiteBootstrap(
ClassDesc.of("BootstrapAndTarget"),

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -32,19 +32,19 @@ import java.lang.constant.DynamicCallSiteDesc;
import java.lang.constant.MethodHandleDesc;
import java.lang.constant.MethodTypeDesc;
import org.testng.annotations.Test;
import static java.lang.constant.ConstantDescs.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
/**
/*
* @test
* @compile IndyDescTest.java
* @run testng IndyDescTest
* @run junit IndyDescTest
* @summary unit tests for java.lang.constant.IndyDescTest
*/
@Test
public class IndyDescTest {
public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType type,
Object... args) {
@ -54,6 +54,7 @@ public class IndyDescTest {
return new ConstantCallSite(MethodHandles.constant(String.class, (String) args[0]));
}
@Test
public void testIndyDesc() throws Throwable {
ClassDesc c = ClassDesc.of("IndyDescTest");
MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
@ -96,6 +97,7 @@ public class IndyDescTest {
assertEquals("foo", csd5.invocationName());
}
@Test
public void testEqualsHashToString() throws Throwable {
ClassDesc c = ClassDesc.of("IndyDescTest");
MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
@ -109,14 +111,14 @@ public class IndyDescTest {
assertNotEquals(csd1, csd3);
assertNotEquals(csd1.hashCode(), csd3.hashCode());
assertEquals(csd1.toString(), "DynamicCallSiteDesc[IndyDescTest::bootstrap(wooga/):()String]");
assertEquals("DynamicCallSiteDesc[IndyDescTest::bootstrap(wooga/):()String]", csd1.toString());
}
@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testEmptyInvocationName() throws Throwable {
ClassDesc c = ClassDesc.of("IndyDescTest");
MethodTypeDesc mt = MethodTypeDesc.of(CD_CallSite, CD_MethodHandles_Lookup, CD_String, CD_MethodType, CD_Object.arrayType());
DirectMethodHandleDesc mh = MethodHandleDesc.ofMethod(DirectMethodHandleDesc.Kind.STATIC, c, "bootstrap", mt);
DynamicCallSiteDesc csd1 = DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CD_String));
Assertions.assertThrows(IllegalArgumentException.class, () -> DynamicCallSiteDesc.of(mh, "", MethodTypeDesc.of(CD_String)));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -37,7 +37,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import org.testng.annotations.Test;
import static java.lang.constant.ConstantDescs.CD_Void;
import static java.lang.constant.ConstantDescs.CD_boolean;
@ -54,20 +53,17 @@ import static java.lang.constant.ConstantDescs.CD_String;
import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.CD_void;
import static java.lang.invoke.MethodHandleInfo.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @compile MethodHandleDescTest.java
* @run testng MethodHandleDescTest
* @run junit MethodHandleDescTest
* @summary unit tests for java.lang.constant.MethodHandleDesc
*/
@Test
public class MethodHandleDescTest extends SymbolicDescTest {
private static ClassDesc helperHolderClass = ClassDesc.of("TestHelpers");
private static ClassDesc testClass = helperHolderClass.nested("TestClass");
@ -89,8 +85,8 @@ public class MethodHandleDescTest extends SymbolicDescTest {
testSymbolicDesc(r);
DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;
assertEquals(r, MethodHandleDesc.of(rr.kind(), rr.owner(), rr.methodName(), rr.lookupDescriptor()));
assertEquals(r.invocationType().resolveConstantDesc(LOOKUP), r.resolveConstantDesc(LOOKUP).type());
assertEquals(MethodHandleDesc.of(rr.kind(), rr.owner(), rr.methodName(), rr.lookupDescriptor()), r);
assertEquals(r.resolveConstantDesc(LOOKUP).type(), r.invocationType().resolveConstantDesc(LOOKUP));
}
else {
testSymbolicDescForwardOnly(r);
@ -114,19 +110,20 @@ public class MethodHandleDescTest extends SymbolicDescTest {
private void testMethodHandleDesc(MethodHandleDesc r, MethodHandle mh) throws ReflectiveOperationException {
testMethodHandleDesc(r);
assertMHEquals(r.resolveConstantDesc(LOOKUP), mh);
assertEquals(mh.describeConstable().orElseThrow(), r);
assertMHEquals(mh, r.resolveConstantDesc(LOOKUP));
assertEquals(r, mh.describeConstable().orElseThrow());
// compare extractable properties: refKind, owner, name, type
MethodHandleInfo mhi = LOOKUP.revealDirect(mh);
DirectMethodHandleDesc rr = (DirectMethodHandleDesc) r;
assertEquals(mhi.getDeclaringClass().descriptorString(), rr.owner().descriptorString());
assertEquals(mhi.getName(), rr.methodName());
assertEquals(mhi.getReferenceKind(), rr.kind().refKind);
assertEquals(rr.owner().descriptorString(), mhi.getDeclaringClass().descriptorString());
assertEquals(rr.methodName(), mhi.getName());
assertEquals(rr.kind().refKind, mhi.getReferenceKind());
MethodType type = mhi.getMethodType();
assertEquals(type.toMethodDescriptorString(), lookupDescriptor(rr));
assertEquals(lookupDescriptor(rr), type.toMethodDescriptorString());
}
@Test
public void testSimpleMHs() throws ReflectiveOperationException {
MethodHandle MH_String_isEmpty = LOOKUP.findVirtual(String.class, "isEmpty", MethodType.fromMethodDescriptorString("()Z", null));
testMethodHandleDesc(MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z"), MH_String_isEmpty);
@ -147,33 +144,16 @@ public class MethodHandleDescTest extends SymbolicDescTest {
MH_ArrayList_new);
testMethodHandleDesc(MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList")), MH_ArrayList_new);
// bad constructor non void return type
try {
MethodHandleDesc.of(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", "()I");
fail("should have failed: non void return type for constructor");
} catch (IllegalArgumentException ex) {
// good
}
assertThrows(IllegalArgumentException.class, () -> MethodHandleDesc.of(Kind.CONSTRUCTOR, ClassDesc.of("java.util.ArrayList"), "<init>", "()I"),
"bad constructor non void return type");
assertThrows(NullPointerException.class, () -> MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList", null)),
"null list of parameters");
// null list of parameters
try {
MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList", null));
fail("should have failed: null list of parameters");
} catch (NullPointerException ex) {
// good
}
// null elements in list of parameters
try {
ClassDesc[] paramList = new ClassDesc[1];
paramList[0] = null;
MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList"), paramList);
fail("should have failed: null content in list of parameters");
} catch (NullPointerException ex) {
// good
}
assertThrows(NullPointerException.class, () -> MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList"), new ClassDesc[] { null }),
"null elements in list of parameters");
}
@Test
public void testAsType() throws Throwable {
MethodHandleDesc mhr = MethodHandleDesc.ofMethod(Kind.STATIC, ClassDesc.of("java.lang.Integer"), "valueOf",
MethodTypeDesc.of(CD_Integer, CD_int));
@ -181,41 +161,33 @@ public class MethodHandleDescTest extends SymbolicDescTest {
testMethodHandleDesc(takesInteger);
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");
assertEquals("MethodHandleDesc[STATIC/Integer::valueOf(int)Integer].asType(Integer)Integer", takesInteger.toString());
try {
Integer i = (Integer) mh1.invokeExact(3);
fail("Expected WMTE");
}
catch (WrongMethodTypeException ignored) { }
assertThrows(WrongMethodTypeException.class, () -> {
Integer _ = (Integer) mh1.invokeExact(3);
});
MethodHandleDesc takesInt = takesInteger.asType(MethodTypeDesc.of(CD_Integer, CD_int));
testMethodHandleDesc(takesInt);
MethodHandle mh2 = takesInt.resolveConstantDesc(LOOKUP);
assertEquals((Integer) 3, (Integer) mh2.invokeExact(3));
try {
Integer i = (Integer) mh2.invokeExact((Integer) 3);
fail("Expected WMTE");
}
catch (WrongMethodTypeException ignored) { }
assertThrows(WrongMethodTypeException.class, () -> {
Integer _ = (Integer) mh2.invokeExact((Integer) 3);
});
// Short circuit optimization
MethodHandleDesc same = mhr.asType(mhr.invocationType());
assertSame(mhr, same);
try {
mhr.asType(null);
fail("Expected NPE");
} catch (NullPointerException ex) {
// good
}
assertThrows(NullPointerException.class, () -> mhr.asType(null));
// @@@ Test varargs adaptation
// @@@ Test bad adaptations and assert runtime error on resolution
// @@@ Test intrinsification of adapted MH
}
@Test
public void testMethodHandleDesc() throws Throwable {
MethodHandleDesc ctorDesc = MethodHandleDesc.of(Kind.CONSTRUCTOR, testClass, "<ignored!>", "()V");
MethodHandleDesc staticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "sm", "(I)I");
@ -229,14 +201,14 @@ public class MethodHandleDescTest extends SymbolicDescTest {
MethodHandleDesc privateStaticMethodDesc = MethodHandleDesc.of(Kind.STATIC, testClass, "psm", "(I)I");
MethodHandleDesc privateStaticIMethodDesc = MethodHandleDesc.of(Kind.INTERFACE_STATIC, testInterface, "psm", "(I)I");
assertEquals(ctorDesc.invocationType(), MethodTypeDesc.of(testClass));
assertEquals(((DirectMethodHandleDesc) ctorDesc).lookupDescriptor(), "()V");
assertEquals(MethodTypeDesc.of(testClass), ctorDesc.invocationType());
assertEquals("()V", ((DirectMethodHandleDesc) ctorDesc).lookupDescriptor());
assertEquals(staticMethodDesc.invocationType().descriptorString(), "(I)I");
assertEquals(((DirectMethodHandleDesc) staticMethodDesc).lookupDescriptor(), "(I)I");
assertEquals("(I)I", staticMethodDesc.invocationType().descriptorString());
assertEquals("(I)I", ((DirectMethodHandleDesc) staticMethodDesc).lookupDescriptor());
assertEquals(instanceMethodDesc.invocationType().descriptorString(), "(" + testClass.descriptorString() + "I)I");
assertEquals(((DirectMethodHandleDesc) instanceMethodDesc).lookupDescriptor(), "(I)I");
assertEquals("(" + testClass.descriptorString() + "I)I", instanceMethodDesc.invocationType().descriptorString());
assertEquals("(I)I", ((DirectMethodHandleDesc) instanceMethodDesc).lookupDescriptor());
for (MethodHandleDesc r : List.of(ctorDesc, staticMethodDesc, staticIMethodDesc, instanceMethodDesc, instanceIMethodDesc))
testMethodHandleDesc(r);
@ -257,29 +229,23 @@ public class MethodHandleDescTest extends SymbolicDescTest {
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 */ }
assertThrows(IllegalAccessException.class, () -> superMethodDesc.resolveConstantDesc(LOOKUP));
assertEquals(-1, (int) superMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5));
try { superIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }
catch (IllegalAccessException e) { /* expected */ }
assertThrows(IllegalAccessException.class, () -> superIMethodDesc.resolveConstantDesc(LOOKUP));
assertEquals(0, (int) superIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5));
try { privateMethodDesc.resolveConstantDesc(LOOKUP); fail(); }
catch (IllegalAccessException e) { /* expected */ }
assertThrows(IllegalAccessException.class, () -> privateMethodDesc.resolveConstantDesc(LOOKUP));
assertEquals(5, (int) privateMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 5));
try { privateIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }
catch (IllegalAccessException e) { /* expected */ }
assertThrows(IllegalAccessException.class, () -> privateIMethodDesc.resolveConstantDesc(LOOKUP));
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 */ }
assertThrows(IllegalAccessException.class, () -> privateStaticMethodDesc.resolveConstantDesc(LOOKUP));
assertEquals(5, (int) privateStaticMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5));
try { privateStaticIMethodDesc.resolveConstantDesc(LOOKUP); fail(); }
catch (IllegalAccessException e) { /* expected */ }
assertThrows(IllegalAccessException.class, () -> privateStaticIMethodDesc.resolveConstantDesc(LOOKUP));
assertEquals(0, (int) privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestInterface.LOOKUP).invokeExact(5));
assertEquals(0, (int) privateStaticIMethodDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(5));
@ -292,34 +258,29 @@ public class MethodHandleDescTest extends SymbolicDescTest {
for (MethodHandleDesc r : List.of(staticSetterDesc, staticGetterDesc, staticGetterIDesc, setterDesc, getterDesc))
testMethodHandleDesc(r);
staticSetterDesc.resolveConstantDesc(LOOKUP).invokeExact(6); assertEquals(TestHelpers.TestClass.sf, 6);
staticSetterDesc.resolveConstantDesc(LOOKUP).invokeExact(6); assertEquals(6, TestHelpers.TestClass.sf);
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);
staticSetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(7); assertEquals(7, TestHelpers.TestClass.sf);
assertEquals(7, (int) staticGetterDesc.resolveConstantDesc(LOOKUP).invokeExact());
assertEquals(7, (int) staticGetterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact());
assertEquals(3, (int) staticGetterIDesc.resolveConstantDesc(LOOKUP).invokeExact());
assertEquals(3, (int) staticGetterIDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact());
setterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance, 6); assertEquals(instance.f, 6);
setterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance, 6); assertEquals(6, instance.f);
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);
setterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance, 7); assertEquals(7, instance.f);
assertEquals(7, (int) getterDesc.resolveConstantDesc(LOOKUP).invokeExact(instance));
assertEquals(7, (int) getterDesc.resolveConstantDesc(TestHelpers.TestClass.LOOKUP).invokeExact(instance));
}
private void assertBadArgs(Supplier<MethodHandleDesc> supplier, String s) {
try {
MethodHandleDesc r = supplier.get();
fail("Expected failure for " + s);
}
catch (IllegalArgumentException e) {
// succeed
}
assertThrows(IllegalArgumentException.class, supplier::get, s);
}
@Test
public void testBadFieldMHs() {
List<String> badGetterDescs = List.of("()V", "(Ljava/lang/Object;)V", "(I)I", "(Ljava/lang/Object;I)I");
List<String> badStaticGetterDescs = List.of("()V", "(Ljava/lang/Object;)I", "(I)I", "(Ljava/lang/Object;I)I");
@ -332,11 +293,12 @@ public class MethodHandleDescTest extends SymbolicDescTest {
badStaticSetterDescs.forEach(s -> assertBadArgs(() -> MethodHandleDesc.of(STATIC_SETTER, helperHolderClass, "x", s), s));
}
@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testBadOwners() {
MethodHandleDesc.ofMethod(VIRTUAL, ClassDesc.ofDescriptor("I"), "x", MethodTypeDesc.ofDescriptor("()I"));
assertThrows(IllegalArgumentException.class, () -> MethodHandleDesc.ofMethod(VIRTUAL, ClassDesc.ofDescriptor("I"), "x", MethodTypeDesc.ofDescriptor("()I")));
}
@Test
public void testSymbolicDescsConstants() throws ReflectiveOperationException {
int tested = 0;
Field[] fields = ConstantDescs.class.getDeclaredFields();
@ -359,10 +321,11 @@ public class MethodHandleDescTest extends SymbolicDescTest {
assertTrue(tested > 0);
}
@Test
public void testKind() {
for (Kind k : Kind.values()) {
assertEquals(Kind.valueOf(k.refKind), Kind.valueOf(k.refKind, k.refKind == MethodHandleInfo.REF_invokeInterface));
assertEquals(Kind.valueOf(k.refKind, k.isInterface), k);
assertEquals(Kind.valueOf(k.refKind, k.refKind == MethodHandleInfo.REF_invokeInterface), Kind.valueOf(k.refKind));
assertEquals(k, Kind.valueOf(k.refKind, k.isInterface));
}
// let's now verify those cases for which the value of the isInterface parameter is ignored
int[] isInterfaceIgnored = new int[] {
@ -374,15 +337,15 @@ public class MethodHandleDescTest extends SymbolicDescTest {
REF_invokeInterface
};
for (int refKind : isInterfaceIgnored) {
assertEquals(Kind.valueOf(refKind, false), Kind.valueOf(refKind, true));
assertEquals(Kind.valueOf(refKind, true), Kind.valueOf(refKind, false));
}
// some explicit tests for REF_invokeStatic and REF_invokeSpecial
assertNotEquals(Kind.valueOf(REF_invokeStatic, false), Kind.valueOf(REF_invokeStatic, true));
assertNotEquals(Kind.valueOf(REF_invokeSpecial, false), Kind.valueOf(REF_invokeSpecial, true));
assertEquals(Kind.valueOf(REF_invokeStatic, false), Kind.STATIC);
assertEquals(Kind.valueOf(REF_invokeStatic, true), Kind.INTERFACE_STATIC);
assertEquals(Kind.valueOf(REF_invokeSpecial, false), Kind.SPECIAL);
assertEquals(Kind.valueOf(REF_invokeSpecial, true), Kind.INTERFACE_SPECIAL);
assertNotEquals(Kind.valueOf(REF_invokeStatic, true), Kind.valueOf(REF_invokeStatic, false));
assertNotEquals(Kind.valueOf(REF_invokeSpecial, true), Kind.valueOf(REF_invokeSpecial, false));
assertEquals(Kind.STATIC, Kind.valueOf(REF_invokeStatic, false));
assertEquals(Kind.INTERFACE_STATIC, Kind.valueOf(REF_invokeStatic, true));
assertEquals(Kind.SPECIAL, Kind.valueOf(REF_invokeSpecial, false));
assertEquals(Kind.INTERFACE_SPECIAL, Kind.valueOf(REF_invokeSpecial, true));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -32,21 +32,20 @@ import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.testng.annotations.Test;
import static java.lang.constant.ConstantDescs.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
/**
/*
* @test
* @bug 8304932
* @compile MethodTypeDescTest.java
* @run testng MethodTypeDescTest
* @run junit MethodTypeDescTest
* @summary unit tests for java.lang.constant.MethodTypeDesc
*/
@Test
public class MethodTypeDescTest extends SymbolicDescTest {
private void testMethodTypeDesc(MethodTypeDesc r) throws ReflectiveOperationException {
@ -75,16 +74,16 @@ public class MethodTypeDescTest extends SymbolicDescTest {
private void testMethodTypeDesc(MethodTypeDesc r, MethodType mt) throws ReflectiveOperationException {
testMethodTypeDesc(r);
assertEquals(r.resolveConstantDesc(LOOKUP), mt);
assertEquals(mt.describeConstable().get(), r);
assertEquals(mt, r.resolveConstantDesc(LOOKUP));
assertEquals(r, mt.describeConstable().get());
assertEquals(r.descriptorString(), mt.toMethodDescriptorString());
assertEquals(r.parameterCount(), mt.parameterCount());
assertEquals(r.parameterList(), mt.parameterList().stream().map(SymbolicDescTest::classToDesc).collect(toList()));
assertEquals(r.parameterArray(), Stream.of(mt.parameterArray()).map(SymbolicDescTest::classToDesc).toArray(ClassDesc[]::new));
assertEquals(mt.toMethodDescriptorString(), r.descriptorString());
assertEquals(mt.parameterCount(), r.parameterCount());
assertEquals(mt.parameterList().stream().map(SymbolicDescTest::classToDesc).collect(toList()), r.parameterList());
assertArrayEquals(Stream.of(mt.parameterArray()).map(SymbolicDescTest::classToDesc).toArray(ClassDesc[]::new), r.parameterArray());
for (int i=0; i<r.parameterCount(); i++)
assertEquals(r.parameterType(i), classToDesc(mt.parameterType(i)));
assertEquals(r.returnType(), classToDesc(mt.returnType()));
assertEquals(classToDesc(mt.parameterType(i)), r.parameterType(i));
assertEquals(classToDesc(mt.returnType()), r.returnType());
}
private void assertMethodType(ClassDesc returnType,
@ -94,13 +93,13 @@ public class MethodTypeDescTest extends SymbolicDescTest {
MethodTypeDesc mtDesc = MethodTypeDesc.of(returnType, paramTypes);
// MTDesc accessors
assertEquals(descriptor, mtDesc.descriptorString());
assertEquals(returnType, mtDesc.returnType());
assertEquals(paramTypes, mtDesc.parameterArray());
assertEquals(Arrays.asList(paramTypes), mtDesc.parameterList());
assertEquals(paramTypes.length, mtDesc.parameterCount());
assertEquals(mtDesc.descriptorString(), descriptor);
assertEquals(mtDesc.returnType(), returnType);
assertArrayEquals(mtDesc.parameterArray(), paramTypes);
assertEquals(mtDesc.parameterList(), Arrays.asList(paramTypes));
assertEquals(mtDesc.parameterCount(), paramTypes.length);
for (int i=0; i<paramTypes.length; i++)
assertEquals(paramTypes[i], mtDesc.parameterType(i));
assertEquals(mtDesc.parameterType(i), paramTypes[i]);
// Consistency between MT and MTDesc
MethodType mt = MethodType.fromMethodDescriptorString(descriptor, null);
@ -110,14 +109,12 @@ public class MethodTypeDescTest extends SymbolicDescTest {
for (String r : returnDescs) {
ClassDesc rc = ClassDesc.ofDescriptor(r);
MethodTypeDesc newDesc = mtDesc.changeReturnType(rc);
assertEquals(newDesc, MethodTypeDesc.of(rc, paramTypes));
assertEquals(MethodTypeDesc.of(rc, paramTypes), newDesc);
testMethodTypeDesc(newDesc, mt.changeReturnType(rc.resolveConstantDesc(LOOKUP)));
}
// try with null parameter
expectThrows(NullPointerException.class, () -> {
MethodTypeDesc newDesc = mtDesc.changeReturnType(null);
});
assertThrows(NullPointerException.class, () -> mtDesc.changeReturnType(null));
// changeParamType
for (int i=0; i<paramTypes.length; i++) {
@ -126,7 +123,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
ClassDesc[] ps = paramTypes.clone();
ps[i] = pc;
MethodTypeDesc newDesc = mtDesc.changeParameterType(i, pc);
assertEquals(newDesc, MethodTypeDesc.of(returnType, ps));
assertEquals(MethodTypeDesc.of(returnType, ps), newDesc);
testMethodTypeDesc(newDesc, mt.changeParameterType(i, pc.resolveConstantDesc(LOOKUP)));
}
}
@ -139,7 +136,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
.mapToObj(j -> paramTypes[j])
.toArray(ClassDesc[]::new);
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(i, i + 1);
assertEquals(newDesc, MethodTypeDesc.of(returnType, ps));
assertEquals(MethodTypeDesc.of(returnType, ps), newDesc);
testMethodTypeDesc(newDesc, mt.dropParameterTypes(i, i+1));
// drop multiple params
@ -147,7 +144,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
var t = new ArrayList<>(Arrays.asList(paramTypes));
t.subList(i, j).clear();
MethodTypeDesc multiDrop = mtDesc.dropParameterTypes(i, j);
assertEquals(multiDrop, MethodTypeDesc.of(returnType, t.toArray(ClassDesc[]::new)));
assertEquals(MethodTypeDesc.of(returnType, t.toArray(ClassDesc[]::new)), multiDrop);
testMethodTypeDesc(multiDrop, mt.dropParameterTypes(i, j));
}
}
@ -162,7 +159,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
.mapToObj(j -> (j < k) ? paramTypes[j] : (j == k) ? p : paramTypes[j-1])
.toArray(ClassDesc[]::new);
MethodTypeDesc newDesc = mtDesc.insertParameterTypes(i, p);
assertEquals(newDesc, MethodTypeDesc.of(returnType, ps));
assertEquals(MethodTypeDesc.of(returnType, ps), newDesc);
testMethodTypeDesc(newDesc, mt.insertParameterTypes(i, p.resolveConstantDesc(LOOKUP)));
}
@ -172,7 +169,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
a.addAll(i, Arrays.asList(addition));
MethodTypeDesc newDesc = mtDesc.insertParameterTypes(i, addition);
assertEquals(newDesc, MethodTypeDesc.of(returnType, a.toArray(ClassDesc[]::new)));
assertEquals(MethodTypeDesc.of(returnType, a.toArray(ClassDesc[]::new)), newDesc);
testMethodTypeDesc(newDesc, mt.insertParameterTypes(i, Arrays.stream(addition).map(d -> {
try {
return (Class<?>) d.resolveConstantDesc(LOOKUP);
@ -190,32 +187,28 @@ public class MethodTypeDescTest extends SymbolicDescTest {
IntStream.rangeClosed(0, paramDescTypes.length - 1)
.mapToObj(i -> ClassDesc.ofDescriptor(paramDescTypes[i])).toArray(ClassDesc[]::new);
MethodTypeDesc mtDesc = MethodTypeDesc.of(returnType, paramTypes);
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.insertParameterTypes(-1, paramTypes);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.insertParameterTypes(-1, paramTypes));
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.insertParameterTypes(paramTypes.length + 1, paramTypes);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.insertParameterTypes(paramTypes.length + 1, paramTypes));
expectThrows(IllegalArgumentException.class, () -> {
{
ClassDesc[] newParamTypes = new ClassDesc[1];
newParamTypes[0] = CD_void;
MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
newDesc = newDesc.insertParameterTypes(0, newParamTypes);
});
assertThrows(IllegalArgumentException.class, () -> newDesc.insertParameterTypes(0, newParamTypes));
}
expectThrows(NullPointerException.class, () -> {
{
MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
newDesc = newDesc.insertParameterTypes(0, null);
});
assertThrows(NullPointerException.class, () -> newDesc.insertParameterTypes(0, null));
}
expectThrows(NullPointerException.class, () -> {
{
ClassDesc[] newParamTypes = new ClassDesc[1];
newParamTypes[0] = null;
MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
newDesc = newDesc.insertParameterTypes(0, newParamTypes);
});
assertThrows(NullPointerException.class, () -> newDesc.insertParameterTypes(0, newParamTypes));
}
}
private void badDropParametersTypes(ClassDesc returnType, String... paramDescTypes) {
@ -224,27 +217,18 @@ public class MethodTypeDescTest extends SymbolicDescTest {
.mapToObj(i -> ClassDesc.ofDescriptor(paramDescTypes[i])).toArray(ClassDesc[]::new);
MethodTypeDesc mtDesc = MethodTypeDesc.of(returnType, paramTypes);
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(-1, 0);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.dropParameterTypes(-1, 0));
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(paramTypes.length, 0);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.dropParameterTypes(paramTypes.length, 0));
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(paramTypes.length + 1, 0);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.dropParameterTypes(paramTypes.length + 1, 0));
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(0, paramTypes.length + 1);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.dropParameterTypes(0, paramTypes.length + 1));
expectThrows(IndexOutOfBoundsException.class, () -> {
MethodTypeDesc newDesc = mtDesc.dropParameterTypes(1, 0);
});
assertThrows(IndexOutOfBoundsException.class, () -> mtDesc.dropParameterTypes(1, 0));
}
@Test
public void testMethodTypeDesc() throws ReflectiveOperationException {
for (String r : returnDescs) {
assertMethodType(ClassDesc.ofDescriptor(r));
@ -257,6 +241,7 @@ public class MethodTypeDescTest extends SymbolicDescTest {
}
}
@Test
public void testBadMethodTypeRefs() {
// ofDescriptor
List<String> badDescriptors = List.of("()II", "()I;", "(I;)", "(I)", "()L", "(V)V",
@ -283,29 +268,32 @@ public class MethodTypeDescTest extends SymbolicDescTest {
assertThrows(IllegalArgumentException.class, () -> MethodTypeDesc.of(CD_int, List.of(CD_void)));
}
@Test
public void testOfArrayImmutability() {
ClassDesc[] args = {CD_Object, CD_int};
var mtd = MethodTypeDesc.of(CD_void, args);
args[1] = CD_void;
assertEquals(mtd, MethodTypeDesc.of(CD_void, CD_Object, CD_int));
assertEquals(MethodTypeDesc.of(CD_void, CD_Object, CD_int), mtd);
mtd.parameterArray()[1] = CD_void;
assertEquals(mtd, MethodTypeDesc.of(CD_void, CD_Object, CD_int));
assertEquals(MethodTypeDesc.of(CD_void, CD_Object, CD_int), mtd);
}
@Test
public void testOfListImmutability() {
List<ClassDesc> args = Arrays.asList(CD_Object, CD_int);
var mtd = MethodTypeDesc.of(CD_void, args);
args.set(1, CD_void);
assertEquals(mtd, MethodTypeDesc.of(CD_void, CD_Object, CD_int));
assertEquals(MethodTypeDesc.of(CD_void, CD_Object, CD_int), mtd);
assertThrows(UnsupportedOperationException.class, () ->
mtd.parameterList().set(1, CD_void));
assertEquals(mtd, MethodTypeDesc.of(CD_void, CD_Object, CD_int));
assertEquals(MethodTypeDesc.of(CD_void, CD_Object, CD_int), mtd);
}
@Test
public void testMissingClass() {
var mtd = MTD_void.insertParameterTypes(0, ClassDesc.of("does.not.exist.DoesNotExist"));
assertThrows(ReflectiveOperationException.class, () -> mtd.resolveConstantDesc(MethodHandles.publicLookup()));

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -21,26 +21,22 @@
* questions.
*/
/**
/*
* @test
* @bug 8215510
* @compile NameValidationTest.java
* @run testng NameValidationTest
* @run junit NameValidationTest
* @summary unit tests for verifying member names
*/
import java.lang.constant.*;
import java.lang.invoke.*;
import org.testng.annotations.Test;
import static java.lang.constant.DirectMethodHandleDesc.*;
import static java.lang.constant.ConstantDescs.*;
import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL;
import static org.testng.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
@Test
public class NameValidationTest {
private static final String[] badMemberNames = new String[] {"xx.xx", "zz;zz", "[l", "aa/aa", "<cinit>"};
@ -49,26 +45,17 @@ public class NameValidationTest {
private static final String[] badClassNames = new String[] {"zz;zz", "[l", "aa/aa", ".", "a..b"};
private static final String[] goodClassNames = new String[] {"3", "~", "$", "qq", "a.a"};
@Test
public void testMemberNames() {
DirectMethodHandleDesc mh = MethodHandleDesc.of(Kind.VIRTUAL, CD_String, "isEmpty", "()Z");
for (String badName : badMemberNames) {
try {
memberNamesHelper(badName, mh, CD_int, null);
fail("Expected failure for name " + badName);
} catch (IllegalArgumentException iae) {
// expected
}
try {
memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]);
fail("Expected failure for name " + badName);
} catch (IllegalArgumentException iae) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> memberNamesHelper(badName, mh, CD_int, null), badName);
assertThrows(IllegalArgumentException.class, () -> memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]), badName);
}
for (String badName : goodMemberNames) {
memberNamesHelper(badName, mh, CD_int, null);
memberNamesHelper(badName, mh, CD_int, new ConstantDesc[0]);
for (String goodName : goodMemberNames) {
memberNamesHelper(goodName, mh, CD_int, null);
memberNamesHelper(goodName, mh, CD_int, new ConstantDesc[0]);
}
}
@ -83,14 +70,10 @@ public class NameValidationTest {
}
}
@Test
public void testClassNames() {
for (String badName : badClassNames) {
try {
ClassDesc.of(badName);
fail("Expected failure for name " + badName);
} catch (IllegalArgumentException iae) {
// expected
}
assertThrows(IllegalArgumentException.class, () -> ClassDesc.of(badName), badName);
}
for (String goodName : goodClassNames) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -32,7 +32,7 @@ import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import static org.testng.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Base class for XxxDesc tests
@ -106,7 +106,7 @@ public abstract class SymbolicDescTest {
if (desc instanceof Constable) {
Optional<ConstantDesc> opt = (Optional<ConstantDesc>) ((Constable) desc).describeConstable();
ConstantDesc sr = (ConstantDesc) opt.orElseThrow().resolveConstantDesc(LOOKUP);
assertEquals(sr, desc);
assertEquals(desc, sr);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -22,37 +22,35 @@
*/
import java.lang.invoke.TypeDescriptor;
import java.lang.constant.ClassDesc;
import org.testng.annotations.Test;
import static java.lang.constant.ConstantDescs.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
/*
* @test
* @compile TypeDescriptorTest.java
* @run testng TypeDescriptorTest
* @run junit TypeDescriptorTest
* @summary unit tests for implementations of java.lang.invoke.TypeDescriptor
*/
@Test
public class TypeDescriptorTest {
private<F extends TypeDescriptor.OfField<F>> void testArray(F f, boolean isArray, F component, F array) {
if (isArray) {
assertTrue(f.isArray());
assertEquals(f.arrayType(), array);
assertEquals(f.componentType(), component);
assertEquals(array, f.arrayType());
assertEquals(component, f.componentType());
}
else {
assertFalse(f.isArray());
assertEquals(f.arrayType(), array);
assertEquals(array, f.arrayType());
assertNull(f.componentType());
}
}
@Test
public void testClass() {
testArray(int.class, false, null, int[].class);
testArray(int[].class, true, int.class, int[][].class);
@ -67,6 +65,7 @@ public class TypeDescriptorTest {
assertFalse(String[].class.isPrimitive());
}
@Test
public void testClassDesc() {
testArray(CD_int, false, null, CD_int.arrayType());

View File

@ -1,4 +1,3 @@
# This file identifies root(s) of the test-ng hierarchy.
TestNG.dirs = .
lib.dirs = /lib/testlibrary/bootlib

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -26,50 +26,34 @@ package jdk.internal.constant;
import java.lang.constant.*;
import java.util.*;
import org.testng.annotations.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import static org.testng.Assert.*;
/**
/*
* @test
* @bug 8303930
* @compile ConstantUtilsTest.java
* @modules java.base/jdk.internal.constant
* @run testng ConstantUtilsTest
* @run junit ConstantUtilsTest
* @summary unit tests for methods of java.lang.constant.ConstantUtils that are not covered by other unit tests
*/
@Test
public class ConstantUtilsTest {
private static ClassDesc thisClass = ClassDesc.of("MethodHandleDescTest");
@Test
public void testValidateMemberName() {
try {
ConstantUtils.validateMemberName(null, false);
fail("");
} catch (NullPointerException e) {
// good
}
try {
ConstantUtils.validateMemberName("", false);
fail("");
} catch (IllegalArgumentException e) {
// good
}
assertThrows(NullPointerException.class, () -> ConstantUtils.validateMemberName(null, false));
assertThrows(NullPointerException.class, () -> ConstantUtils.validateMemberName("", false));
List<String> badNames = List.of(".", ";", "[", "/", "<", ">");
for (String n : badNames) {
try {
ConstantUtils.validateMemberName(n, true);
fail(n);
} catch (IllegalArgumentException e) {
// good
}
assertThrows(IllegalArgumentException.class, () -> ConstantUtils.validateMemberName(n, true), n);
}
}
@Test
public void testSkipOverFieldSignatureVoid() {
int ret = ConstantUtils.skipOverFieldSignature("(V)V", 1, 4);
assertEquals(ret, 0, "Descriptor of (V)V starting at index 1, void disallowed");
assertEquals(0, ret, "Descriptor of (V)V starting at index 1, void disallowed");
}
}