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

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