1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.MarshallingContext; 4 import com.thoughtworks.xstream.converters.ConversionException; 5 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter; 6 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 7 8 import java.lang.reflect.Field ; 9 import java.util.regex.Matcher ; 10 import java.util.regex.Pattern ; 11 12 18 public class StackTraceElementConverter extends AbstractBasicConverter { 19 20 26 private static final Pattern PATTERN = Pattern.compile("^(.+)\\.([^\\(]+)\\(([^:]*)(:(\\d+))?\\)$"); 27 28 private final StackTraceElementFactory factory = new StackTraceElementFactory(); 29 30 public boolean canConvert(Class type) { 31 return StackTraceElement .class.equals(type); 32 } 33 34 protected Object fromString(String str) { 35 Matcher matcher = PATTERN.matcher(str); 36 if (matcher.matches()) { 37 String declaringClass = matcher.group(1); 38 String methodName = matcher.group(2); 39 String fileName = matcher.group(3); 40 if (fileName.equals("Unknown Source")) { 41 return factory.unknownSourceElement(declaringClass, methodName); 42 } else if (fileName.equals("Native Method")) { 43 return factory.nativeMethodElement(declaringClass, methodName); 44 } else { 45 if (matcher.group(4) != null) { 46 int lineNumber = Integer.parseInt(matcher.group(5)); 47 return factory.element(declaringClass, methodName, fileName, lineNumber); 48 } else { 49 return factory.element(declaringClass, methodName, fileName); 50 } 51 } 52 } else { 53 throw new ConversionException("Could not parse StackTraceElement : " + str); 54 } 55 } 56 57 } 58 | Popular Tags |