/** Checks all of the methods in gen for consistency */
  public static void checkMgens(final ClassGen gen) {

    if (skip_checks) return;

    Method[] methods = gen.getMethods();
    for (int i = 0; i < methods.length; i++) {
      Method method = methods[i];
      // System.out.println ("Checking method " + method + " in class "
      // + gen.getClassName());
      checkMgen(new MethodGen(method, gen.getClassName(), gen.getConstantPool()));
    }

    if (false) {
      Throwable t = new Throwable();
      t.fillInStackTrace();
      StackTraceElement[] ste = t.getStackTrace();
      StackTraceElement caller = ste[1];
      System.out.printf(
          "%s.%s (%s line %d)",
          caller.getClassName(),
          caller.getMethodName(),
          caller.getFileName(),
          caller.getLineNumber());
      for (int ii = 2; ii < ste.length; ii++)
        System.out.printf(" [%s line %d]", ste[ii].getFileName(), ste[ii].getLineNumber());
      System.out.printf("\n");
      dump_methods(gen);
    }
  }
Beispiel #2
0
 private void printScriptFrames(StringBuilder sb, Realm realm, Throwable e, int level) {
   final String indent = Strings.repeat('\t', level);
   final int maxDepth = options.stacktraceDepth;
   int depth = 0;
   StackTraceElement[] stackTrace = StackTraces.scriptStackTrace(e);
   for (; depth < Math.min(stackTrace.length, maxDepth); ++depth) {
     StackTraceElement element = stackTrace[depth];
     String methodName = element.getMethodName();
     String fileName = element.getFileName();
     int lineNumber = element.getLineNumber();
     sb.append(indent)
         .append("at ")
         .append(methodName)
         .append(" (")
         .append(fileName)
         .append(':')
         .append(lineNumber)
         .append(")\n");
   }
   if (depth < stackTrace.length) {
     int skipped = stackTrace.length - depth;
     sb.append("\t.. ").append(skipped).append(" frames omitted\n");
   }
   if (e.getSuppressed().length > 0 && level == 1) {
     Throwable suppressed = e.getSuppressed()[0];
     String message;
     if (suppressed instanceof ScriptException) {
       message = ((ScriptException) suppressed).getMessage(realm.defaultContext());
     } else {
       message = Objects.toString(suppressed.getMessage(), suppressed.getClass().getSimpleName());
     }
     sb.append(indent).append(formatMessage("suppressed_exception", message)).append('\n');
     printScriptFrames(sb, realm, suppressed, level + 1);
   }
 }
Beispiel #3
0
  /**
   * Prints out the given debug message to System.out, prefixed by the calling class name, method
   * and line number, appending the stacktrace of the given exception.
   */
  public static void out(final String _debug_msg, final Throwable _exception) {
    String header = "DEBUG::";
    header = header + new Date(SystemTime.getCurrentTime()).toString() + "::";
    String className;
    String methodName;
    int lineNumber;
    String trace_trace_tail = null;

    try {
      throw new Exception();
    } catch (Exception e) {
      StackTraceElement[] st = e.getStackTrace();

      StackTraceElement first_line = st[2];
      className = first_line.getClassName() + "::";
      methodName = first_line.getMethodName() + "::";
      lineNumber = first_line.getLineNumber();

      trace_trace_tail = getCompressedStackTrace(e, 3, 200, false);
    }

    diagLoggerLogAndOut(header + className + (methodName) + lineNumber + ":", true);
    if (_debug_msg.length() > 0) {
      diagLoggerLogAndOut("  " + _debug_msg, true);
    }
    if (trace_trace_tail != null) {
      diagLoggerLogAndOut("    " + trace_trace_tail, true);
    }
    if (_exception != null) {
      diagLoggerLogAndOut(_exception);
    }
  }
    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;
      }
    }
  // [JACKSON-888]
  public void testStackTraceElement() throws Exception {
    StackTraceElement elem = null;
    try {
      throw new IllegalStateException();
    } catch (Exception e) {
      elem = e.getStackTrace()[0];
    }
    String json = MAPPER.writeValueAsString(elem);
    StackTraceElement back = MAPPER.readValue(json, StackTraceElement.class);

    assertEquals("testStackTraceElement", back.getMethodName());
    assertEquals(elem.getLineNumber(), back.getLineNumber());
    assertEquals(elem.getClassName(), back.getClassName());
    assertEquals(elem.isNativeMethod(), back.isNativeMethod());
    assertTrue(back.getClassName().endsWith("TestJdkTypes"));
    assertFalse(back.isNativeMethod());
  }
 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() + ")");
   }
 }
