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

https://github.com/openjdk/jdk · Java · 124 lines · 79 code · 10 blank · 35 comment · 11 complexity · 6c4b525d6347bc9e3bd1b793c9e4fe7e MD5 · raw file

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