private static SerializableThrowable createSerializable(Throwable t) { SerializableThrowable throwable = new SerializableThrowable(null, t.getMessage()); throwable.setStackTrace(t.getStackTrace()); throwable.initCause(t.getCause()); throwable.originalThrowable = t; ThrowableTypeResolver.resolveDesignatedType(throwable, t); return throwable; } }
/** * Create a new {@link SerializableThrowable} from a provided throwable and its causes * recursively. * * @return a new SerializableThrowable or the passed object itself if it is {@code null} or * already a SerializableThrowable. */ public static SerializableThrowable fromThrowable(Throwable throwable) { if (throwable instanceof SerializableThrowable) { return (SerializableThrowable) throwable; } else if (throwable != null) { return createSerializable(throwable); } else { return null; } }
public void setException(Throwable exception) { thrown = SerializableThrowable.fromThrowable(exception); // Try to improve exception message if there is no class metadata available if (!thrown.isExactDesignatedTypeKnown()) { improveDesignatedType(thrown, exception); } }
public static void serialize( SerializationStreamWriter streamWriter, SerializableThrowable instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeString(instance.getDesignatedType()); streamWriter.writeBoolean(instance.isExactDesignatedTypeKnown()); streamWriter.writeObject(instance.getStackTrace()); streamWriter.writeObject(instance.getCause()); }
public static void deserialize( SerializationStreamReader streamReader, SerializableThrowable instance) throws SerializationException { String type = streamReader.readString(); boolean typeIsExact = streamReader.readBoolean(); instance.setDesignatedType(type, typeIsExact); instance.setStackTrace((StackTraceElement[]) streamReader.readObject()); instance.initCause((Throwable) streamReader.readObject()); }
private AssertionFailedError toAssertionFailedError(SerializableThrowable thrown) { AssertionFailedError error = new AssertionFailedError(thrown.getMessage()); error.setStackTrace(thrown.getStackTrace()); if (thrown.getCause() != null) { error.initCause(thrown.getCause()); } return error; }
/** * Initializes the cause of this throwable. * <p> * This method will convert the cause to {@link SerializableThrowable} if it is not already. */ @Override public Throwable initCause(Throwable cause) { return super.initCause(fromThrowable(cause)); }
/** * Returns best effort type info by checking against some common exceptions for unit tests. */ private static void improveDesignatedType(SerializableThrowable t, Throwable designatedType) { if (designatedType instanceof AssertionFailedError) { String className = "junit.framework.AssertionFailedError"; t.setDesignatedType(className, AssertionFailedError.class == designatedType.getClass()); } else if (designatedType instanceof TimeoutException) { String className = "com.google.gwt.junit.client.TimeoutException"; t.setDesignatedType(className, TimeoutException.class == designatedType.getClass()); } } }
@GwtIncompatible public boolean isExceptionOf(Class<?> expectedException) { try { return thrown == null ? false : expectedException.isAssignableFrom(Class.forName(thrown.getDesignatedType())); } catch (Exception e) { return false; } }
@Override public String toString() { String type = exactTypeKnown ? typeName : (typeName + "(EXACT TYPE UNKNOWN)"); String msg = getMessage(); return msg == null ? type : (type + ": " + msg); }
public static SerializableThrowable instantiate(SerializationStreamReader streamReader) throws SerializationException { return new SerializableThrowable(null, streamReader.readString()); }
public static void serialize( SerializationStreamWriter streamWriter, SerializableThrowable instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeString(instance.getDesignatedType()); streamWriter.writeBoolean(instance.isExactDesignatedTypeKnown()); streamWriter.writeObject(instance.getStackTrace()); streamWriter.writeObject(instance.getCause()); }
public static void deserialize( SerializationStreamReader streamReader, SerializableThrowable instance) throws SerializationException { String type = streamReader.readString(); boolean typeIsExact = streamReader.readBoolean(); instance.setDesignatedType(type, typeIsExact); instance.setStackTrace((StackTraceElement[]) streamReader.readObject()); instance.initCause((Throwable) streamReader.readObject()); }
private AssertionFailedError toAssertionFailedError(SerializableThrowable thrown) { AssertionFailedError error = new AssertionFailedError(thrown.getMessage()); error.setStackTrace(thrown.getStackTrace()); if (thrown.getCause() != null) { error.initCause(thrown.getCause()); } return error; }
public static void serialize(SerializationStreamWriter writer, LogRecord lr) throws SerializationException { // Although Level is serializable, the Level in LogRecord is actually // extending Level, which serialization does not like, so we // manually just serialize the name. writer.writeString(lr.getLevel().getName()); writer.writeString(lr.getMessage()); writer.writeString(lr.getLoggerName()); writer.writeLong(lr.getMillis()); writer.writeObject(SerializableThrowable.fromThrowable(lr.getThrown())); } }
public static void resolveDesignatedType(SerializableThrowable throwable, Throwable designated) { if (Class.isClassMetadataEnabled()) { throwable.setDesignatedType(designated.getClass().getName(), true); } else { String resolvedName; resolvedType = Throwable.class; throwable.setDesignatedType(resolvedName, resolvedType == designated.getClass());
public boolean isExceptionOf(Class<?> expectedException) { try { return thrown == null ? false : expectedException.isAssignableFrom(Class.forName(thrown.getDesignatedType())); } catch (Exception e) { return false; } }
@Override public String toString() { String type = exactTypeKnown ? typeName : (typeName + "(EXACT TYPE UNKNOWN)"); String msg = getMessage(); return msg == null ? type : (type + ": " + msg); }
public static SerializableThrowable instantiate(SerializationStreamReader streamReader) throws SerializationException { return new SerializableThrowable(null, streamReader.readString()); }
public static void serialize( SerializationStreamWriter streamWriter, SerializableThrowable instance) throws SerializationException { streamWriter.writeString(instance.getMessage()); streamWriter.writeString(instance.getDesignatedType()); streamWriter.writeBoolean(instance.isExactDesignatedTypeKnown()); streamWriter.writeObject(instance.getStackTrace()); streamWriter.writeObject(instance.getCause()); }