Skip to content

Commit 3930bbb

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Created ExceptionUtils helper class
1 parent 9e37539 commit 3930bbb

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Add more tests.
1313
- Add `JavaDoc`.
1414

15+
## [1.6.0](https://github.com/appulse-projects/utils-java/releases/tag/1.6.0) - 2018-05-22
16+
17+
Created ExceptionUtils helper class.
18+
19+
### Added
20+
21+
- `ExceptionUtils` helper class.
22+
- Add `ExceptionUtils`.`softException` wrapper for sneaky throws exceptions.
23+
1524
## [1.5.2](https://github.com/appulse-projects/utils-java/releases/tag/1.5.2) - 2018-04-22
1625

1726
Speed-up unsigned functions execution.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ limitations under the License.
2424

2525
<groupId>io.appulse</groupId>
2626
<artifactId>utils-java</artifactId>
27-
<version>1.5.2</version>
27+
<version>1.6.0</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
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 io.appulse.utils;
18+
19+
/**
20+
* Different exception helpers.
21+
*
22+
* @author Artem Labazin
23+
* @since 1.6.0
24+
*/
25+
public final class ExceptionUtils {
26+
27+
/**
28+
* Removes 'checkedness' of the setted exception. It works like @{code @SneakyThrows} annotation in Project Lombok.
29+
* <p>
30+
* For more information, see this:
31+
* http://johannesbrodwall.com/2018/05/15/a-wicked-java-trick-to-make-the-jvm-forget-to-check-exceptions/
32+
*
33+
* @param ex checked exception.
34+
*
35+
* @return exception withoud checkedness.
36+
*
37+
* @since 1.6.0
38+
*/
39+
public static RuntimeException softException (Exception ex) {
40+
return checkednessRemover(ex);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
private static <T extends Exception> T checkednessRemover (Exception ex) throws T {
45+
throw (T) ex;
46+
}
47+
48+
private ExceptionUtils () {
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
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 io.appulse.utils;
18+
19+
import static io.appulse.utils.ExceptionUtils.softException;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22+
23+
import java.io.IOException;
24+
25+
import org.junit.Test;
26+
27+
/**
28+
*
29+
* @author Artem Labazin
30+
* @since 1.6.0
31+
*/
32+
public class ExceptionUtilsTest {
33+
34+
@Test
35+
public void test () {
36+
assertThat(doSomething(true)).isTrue();
37+
38+
assertThatThrownBy(() -> doSomething(false)).isExactlyInstanceOf(IOException.class);
39+
}
40+
41+
private boolean doSomething (boolean value) {
42+
if (value) {
43+
return value;
44+
}
45+
throw softException(new IOException());
46+
}
47+
}

0 commit comments

Comments
 (0)