// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: Throwable.jlc,v 1.15 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/System.h" #include "jakelib2/lang/Throwable.h" #include "jakelib2/lang/Thread.h" #include "jakelib2/lang/StackTrace.h" #include using namespace jakelib::lang; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.lang.Throwable", Throwable, Object) /*****************************************************************************\ * Throwable | *****************************************************************************/ Throwable::Throwable() { cause = this; message = String::emptyString; fillInStackTrace(); } Throwable::Throwable(String* msg) { cause = this; initMsg(msg); fillInStackTrace(); } Throwable::Throwable(String *msg, Throwable *cause) { initMsg(msg); initCause(cause); fillInStackTrace(); } Throwable::Throwable(Throwable* cause) { initMsg((cause == null) ? null : cause->toString()); initCause(cause); fillInStackTrace(); } /*****************************************************************************\ * getMessage | *****************************************************************************/ String* Throwable::getMessage() { return message; } /*****************************************************************************\ * toString | *****************************************************************************/ String* Throwable::toString() { return getClass()->getName() .. `": "` .. message; } /*****************************************************************************\ * toString | *****************************************************************************/ void Throwable::initMsg(String *msg) { if (msg == null) message = String::emptyString; else message = msg; } /*****************************************************************************\ * printStackTrace | *****************************************************************************/ void Throwable::printStackTrace() { printStackTrace(jakelib::lang::System::err); } /*+++AlFa 01/24/2003 this is original Classpath inplementation we don't have PrintStream, so it's implemented using PrintWriter void Throwable::printStackTrace(PrintStream* s) { s->print(stackTraceString()); }*/ void Throwable::printStackTrace(PrintWriter* pw) { pw->print(stackTraceString()); } /*****************************************************************************\ * initCause | *****************************************************************************/ Throwable* Throwable::initCause(Throwable *cause) { if (cause == this) throw new IllegalArgumentException(); if (this->cause != this) throw new IllegalStateException(); this->cause = cause; return this; } /*****************************************************************************\ * toString | *****************************************************************************/ Throwable* Throwable::getCause() { return (cause == this) ? null : cause; } /*****************************************************************************\ * stackTraceString | *****************************************************************************/ String* Throwable::stackTraceString() { StringBuffer sb; // Main stacktrace StackTraceElements* stack = getStackTrace(); stackTraceStringBuffer(&sb, toString(), stack, 0); // The cause(s) Throwable* cause = getCause(); // while (cause != null) { // // Cause start first line // sb.append(`"Caused by: "`); // // Cause stacktrace // StackTraceElements* parentStack = stack; // stack = cause->getStackTrace(); // if (parentStack == null || parentStack->length() == 0) // stackTraceStringBuffer(&sb, cause->toString(), stack, 0); // else { // int equal = 0; // Count how many of the last stack frames are equal // int frame = stack->length()-1; // int parentFrame = parentStack->length()-1; // while (frame > 0 && parentFrame > 0) { // if ((*stack)[frame]->equals((*parentStack)[parentFrame])) { // equal++; // frame--; // parentFrame--; // } // else // break; // } // stackTraceStringBuffer(&sb, cause->toString(), stack, equal); // } // cause = cause->getCause(); // } return sb.toString(); } /*****************************************************************************\ * stackTraceStringBuffer | *****************************************************************************/ void Throwable::stackTraceStringBuffer(StringBuffer* sb, String* name, StackTraceElements* stack, int equal) { String* nl = System::eol; // (finish) first line sb->append(name); sb->append(System::eol); // The stacktrace if (stack == null || stack->length() == 0) { sb->append(`" <>"`); sb->append(nl); } else { StackTraceElements& st = *stack;//just a handy reference to use nice array operators for (int i = 0; i < (st.length() - equal); i++) { sb->append(`" at "`); sb->append(st.get(i) == null ? `"<>"` : st.get(i)->toString()); sb->append(nl); } if (equal > 0) { sb->append(`" ..."`); sb->append(equal); sb->append(`" more"`); sb->append(nl); } } } /*****************************************************************************\ * fillInStackTrace | *****************************************************************************/ Throwable* Throwable::fillInStackTrace() { #ifdef JAKELIB_STACKTRACE StackTrace *st = Thread::currentThread()->getStackTrace(); int size = st->getElemCount(); stackTrace = new StackTraceElements(size); //System::out->println(`"Throwable::fillInStackTrace: elemCount="`..size); for (int idx = 0; idx < size; idx++) { TmpStackTraceElement *tmp = st->get(idx); String *methodName = null; String *className = null; int len = (tmp->methodName != 0) ? (int)strlen(tmp->methodName) : -1; int startX = 0; int endX = len; #if defined(POSIX) || defined(MSVC) for (int x = 0; x < len; x++) { if (tmp->methodName[x] == ' ') startX = x + 1; else if (tmp->methodName[x] == ':') { for (; x < len; x++) { if (tmp->methodName[x] == '(' || tmp->methodName[x] == ' ') { endX = x; break; } } break; } else if (tmp->methodName[x] == '(') { endX = x; } } methodName = new String(tmp->methodName, startX, endX - startX); className = null; #endif StackTraceElement *elem = new StackTraceElement(className, methodName, new String(tmp->fileName), tmp->lineNumber); stackTrace->set(size - idx - 1, elem); } #else stackTrace = new StackTraceElements(0); #endif return this; } /*****************************************************************************\ * getStackTrace | *****************************************************************************/ StackTraceElements* Throwable::getStackTrace() { return stackTrace; } /*****************************************************************************\ * setStackTrace | *****************************************************************************/ void Throwable::setStackTrace(StackTraceElements* stackTrace) { jint i = stackTrace->length(); StackTraceElements& st = *(new StackTraceElements(i)); st = *stackTrace; while (--i >= 0) { if (st.get(i) == null) throw new NullPointerException(`"Element "` .. i .. `" null"`); } this->stackTrace = &st; }