Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) Azureus Software, Inc, All Rights Reserved.
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import android.content.res.Resources;

public class Main {
    public static String getCompressedStackTrace() {
        try {
            throw new Exception();
        } catch (Exception e) {
            return getCompressedStackTrace(e, 1, 12);
        }
    }

    public static String getCompressedStackTrace(int limit) {
        try {
            throw new Exception();
        } catch (Exception e) {
            return getCompressedStackTrace(e, 1, limit);
        }
    }

    public static String getCompressedStackTrace(Throwable t, int startAt, int limit) {
        try {
            StackTraceElement[] stackTrace = t.getStackTrace();
            if (stackTrace.length < startAt) {
                return "";
            }
            StringBuilder sb = new StringBuilder("");
            for (int i = startAt; i < stackTrace.length && i < startAt + limit; i++) {
                StackTraceElement element = stackTrace[i];
                String classname = element.getClassName();
                String cnShort;
                boolean showLineNumber = true;
                boolean breakAfter = false;
                if (classname.startsWith("com.vuze.android.remote.")) {
                    cnShort = classname.substring(24, classname.length());
                } else if (classname.equals("android.os.Handler")) {
                    showLineNumber = false;
                    cnShort = "Handler";
                } else if (classname.equals("android.os.Looper")) {
                    showLineNumber = false;
                    cnShort = "Looper";
                    breakAfter = true;
                } else if (classname.length() < 9) { // include full if something like aa.ab.ac
                    cnShort = classname;
                } else {
                    int len = classname.length();
                    int start = len > 14 ? len - 14 : 0;

                    int pos = classname.indexOf('.', start);
                    if (pos >= 0) {
                        start = pos + 1;
                    }
                    cnShort = classname.substring(start, len);
                }
                if (i != startAt) {
                    sb.append(", ");
                }
                sb.append(cnShort);
                sb.append('.');
                sb.append(element.getMethodName());
                if (showLineNumber) {
                    sb.append(':');
                    sb.append(element.getLineNumber());
                }
                if (breakAfter) {
                    break;
                }
            }
            Throwable cause = t.getCause();
            if (cause != null) {
                sb.append("\n|Cause ");
                sb.append(cause.getClass().getSimpleName());
                if (cause instanceof Resources.NotFoundException || cause instanceof RuntimeException) {
                    sb.append(' ');
                    sb.append(cause.getMessage());
                }
                sb.append(' ');
                sb.append(getCompressedStackTrace(cause, 0, 9));
            }
            return sb.toString();
        } catch (Throwable derp) {
            return "derp " + derp.getClass().getSimpleName();
        }
    }
}