1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.util.Stringifier;
    19  import org.as2lib.env.except.StackTraceElementStringifier;
    20  
    21  /**
    22   * {@code StackTraceElement} represents an element in the stack trace returned by the
    23   * {@link Throwable#getStackTrace} method.
    24   *
    25   * <p>A stack trace element is simply a data holder. It holds the thrower, the method
    26   * that threw the throwable and the arguments passed to the method that threw the
    27   * throwable.
    28   *
    29   * <p>This class is also responsible for stringifying itself. This functionality is
    30   * used by the {@link ThrowableStringifier}. You can set your own stringifier to get a
    31   * custom string representation of the stack trace element via the static
    32   * {@code #setStringifier} method.
    33   *
    34   * @author Simon Wacker
    35   * @author Martin Heidegger
    36   */
    37  class org.as2lib.env.except.StackTraceElement extends BasicClass {
    38  	
    39  	/** Stringifier to stringify stack trace elements. */
    40  	private static var stringifier:Stringifier;
    41  	
    42  	/** The object that declares the method that thew the throwable. */
    43  	private var thrower;
    44  	
    45  	/** The throwing method. */
    46  	private var method:Function;
    47  	
    48  	/** The arguments passed to the throwing method. */
    49  	private var args:Array;
    50  	
    51  	/**
    52  	 * Returns the stringifier to stringify stack trace elements.
    53  	 *
    54  	 * <p>The returned stringifier is either the default
    55  	 * {@link StackTraceElementStringifier} if no custom stringifier was set or if the
    56  	 * stringifier was set to {@code null} or the set stringifier.
    57  	 *
    58  	 * @return the default or the custom stringifier
    59  	 */
    60  	public static function getStringifier(Void):Stringifier {
    61  		if (!stringifier) stringifier = new StackTraceElementStringifier();
    62  		return stringifier;
    63  	}
    64  	
    65  	/**
    66  	 * Sets the stringifier to stringify stack trace elements.
    67  	 *
    68  	 * <p>If {@code stackTraceElementStringifier} is {@code null} the static
    69  	 * {@link #getStringifier} method returns the default stringifier.
    70  	 *
    71  	 * @param stackTraceElementStringifier the stringifier to stringify stack trace
    72  	 * elements
    73  	 */
    74  	public static function setStringifier(stackTraceElementStringifier:Stringifier):Void {
    75  		stringifier = stackTraceElementStringifier;
    76  	}
    77  	
    78  	/**
    79  	 * Constructs a new {@code SimpleStackTraceElement} instance.
    80  	 *
    81  	 * @param thrower the object that declares the method that threw the throwable
    82  	 * @param method the method that threw the throwable
    83  	 * @param args the arguments passed to the throwing method
    84  	 */
    85  	public function StackTraceElement(thrower, method:Function, args:Array) {
    86  		this.thrower = thrower ? thrower : null;
    87  		this.method = method ? method : null;
    88  		this.args = args ? args.concat() : null;
    89  	}
    90  	
    91  	/**
    92  	 * Returns the object that declares the method that threw the throwable.
    93  	 *
    94  	 * @return the object that declares the method that threw the throwable
    95  	 */
    96  	public function getThrower(Void) {
    97  		return thrower;
    98  	}
    99  	
   100  	/**
   101  	 * Returns the method that threw the throwable.
   102  	 *
   103  	 * @return the method that threw the throwable
   104  	 */
   105  	public function getMethod(Void):Function {
   106  		return method;
   107  	}
   108  	
   109  	/**
   110  	 * Returns the arguments that have been passed to the method that threw the
   111  	 * throwable.
   112  	 *
   113  	 * @return the arguments passed to the method that threw the throwable
   114  	 */
   115  	public function getArguments(Void):Array {
   116  		return args.concat();
   117  	}
   118  	
   119  	/**
   120  	 * Returns the string representation of this stack trace element.
   121  	 *
   122  	 * <p>The string representation is obtained via the stringifier returned by the
   123  	 * static {@link #getStringifier} method.
   124  	 *
   125  	 * @return the string representation of this stack trace element
   126  	 */
   127  	public function toString():String {
   128  		return getStringifier().execute(this);
   129  	}
   130  	
   131  }