Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/reflect/Reflection/GetCallerClassTest.java
38839 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import boot.GetCallerClass;24import java.lang.reflect.*;25import sun.reflect.CallerSensitive;26import sun.reflect.Reflection;2728public class GetCallerClassTest {29private final GetCallerClass gcc; // boot.GetCallerClass is in bootclasspath30GetCallerClassTest() {31this.gcc = new GetCallerClass();32}3334public static void main(String[] args) throws Exception {35GetCallerClassTest gcct = new GetCallerClassTest();36// ensure methods are annotated with @CallerSensitive37ensureAnnotationPresent(boot.GetCallerClass.class, "getCallerLoader", true);38ensureAnnotationPresent(GetCallerClassTest.class, "testNonSystemMethod", false);39// call Reflection.getCallerClass from bootclasspath with and without @CS40gcct.testCallerSensitiveMethods();41// call Reflection.getCallerClass from classpath with @CS42gcct.testNonSystemMethod();43}4445private static void ensureAnnotationPresent(Class<?> c, String name, boolean cs)46throws NoSuchMethodException47{48Method m = c.getDeclaredMethod(name);49if (!m.isAnnotationPresent(CallerSensitive.class)) {50throw new RuntimeException("@CallerSensitive not present in method " + m);51}52if (Reflection.isCallerSensitive(m) != cs) {53throw new RuntimeException("Unexpected: isCallerSensitive returns " +54Reflection.isCallerSensitive(m));55}56}5758private void testCallerSensitiveMethods() {59try {60ClassLoader cl = gcc.getCallerLoader();61if (cl != GetCallerClassTest.class.getClassLoader()) {62throw new RuntimeException("mismatched class loader");63}64gcc.missingCallerSensitiveAnnotation();65throw new RuntimeException("getCallerLoader not marked with @CallerSensitive");66} catch (InternalError e) {67StackTraceElement[] stackTrace = e.getStackTrace();68checkStackTrace(stackTrace, e);69if (!stackTrace[1].getClassName().equals(GetCallerClass.class.getName()) ||70!stackTrace[1].getMethodName().equals("missingCallerSensitiveAnnotation")) {71throw new RuntimeException("Unexpected error: " + e.getMessage(), e);72}73if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||74!stackTrace[2].getMethodName().equals("testCallerSensitiveMethods")) {75throw new RuntimeException("Unexpected error: " + e.getMessage(), e);76}77System.out.println("Expected error: " + e.getMessage());78}79}8081@CallerSensitive82private void testNonSystemMethod() {83try {84Class<?> c = Reflection.getCallerClass();85throw new RuntimeException("@CallerSensitive testNonSystemMethods not supported");86} catch (InternalError e) {87StackTraceElement[] stackTrace = e.getStackTrace();88checkStackTrace(stackTrace, e);89if (!stackTrace[1].getClassName().equals(GetCallerClassTest.class.getName()) ||90!stackTrace[1].getMethodName().equals("testNonSystemMethod")) {91throw new RuntimeException("Unexpected error: " + e.getMessage(), e);92}93if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||94!stackTrace[2].getMethodName().equals("main")) {95throw new RuntimeException("Unexpected error: " + e.getMessage(), e);96}97System.out.println("Expected error: " + e.getMessage());98}99}100101private void checkStackTrace(StackTraceElement[] stackTrace, Error e) {102if (stackTrace.length < 3) {103throw new RuntimeException("Unexpected error: " + e.getMessage(), e);104}105106if (!stackTrace[0].getClassName().equals("sun.reflect.Reflection") ||107!stackTrace[0].getMethodName().equals("getCallerClass")) {108throw new RuntimeException("Unexpected error: " + e.getMessage(), e);109}110111}112}113114115