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.StackTraceElement; 20 import org.as2lib.env.reflect.ReflectUtil; 21 import org.as2lib.util.StringUtil; 22 23 /** 24 * {@code StackTraceElementStringifier} stringifies {@link StackTraceElement} 25 * instances. 26 * 27 * @author Simon Wacker 28 * @author Martin Heidegger 29 */ 30 class org.as2lib.env.except.StackTraceElementStringifier extends BasicClass implements Stringifier { 31 32 /** Default name if an information is unknown. */ 33 public static var UNKNOWN:String = "[unknown]"; 34 35 /** Show the real arguments. */ 36 private var showArgumentsValues:Boolean; 37 38 /** 39 * Constructs a new {@code StackTraceElementStringifier} instance. 40 * 41 * <p>By default the types of the arguments are shown and not their value. 42 * 43 * @param showArgumentsValues determines whether to show a string representation 44 * of the arguments' values, that is the string that is returned by their 45 * {@code toString} methods ({@code true}) or only the types of the arguments 46 * ({@code false}). 47 */ 48 public function StackTraceElementStringifier(showArgumentsValues:Boolean) { 49 this.showArgumentsValues = showArgumentsValues; 50 } 51 52 /** 53 * Returns the string representation of the passed-in {@link StackTraceElement} 54 * instance. 55 * 56 * <p>The string representation is composed as follows: 57 * <pre> 58 * static theFullQualifiedNameOfTheThrower.theMethodName(theFirstArgument, ..) 59 * </pre> 60 * 61 * <p>Depending on the settings arguments are either represented by their types of 62 * by the result of their {@code toString} methods. 63 * 64 * <p>A real string representation could look like this: 65 * <pre>org.as2lib.data.holder.MyDataHolder.setMaximumLength(Number)</pre> 66 * 67 * <p>Or this: 68 * <pre>org.as2lib.data.holder.MyDataHolder.setMaximumLength(-2)</pre> 69 * 70 * <p>If an element is {@code null} or its string representation could not been 71 * obtained the string '[unknown]' is used. 72 * 73 * <p>If the method of the stack trace element is the constructor of the thrower 74 * the string {@code "new"} is used. 75 * 76 * @param target the {@code StackTraceElement} instance to stringify 77 * @return the string representation of the passed-in {@code target} element 78 */ 79 public function execute(target):String { 80 var element:StackTraceElement = target; 81 var result:String; 82 try { 83 var info:Array = ReflectUtil.getTypeAndMethodInfo(element.getThrower(), element.getMethod()); 84 result = info[0] == null ? UNKNOWN + " " : (info[0] ? "static " : ""); 85 result += info[1] == null ? UNKNOWN : info[1]; 86 result += "." + (info[2] == null ? UNKNOWN : info[2]); 87 result += "("; 88 if (showArgumentsValues) { 89 result += element.getArguments().toString() ? element.getArguments().toString() : UNKNOWN; 90 } else { 91 var args:Array = element.getArguments(); 92 for (var i:Number = 0; i < args.length; i++) { 93 var argType:String = ReflectUtil.getTypeName(args[i]); 94 if (argType == null) argType = UNKNOWN; 95 result += argType; 96 if (i < args.length-1) result += ", "; 97 } 98 } 99 result += ")"; 100 } catch(e) { 101 result = "Exception was thrown during generation of string representation of stack trace element: \n" + StringUtil.addSpaceIndent(e.toString(), 2); 102 } 103 return result; 104 } 105 106 }