/jdk/test/sun/reflect/GetCallerClass.java

https://github.com/cccxxxcccxxx/jdk7u-hotspot · Java · 105 lines · 63 code · 6 blank · 36 comment · 19 complexity · d5b3c88af2708b274b8c7b66982bc30d 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 8016814 8014925 8021946
  26. * @summary Test sun.reflect.Reflection.getCallerClass(int) disabled by default
  27. * @compile -XDignore.symbol.file GetCallerClass.java
  28. * @run main/othervm GetCallerClass
  29. * @run main/othervm -Djdk.reflect.allowGetCallerClass GetCallerClass
  30. * @run main/othervm -Djdk.reflect.allowGetCallerClass=true GetCallerClass
  31. * @run main/othervm -Djdk.reflect.allowGetCallerClass=false GetCallerClass
  32. */
  33. public class GetCallerClass {
  34. public static void main(String[] args) throws Exception {
  35. String s = System.getProperty("jdk.reflect.allowGetCallerClass");
  36. boolean allowed;
  37. if (s == null || s.equals("") || s.equals("true")) {
  38. allowed = true;
  39. } else if (s.equals("false")) {
  40. allowed = false;
  41. } else {
  42. throw new RuntimeException("Unsupported test setting");
  43. }
  44. try {
  45. Class<?> c = Test.test();
  46. if (!allowed) {
  47. throw new RuntimeException("Reflection.getCallerClass should not be allowed");
  48. }
  49. Class<?> caller = Test.caller();
  50. if (c != GetCallerClass.class || caller != c) {
  51. throw new RuntimeException("Incorrect caller: " + c);
  52. }
  53. Test.selfTest();
  54. } catch (UnsupportedOperationException e) {
  55. if (allowed) throw e;
  56. }
  57. }
  58. @sun.reflect.CallerSensitive
  59. public Class<?> getCallerClass() {
  60. // 0: Reflection 1: getCallerClass 2: Test.test 3: main
  61. return sun.reflect.Reflection.getCallerClass(3);
  62. }
  63. static class Test {
  64. // Returns the caller of this method
  65. public static Class<?> test() {
  66. return new GetCallerClass().getCallerClass();
  67. }
  68. @sun.reflect.CallerSensitive
  69. public static Class<?> caller() {
  70. return sun.reflect.Reflection.getCallerClass();
  71. }
  72. @sun.reflect.CallerSensitive
  73. public static void selfTest() {
  74. // 0: Reflection 1: Test.selfTest
  75. Class<?> c = sun.reflect.Reflection.getCallerClass(1);
  76. if (c != Test.class || caller() != c) {
  77. throw new RuntimeException("Incorrect caller: " + c);
  78. }
  79. Inner1.deep();
  80. }
  81. static class Inner1 {
  82. static void deep() {
  83. deeper();
  84. }
  85. static void deeper() {
  86. Inner2.deepest();
  87. }
  88. static class Inner2 {
  89. static void deepest() {
  90. // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest
  91. Class<?> c = sun.reflect.Reflection.getCallerClass(4);
  92. if (c != Test.class) {
  93. throw new RuntimeException("Incorrect caller: " + c);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }