KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > StackTraceElementFactory


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4
5 import java.lang.reflect.Field JavaDoc;
6
7 /**
8  * Factory for creating StackTraceElements.
9  * Factory for creating StackTraceElements.
10  *
11  * @author <a HREF="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
12  * @author Joe Walnes
13  */

14 public class StackTraceElementFactory {
15
16     public StackTraceElement JavaDoc nativeMethodElement(String JavaDoc declaringClass, String JavaDoc methodName) {
17         return create(declaringClass, methodName, "Native Method", -2);
18     }
19
20     public StackTraceElement JavaDoc unknownSourceElement(String JavaDoc declaringClass, String JavaDoc methodName) {
21         return create(declaringClass, methodName, "Unknown Source", -1);
22     }
23
24     public StackTraceElement JavaDoc element(String JavaDoc declaringClass, String JavaDoc methodName, String JavaDoc fileName) {
25         return create(declaringClass, methodName, fileName, -1);
26     }
27
28     public StackTraceElement JavaDoc element(String JavaDoc declaringClass, String JavaDoc methodName, String JavaDoc fileName, int lineNumber) {
29         return create(declaringClass, methodName, fileName, lineNumber);
30     }
31
32     private StackTraceElement JavaDoc create(String JavaDoc declaringClass, String JavaDoc methodName, String JavaDoc fileName, int lineNumber) {
33         StackTraceElement JavaDoc result = new Throwable JavaDoc().getStackTrace()[0];
34         setField(result, "declaringClass", declaringClass);
35         setField(result, "methodName", methodName);
36         setField(result, "fileName", fileName);
37         setField(result, "lineNumber", new Integer JavaDoc(lineNumber));
38         return result;
39     }
40
41     private void setField(StackTraceElement JavaDoc element, String JavaDoc fieldName, Object JavaDoc value) {
42         try {
43             final Field JavaDoc field = StackTraceElement JavaDoc.class.getDeclaredField(fieldName);
44             field.setAccessible(true);
45             field.set(element, value);
46         } catch (Exception JavaDoc e) {
47             throw new ConversionException(e);
48         }
49     }
50
51 }
52
Popular Tags