01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 30. May 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.extended;
13:
14: import com.thoughtworks.xstream.converters.ConversionException;
15:
16: import java.lang.reflect.Field;
17:
18: /**
19: * Factory for creating StackTraceElements.
20: * Factory for creating StackTraceElements.
21: *
22: * @author <a href="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
23: * @author Joe Walnes
24: */
25: public class StackTraceElementFactory {
26:
27: public StackTraceElement nativeMethodElement(String declaringClass,
28: String methodName) {
29: return create(declaringClass, methodName, "Native Method", -2);
30: }
31:
32: public StackTraceElement unknownSourceElement(
33: String declaringClass, String methodName) {
34: return create(declaringClass, methodName, "Unknown Source", -1);
35: }
36:
37: public StackTraceElement element(String declaringClass,
38: String methodName, String fileName) {
39: return create(declaringClass, methodName, fileName, -1);
40: }
41:
42: public StackTraceElement element(String declaringClass,
43: String methodName, String fileName, int lineNumber) {
44: return create(declaringClass, methodName, fileName, lineNumber);
45: }
46:
47: private StackTraceElement create(String declaringClass,
48: String methodName, String fileName, int lineNumber) {
49: StackTraceElement result = new Throwable().getStackTrace()[0];
50: setField(result, "declaringClass", declaringClass);
51: setField(result, "methodName", methodName);
52: setField(result, "fileName", fileName);
53: setField(result, "lineNumber", new Integer(lineNumber));
54: return result;
55: }
56:
57: private void setField(StackTraceElement element, String fieldName,
58: Object value) {
59: try {
60: final Field field = StackTraceElement.class
61: .getDeclaredField(fieldName);
62: field.setAccessible(true);
63: field.set(element, value);
64: } catch (Exception e) {
65: throw new ConversionException(e);
66: }
67: }
68:
69: }
|