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

https://github.com/openjdk/valhalla · Java · 91 lines · 56 code · 13 blank · 22 comment · 0 complexity · 800ac360b38205546eba991f2da3e763 MD5 · raw file

  1. /*
  2. * Copyright (c) 2013, 2021, 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. package boot;
  24. import static java.lang.System.out;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.Stream;
  28. public class GetCallerClass {
  29. public Class<?> missingCallerSensitiveAnnotation() {
  30. return jdk.internal.reflect.Reflection.getCallerClass();
  31. }
  32. @jdk.internal.reflect.CallerSensitive
  33. public Class<?> getCallerClass() {
  34. var caller = jdk.internal.reflect.Reflection.getCallerClass();
  35. out.println("caller: " + caller);
  36. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  37. return caller;
  38. }
  39. private Class<?> getCallerClass(Class<?> caller) {
  40. out.println("caller: " + caller);
  41. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  42. return caller;
  43. }
  44. @jdk.internal.reflect.CallerSensitive
  45. public static Class<?> getCallerClassStatic() {
  46. var caller = jdk.internal.reflect.Reflection.getCallerClass();
  47. out.println("caller: " + caller);
  48. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  49. return caller;
  50. }
  51. private static Class<?> getCallerClassStatic(Class<?> caller) {
  52. out.println("caller: " + caller);
  53. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  54. return caller;
  55. }
  56. @jdk.internal.reflect.CallerSensitive
  57. public Class<?> getCallerClassNoAlt() {
  58. var caller = jdk.internal.reflect.Reflection.getCallerClass();
  59. out.println("caller: " + caller);
  60. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  61. return caller;
  62. }
  63. @jdk.internal.reflect.CallerSensitive
  64. public static Class<?> getCallerClassStaticNoAlt() {
  65. var caller = jdk.internal.reflect.Reflection.getCallerClass();
  66. out.println("caller: " + caller);
  67. out.println(StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES).walk(toStackTrace()));
  68. return caller;
  69. }
  70. private static Function<Stream<StackWalker.StackFrame>, String> toStackTrace() {
  71. return frames -> frames
  72. .takeWhile(
  73. frame -> !frame.getClassName().equals("GetCallerClassTest") ||
  74. !frame.getMethodName().equals("main"))
  75. .map(Object::toString)
  76. .collect(Collectors.joining("\n ", " ", "\n"));
  77. }
  78. }