157 */ 158 public final void setColumnNumber(final int column) { 159 this.column = column; 160 } 161 162 /** 163 * Returns array javascript stack frames from the given exception object. 164 * 165 * @param exception exception from which stack frames are retrieved and filtered 166 * @return array of javascript stack frames 167 */ 168 public static StackTraceElement[] getScriptFrames(final Throwable exception) { 169 final StackTraceElement[] frames = exception.getStackTrace(); 170 final List<StackTraceElement> filtered = new ArrayList<>(); 171 for (final StackTraceElement st : frames) { 172 if (ECMAErrors.isScriptFrame(st)) { 173 final String className = "<" + st.getFileName() + ">"; 174 String methodName = st.getMethodName(); 175 if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) { 176 methodName = "<program>"; 177 } 178 179 if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) { 180 methodName = "<anonymous>"; 181 } 182 183 filtered.add(new StackTraceElement(className, methodName, 184 st.getFileName(), st.getLineNumber())); 185 } 186 } 187 return filtered.toArray(new StackTraceElement[0]); 188 } 189 190 /** 191 * Return a formatted script stack trace string with frames information separated by '\n' 192 * 193 * @param exception exception for which script stack string is returned 194 * @return formatted stack trace string 195 */ 196 public static String getScriptStackString(final Throwable exception) { 197 final StringBuilder buf = new StringBuilder(); 198 final StackTraceElement[] frames = getScriptFrames(exception); 199 for (final StackTraceElement st : frames) { 200 buf.append("\tat "); 201 buf.append(st.getMethodName()); 202 buf.append(" ("); 203 buf.append(st.getFileName()); 204 buf.append(':'); 205 buf.append(st.getLineNumber()); 206 buf.append(")\n"); 207 } | 157 */ 158 public final void setColumnNumber(final int column) { 159 this.column = column; 160 } 161 162 /** 163 * Returns array javascript stack frames from the given exception object. 164 * 165 * @param exception exception from which stack frames are retrieved and filtered 166 * @return array of javascript stack frames 167 */ 168 public static StackTraceElement[] getScriptFrames(final Throwable exception) { 169 final StackTraceElement[] frames = exception.getStackTrace(); 170 final List<StackTraceElement> filtered = new ArrayList<>(); 171 for (final StackTraceElement st : frames) { 172 if (ECMAErrors.isScriptFrame(st)) { 173 final String className = "<" + st.getFileName() + ">"; 174 String methodName = st.getMethodName(); 175 if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) { 176 methodName = "<program>"; 177 } else { 178 methodName = stripMethodName(methodName); 179 } 180 181 filtered.add(new StackTraceElement(className, methodName, 182 st.getFileName(), st.getLineNumber())); 183 } 184 } 185 return filtered.toArray(new StackTraceElement[0]); 186 } 187 188 private static String stripMethodName(final String methodName) { 189 String name = methodName; 190 191 final int nestedSeparator = name.lastIndexOf(CompilerConstants.NESTED_FUNCTION_SEPARATOR.symbolName()); 192 if (nestedSeparator >= 0) { 193 name = name.substring(nestedSeparator + 1); 194 } 195 196 final int idSeparator = name.indexOf(CompilerConstants.ID_FUNCTION_SEPARATOR.symbolName()); 197 if (idSeparator >= 0) { 198 name = name.substring(0, idSeparator); 199 } 200 201 return name.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName()) ? "<anonymous>" : name; 202 } 203 204 /** 205 * Return a formatted script stack trace string with frames information separated by '\n' 206 * 207 * @param exception exception for which script stack string is returned 208 * @return formatted stack trace string 209 */ 210 public static String getScriptStackString(final Throwable exception) { 211 final StringBuilder buf = new StringBuilder(); 212 final StackTraceElement[] frames = getScriptFrames(exception); 213 for (final StackTraceElement st : frames) { 214 buf.append("\tat "); 215 buf.append(st.getMethodName()); 216 buf.append(" ("); 217 buf.append(st.getFileName()); 218 buf.append(':'); 219 buf.append(st.getLineNumber()); 220 buf.append(")\n"); 221 } |