jdk/test/sun/reflect/CallerSensitive/CallerSensitiveFinder.java
changeset 37377 bd3b98a11a14
parent 37376 5daa5fbad3ce
parent 37374 ee1b8619eddb
child 37380 55c2549714af
equal deleted inserted replaced
37376:5daa5fbad3ce 37377:bd3b98a11a14
     1 /*
       
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import com.sun.tools.classfile.*;
       
    25 import com.sun.tools.jdeps.ClassFileReader;
       
    26 import static com.sun.tools.classfile.ConstantPool.*;
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.io.UncheckedIOException;
       
    30 import java.net.URI;
       
    31 import java.nio.file.FileSystem;
       
    32 import java.nio.file.FileSystems;
       
    33 import java.nio.file.Files;
       
    34 import java.nio.file.Path;
       
    35 import java.nio.file.Paths;
       
    36 import java.util.ArrayList;
       
    37 import java.util.Collections;
       
    38 import java.util.List;
       
    39 import java.util.concurrent.Callable;
       
    40 import java.util.concurrent.ExecutionException;
       
    41 import java.util.concurrent.ExecutorService;
       
    42 import java.util.concurrent.Executors;
       
    43 import java.util.concurrent.FutureTask;
       
    44 import java.util.stream.Stream;
       
    45 
       
    46 /*
       
    47  * @test
       
    48  * @bug 8010117
       
    49  * @summary Verify if CallerSensitive methods are annotated with
       
    50  *          sun.reflect.CallerSensitive annotation
       
    51  * @modules jdk.jdeps/com.sun.tools.classfile jdk.jdeps/com.sun.tools.jdeps
       
    52  * @build CallerSensitiveFinder
       
    53  * @run main/othervm/timeout=900 CallerSensitiveFinder
       
    54  */
       
    55 public class CallerSensitiveFinder {
       
    56     private static int numThreads = 3;
       
    57     private static boolean verbose = false;
       
    58     private final ExecutorService pool;
       
    59 
       
    60     public static void main(String[] args) throws Exception {
       
    61         Stream<Path> classes = null;
       
    62         String testclasses = System.getProperty("test.classes", ".");
       
    63         int i = 0;
       
    64         while (i < args.length) {
       
    65             String arg = args[i++];
       
    66             if (arg.equals("-v")) {
       
    67                 verbose = true;
       
    68             } else {
       
    69                 Path p = Paths.get(testclasses, arg);
       
    70                 if (!p.toFile().exists()) {
       
    71                     throw new IllegalArgumentException(arg + " does not exist");
       
    72                 }
       
    73                 classes = Stream.of(p);
       
    74             }
       
    75         }
       
    76 
       
    77         if (classes == null) {
       
    78             classes = getPlatformClasses();
       
    79         }
       
    80 
       
    81         CallerSensitiveFinder csfinder = new CallerSensitiveFinder();
       
    82         List<String> errors = csfinder.run(classes);
       
    83 
       
    84         if (!errors.isEmpty()) {
       
    85             throw new RuntimeException(errors.size() +
       
    86                     " caller-sensitive methods are missing @CallerSensitive annotation");
       
    87         }
       
    88     }
       
    89 
       
    90     private final List<String> csMethodsMissingAnnotation =
       
    91             Collections.synchronizedList(new ArrayList<>());
       
    92     private final ReferenceFinder finder;
       
    93     public CallerSensitiveFinder() {
       
    94         this.finder = new ReferenceFinder(getFilter(), getVisitor());
       
    95         pool = Executors.newFixedThreadPool(numThreads);
       
    96 
       
    97     }
       
    98 
       
    99     private ReferenceFinder.Filter getFilter() {
       
   100         final String classname = "sun/reflect/Reflection";
       
   101         final String method = "getCallerClass";
       
   102         return new ReferenceFinder.Filter() {
       
   103             public boolean accept(ConstantPool cpool, CPRefInfo cpref) {
       
   104                 try {
       
   105                     CONSTANT_NameAndType_info nat = cpref.getNameAndTypeInfo();
       
   106                     return cpref.getClassName().equals(classname) && nat.getName().equals(method);
       
   107                 } catch (ConstantPoolException ex) {
       
   108                     throw new RuntimeException(ex);
       
   109                 }
       
   110             }
       
   111         };
       
   112     }
       
   113 
       
   114     private ReferenceFinder.Visitor getVisitor() {
       
   115         return new ReferenceFinder.Visitor() {
       
   116             public void visit(ClassFile cf, Method m,  List<CPRefInfo> refs) {
       
   117                 try {
       
   118                     String name = String.format("%s#%s %s", cf.getName(),
       
   119                                                 m.getName(cf.constant_pool),
       
   120                                                 m.descriptor.getValue(cf.constant_pool));
       
   121                     if (!CallerSensitiveFinder.isCallerSensitive(m, cf.constant_pool)) {
       
   122                         csMethodsMissingAnnotation.add(name);
       
   123                         System.err.println("Missing @CallerSensitive: " + name);
       
   124                     } else {
       
   125                         if (verbose) {
       
   126                             System.out.format("@CS  %s%n", name);
       
   127                         }
       
   128                     }
       
   129                 } catch (ConstantPoolException ex) {
       
   130                     throw new RuntimeException(ex);
       
   131                 }
       
   132             }
       
   133         };
       
   134     }
       
   135 
       
   136     public List<String> run(Stream<Path> classes)throws IOException, InterruptedException,
       
   137             ExecutionException, ConstantPoolException
       
   138     {
       
   139         classes.forEach(this::processPath);
       
   140         waitForCompletion();
       
   141         pool.shutdown();
       
   142         return csMethodsMissingAnnotation;
       
   143     }
       
   144 
       
   145     void processPath(Path path) {
       
   146         try {
       
   147             ClassFileReader reader = ClassFileReader.newInstance(path);
       
   148             for (ClassFile cf : reader.getClassFiles()) {
       
   149                 String classFileName = cf.getName();
       
   150                 // for each ClassFile
       
   151                 //    parse constant pool to find matching method refs
       
   152                 //      parse each method (caller)
       
   153                 //      - visit and find method references matching the given method name
       
   154                 pool.submit(getTask(cf));
       
   155             }
       
   156         } catch (IOException x) {
       
   157             throw new UncheckedIOException(x);
       
   158         } catch (ConstantPoolException x) {
       
   159             throw new RuntimeException(x);
       
   160         }
       
   161     }
       
   162 
       
   163     private static final String CALLER_SENSITIVE_ANNOTATION = "Lsun/reflect/CallerSensitive;";
       
   164     private static boolean isCallerSensitive(Method m, ConstantPool cp)
       
   165             throws ConstantPoolException
       
   166     {
       
   167         RuntimeAnnotations_attribute attr =
       
   168             (RuntimeAnnotations_attribute)m.attributes.get(Attribute.RuntimeVisibleAnnotations);
       
   169         int index = 0;
       
   170         if (attr != null) {
       
   171             for (int i = 0; i < attr.annotations.length; i++) {
       
   172                 Annotation ann = attr.annotations[i];
       
   173                 String annType = cp.getUTF8Value(ann.type_index);
       
   174                 if (CALLER_SENSITIVE_ANNOTATION.equals(annType)) {
       
   175                     return true;
       
   176                 }
       
   177             }
       
   178         }
       
   179         return false;
       
   180     }
       
   181 
       
   182     private final List<FutureTask<Void>> tasks = new ArrayList<FutureTask<Void>>();
       
   183     private FutureTask<Void> getTask(final ClassFile cf) {
       
   184         FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
       
   185             public Void call() throws Exception {
       
   186                 finder.parse(cf);
       
   187                 return null;
       
   188             }
       
   189         });
       
   190         tasks.add(task);
       
   191         return task;
       
   192     }
       
   193 
       
   194     private void waitForCompletion() throws InterruptedException, ExecutionException {
       
   195         for (FutureTask<Void> t : tasks) {
       
   196             t.get();
       
   197         }
       
   198         if (tasks.isEmpty()) {
       
   199             throw new RuntimeException("No classes found, or specified.");
       
   200         }
       
   201         System.out.println("Parsed " + tasks.size() + " classfiles");
       
   202     }
       
   203 
       
   204     static Stream<Path> getPlatformClasses() throws IOException {
       
   205         Path home = Paths.get(System.getProperty("java.home"));
       
   206 
       
   207         // Either an exploded build or an image.
       
   208         File classes = home.resolve("modules").toFile();
       
   209         if (classes.isDirectory()) {
       
   210             return Stream.of(classes.toPath());
       
   211         } else {
       
   212             return jrtPaths();
       
   213         }
       
   214     }
       
   215 
       
   216     static Stream<Path> jrtPaths() {
       
   217         FileSystem jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
       
   218         Path root = jrt.getPath("/");
       
   219 
       
   220         try {
       
   221             return Files.walk(root)
       
   222                     .filter(p -> p.getNameCount() > 1)
       
   223                     .filter(p -> p.toString().endsWith(".class"));
       
   224         } catch (IOException x) {
       
   225             throw new UncheckedIOException(x);
       
   226         }
       
   227     }
       
   228 }