/test/sun/reflect/Reflection/GetCallerClassWithDepth.java

https://bitbucket.org/screenconnect/openjdk8-jdk · Java · 92 lines · 50 code · 8 blank · 34 comment · 2 complexity · ea5b20917aa6751aa00479c99e10f4ef MD5 · raw file

  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. * @test
  25. * @bug 8025799
  26. * @summary sun.reflect.Reflection.getCallerClass(int)
  27. * @run main GetCallerClassWithDepth
  28. */
  29. public class GetCallerClassWithDepth {
  30. public static void main(String[] args) throws Exception {
  31. Class<?> c = Test.test();
  32. assertEquals(c, GetCallerClassWithDepth.class);
  33. Class<?> caller = Test.caller();
  34. assertEquals(caller, GetCallerClassWithDepth.class);
  35. Test.selfTest();
  36. try {
  37. sun.reflect.Reflection.getCallerClass(-1);
  38. throw new RuntimeException("getCallerClass(-1) should fail");
  39. } catch (Error e) {
  40. System.out.println("Expected: " + e.getMessage());
  41. }
  42. }
  43. public Class<?> getCallerClass() {
  44. // 0: Reflection 1: getCallerClass 2: Test.test 3: main
  45. return sun.reflect.Reflection.getCallerClass(3);
  46. }
  47. static void assertEquals(Class<?> c, Class<?> expected) {
  48. if (c != expected) {
  49. throw new RuntimeException("Incorrect caller: " + c);
  50. }
  51. }
  52. static class Test {
  53. // Returns the caller of this method
  54. public static Class<?> test() {
  55. return new GetCallerClassWithDepth().getCallerClass();
  56. }
  57. // Returns the caller of this method
  58. public static Class<?> caller() {
  59. // 0: Reflection 1: Test.caller 2: main
  60. return sun.reflect.Reflection.getCallerClass(2);
  61. }
  62. public static void selfTest() {
  63. // 0: Reflection 1: Test.selfTest
  64. Class<?> c = sun.reflect.Reflection.getCallerClass(1);
  65. assertEquals(c, Test.class);
  66. Inner1.deep();
  67. }
  68. static class Inner1 {
  69. static void deep() {
  70. deeper();
  71. }
  72. static void deeper() {
  73. Inner2.deepest();
  74. }
  75. static class Inner2 {
  76. static void deepest() {
  77. // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest
  78. Class<?> c = sun.reflect.Reflection.getCallerClass(4);
  79. assertEquals(c, Test.class);
  80. }
  81. }
  82. }
  83. }
  84. }