RFR: 8342206: Convenience method to check if a ClassEntry matches a ClassDesc

ExE Boss duke at openjdk.org
Wed Oct 16 05:54:10 UTC 2024


On Tue, 15 Oct 2024 17:34:53 GMT, Chen Liang <liach at openjdk.org> wrote:

> Currently, to efficiently check if a `ClassEntry`, such as one from an `InvokeInstruction`, is of a particular class, we use such a pattern (as seen in JEP 486 [Appendix](https://openjdk.org/jeps/486#Appendix)):
> 
> inst.owner().name().equalsString("java/lang/System")
> 
> This pattern has a few issues:
> 
> 1. This is not straightforward. Users may be tempted to write
> 
> inst.owner().asSymbol().equals(CD_System)
> 
> unaware of the degraded performance from extra conversions.
> 
> 2. We aim to reduce the use of "internal names" with encapsulation. Direct use of `"java/lang/System"` goes against this, and we have no constants offered to reduce such error-prone use of internal name.
> 
> Thus, I propose a new API in `ClassEntry`:
> 
> boolean equalsSymbol(ClassDesc)
> 
> that can be used conveniently to check the identity of a class entry with a clean syntax and better performance.

src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java line 609:

> 607:             var mySym = this.sym;
> 608:             if (mySym != null)
> 609:                 return mySym.equals(symbol);

Note that `ClassEntry::asSymbol()` also caches array `ClassDesc`s in `ref1.typeSym` so that the symbol cache can be shared with `FieldRefEntry` and `ConstantDynamicEntry`.

--------------------------------------------------------------------------------

I suggest adding the following check:

if (ref1.typeSym instanceof ClassDesc ref1Sym && ref1Sym.isArray()) {
	return ref1Sym.equals(symbol);
}

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/21526#discussion_r1802394895


More information about the core-libs-dev mailing list