1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 import java.lang.reflect.Constructor ; 27 28 import com.sun.appserv.management.util.misc.GSetUtil; 29 30 31 32 40 public final class ThrowableMapper 41 { 42 final Throwable mOriginal; 43 44 final Set <String > mOKPackages; 45 46 50 protected final static Set <String > OK_PACKAGES = 51 GSetUtil.newUnmodifiableStringSet( "java.", "javax." ); 52 53 public 54 ThrowableMapper( final Throwable t ) 55 { 56 mOriginal = t; 57 mOKPackages = OK_PACKAGES; 58 } 59 60 private static boolean 61 shouldMap( final Throwable t ) 62 { 63 final String tClass = t.getClass().getName(); 64 65 boolean shouldMap = true; 66 67 for( final String prefix : OK_PACKAGES ) 68 { 69 if ( tClass.startsWith( prefix ) ) 70 { 71 shouldMap = false; 72 break; 73 } 74 } 75 76 return( shouldMap ); 77 } 78 79 80 public static Throwable 81 map( final Throwable t ) 82 { 83 Throwable result = t; 84 85 if ( t != null ) 86 { 87 final Throwable tCause = t.getCause(); 88 final Throwable tCauseMapped = map( tCause ); 89 90 93 if ( shouldMap( t ) ) 94 { 95 final String msg =t.getMessage(); 97 98 if ( t instanceof Error ) 99 { 100 result = new Error ( msg, tCauseMapped ); 101 } 102 else if ( t instanceof RuntimeException ) 103 { 104 result = new RuntimeException ( msg, tCauseMapped ); 105 } 106 else if ( t instanceof Exception ) 107 { 108 result = new Exception ( msg, tCauseMapped ); 109 } 110 else 111 { 112 result = new Throwable ( msg, tCauseMapped ); 113 } 114 115 result.setStackTrace( t.getStackTrace() ); 116 } 117 else if ( tCauseMapped != tCause ) 118 { 119 try 123 { 124 final Constructor <? extends Throwable > c = 125 t.getClass().getConstructor( String .class, Throwable .class ); 126 result = c.newInstance( t.getMessage(), tCauseMapped); 127 } 128 catch( final Throwable t1 ) 129 { 130 try 131 { 132 final Constructor <? extends Throwable > c = 133 t.getClass().getConstructor( String .class ); 134 result = c.newInstance( t.getMessage() ); 135 result.initCause( tCauseMapped ); 136 } 137 catch( final Throwable t2 ) 138 { 139 result = new Throwable ( t.getMessage(), tCauseMapped ); 140 } 141 } 142 143 result.setStackTrace( tCause.getStackTrace() ); 144 } 145 else 146 { 147 result = t; 148 } 149 } 150 151 return( result ); 152 } 153 154 162 public Throwable 163 map() 164 { 165 return( map( mOriginal ) ); 166 } 167 } 168 169 170 171 172 173 174 175 176 | Popular Tags |