Skip to content

Commit b670b5a

Browse files
authored
Skip UnnecessaryCatch after any missing types via a precondition (#784)
1 parent 0f1d5db commit b670b5a

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/main/java/org/openrewrite/staticanalysis/UnnecessaryCatch.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20-
import org.openrewrite.ExecutionContext;
21-
import org.openrewrite.Option;
22-
import org.openrewrite.Recipe;
23-
import org.openrewrite.TreeVisitor;
20+
import org.openrewrite.*;
2421
import org.openrewrite.internal.ListUtils;
2522
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.NoMissingTypes;
2624
import org.openrewrite.java.tree.*;
2725
import org.openrewrite.java.tree.J.NewClass;
2826

2927
import java.util.*;
30-
import java.util.concurrent.atomic.AtomicBoolean;
3128

3229
@EqualsAndHashCode(callSuper = false)
3330
@Value
@@ -61,8 +58,7 @@ public String getDescription() {
6158

6259
@Override
6360
public TreeVisitor<?, ExecutionContext> getVisitor() {
64-
return new JavaIsoVisitor<ExecutionContext>() {
65-
61+
return Preconditions.check(new NoMissingTypes(), new JavaIsoVisitor<ExecutionContext>() {
6662
private static final String JAVA_LANG_EXCEPTION = "java.lang.Exception";
6763
private static final String JAVA_LANG_ERROR = "java.lang.Error";
6864
private static final String JAVA_LANG_RUNTIME_EXCEPTION = "java.lang.RuntimeException";
@@ -93,16 +89,12 @@ public J.Try visitTry(J.Try tryable, ExecutionContext ctx) {
9389
}
9490

9591
List<JavaType> thrownExceptions = new ArrayList<>();
96-
AtomicBoolean missingTypeInformation = new AtomicBoolean(false);
9792
//Collect any checked exceptions thrown from the try block.
9893
new JavaIsoVisitor<Integer>() {
9994
@Override
10095
public NewClass visitNewClass(NewClass newClass, Integer integer) {
10196
JavaType.Method methodType = newClass.getMethodType();
102-
if (methodType == null) {
103-
//Do not make any changes if there is missing type information.
104-
missingTypeInformation.set(true);
105-
} else {
97+
if (methodType != null) {
10698
thrownExceptions.addAll(methodType.getThrownExceptions());
10799
}
108100
return super.visitNewClass(newClass, integer);
@@ -111,10 +103,7 @@ public NewClass visitNewClass(NewClass newClass, Integer integer) {
111103
@Override
112104
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) {
113105
JavaType.Method methodType = method.getMethodType();
114-
if (methodType == null) {
115-
//Do not make any changes if there is missing type information.
116-
missingTypeInformation.set(true);
117-
} else {
106+
if (methodType != null) {
118107
thrownExceptions.addAll(methodType.getThrownExceptions());
119108
}
120109
return super.visitMethodInvocation(method, integer);
@@ -123,21 +112,13 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
123112
@Override
124113
public J.Throw visitThrow(J.Throw thrown, Integer integer) {
125114
JavaType type = thrown.getException().getType();
126-
if (type == null) {
127-
//Do not make any changes if there is missing type information.
128-
missingTypeInformation.set(true);
129-
} else {
115+
if (type != null) {
130116
thrownExceptions.add(type);
131117
}
132118
return super.visitThrow(thrown, integer);
133119
}
134120
}.visit(t.getBody(), 0);
135121

136-
//If there is any missing type information, it is not safe to make any transformations.
137-
if (missingTypeInformation.get()) {
138-
return t;
139-
}
140-
141122
Set<JavaType> unnecessaryTypes = getUnnecessaryTypes(t, thrownExceptions);
142123
if (unnecessaryTypes.isEmpty()) {
143124
return t;
@@ -254,6 +235,6 @@ private boolean isCheckedException(JavaType type) {
254235
!TypeUtils.isOfClassType(exceptionClass, JAVA_LANG_EXCEPTION) &&
255236
!TypeUtils.isOfClassType(exceptionClass, JAVA_LANG_THROWABLE);
256237
}
257-
};
238+
});
258239
}
259240
}

0 commit comments

Comments
 (0)