/src/test/jdk/jdk/internal/reflect/Reflection/GetCallerClassTest.java

https://github.com/corretto/corretto-11 · Java · 113 lines · 79 code · 9 blank · 25 comment · 11 complexity · e5f328f1a5955c38f593a0bd95504c1c 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. import boot.GetCallerClass;
  24. import java.lang.reflect.*;
  25. import jdk.internal.reflect.CallerSensitive;
  26. import jdk.internal.reflect.Reflection;
  27. public class GetCallerClassTest {
  28. private final GetCallerClass gcc; // boot.GetCallerClass is in bootclasspath
  29. GetCallerClassTest() {
  30. this.gcc = new GetCallerClass();
  31. }
  32. public static void main(String[] args) throws Exception {
  33. GetCallerClassTest gcct = new GetCallerClassTest();
  34. // ensure methods are annotated with @CallerSensitive
  35. ensureAnnotationPresent(boot.GetCallerClass.class, "getCallerLoader", true);
  36. ensureAnnotationPresent(GetCallerClassTest.class, "testNonSystemMethod", false);
  37. // call Reflection.getCallerClass from bootclasspath with and without @CS
  38. gcct.testCallerSensitiveMethods();
  39. // call Reflection.getCallerClass from classpath with @CS
  40. gcct.testNonSystemMethod();
  41. }
  42. private static void ensureAnnotationPresent(Class<?> c, String name, boolean cs)
  43. throws NoSuchMethodException
  44. {
  45. Method m = c.getDeclaredMethod(name);
  46. if (!m.isAnnotationPresent(CallerSensitive.class)) {
  47. throw new RuntimeException("@CallerSensitive not present in method " + m);
  48. }
  49. if (Reflection.isCallerSensitive(m) != cs) {
  50. throw new RuntimeException("Unexpected: isCallerSensitive returns " +
  51. Reflection.isCallerSensitive(m));
  52. }
  53. }
  54. private void testCallerSensitiveMethods() {
  55. try {
  56. ClassLoader cl = gcc.getCallerLoader();
  57. if (cl != GetCallerClassTest.class.getClassLoader()) {
  58. throw new RuntimeException("mismatched class loader");
  59. }
  60. gcc.missingCallerSensitiveAnnotation();
  61. throw new RuntimeException("getCallerLoader not marked with @CallerSensitive");
  62. } catch (InternalError e) {
  63. StackTraceElement[] stackTrace = e.getStackTrace();
  64. checkStackTrace(stackTrace, e);
  65. if (!stackTrace[1].getClassName().equals(GetCallerClass.class.getName()) ||
  66. !stackTrace[1].getMethodName().equals("missingCallerSensitiveAnnotation")) {
  67. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  68. }
  69. if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||
  70. !stackTrace[2].getMethodName().equals("testCallerSensitiveMethods")) {
  71. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  72. }
  73. System.out.println("Expected error: " + e.getMessage());
  74. }
  75. }
  76. @CallerSensitive
  77. private void testNonSystemMethod() {
  78. try {
  79. Class<?> c = Reflection.getCallerClass();
  80. throw new RuntimeException("@CallerSensitive testNonSystemMethods not supported");
  81. } catch (InternalError e) {
  82. StackTraceElement[] stackTrace = e.getStackTrace();
  83. checkStackTrace(stackTrace, e);
  84. if (!stackTrace[1].getClassName().equals(GetCallerClassTest.class.getName()) ||
  85. !stackTrace[1].getMethodName().equals("testNonSystemMethod")) {
  86. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  87. }
  88. if (!stackTrace[2].getClassName().equals(GetCallerClassTest.class.getName()) ||
  89. !stackTrace[2].getMethodName().equals("main")) {
  90. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  91. }
  92. System.out.println("Expected error: " + e.getMessage());
  93. }
  94. }
  95. private void checkStackTrace(StackTraceElement[] stackTrace, Error e) {
  96. if (stackTrace.length < 3) {
  97. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  98. }
  99. if (!stackTrace[0].getClassName().equals("jdk.internal.reflect.Reflection") ||
  100. !stackTrace[0].getMethodName().equals("getCallerClass")) {
  101. throw new RuntimeException("Unexpected error: " + e.getMessage(), e);
  102. }
  103. }
  104. }