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

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