jdk/test/sun/reflect/Reflection/GetCallerClassWithDepth.java
author alexsch
Thu, 31 Jul 2014 14:28:10 +0400
changeset 26019 10a56d28f48d
parent 21284 e6dfe841a88f
child 30820 0d4717a011d3
permissions -rw-r--r--
8051838: [Findbugs]sun.awt.image.MultiResolutionCachedImage expose internal representation Reviewed-by: serb, pchelko
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21284
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     1
/*
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     4
 *
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     8
 *
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    13
 * accompanied this code).
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    14
 *
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    18
 *
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    21
 * questions.
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    22
 */
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    23
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    24
/*
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    25
 * @test
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    26
 * @bug 8025799
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    27
 * @summary sun.reflect.Reflection.getCallerClass(int)
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    28
 * @run main GetCallerClassWithDepth
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    29
 */
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    30
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    31
public class GetCallerClassWithDepth {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    32
    public static void main(String[] args) throws Exception {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    33
        Class<?> c = Test.test();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    34
        assertEquals(c, GetCallerClassWithDepth.class);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    35
        Class<?> caller = Test.caller();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    36
        assertEquals(caller, GetCallerClassWithDepth.class);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    37
        Test.selfTest();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    38
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    39
        try {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    40
            sun.reflect.Reflection.getCallerClass(-1);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    41
            throw new RuntimeException("getCallerClass(-1) should fail");
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    42
        } catch (Error e) {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    43
            System.out.println("Expected: " + e.getMessage());
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    44
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    45
    }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    46
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    47
    public Class<?> getCallerClass() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    48
        // 0: Reflection 1: getCallerClass 2: Test.test 3: main
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    49
        return sun.reflect.Reflection.getCallerClass(3);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    50
    }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    51
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    52
    static void assertEquals(Class<?> c, Class<?> expected) {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    53
        if (c != expected) {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    54
            throw new RuntimeException("Incorrect caller: " + c);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    55
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    56
    }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    57
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    58
    static class Test {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    59
        // Returns the caller of this method
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    60
        public static Class<?> test() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    61
            return new GetCallerClassWithDepth().getCallerClass();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    62
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    63
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    64
        // Returns the caller of this method
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    65
        public static Class<?> caller() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    66
            // 0: Reflection 1: Test.caller 2: main
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    67
            return sun.reflect.Reflection.getCallerClass(2);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    68
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    69
        public static void selfTest() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    70
            // 0: Reflection 1: Test.selfTest
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    71
            Class<?> c = sun.reflect.Reflection.getCallerClass(1);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    72
            assertEquals(c, Test.class);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    73
            Inner1.deep();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    74
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    75
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    76
        static class Inner1 {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    77
            static void deep() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    78
                 deeper();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    79
            }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    80
            static void deeper() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    81
                 Inner2.deepest();
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    82
            }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    83
            static class Inner2 {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    84
                static void deepest() {
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    85
                    // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    86
                    Class<?> c = sun.reflect.Reflection.getCallerClass(4);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    87
                    assertEquals(c, Test.class);
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    88
                }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    89
            }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    90
        }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    91
    }
e6dfe841a88f 8025799: Restore sun.reflect.Reflection.getCallerClass(int) until a replacement API is provided
mchung
parents:
diff changeset
    92
}