< prev index next >

test/jdk/jdk/internal/reflect/CallerSensitive/CallerSensitiveFinder.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2013, 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. --- 1,7 ---- /* ! * Copyright (c) 2013, 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,44 **** * 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.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; - import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import java.util.stream.Stream; --- 20,43 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ import com.sun.tools.classfile.*; 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.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; + import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import java.util.stream.Stream;
*** 85,96 **** throw new RuntimeException(errors.size() + " caller-sensitive methods are missing @CallerSensitive annotation"); } } ! private final List<String> csMethodsMissingAnnotation = ! Collections.synchronizedList(new ArrayList<>()); private final ReferenceFinder finder; public CallerSensitiveFinder() { this.finder = new ReferenceFinder(getFilter(), getVisitor()); pool = Executors.newFixedThreadPool(numThreads); --- 84,94 ---- throw new RuntimeException(errors.size() + " caller-sensitive methods are missing @CallerSensitive annotation"); } } ! private final List<String> csMethodsMissingAnnotation = new CopyOnWriteArrayList<>(); private final ReferenceFinder finder; public CallerSensitiveFinder() { this.finder = new ReferenceFinder(getFilter(), getVisitor()); pool = Executors.newFixedThreadPool(numThreads);
*** 140,183 **** } public List<String> run(Stream<Path> classes)throws IOException, InterruptedException, ExecutionException, ConstantPoolException { ! classes.forEach(this::processPath); waitForCompletion(); - pool.shutdown(); return csMethodsMissingAnnotation; } - 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 { RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)m.attributes.get(Attribute.RuntimeVisibleAnnotations); - int index = 0; if (attr != null) { for (int i = 0; i < attr.annotations.length; i++) { Annotation ann = attr.annotations[i]; String annType = cp.getUTF8Value(ann.type_index); if (CALLER_SENSITIVE_ANNOTATION.equals(annType)) { --- 138,158 ---- } public List<String> run(Stream<Path> classes)throws IOException, InterruptedException, ExecutionException, ConstantPoolException { ! classes.forEach(p -> pool.submit(getTask(p))); waitForCompletion(); return csMethodsMissingAnnotation; } private static final String CALLER_SENSITIVE_ANNOTATION = "Ljdk/internal/reflect/CallerSensitive;"; private static boolean isCallerSensitive(Method m, ConstantPool cp) throws ConstantPoolException { RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)m.attributes.get(Attribute.RuntimeVisibleAnnotations); if (attr != null) { for (int i = 0; i < attr.annotations.length; i++) { Annotation ann = attr.annotations[i]; String annType = cp.getUTF8Value(ann.type_index); if (CALLER_SENSITIVE_ANNOTATION.equals(annType)) {
*** 186,200 **** } } return false; } ! 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>() { public Void call() throws Exception { ! finder.parse(cf); return null; } }); tasks.add(task); return task; --- 161,188 ---- } } return false; } ! 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 { ! 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;
*** 205,237 **** t.get(); } if (tasks.isEmpty()) { throw new RuntimeException("No classes found, or specified."); } 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("/"); try { return Files.walk(root) .filter(p -> p.getNameCount() > 1) ! .filter(p -> p.toString().endsWith(".class")); } catch (IOException x) { throw new UncheckedIOException(x); } } } --- 193,221 ---- 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(); ! 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") && ! !p.toString().equals("module-info.class")); } catch (IOException x) { throw new UncheckedIOException(x); } } }
< prev index next >