8319462: Signature.ClassTypeSig::classDesc() incorrect for inner class types

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2023-11-07 12:22:40 +00:00 committed by Adam Sotona
parent 541ff7149f
commit d22e368cb5
2 changed files with 25 additions and 3 deletions
src/java.base/share/classes/jdk/internal/classfile
test/jdk/jdk/classfile

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
@ -118,7 +118,9 @@ public sealed interface Signature {
/** {@return the class name, as a symbolic descriptor} */
default ClassDesc classDesc() {
return ClassDesc.ofInternalName(className());
var outer = outerType();
return outer.isEmpty() ? ClassDesc.ofInternalName(className())
: outer.get().classDesc().nested(className());
}
/** {@return the type arguments of the class} */

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
@ -26,12 +26,14 @@
* @summary Testing Signatures.
* @run junit SignaturesTest
*/
import java.io.IOException;
import java.lang.constant.ClassDesc;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
@ -166,4 +168,22 @@ class SignaturesTest {
});
System.out.println("SignaturesTest - tested signatures of " + csc + " classes, " + msc + " methods, " + fsc + " fields and " + rsc + " record components");
}
static class Outer<T1> {
class Inner<T2> {}
}
static class Observer extends ArrayList<Outer<String>.Inner<Long>>{}
@Test
void testClassSignatureClassDesc() throws IOException {
var observerCf = Classfile.of().parse(Path.of(System.getProperty("test.classes"), "SignaturesTest$Observer.class"));
var sig = observerCf.findAttribute(Attributes.SIGNATURE).orElseThrow().asClassSignature();
var innerSig = (ClassTypeSig) ((ClassTypeSig) sig.superclassSignature()) // ArrayList
.typeArgs().getFirst() // Outer<String>.Inner<Long>
.boundType().orElseThrow(); // assert it's exact bound
assertEquals("Inner", innerSig.className(), "simple name in signature");
assertEquals(Outer.Inner.class.describeConstable().orElseThrow(), innerSig.classDesc(),
"ClassDesc derived from signature");
}
}