提交 c871fee9 编写于 作者: A alanb

8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)

Reviewed-by: alanb
Contributed-by: martinrb@google.com
上级 661b8ba3
...@@ -43,7 +43,8 @@ public class PrivilegedCallables { ...@@ -43,7 +43,8 @@ public class PrivilegedCallables {
final Random rnd = new Random(); final Random rnd = new Random();
@SuppressWarnings("serial") Throwable[] throwables = { @SuppressWarnings("serial")
Throwable[] throwables = {
new Exception() {}, new Exception() {},
new RuntimeException() {}, new RuntimeException() {},
new Error() {} new Error() {}
...@@ -51,6 +52,11 @@ public class PrivilegedCallables { ...@@ -51,6 +52,11 @@ public class PrivilegedCallables {
Throwable randomThrowable() { Throwable randomThrowable() {
return throwables[rnd.nextInt(throwables.length)]; return throwables[rnd.nextInt(throwables.length)];
} }
void throwThrowable(Throwable t) throws Exception {
if (t instanceof Error) throw (Error) t;
if (t instanceof RuntimeException) throw (RuntimeException) t;
throw (Exception) t;
}
//---------------------------------------------------------------- //----------------------------------------------------------------
// A Policy class designed to make permissions fiddling very easy. // A Policy class designed to make permissions fiddling very easy.
...@@ -119,9 +125,8 @@ public class PrivilegedCallables { ...@@ -119,9 +125,8 @@ public class PrivilegedCallables {
if (rnd.nextBoolean()) { if (rnd.nextBoolean()) {
final Throwable t = randomThrowable(); final Throwable t = randomThrowable();
real = new Callable<Integer>() { real = new Callable<Integer>() {
@SuppressWarnings("deprecation")
public Integer call() throws Exception { public Integer call() throws Exception {
Thread.currentThread().stop(t); throwThrowable(t);
return null; }}; return null; }};
try { try {
c.call(); c.call();
......
...@@ -31,10 +31,9 @@ import java.util.concurrent.*; ...@@ -31,10 +31,9 @@ import java.util.concurrent.*;
public class Throw { public class Throw {
@SuppressWarnings("deprecation")
static void THROW(final Throwable t) { static void THROW(final Throwable t) {
if (t != null) if (t != null)
Thread.currentThread().stop(t); Throw.<RuntimeException>uncheckedThrow(t);
} }
Callable<Void> thrower(final Throwable t) { Callable<Void> thrower(final Throwable t) {
...@@ -138,4 +137,8 @@ public class Throw { ...@@ -138,4 +137,8 @@ public class Throw {
catch (Throwable t) { catch (Throwable t) {
if (k.isAssignableFrom(t.getClass())) pass(); if (k.isAssignableFrom(t.getClass())) pass();
else unexpected(t);}} else unexpected(t);}}
@SuppressWarnings("unchecked") static <T extends Throwable>
void uncheckedThrow(Throwable t) throws T {
throw (T)t; // rely on vacuous cast
}
} }
...@@ -101,8 +101,10 @@ public class ThrowingTasks { ...@@ -101,8 +101,10 @@ public class ThrowingTasks {
static class Thrower implements Runnable { static class Thrower implements Runnable {
Throwable t; Throwable t;
Thrower(Throwable t) { this.t = t; } Thrower(Throwable t) { this.t = t; }
@SuppressWarnings("deprecation") public void run() {
public void run() { if (t != null) Thread.currentThread().stop(t); } if (t != null)
ThrowingTasks.<RuntimeException>uncheckedThrow(t);
}
} }
static final Thrower noThrower = new Thrower(null); static final Thrower noThrower = new Thrower(null);
...@@ -265,4 +267,8 @@ public class ThrowingTasks { ...@@ -265,4 +267,8 @@ public class ThrowingTasks {
try {realMain(args);} catch (Throwable t) {unexpected(t);} try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");} if (failed > 0) throw new AssertionError("Some tests failed");}
@SuppressWarnings("unchecked") static <T extends Throwable>
void uncheckedThrow(Throwable t) throws T {
throw (T)t; // rely on vacuous cast
}
} }
...@@ -37,7 +37,7 @@ import java.util.concurrent.locks.*; ...@@ -37,7 +37,7 @@ import java.util.concurrent.locks.*;
* tryAcquire method that randomly throws various Throwable * tryAcquire method that randomly throws various Throwable
* subclasses. * subclasses.
*/ */
@SuppressWarnings({"deprecation", "serial"}) @SuppressWarnings("serial")
public class FlakyMutex implements Lock { public class FlakyMutex implements Lock {
static class MyError extends Error {} static class MyError extends Error {}
static class MyException extends Exception {} static class MyException extends Exception {}
...@@ -49,7 +49,7 @@ public class FlakyMutex implements Lock { ...@@ -49,7 +49,7 @@ public class FlakyMutex implements Lock {
switch (rnd.nextInt(10)) { switch (rnd.nextInt(10)) {
case 0: throw new MyError(); case 0: throw new MyError();
case 1: throw new MyRuntimeException(); case 1: throw new MyRuntimeException();
case 2: Thread.currentThread().stop(new MyException()); break; case 2: FlakyMutex.<RuntimeException>uncheckedThrow(new MyException());
default: /* Do nothing */ break; default: /* Do nothing */ break;
} }
} }
...@@ -146,4 +146,8 @@ public class FlakyMutex implements Lock { ...@@ -146,4 +146,8 @@ public class FlakyMutex implements Lock {
try {realMain(args);} catch (Throwable t) {unexpected(t);} try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");} if (failed > 0) throw new AssertionError("Some tests failed");}
@SuppressWarnings("unchecked") static <T extends Throwable>
void uncheckedThrow(Throwable t) throws T {
throw (T)t; // rely on vacuous cast
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册