/test/jdk/java/lang/invoke/callerSensitive/src/java.base/java/util/CSM.java

https://github.com/openjdk/valhalla · Java · 87 lines · 38 code · 10 blank · 39 comment · 0 complexity · 3a03ab9e6973374f5cacb7d9a07416eb MD5 · raw file

  1. /*
  2. * Copyright (c) 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 java.util;
  24. import java.lang.StackWalker.StackFrame;
  25. import java.util.stream.Stream;
  26. import jdk.internal.reflect.CallerSensitive;
  27. import jdk.internal.reflect.CallerSensitiveAdapter;
  28. import jdk.internal.reflect.Reflection;
  29. import static java.lang.StackWalker.Option.*;
  30. public class CSM {
  31. public final Class<?> caller;
  32. public final List<StackFrame> stackFrames;
  33. public final boolean adapter;
  34. CSM(Class<?> caller, boolean adapter) {
  35. this.caller = caller;
  36. StackWalker sw = StackWalker.getInstance(Set.of(RETAIN_CLASS_REFERENCE, SHOW_HIDDEN_FRAMES));
  37. this.stackFrames = sw.walk(Stream::toList);
  38. this.adapter = adapter;
  39. }
  40. /**
  41. * Returns the caller of this caller-sensitive method returned by
  42. * by Reflection::getCallerClass except if this method is called
  43. * via method handle.
  44. */
  45. @CallerSensitive
  46. public static CSM caller() {
  47. return caller(Reflection.getCallerClass());
  48. }
  49. /**
  50. * If caller() is invoked via method handle, this alternate method is
  51. * called instead. The caller class would be the lookup class.
  52. */
  53. @CallerSensitiveAdapter
  54. private static CSM caller(Class<?> caller) {
  55. return new CSM(caller, true);
  56. }
  57. /**
  58. * Returns the caller of this caller-sensitive method returned by
  59. * by Reflection::getCallerClass.
  60. */
  61. @CallerSensitive
  62. public static CSM callerNoAlternateImpl() {
  63. return new CSM(Reflection.getCallerClass(), false);
  64. }
  65. @CallerSensitive
  66. public static CSM caller(Object o1, Object o2, Object o3, Object o4) {
  67. return caller(o1, o2, o3, o4, Reflection.getCallerClass());
  68. }
  69. /**
  70. * If caller() is invoked via method handle, this alternate method is
  71. * called instead. The caller class would be the lookup class.
  72. */
  73. @CallerSensitiveAdapter
  74. private static CSM caller(Object o1, Object o2, Object o3, Object o4, Class<?> caller) {
  75. return new CSM(caller, true);
  76. }
  77. }