Skip to content

Commit 8bb0ca4

Browse files
committed
8353917: jnativescan: Simplify ClassResolver
Reviewed-by: mcimadamore
1 parent 36069f6 commit 8bb0ca4

File tree

5 files changed

+183
-224
lines changed

5 files changed

+183
-224
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,8 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.io.UncheckedIOException;
30+
import java.lang.classfile.ClassFile;
31+
import java.lang.classfile.ClassModel;
3032
import java.lang.module.ModuleReader;
3133
import java.lang.module.ModuleReference;
3234
import java.net.URI;
@@ -40,7 +42,12 @@ sealed interface ClassFileSource {
4042
String moduleName();
4143
Path path();
4244

43-
Stream<byte[]> classFiles(Runtime.Version version) throws IOException;
45+
Stream<byte[]> classFiles() throws IOException;
46+
47+
default Stream<ClassModel> classModels() throws IOException {
48+
ClassFile classFile = ClassFile.of();
49+
return classFiles().map(classFile::parse);
50+
}
4451

4552
record Module(ModuleReference reference) implements ClassFileSource {
4653
@Override
@@ -55,7 +62,7 @@ public Path path() {
5562
}
5663

5764
@Override
58-
public Stream<byte[]> classFiles(Runtime.Version version) throws IOException {
65+
public Stream<byte[]> classFiles() throws IOException {
5966
ModuleReader reader = reference().open();
6067
return reader.list()
6168
.filter(resourceName -> resourceName.endsWith(".class"))
@@ -75,14 +82,14 @@ public Stream<byte[]> classFiles(Runtime.Version version) throws IOException {
7582
}
7683
}
7784

78-
record ClassPathJar(Path path) implements ClassFileSource {
85+
record ClassPathJar(Path path, Runtime.Version version) implements ClassFileSource {
7986
@Override
8087
public String moduleName() {
8188
return "ALL-UNNAMED";
8289
}
8390

8491
@Override
85-
public Stream<byte[]> classFiles(Runtime.Version version) throws IOException {
92+
public Stream<byte[]> classFiles() throws IOException {
8693
JarFile jf = new JarFile(path().toFile(), false, ZipFile.OPEN_READ, version);
8794
return jf.versionedStream()
8895
.filter(je -> je.getName().endsWith(".class"))
@@ -109,7 +116,7 @@ public String moduleName() {
109116
}
110117

111118
@Override
112-
public Stream<byte[]> classFiles(Runtime.Version version) throws IOException {
119+
public Stream<byte[]> classFiles() throws IOException {
113120
return Files.walk(path)
114121
.filter(file -> Files.isRegularFile(file) && file.toString().endsWith(".class"))
115122
.map(file -> {

src/jdk.jdeps/share/classes/com/sun/tools/jnativescan/ClassResolver.java

Lines changed: 0 additions & 163 deletions
This file was deleted.

src/jdk.jdeps/share/classes/com/sun/tools/jnativescan/JNativeScanTask.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,17 +26,18 @@
2626

2727
import java.io.IOException;
2828
import java.io.PrintWriter;
29+
import java.lang.classfile.ClassModel;
2930
import java.lang.constant.ClassDesc;
3031
import java.lang.module.Configuration;
3132
import java.lang.module.ModuleFinder;
3233
import java.lang.module.ResolvedModule;
33-
import java.net.URI;
3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
3636
import java.util.*;
3737
import java.util.jar.JarFile;
3838
import java.util.jar.Manifest;
3939
import java.util.stream.Collectors;
40+
import java.util.stream.Stream;
4041
import java.util.zip.ZipFile;
4142

4243
class JNativeScanTask {
@@ -76,11 +77,26 @@ public void run() throws JNativeScanFatalError {
7677
Set<String> errors = new LinkedHashSet<>();
7778
Diagnostics diagnostics = (context, error) ->
7879
errors.add("Error while processing method: " + context + ": " + error.getMessage());
79-
SortedMap<ClassFileSource, SortedMap<ClassDesc, List<RestrictedUse>>> allRestrictedMethods;
80-
try(ClassResolver classesToScan = ClassResolver.forClassFileSources(toScan, version);
81-
ClassResolver systemClassResolver = ClassResolver.forSystemModules(version)) {
82-
NativeMethodFinder finder = NativeMethodFinder.create(diagnostics, classesToScan, systemClassResolver);
83-
allRestrictedMethods = finder.findAll();
80+
SortedMap<ClassFileSource, SortedMap<ClassDesc, List<RestrictedUse>>> allRestrictedMethods
81+
= new TreeMap<>(Comparator.comparing(ClassFileSource::path));
82+
try(SystemClassResolver systemClassResolver = SystemClassResolver.forRuntimeVersion(version)) {
83+
NativeMethodFinder finder = NativeMethodFinder.create(diagnostics, systemClassResolver);
84+
85+
for (ClassFileSource source : toScan) {
86+
SortedMap<ClassDesc, List<RestrictedUse>> perClass
87+
= new TreeMap<>(Comparator.comparing(JNativeScanTask::qualName));
88+
try (Stream<ClassModel> stream = source.classModels()) {
89+
stream.forEach(classModel -> {
90+
List<RestrictedUse> restrictedUses = finder.find(classModel);
91+
if (!restrictedUses.isEmpty()) {
92+
perClass.put(classModel.thisClass().asSymbol(), restrictedUses);
93+
}
94+
});
95+
}
96+
if (!perClass.isEmpty()) {
97+
allRestrictedMethods.put(source, perClass);
98+
}
99+
}
84100
} catch (IOException e) {
85101
throw new RuntimeException(e);
86102
}
@@ -115,7 +131,7 @@ private List<ClassFileSource> findAllClassPathJars() throws JNativeScanFatalErro
115131
jarsToScan.offer(otherJar);
116132
}
117133
}
118-
result.add(new ClassFileSource.ClassPathJar(jar));
134+
result.add(new ClassFileSource.ClassPathJar(jar, version));
119135
}
120136
} else if (Files.isDirectory(path)) {
121137
result.add(new ClassFileSource.ClassPathDirectory(path));

0 commit comments

Comments
 (0)