1 30 31 package de.susebox.java.lang; 32 33 import java.lang.reflect.Method ; 37 import java.text.MessageFormat ; 38 import de.susebox.java.lang.ThrowableList; 39 40 41 45 57 public final class ThrowableMessageFormatter { 58 59 62 public static final String MSG_IDENTATION = " "; 63 64 97 public static final String getMessage(ThrowableList ex) { 98 if (ex.isWrapper()) { 100 return ex.getCause().getMessage(); 101 } 102 103 StringBuffer msg = new StringBuffer (); 105 String fmt = ex.getFormat(); 106 Throwable next = ex.getCause(); 107 108 if (next != null) { 110 msg.append(EOL_SEQUENCE); 111 msg.append(MSG_IDENTATION); 112 msg.append(next.toString()); 113 if (fmt != null) { 114 msg.append(EOL_SEQUENCE); 115 msg.append(MSG_IDENTATION); 116 } 117 } 118 119 if (fmt != null) { 121 Object [] args = ex.getArguments(); 122 123 if (args == null) { 124 msg.append(fmt); 125 } else { 126 try { 127 msg.append(MessageFormat.format(fmt, args)); 128 } catch (IllegalArgumentException argEx) { 129 msg.append(argEx.getMessage()); 130 msg.append(EOL_SEQUENCE); 131 msg.append("While formatting this message:"); 132 msg.append(EOL_SEQUENCE); 133 msg.append(fmt); 134 } 135 } 136 } 137 return msg.toString(); 138 } 139 140 static { 144 try { 145 Method causeMethod = Throwable .class.getMethod("getCause", new Class [] {} ); 146 GET_CAUSE_AVAILABLE = true; 147 } catch (NoSuchMethodException ex) { 148 GET_CAUSE_AVAILABLE = false; 149 } 150 } 151 152 private static final String EOL_SEQUENCE = System.getProperty("line.separator"); 156 private static boolean GET_CAUSE_AVAILABLE; 157 } 158 | Popular Tags |