Show
Ignore:
Timestamp:
04/07/10 06:33:35 (2 years ago)
Author:
amandel
Message:

Add utility method to get property like values from any Throwable.
Fix check for VOID return type.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/java/org/jcoderz/commons/util/ThrowableUtil.java

    r1563 r1625  
    3838import java.lang.reflect.Method; 
    3939import java.lang.reflect.Modifier; 
     40import java.util.HashMap; 
     41import java.util.Map; 
    4042import java.util.logging.Level; 
    4143import java.util.logging.Logger; 
     
    5153{ 
    5254   private static final int MAX_REASONABLE_PARAMETER_LENGTH = 10000; 
    53 /** Name of getter methods start with this prefix. */ 
     55   /** Name of getter methods start with this prefix. */ 
    5456   private static final String GETTER_METHOD_PREFIX = "get"; 
     57   /** Name of getter methods start with this prefix. */ 
     58   private static final String BOOLEAN_GETTER_METHOD_PREFIX = "is"; 
    5559   /** Length of the getter prefix. */ 
    5660   private static final int GETTER_METHOD_PREFIX_LENGTH 
    5761       = GETTER_METHOD_PREFIX.length(); 
     62   /** Length of the boolean getter prefix. */ 
     63   private static final int BOOLEAN_GETTER_METHOD_PREFIX_LENGTH 
     64       = BOOLEAN_GETTER_METHOD_PREFIX.length(); 
     65    
    5866   /** 
    5967    * Stores the Throwable.getCause() method if this method is available. 
     
    189197   } 
    190198 
     199   /** 
     200    * Tries to read additional property like information from this throwable  
     201    * and fills it in a map suitable for detailed information output.  
     202    * @param thr the throwable to analyze. 
     203    * @return a Map pointing from String property names to the value.  
     204    */ 
     205   public static Map/*<String, Object>*/ getProperties(Throwable thr) 
     206   { 
     207       final Map/*<String, Object>*/ result = new HashMap(); 
     208       final Method[] methods = thr.getClass().getMethods(); 
     209       for (int i = 0; i < methods.length; i++) 
     210       { 
     211          final int modifier = methods[i].getModifiers(); 
     212          if (methods[i].getDeclaringClass() != Throwable.class 
     213              && methods[i].getDeclaringClass() != Object.class 
     214              && methods[i].getParameterTypes().length == 0 
     215              && Modifier.isPublic(modifier) 
     216              && !Modifier.isStatic(modifier) 
     217              && !methods[i].getReturnType().equals(Void.TYPE)) 
     218          { 
     219              try 
     220              { 
     221                  if (methods[i].getName().startsWith(GETTER_METHOD_PREFIX)) 
     222                  { 
     223                      final Object value 
     224                          = methods[i].invoke(thr, (Object[]) null); 
     225                      final String key 
     226                          = methods[i].getName().substring( 
     227                              GETTER_METHOD_PREFIX_LENGTH); 
     228                      result.put(key, value); 
     229                  } 
     230                  else if (methods[i].getName().startsWith( 
     231                          BOOLEAN_GETTER_METHOD_PREFIX) 
     232                      && (methods[i].getReturnType().equals(Boolean.class) 
     233                          || methods[i].getReturnType().equals( 
     234                              java.lang.Boolean.TYPE))) 
     235                  { 
     236                      final Object value 
     237                          = methods[i].invoke(thr, (Object[]) null); 
     238                      final String key 
     239                          = methods[i].getName().substring( 
     240                              BOOLEAN_GETTER_METHOD_PREFIX_LENGTH); 
     241                      result.put(key, value); 
     242                  } 
     243              } 
     244              catch (InvocationTargetException e) 
     245              { 
     246                  // Ignore this property, continue with next 
     247              } 
     248              catch (IllegalArgumentException e) 
     249              { 
     250                  // Ignore this property, continue with next 
     251              } 
     252              catch (IllegalAccessException e) 
     253              { 
     254                  // Ignore this property, continue with next 
     255              } 
     256          } 
     257       } 
     258       return result; 
     259   } 
     260    
    191261   /** 
    192262    * Dumps the stack trace of the given throwable to its String representation. 
     
    280350              && !Modifier.isStatic(modifier) 
    281351              && methods[i].getName().startsWith(GETTER_METHOD_PREFIX) 
    282               && !methods[i].getReturnType().equals(Void.class)) 
     352              && !methods[i].getReturnType().equals(Void.TYPE)) 
    283353          { 
    284354              final Object result = methods[i].invoke(thr, (Object[]) null);