RFR: 8353917: jnativescan: Simplify ClassResolver

Jorn Vernee jvernee at openjdk.org
Tue Apr 8 11:44:59 UTC 2025


On Mon, 7 Apr 2025 16:36:07 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> `jnativescan` uses the `ClassResolver` class to find both system classes, as well as application classes. In principle, a class resolver supports both iterating over all classes from that particular source, as well as looking up classes by name. However, the `ClassResolver` for system classes doesn't support iterating, and the lookup functionality for `ClassResolvers` from other sources is never used (only iterating).
> 
> This patch proposes to split the iterating functionality out of `ClassResolver`. Clients that were using this functionality will switch to using the iteration functionality that is available on `ClassFileSource` directly (whose classes were previously being wrapped in a `ClassResolver`). As a result of this latter change, we can now also let `NativeMethodFinder` operate on individual classes. We just iterate over all the class file sources in `JNativeScanTask`, and feed individual class models to `NativeMethodFinder`. I think this is a good conceptual simplification of `NativeMethodFinder`,
> 
> Since `ClassResolver` is now only used for system classes, I've removed it, leaving only its single implementation: `SystemClassResolver`.
> 
> Testing: running `langtools_jnativescan` test suite.

src/jdk.jdeps/share/classes/com/sun/tools/jnativescan/ClassFileSource.java line 86:

> 84: 
> 85:     record ClassPathJar(Path path, Runtime.Version version) implements ClassFileSource {
> 86:         @Override

The runtime version was only being used by `ClassPathJar::classFiles`, so I've made it a property of `ClassPathjar` instead of a parameter to that method, which is not used by the other implementations.

src/jdk.jdeps/share/classes/com/sun/tools/jnativescan/NativeMethodFinder.java line 70:

> 68:     }
> 69: 
> 70:     private SortedSet<MethodRef> findRestrictedMethodInvocations(MethodModel methodModel) {

Created this helper method to reduce the overall nesting of the `find` method.

src/jdk.jdeps/share/classes/com/sun/tools/jnativescan/SystemClassResolver.java line 41:

> 39: import java.util.*;
> 40: 
> 41: class SystemClassResolver implements AutoCloseable {

Code in this class is unchanged from the old implementation in `ClassFileResolver`, except that the class was moved to the top level, and some code moved around within the class to match existing style.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24493#discussion_r2031619068
PR Review Comment: https://git.openjdk.org/jdk/pull/24493#discussion_r2031620942
PR Review Comment: https://git.openjdk.org/jdk/pull/24493#discussion_r2031623738


More information about the core-libs-dev mailing list