ThrowableMapperpublic final class ThrowableMapper extends Object Maps a Throwable to another one in order to avoid the transfer
of non-standard (proprietary) Exception types, which could result in
ClassNotFoundException on remote clients.
Any Throwable which either is, or contains,
a Throwable which is not in the allowed packages is converted. |
Fields Summary |
---|
final Throwable | mOriginal | final Set | mOKPackages | protected static final Set | OK_PACKAGESBy default, any Throwable whose package does not start with one
of these packages must be mapped to something standard. |
Constructors Summary |
---|
public ThrowableMapper(Throwable t)
mOriginal = t;
mOKPackages = OK_PACKAGES;
|
Methods Summary |
---|
public static java.lang.Throwable | map(java.lang.Throwable t)
Throwable result = t;
if ( t != null )
{
final Throwable tCause = t.getCause();
final Throwable tCauseMapped = map( tCause );
// if either this Exception or its cause needs/was mapped,
// then we must form a new Exception
if ( shouldMap( t ) )
{
// the Throwable itself needs to be mapped
final String msg =t.getMessage();
if ( t instanceof Error )
{
result = new Error( msg, tCauseMapped );
}
else if ( t instanceof RuntimeException )
{
result = new RuntimeException( msg, tCauseMapped );
}
else if ( t instanceof Exception )
{
result = new Exception( msg, tCauseMapped );
}
else
{
result = new Throwable( msg, tCauseMapped );
}
result.setStackTrace( t.getStackTrace() );
}
else if ( tCauseMapped != tCause )
{
// the Throwable doesn't need mapping, but its Cause does
// create a Throwable of the same class, and insert its
// cause and stack trace.
try
{
final Constructor<? extends Throwable> c =
t.getClass().getConstructor( String.class, Throwable.class );
result = c.newInstance( t.getMessage(), tCauseMapped);
}
catch( final Throwable t1 )
{
try
{
final Constructor<? extends Throwable> c =
t.getClass().getConstructor( String.class );
result = c.newInstance( t.getMessage() );
result.initCause( tCauseMapped );
}
catch( final Throwable t2 )
{
result = new Throwable( t.getMessage(), tCauseMapped );
}
}
result.setStackTrace( tCause.getStackTrace() );
}
else
{
result = t;
}
}
return( result );
| public java.lang.Throwable | map()Map the original Throwable to one that is non-proprietary (standard).
Possible results include java.lang.Exception, java.lang.RuntimeException,
java.lang.Error. The original stack trace and exception chain is
preserved, each element in that chain being mapped if necessary.
return( map( mOriginal ) );
| private static boolean | shouldMap(java.lang.Throwable t)
final String tClass = t.getClass().getName();
boolean shouldMap = true;
for( final String prefix : OK_PACKAGES )
{
if ( tClass.startsWith( prefix ) )
{
shouldMap = false;
break;
}
}
return( shouldMap );
|
|