content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.core.xyz;
  
  import org.miaixz.bus.core.lang.caller.Caller;
  import org.miaixz.bus.core.lang.caller.StackTraceCaller;
  
  /**
   * 调用者。可以通过此类的方法获取调用者、多级调用者以及判断是否被调用
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public class CallerKit {
  
      private static final Caller INSTANCE;
  
      static {
          INSTANCE = tryCreateCaller();
      }
  
      /**
       * 获得调用者
       *
       * @return 调用者
       */
      public static Class<?> getCaller() {
          return INSTANCE.getCaller();
      }
  
      /**
       * 获得调用者的调用者
       *
       * @return 调用者的调用者
       */
      public static Class<?> getCallers() {
          return INSTANCE.getCallers();
      }
  
      /**
       * 获得调用者，指定第几级调用者 调用者层级关系：
       *
       * <pre>
       * 0 CallerKit
       * 1 调用CallerKit中方法的类
       * 2 调用者的调用者
       * ...
       * </pre>
       *
       * @param depth 层级。0表示本身，1表示调用CallerKit的类，2表示调用者的调用者，依次类推
       * @return 第几级调用者
       */
      public static Class<?> getCaller(final int depth) {
          return INSTANCE.getCaller(depth);
      }
  
      /**
       * 是否被指定类调用
       *
       * @param clazz 调用者类
       * @return 是否被调用
       */
      public static boolean isCalledBy(final Class<?> clazz) {
          return INSTANCE.isCalledBy(clazz);
      }
  
      /**
       * 获取调用此方法的方法名
       *
       * @param isFullName 是否返回全名，全名包括方法所在类的全路径名
       * @return 调用此方法的方法名
       */
      public static String getCallerMethodName(final boolean isFullName) {
          final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
          final String methodName = stackTraceElement.getMethodName();
          if (!isFullName) {
              return methodName;
          }
  
          return stackTraceElement.getClassName() + "." + methodName;
      }
  
      /**
       * 尝试创建{@link Caller}实现
       *
       * @return {@link Caller}实现
       */
      private static Caller tryCreateCaller() {
          return new StackTraceCaller();
      }
  
  }
  