Skip to content

Commit

Permalink
Skip constructors in UnnecessaryThrows for now (#444)
Browse files Browse the repository at this point in the history
* add failing test for issue #443 in UnnecessaryThrowsTest

* Skip constructors in `UnnecessaryThrows` for now

Fixes #443

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
sebastianhaeni and timtebeek authored Jan 13, 2025
1 parent 648b69f commit 1b90aec
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void removeThrownTypes(JavaType.@Nullable Method type) {

private Set<JavaType.FullyQualified> findExceptionCandidates(J.@Nullable MethodDeclaration method) {

if (method == null || method.getMethodType() == null || method.isAbstract()) {
if (method == null || method.getMethodType() == null || method.isAbstract() || method.isConstructor()) {
return Collections.emptySet();
}

Expand All @@ -154,7 +154,7 @@ private Set<JavaType.FullyQualified> findExceptionCandidates(J.@Nullable MethodD
if (exception.getType() == null || exception.getType() instanceof JavaType.Unknown) {
return Collections.emptySet();
}
if (exception.getType() instanceof JavaType.FullyQualified && !TypeUtils.isAssignableTo(JavaType.ShallowClass.build("java.lang.RuntimeException"), exception.getType())) {
if (exception.getType() instanceof JavaType.FullyQualified && !TypeUtils.isAssignableTo("java.lang.RuntimeException", exception.getType())) {
candidates.add(TypeUtils.asFullyQualified(exception.getType()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
Expand Down Expand Up @@ -180,6 +181,61 @@ void test() throws Exception {
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/443")
@Test
void necessaryThrowsFromConstructor() {
rewriteRun(
//language=java
java(
"""
import java.io.IOException;
class Test {
String str = test();
Test() throws IOException {}
String test() throws IOException {
throw new IOException();
}
}
"""
)
);
}

@ExpectedToFail("Not yet implemented")
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/443")
@Test
void necessaryThrowsFromConstructorWithUnused() {
rewriteRun(
//language=java
java(
"""
import java.io.IOException;
import java.util.concurrent.ExecutionException;
class Test {
String str = test();
Test() throws IOException, ExecutionException {}
String test() throws IOException {
throw new IOException();
}
}
""",
"""
import java.io.IOException;
class Test {
String str = test();
Test() throws IOException {}
String test() throws IOException {
throw new IOException();
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/897")
@Test
void necessaryThrowsOnInterfaceWithExplicitOverride() {
Expand Down

0 comments on commit 1b90aec

Please sign in to comment.