Beispiel #7
0
 private static void printElementInfo(OutputStream stream, StackTraceElement element) {
   print(stream, element.getClassName());
   print(stream, ":");
   print(stream, element.getMethodName());
   print(stream, " ");
   print(stream, "(");
   print(stream, element.getFileName());
   print(stream, ":");
   print(stream, element.getLineNumber());
   print(stream, ")");
   printNewLine(stream);
 }
Beispiel #8
0
  public static void printStackTrace(Throwable e, Object context) {
    String header = "DEBUG::";
    header = header + new Date(SystemTime.getCurrentTime()).toString() + "::";
    String className = "?::";
    String methodName = "?::";
    int lineNumber = -1;

    try {
      throw new Exception();
    } catch (Exception f) {
      StackTraceElement[] st = f.getStackTrace();

      for (int i = 1; i < st.length; i++) {
        StackTraceElement first_line = st[i];
        className = first_line.getClassName() + "::";
        methodName = first_line.getMethodName() + "::";
        lineNumber = first_line.getLineNumber();

        // skip stuff generated by the logger

        if (className.indexOf(".logging.") != -1 || className.endsWith(".Debug::")) {

          continue;
        }

        break;
      }
    }

    diagLoggerLogAndOut(header + className + (methodName) + lineNumber + ":", true);

    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos));

      if (context != null) {
        pw.print("  ");
        pw.println(context);
      }
      pw.print("  ");
      e.printStackTrace(pw);

      pw.close();

      String stack = baos.toString();

      diagLoggerLogAndOut(stack, true);
    } catch (Throwable ignore) {

      e.printStackTrace();
    }
  }
Beispiel #9
0
 protected String getTitle(Throwable t) {
   String message = t.getMessage();
   String tit = message;
   if (tit == null) {
     StackTraceElement el = t.getStackTrace()[0];
     tit =
         t.getClass().getName().substring(t.getClass().getPackage().getName().length() + 1)
             + " "
             + el.getFileName()
             + ":"
             + el.getLineNumber();
   }
   return tit;
 }
 @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;
 }
Beispiel #11
0
 private static void printThrowable(OutputStream stream, Throwable throwable) {
   printBytes(stream, ("Exception " + throwable.toString()).getBytes());
   printNewLine(stream);
   StackTraceElement[] stackTraceElements = throwable.getStackTrace();
   for (int i = stackTraceElements.length - 1; i >= 0; i--) {
     StackTraceElement element = stackTraceElements[i];
     printBytes(stream, element.getFileName().getBytes());
     printBytes(stream, ":".getBytes());
     printBytes(stream, String.valueOf(element.getLineNumber()).getBytes());
     printNewLine(stream);
   }
   if (throwable.getCause() != null) {
     printBytes(stream, "Caused by:\n".getBytes());
     printThrowable(stream, throwable.getCause());
   }
 }
 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() + ")");
     }
   }
 }
Beispiel #13
0
 static StackTraceElement[] truncate(Throwable e, int depth) {
   StackTraceElement[] stackTrace = e.getStackTrace();
   if (stackTrace.length > depth) {
     int omitted = stackTrace.length - depth;
     stackTrace = Arrays.copyOf(stackTrace, depth + 1);
     stackTrace[depth] = new StackTraceElement("..", "", "Frames omitted", omitted);
   }
   if (REMOVE_PACKAGE_NAME) {
     for (int i = 0; i < stackTrace.length; ++i) {
       StackTraceElement element = stackTrace[i];
       String className = element.getClassName();
       if (className.startsWith(PACKAGE_NAME)) {
         stackTrace[i] =
             new StackTraceElement(
                 className.substring(PACKAGE_NAME.length()),
                 element.getMethodName(),
                 element.getFileName(),
                 element.getLineNumber());
       }
     }
   }
   return stackTrace;
 }