jdk/test/sun/reflect/Reflection/GetCallerClassTest.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 boot.GetCallerClass;
       
    25 import java.lang.reflect.*;
       
    26 import sun.reflect.CallerSensitive;
       
    27 import sun.reflect.Reflection;
       
    28 
       
    29 public class GetCallerClassTest {
       
    30     private final GetCallerClass gcc;   // boot.GetCallerClass is in bootclasspath
       
    31     GetCallerClassTest() {
       
    32         this.gcc = new GetCallerClass();
       
    33     }
       
    34 
       
    35     public static void main(String[] args) throws Exception {
       
    36         GetCallerClassTest gcct = new GetCallerClassTest();
       
    37         // ensure methods are annotated with @CallerSensitive
       
    38         ensureAnnotationPresent(boot.GetCallerClass.class, "getCallerLoader", true);
       
    39         ensureAnnotationPresent(GetCallerClassTest.class, "testNonSystemMethod", false);
       
    40         // call Reflection.getCallerClass from bootclasspath with and without @CS
       
    41         gcct.testCallerSensitiveMethods();
       
    42         // call Reflection.getCallerClass from classpath with @CS
       
    43         gcct.testNonSystemMethod();
       
    44     }
       
    45 
       
    46     private static void ensureAnnotationPresent(Class<?> c, String name, boolean cs)
       
    47         throws NoSuchMethodException
       
    48     {
       
    49         Method m = c.getDeclaredMethod(name);
       
    50         if (!m.isAnnotationPresent(CallerSensitive.class)) {
       
    51             throw new RuntimeException("@CallerSensitive not present in method " + m);
       
    52         }
       
    53         if (Reflection.isCallerSensitive(m) != cs) {
       
    54             throw new RuntimeException("Unexpected: isCallerSensitive returns " +
       
    55                 Reflection.isCallerSensitive(m));
       
    56         }
       
    57     }
       
    58 
       
    59     private void testCallerSensitiveMethods() {
       
    60         try {
       
    61             ClassLoader cl = gcc.getCallerLoader();
       
    62             if (cl != GetCallerClassTest.class.getClassLoader()) {
       
    63                 throw new RuntimeException("mismatched class loader");
       
    64             }
       
    65             gcc.missingCallerSensitiveAnnotation();
       
    66             throw new RuntimeException("getCallerLoader not marked with @CallerSensitive");
       
    67         } catch (InternalError e) {
       
    68             StackTraceElement[] stackTrace = e.getStackTrace();
       
    69             checkStackTrace(stackTrace, e);
       
    70             if (!stackTrace[1].getClassName().equals(GetCallerClass.class.getName()) ||
       
    71                 !stackTrace[1].getMethodName().equals("missingCallerSensitiveAnnotation")) {
       
    72                 throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
    73             }
       
    74             if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||
       
    75                 !stackTrace[2].getMethodName().equals("testCallerSensitiveMethods")) {
       
    76                 throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
    77             }
       
    78             System.out.println("Expected error: " + e.getMessage());
       
    79         }
       
    80     }
       
    81 
       
    82     @CallerSensitive
       
    83     private void testNonSystemMethod() {
       
    84         try {
       
    85             Class<?> c = Reflection.getCallerClass();
       
    86             throw new RuntimeException("@CallerSensitive testNonSystemMethods not supported");
       
    87         } catch (InternalError e) {
       
    88             StackTraceElement[] stackTrace = e.getStackTrace();
       
    89             checkStackTrace(stackTrace, e);
       
    90             if (!stackTrace[1].getClassName().equals(GetCallerClassTest.class.getName()) ||
       
    91                 !stackTrace[1].getMethodName().equals("testNonSystemMethod")) {
       
    92                 throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
    93             }
       
    94             if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||
       
    95                 !stackTrace[2].getMethodName().equals("main")) {
       
    96                 throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
    97             }
       
    98             System.out.println("Expected error: " + e.getMessage());
       
    99         }
       
   100     }
       
   101 
       
   102     private void checkStackTrace(StackTraceElement[] stackTrace, Error e) {
       
   103         if (stackTrace.length < 3) {
       
   104             throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
   105         }
       
   106 
       
   107         if (!stackTrace[0].getClassName().equals("sun.reflect.Reflection") ||
       
   108             !stackTrace[0].getMethodName().equals("getCallerClass")) {
       
   109             throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
       
   110         }
       
   111 
       
   112     }
       
   113 }