/test/jdk/java/lang/StackWalker/CallerSensitiveMethod/src/java.base/java/util/CSM.java

https://github.com/openjdk/jdk · Java · 77 lines · 37 code · 9 blank · 31 comment · 0 complexity · f14b475b49cc794340531947f685a985 MD5 · raw file

  1. /*
  2. * Copyright (c) 2016, 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 static java.lang.StackWalker.Option.*;
  25. import java.lang.StackWalker.StackFrame;
  26. import java.util.stream.Collectors;
  27. import jdk.internal.reflect.CallerSensitive;
  28. import jdk.internal.reflect.Reflection;
  29. public class CSM {
  30. private static StackWalker walker =
  31. StackWalker.getInstance(EnumSet.of(RETAIN_CLASS_REFERENCE,
  32. SHOW_HIDDEN_FRAMES,
  33. SHOW_REFLECT_FRAMES));
  34. public static class Result {
  35. public final List<Class<?>> callers;
  36. public final List<StackWalker.StackFrame> frames;
  37. Result(List<Class<?>> callers,
  38. List<StackFrame> frames) {
  39. this.callers = callers;
  40. this.frames = frames;
  41. }
  42. }
  43. /**
  44. * Returns the caller of this caller-sensitive method returned by
  45. * by Reflection::getCallerClass.
  46. *
  47. * StackWalker::getCallerClass is expected to throw UOE
  48. */
  49. @CallerSensitive
  50. public static Class<?> caller() {
  51. Class<?> c1 = Reflection.getCallerClass();
  52. try {
  53. Class<?> c2 = walker.getCallerClass();
  54. throw new RuntimeException("Exception not thrown by StackWalker::getCallerClass");
  55. } catch (UnsupportedOperationException e) {}
  56. return c1;
  57. }
  58. /**
  59. * Returns the caller of this non-caller-sensitive method.
  60. */
  61. public static Result getCallerClass() {
  62. Class<?> caller = walker.getCallerClass();
  63. return new Result(List.of(caller), dump());
  64. }
  65. static List<StackFrame> dump() {
  66. return walker.walk(s -> s.collect(Collectors.toList()));
  67. }
  68. }