Example usage for java.lang Throwable printStackTrace

List of usage examples for java.lang Throwable printStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable printStackTrace.

Prototype

public void printStackTrace(PrintWriter s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print writer.

Usage

From source file:com.nabla.wapp.server.general.Util.java

public static String formatInternalErrorDescription(final Throwable x) {
    if (x == null)
        return null;
    final Writer buffer = new StringWriter();
    final PrintWriter pw = new PrintWriter(buffer);
    x.printStackTrace(pw);
    return buffer.toString();
}

From source file:com.javadeobfuscator.deobfuscator.DeobfuscatorMain.java

public static int run(String[] args) {
    Options options = new Options();
    options.addOption("transformer", true, "A transformer to use");
    options.addOption("path", true, "A JAR to be placed in the classpath");
    options.addOption("input", true, "The input file");
    options.addOption("output", true, "The output file");
    //TODO://  w  w w. ja va  2s. c om
    // * keepClass
    // * custom normalizer name

    CommandLineParser parser = new DefaultParser();
    try {
        Deobfuscator deobfuscator = new Deobfuscator();
        CommandLine cmd = parser.parse(options, args);

        if (!cmd.hasOption("input")) {
            System.out.println("No input jar specified");
            return 3;
        }

        if (!cmd.hasOption("output")) {
            System.out.println("No output jar specified");
            return 4;
        }

        File input = new File(cmd.getOptionValue("input"));
        if (!input.exists()) {
            System.out.println("Input file does not exist");
            return 5;
        }

        File output = new File(cmd.getOptionValue("output"));
        if (output.exists()) {
            System.out.println("Warning! Output file already exists");
        }

        deobfuscator.withInput(input).withOutput(output);

        String[] transformers = cmd.getOptionValues("transformer");
        if (transformers == null || transformers.length == 0) {
            System.out.println("No transformers specified");
            return 2;
        }
        for (String transformer : transformers) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName("com.javadeobfuscator.deobfuscator.transformers." + transformer);
            } catch (ClassNotFoundException exception) {
                try {
                    clazz = Class.forName(transformer);
                } catch (ClassNotFoundException exception1) {
                    System.out.println("Could not locate transformer " + transformer);
                }
            }
            if (clazz != null) {
                if (Transformer.class.isAssignableFrom(clazz)) {
                    deobfuscator.withTransformer(clazz.asSubclass(Transformer.class));
                } else {
                    System.out.println(clazz.getCanonicalName()
                            + " does not extend com.javadeobfuscator.deobfuscator.transformers.Transformer");
                }
            }
        }

        String[] paths = cmd.getOptionValues("path");
        if (paths != null) {
            for (String path : paths) {
                File file = new File(path);
                if (file.exists()) {
                    deobfuscator.withClasspath(file);
                } else {
                    System.out.println("Could not find classpath file " + path);
                }
            }
        }

        try {
            deobfuscator.start();
            return 0;
        } catch (Deobfuscator.NoClassInPathException ex) {
            System.out.println("Could not locate a class file.");
            System.out.println("Have you added the necessary files to the -path argument?");
            System.out.println("The error was:");
            ex.printStackTrace(System.out);
            return -2;
        } catch (Throwable t) {
            System.out.println("Deobfuscation failed. Please open a ticket on GitHub");
            t.printStackTrace(System.out);
            return -1;
        }
    } catch (ParseException e) {
        return 1;
    }
}

From source file:com.partypoker.poker.engagement.utils.EngagementBundleToJSON.java

/**
 * Recursive function to write a value to JSON.
 * @param json the JSON serializer.//  ww  w  .j  a  v a  2  s. com
 * @param value the value to write in JSON.
 */
private static void convert(JSONStringer json, Object value) throws JSONException {
    /* Handle null */
    if (value == null)
        json.value(null);

    /* The function is recursive if it encounters a bundle */
    else if (value instanceof Bundle) {
        /* Cast bundle */
        Bundle bundle = (Bundle) value;

        /* Open object */
        json.object();

        /* Traverse bundle */
        for (String key : bundle.keySet()) {
            /* Write key */
            json.key(key);

            /* Recursive call to write the value */
            convert(json, bundle.get(key));
        }

        /* End object */
        json.endObject();
    }

    /* Handle array, write it as a JSON array */
    else if (value.getClass().isArray()) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++)
            convert(json, Array.get(value, i));

        /* Close array */
        json.endArray();
    }

    /* Handle ArrayList, write it as a JSON array */
    else if (value instanceof ArrayList<?>) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        ArrayList<?> arrayList = (ArrayList<?>) value;
        for (Object val : arrayList)
            convert(json, val);

        /* Close array */
        json.endArray();
    }

    /* Format throwable values with the stack trace */
    else if (value instanceof Throwable) {
        Throwable t = (Throwable) value;
        StringWriter text = new StringWriter();
        t.printStackTrace(new PrintWriter(text));
        json.value(text.toString());
    }

    /* Other values are handled directly by JSONStringer (numerical, boolean and String) */
    else
        json.value(value);
}

From source file:org.commonjava.aprox.bind.jaxrs.util.ResponseUtils.java

public static CharSequence formatEntity(final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    if (message != null) {
        sw.append(message);/*from  w ww  . j  a va 2  s .c o m*/
        sw.append("\nError was:\n\n");
    }

    sw.append(error.getMessage());

    final Throwable cause = error.getCause();
    if (cause != null) {
        sw.append("\n\n");
        cause.printStackTrace(new PrintWriter(sw));
    }

    sw.write('\n');

    return sw.toString();
}

From source file:com.dsf.dbxtract.cdc.App.java

final static void error(String message, Throwable t) {
    if (message != null)
        System.out.printf("[INFO] %s\n", message);
    if (t != null)
        t.printStackTrace(System.out);
}

From source file:eu.semaine.jms.JMSLogger.java

public static String toLogMessageText(long usertime, Object... objects) {
    StringBuilder builder = new StringBuilder();
    if (usertime >= 0) {
        builder.append("[")
                .append(new SimpleDateFormat(MessageLogComponent.TIME_FORMAT).format(new Date(usertime)))
                .append("]");

    }//from  w  ww.ja  va 2  s .com
    for (Object o : objects) {
        if (builder.length() > 0) {
            builder.append(" ");
        }
        if (o instanceof Throwable) {
            Throwable t = (Throwable) o;
            StringWriter sw = new StringWriter();
            t.printStackTrace(new PrintWriter(sw));
            builder.append(sw.toString());
        } else {
            builder.append(o.toString());
        }
    }
    String logMessageText = builder.toString();
    return logMessageText;
}

From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java

private static String getStackTrace(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    throwable.printStackTrace(pw);
    return sw.getBuffer().toString();
}

From source file:net.heroicefforts.viable.android.rep.it.Main.java

private static final String toString(Throwable t) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    t.printStackTrace(ps);
    ps.flush();/*  w  ww  .j  a  va2s. c  o  m*/
    return baos.toString();
}

From source file:edu.umn.msi.tropix.common.logging.ExceptionUtils.java

public static String toString(@Nullable final Throwable throwable) {
    if (throwable == null) {
        return null;
    }//from   ww  w.  j  a v  a 2s.com
    final StringWriter stackWriter = new StringWriter();
    throwable.printStackTrace(new PrintWriter(stackWriter));
    return stackWriter.toString();
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Converts a stacktrace into a String/*from w  w  w  .  ja va  2s.com*/
 *
 * @param t a Throwable
 * @return the whole stacktrace in a String
 */
public static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    pw.flush();
    sw.flush();
    return sw.toString();
}