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