/*
 * 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: StackTraceElement.h,v 1.11 2003/02/19 08:33:02 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_LANG_STACKTRACEELEMENT_H
# define INCLUDE_JAKELIB2_LANG_STACKTRACEELEMENT_H
#include "../io/Serializable.h"

namespace jakelib {
  namespace lang {

    /**
     * This class encapsulates a stack entry containing the
     * method name, class name, file name and line number.
     *
     * \c TmpStackTraceElememnt is not derived from \c Object
     * and is not automatically garbage collected.
     * Used only internally for speed optimization.
     *
     * @see StackTraceElement
     */
    class TmpStackTraceElement
      {
      public:

        /**
         * Constructs a new \c TmpStackTraceElement.
         *
         * Pushes the new stack trace element on the current \c Thread's
         * stack.
         */
        TmpStackTraceElement(char *methodName,
                             char *fileName, int lineNumber);

        /**
         * Removes this \c TmpStackTraceElement from the \c Thread's
         * stack.
         */
        ~TmpStackTraceElement();

        /** The method name */
        char *methodName;

        /** The file name */
        char *fileName;

        /** The line number */
        int lineNumber;
      };


    /**
     * One function call or stack trace element. Gives information about
     * the execution point such as the source file name, the line number,
     * the fully qualified class name, the method name, if this information 
     * is known.
     *
     * @author Mark Wielaard <mark@klomp.org>
     * @author Eric Blake <ebb9@email.byu.edu>
     * @author Alex Fabijanic <fabijanica@nucorsteel.com>(jakelib2 port)
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class StackTraceElement : public Object, public jakelib::io::Serializable
      {
        friend class Throwable;

      protected:

        /**
         * A constructor for the StackTraceElement class, to be
         * called by \c Throwable::fillInStackTrace.
         * There are no public constructors defined for this class. Creation
         * of new elements is implementation specific.
         *
         * @param className the fully qualified name of the class, null if unknown
         * @param methodName the name of the method, null if unknown
         * @param fileName the name of the file, null if unknown
         * @param lineNumber the line in the file, negative if unknown
         */
        StackTraceElement(String* className, String* methodName,
                          String* fileName, int lineNumber);
        
        /**
         * Returns the name of the file, or null if unknown. This is usually
         * obtained from the <code>SourceFile</code> attribute of the class file
         * format, if present.
         *
         * @return the file name
         */
      public: String* getFileName();

        /**
         * Returns the line number in the file, or a negative number if unknown.
         * This is usually obtained from the <code>LineNumberTable</code> attribute
         * of the method in the class file format, if present.
         *
         * @return the line number
         */
      public: jint getLineNumber();

        /**
         * Returns the fully qualified class name, or null if unknown.
         *
         * @return the class name
         */
      public: String* getClassName();

        /**
         * Returns the method name in the class, or null if unknown. If the
         * execution point is in a constructor, the name is
         * <code>&lt;init&gt;</code>; if the execution point is in the class
         * initializer, the name is <code>&lt;clinit&gt;</code>.
         *
         * @return the method name
         */
      public: String* getMethodName();

        /**
         * Returns a string representation of this stack trace element. The
         * returned String is implementation specific. This implementation
         * returns the following String: "[class][.][method]([file][:line])".
         * If the fully qualified class name or the method is unknown it is
         * omitted including the point seperator. If the source file name is
         * unknown it is replaced by "Unknown Source". If the line number
         * is unknown it and the colon are omitted.
         *
         * @return a string representation of this execution point
         */
      public: String* toString();

        /**
         * Returns true if the given object is also a StackTraceElement and all
         * attributes, are equal (either the same attribute
         * between the two elments are null, or both satisfy Object.equals).
         *
         * @param o the object to compare
         * @return true if the two are equal
         */
      public: jboolean equals(Object* o);

        /**
         * Returns the hashCode of this StackTraceElement. This implementation
         * computes the hashcode by xor-ing the hashcode of all attributes.
         *
         * @return the hashcode
         */
      public: jint hashCode();

        /**
         * Compare two objects according to Collection semantics.
         *
         * @param o1 the first object
         * @param o2 the second object
         * @return o1 == null ? o2 == null : o1.equals(o2)
         */
      private: static jboolean equals(Object* o1, Object* o2);

        /**
         * Hash an object according to Collection semantics.
         *
         * @param o the object to hash
         * @return o1 == null ? 0 : o1.hashCode()
         */
      private: static int hashCode(Object* o);

      private:

        /**
         * The name of the file, null if unknown.
         *
         * @serial the source code filename, if known
         */
        String* fileName;
        
        /**
         * The line number in the file, negative if unknown.
         *
         * @serial the source code line number, if known
         */
        jint lineNumber;

        /**
         * The fully qualified class name, null if unknown.
         *
         * @serial the enclosing class, if known
         */
        String* className;

        /**
         * The method name in the class, null if unknown.
         *
         * @serial the enclosing method, if known
         */
        String* methodName;

        JAKELIB_DECLARE_CLASS(StackTraceElement);
      };

    JAKELIB_DECLARE_ARRAY(StackTraceElement);
  }
}

#endif
