JDK-8057919 Class.getSimpleName() should work for non-JLS compliant class names
John Rose
john.r.rose at oracle.com
Wed Jun 17 21:35:32 UTC 2015
On Jun 16, 2015, at 8:36 PM, David Holmes <david.holmes at oracle.com> wrote:
>
> On 17/06/2015 8:39 AM, John Rose wrote:
>> What I'm suggesting is that a BC generator might emit a zero length name for a non-anon class, leading to confusion when C.isAnon is called.
>
> Is a zero-length "name" for a non-anonymous class valid?
For better or worse, the JVMS does not require anything more than that the inner class name be a valid CONSTANT_Utf8.
(N.B. This is *not* the real bytecode name of the inner class. It is a simple name, as stored in the InnerClasses table.)
In hindsight, we could have added a check to say that it has to be a valid identifier (ClassFileParser::verify_unqualified_name).
> getSimpleName() is specified to return "" for anonymous classes. Seems broken to allow "" for non-anonymous.
I agree that it's broken to *do* this, but we need to allow it (however broken) if the JVMS allows it. Unless we amend the JVMS.
— John
> David
>
>> – John
>>
>>> On Jun 16, 2015, at 5:09 AM, Vladimir Ivanov <vladimir.x.ivanov at oracle.com> wrote:
>>>
>>> John,
>>>
>>>>
>>>> That might be an issue here. Just as (String)null and (String)"" are distinct values in Java, in the class file a CP ref of zero differs from a CP ref to a CONSTANT_Utf8 of the empty string "". But the method Class.isAnonymousClass does not make a distinction between those two cases, which might sometimes lead to confusion. I would view this as a bug in Class.isAnonymousClass.
>>> For non-top level classes, Class.getSimpleName calls Class.getSimpleBinaryName which converts null to empty string, assuming it's an anonymous class. So, callers should not see null values.
>>>
>>> Best regards,
>>> Vladimir Ivanov
More information about the core-libs-dev
mailing list