Skip to content

Commit 67f5139

Browse files
committed
[Bugfix] Fix ClassCastException of ExceptionUtil
1 parent 9d201bb commit 67f5139

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

seatunnel-engine/seatunnel-engine-common/src/main/java/org/apache/seatunnel/engine/common/utils/ExceptionUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ public static Throwable peel(Throwable t) {
116116

117117
/** javac hack for unchecking the checked exception. */
118118
@SuppressWarnings("unchecked")
119-
public static <T extends Exception> void sneakyThrow(Throwable t) throws T {
119+
public static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
120+
throw (T) t;
121+
}
122+
123+
@SuppressWarnings("unchecked")
124+
public static <T extends Exception> void sneakyThrow(Exception t) throws T {
120125
throw (T) t;
121126
}
122127

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.seatunnel.engine.common.utils;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
23+
public class ExceptionUtilTest {
24+
25+
@Test
26+
void throwsCheckedException() {
27+
Exception exception = new Exception("Checked Exception");
28+
assertThrows(Exception.class, () -> ExceptionUtil.sneakyThrow(exception));
29+
}
30+
31+
@Test
32+
void throwsUncheckedException() {
33+
RuntimeException exception = new RuntimeException("Unchecked Exception");
34+
assertThrows(RuntimeException.class, () -> ExceptionUtil.sneakyThrow(exception));
35+
}
36+
37+
@Test
38+
void throwsError() {
39+
Error error = new Error("Error");
40+
assertThrows(Error.class, () -> ExceptionUtil.sneakyThrow(error));
41+
}
42+
43+
@Test
44+
void throwsNullPointerExceptionWhenNull() {
45+
assertThrows(NullPointerException.class, () -> ExceptionUtil.sneakyThrow(null));
46+
}
47+
}

0 commit comments

Comments
 (0)