Skip to content

Commit 50555ec

Browse files
author
David R. MacIver
committed
clone exceptions before fixing instead of mutating in place. I stand by all naming conventions used in this commit.
1 parent 240da0d commit 50555ec

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

src/com/rabbitmq/client/ShutdownSignalException.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
//
3131
package com.rabbitmq.client;
3232

33+
import com.rabbitmq.utility.SensibleClone;
34+
3335
/**
3436
* Encapsulates a shutdown condition for a connection to an AMQP broker.
3537
* Depending on HardError when calling
@@ -38,7 +40,7 @@
3840
* this exception.
3941
*/
4042

41-
public class ShutdownSignalException extends RuntimeException {
43+
public class ShutdownSignalException extends RuntimeException implements SensibleClone<ShutdownSignalException>{
4244
/** True if the connection is shut down, or false if this signal refers to a channel */
4345
private final boolean _hardError;
4446

@@ -92,6 +94,14 @@ public ShutdownSignalException(boolean hardError,
9294
/** @return Reference to Connection or Channel object that fired the signal **/
9395
public Object getReference() { return _ref; }
9496

97+
public ShutdownSignalException sensibleClone(){
98+
try {
99+
return (ShutdownSignalException)super.clone();
100+
} catch (CloneNotSupportedException e){
101+
// You've got to be kidding me
102+
throw new Error(e);
103+
}
104+
}
95105
}
96106

97107

src/com/rabbitmq/utility/BlockingValueOrException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import java.util.concurrent.TimeoutException;
3434

35-
public class BlockingValueOrException<V, E extends Throwable>
35+
public class BlockingValueOrException<V, E extends Throwable & SensibleClone<E>>
3636
extends BlockingCell<ValueOrException<V, E>>
3737
{
3838
public void setValue(V v) {

src/com/rabbitmq/utility/Utility.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public ThrowableCreatedElsewhere(Throwable throwable){
5050
}
5151
}
5252

53-
public static <T extends Throwable> T fixStackTrace(T throwable){
53+
public static <T extends Throwable & SensibleClone<T>> T fixStackTrace(T throwable){
54+
throwable = throwable.sensibleClone();
55+
5456
if(throwable.getCause() == null){
5557
// We'd like to preserve the original stack trace in the cause.
5658
// Unfortunately Java doesn't let you set the cause once it's been

src/com/rabbitmq/utility/ValueOrException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//
3131
package com.rabbitmq.utility;
3232

33-
public class ValueOrException<V, E extends Throwable> {
33+
public class ValueOrException<V, E extends Throwable & SensibleClone<E>> {
3434
private final boolean _useValue;
3535
private final V _value;
3636
private final E _exception;
@@ -57,7 +57,7 @@ private ValueOrException(V value, E exception, boolean useValue) {
5757
* @param value the value to wrap as a ValueOrException
5858
* @return the wrapped value
5959
*/
60-
public static <V, E extends Throwable> ValueOrException<V, E> makeValue(V value) {
60+
public static <V, E extends Throwable & SensibleClone<E>> ValueOrException<V, E> makeValue(V value) {
6161
return new ValueOrException<V, E>(value, null, true);
6262
}
6363

@@ -66,7 +66,7 @@ public static <V, E extends Throwable> ValueOrException<V, E> makeValue(V value)
6666
* @param exception the exception to wrap as a ValueOrException
6767
* @return the wrapped exception
6868
*/
69-
public static <V, E extends Throwable> ValueOrException<V, E> makeException(E exception) {
69+
public static <V, E extends Throwable & SensibleClone<E>> ValueOrException<V, E> makeException(E exception) {
7070
return new ValueOrException<V, E>(null, exception, false);
7171
}
7272

test/src/com/rabbitmq/client/test/ValueOrExceptionTest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,50 @@
3030
//
3131
package com.rabbitmq.client.test;
3232

33-
import java.io.IOException;
34-
3533
import junit.framework.TestCase;
3634
import junit.framework.TestSuite;
3735

3836
import com.rabbitmq.utility.ValueOrException;
37+
import com.rabbitmq.utility.SensibleClone;
3938

4039

4140
public class ValueOrExceptionTest extends TestCase {
41+
public static class InsufficientMagicException extends Exception implements SensibleClone<InsufficientMagicException>{
42+
public InsufficientMagicException(String message){
43+
super(message);
44+
}
45+
46+
public InsufficientMagicException sensibleClone(){
47+
return new InsufficientMagicException(getMessage());
48+
}
49+
}
50+
51+
4252
public static TestSuite suite()
4353
{
4454
TestSuite suite = new TestSuite("valueOrEx");
4555
suite.addTestSuite(ValueOrExceptionTest.class);
4656
return suite;
4757
}
4858

49-
public void testStoresValue() throws IOException {
59+
public void testStoresValue() throws InsufficientMagicException {
5060
Integer value = new Integer(3);
51-
ValueOrException<Integer, IOException> valueOrEx = ValueOrException.<Integer, IOException>makeValue(value);
61+
ValueOrException<Integer, InsufficientMagicException> valueOrEx = ValueOrException.<Integer, InsufficientMagicException>makeValue(value);
5262

5363
Integer returnedValue = valueOrEx.getValue();
5464
assertTrue(returnedValue == value);
5565
}
5666

57-
public void testStoresException() {
58-
IOException exception = new IOException("dummy message");
59-
ValueOrException<Integer, IOException> valueOrEx = ValueOrException.<Integer, IOException>makeException(exception);
67+
public void testClonesException() {
68+
InsufficientMagicException exception = new InsufficientMagicException("dummy message");
69+
ValueOrException<Integer, InsufficientMagicException> valueOrEx = ValueOrException.<Integer, InsufficientMagicException>makeException(exception);
6070

6171
try {
6272
valueOrEx.getValue();
6373
fail("Expected exception");
64-
} catch(IOException returnedException) {
65-
assertTrue(returnedException == exception);
74+
} catch(InsufficientMagicException returnedException) {
75+
assertTrue(returnedException != exception);
76+
assertEquals(returnedException.getMessage(), exception.getMessage());
6677
}
6778
}
6879
}

0 commit comments

Comments
 (0)