public static void printElement(Site s, int index) {
   if (s == null || index >= s.site().length) {
     ps.print("(null)");
   } else {
     StackTraceElement e = s.site()[index];
     ps.print(e.getMethodName() + " (" + e.getFileName() + ":" + e.getLineNumber() + ")");
   }
 }
  /**
   * Returns the class this method was called 'framesToSkip' frames up the caller hierarchy. JDK
   * used to have {@link sun.reflect.Reflection#getCallerClass(int)} until jdk 1.8 build 87. So we
   * have to resort to slow stack unwinding.
   *
   * <p>NOTE: <b>Extremely expensive! Please consider not using it. These aren't the droids you're
   * looking for!</b>
   */
  @SuppressWarnings("JavadocReference")
  @Nullable
  public static Class getCallerClass(int framesToSkip) {
    int frames = framesToSkip;

    StackTraceElement[] stackTrace = new Throwable().getStackTrace();
    String className = null;
    for (int i = 1; i <= frames; i++) {
      if (i >= stackTrace.length) {
        break;
      }
      StackTraceElement element = stackTrace[i];
      className = element.getClassName();
      if (className.equals("java.lang.reflect.Method")
          || className.equals("sun.reflect.NativeMethodAccessorImpl")
          || className.equals("sun.reflect.DelegatingMethodAccessorImpl")) {
        frames++;
        continue;
      }
      if (i == frames) {
        break;
      }
    }

    if (className == null) {
      // some plugins incorrectly expect not-null class too far up the caller chain,
      // so provide some not-null class for them
      className = ReflectionUtil.class.getName();
    }

    try {
      return Class.forName(className);
    } catch (ClassNotFoundException ignored) {
    }
    ClassLoader pluginClassLoader = PLUGIN_CLASS_LOADER_DETECTOR.fun(className);
    if (pluginClassLoader != null) {
      try {
        return Class.forName(className, false, pluginClassLoader);
      } catch (ClassNotFoundException ignored) {
      }
    }
    LOG.error(
        "Could not load class '"
            + className
            + "' using classLoader "
            + ReflectionUtil.class.getClassLoader()
            + "."
            + (stackTrace[1].getClassName().equals("com.intellij.openapi.util.IconLoader")
                ? " Use getIcon(String, Class) instead."
                : ""));
    return null;
  }
 @Override
 public int hashCode() {
   if (hashCode == 0) {
     int res = 1 + site.length * 17;
     int max = Math.max(site.length, 10);
     for (int i = offset; i < max; i++) {
       StackTraceElement e = site[i];
       int lineNumber = e.getLineNumber();
       res = res * 19 + 31 * lineNumber;
     }
     hashCode = res;
   }
   return hashCode;
 }
 public static void printElements(Site s, int index, int nelems) {
   if (s == null || index >= s.site().length) {
     ps.print("(null)");
   } else {
     for (int i = 0; i < nelems; i++) {
       int j = index + i;
       if (j >= s.site().length) {
         break;
       }
       if (i > 0) {
         ps.print(", ");
       }
       StackTraceElement e = s.site()[j];
       ps.print(e.getMethodName() + " (" + e.getFileName() + ":" + e.getLineNumber() + ")");
     }
   }
 }
    public static void printSite(Site s, StringBuilder out) {
      if (s == null) {
        out.append("(null)");
        return;
      }
      StackTraceElement[] st = s.site();

      int interestingSitesPrinted = 0;
      for (int i = s.offset; i < st.length; i++) {
        StackTraceElement e = st[i];
        String fileName = e.getFileName();
        if (THIS_FILE_NAME.equals(fileName)) {
          continue;
        }
        out.append(" " + e.getMethodName() + "(" + e.getFileName() + ":" + e.getLineNumber() + ")");
        interestingSitesPrinted++;
        if (interestingSitesPrinted <= SITES_TO_PRINT) {
          continue;
        }
        if (fileName == null || "View.java".equals(fileName)) {
          continue;
        }
        String methodName = e.getMethodName();
        if (skipMethods.contains(methodName)) {
          continue;
        }
        break;
      }
    }
示例#6
0
  protected static boolean wasCalledDuringClassLoading() {
    if (LOCK.isHeldByCurrentThread()) return true;
    LOCK.lock();

    try {
      StackTrace st = new StackTrace(new Throwable());
      int n = st.getDepth();

      for (int i = 3; i < n; i++) {
        StackTraceElement ste = st.getElement(i);

        if ("ClassLoader.java".equals(ste.getFileName())
            && "loadClass".equals(ste.getMethodName())) {
          return true;
        }
      }

      return false;
    } finally {
      LOCK.unlock();
    }
  }