< prev index next >
test/jdk/jdk/internal/reflect/CallerSensitive/CheckCSMs.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -20,17 +20,16 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import com.sun.tools.classfile.*;
-import com.sun.tools.jdeps.ClassFileReader;
import static com.sun.tools.classfile.ConstantPool.*;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.UncheckedIOException;
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.nio.file.Paths;
import java.util.ArrayList;
@@ -97,13 +96,12 @@
public Set<String> run(Stream<Path> classes)
throws IOException, InterruptedException, ExecutionException,
ConstantPoolException
{
- classes.forEach(this::processPath);
+ classes.forEach(p -> pool.submit(getTask(p)));
waitForCompletion();
- pool.shutdown();
return nonFinalCSMs;
}
private ReferenceFinder.Filter getFilter() {
@@ -147,31 +145,10 @@
}
}
};
}
- void processPath(Path path) {
- try {
- ClassFileReader reader = ClassFileReader.newInstance(path);
- for (ClassFile cf : reader.getClassFiles()) {
- if (cf.access_flags.is(AccessFlags.ACC_MODULE))
- continue;
-
- String classFileName = cf.getName();
- // for each ClassFile
- // parse constant pool to find matching method refs
- // parse each method (caller)
- // - visit and find method references matching the given method name
- pool.submit(getTask(cf));
- }
- } catch (IOException x) {
- throw new UncheckedIOException(x);
- } catch (ConstantPoolException x) {
- throw new RuntimeException(x);
- }
- }
-
private static final String CALLER_SENSITIVE_ANNOTATION
= "Ljdk/internal/reflect/CallerSensitive;";
private static boolean isCallerSensitive(Method m, ConstantPool cp)
throws ConstantPoolException
@@ -200,15 +177,28 @@
return m.access_flags.is(AccessFlags.ACC_STATIC) ||
m.access_flags.is(AccessFlags.ACC_FINAL) ||
cf.access_flags.is(AccessFlags.ACC_FINAL);
}
- private final List<FutureTask<Void>> tasks = new ArrayList<FutureTask<Void>>();
- private FutureTask<Void> getTask(final ClassFile cf) {
- FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
+ private final List<FutureTask<Void>> tasks = new ArrayList<>();
+
+ /*
+ * Each task parses the class file of the given path.
+ * - parse constant pool to find matching method refs
+ * - parse each method (caller)
+ * - visit and find method references matching the given method name
+ */
+ private FutureTask<Void> getTask(Path p) {
+ FutureTask<Void> task = new FutureTask<>(new Callable<>() {
public Void call() throws Exception {
- finder.parse(cf);
+ try (InputStream is = Files.newInputStream(p)) {
+ finder.parse(ClassFile.read(is));
+ } catch (IOException x) {
+ throw new UncheckedIOException(x);
+ } catch (ConstantPoolException x) {
+ throw new RuntimeException(x);
+ }
return null;
}
});
tasks.add(task);
return task;
@@ -219,33 +209,29 @@
t.get();
}
if (tasks.isEmpty()) {
throw new RuntimeException("No classes found, or specified.");
}
+ pool.shutdown();
System.out.println("Parsed " + tasks.size() + " classfiles");
}
static Stream<Path> getPlatformClasses() throws IOException {
Path home = Paths.get(System.getProperty("java.home"));
// Either an exploded build or an image.
File classes = home.resolve("modules").toFile();
- if (classes.isDirectory()) {
- return Stream.of(classes.toPath());
- } else {
- return jrtPaths();
- }
- }
-
- static Stream<Path> jrtPaths() {
- FileSystem jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
- Path root = jrt.getPath("/");
+ Path root = classes.isDirectory()
+ ? classes.toPath()
+ : FileSystems.getFileSystem(URI.create("jrt:/"))
+ .getPath("/");
try {
return Files.walk(root)
.filter(p -> p.getNameCount() > 1)
- .filter(p -> p.toString().endsWith(".class"));
+ .filter(p -> p.toString().endsWith(".class") &&
+ !p.toString().equals("module-info.class"));
} catch (IOException x) {
throw new UncheckedIOException(x);
}
}
}
< prev index next >