-
Notifications
You must be signed in to change notification settings - Fork 90
Add OnlyCatchDeclaredExceptions recipe to address RSPEC-S2221
#601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
eab8f49
(RSPEC-S2221) SpecifyGenericExceptionCatches recipe
e61f000
Update src/main/java/org/openrewrite/staticanalysis/SpecifyGenericExc…
stefanodallapalma 3d18c37
Update src/test/java/org/openrewrite/staticanalysis/SpecifyGenericExc…
stefanodallapalma 00e9021
Apply suggestions from code review
stefanodallapalma 0a7c667
Fixed conflict introduced by applying batch suggestions
stefanodallapalma b45b786
Apply formatter
timtebeek 3bdf1ac
No need for a named internal class
timtebeek e72097c
Merge branch 'main' into s2221
timtebeek 60fb77e
Merge branch 'openrewrite:main' into s2221
stefanodallapalma 0c51042
Merge branch 'main' into s2221
timtebeek f393b20
Show difference between declared and undeclared runtime exceptions
timtebeek 6614563
Use `reduce`
timtebeek cde05ed
Use `ListUtils.map`
timtebeek 7d4cfe2
Handle existing multi catch that's incomplete
timtebeek 7c88c9a
Rename recipe to make intention clear
timtebeek f1da21c
Drop need for `hasGenericCatch`
timtebeek 287ba46
Rename method to show intention
timtebeek 527cd14
Add a precondition to limit where recipe applies
timtebeek 8e94573
Generate template with shortened types and imports
timtebeek c8d13cc
Always add imports in case of missing types
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
src/main/java/org/openrewrite/staticanalysis/SpecifyGenericExceptionCatches.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.openrewrite.Cursor; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.ShortenFullyQualifiedTypeReferences; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.JavaType.FullyQualified; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
||
| public class SpecifyGenericExceptionCatches extends Recipe { | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Replace `catch(Exception)` with specific exceptions thrown in the try block"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replaces `catch(Exception e)` blocks with a multi-catch block " + | ||
| "(`catch (SpecificException1 | SpecificException2 e)`) containing only the checked exceptions " + | ||
| "explicitly thrown by method or constructor invocations within the `try` block that are not " + | ||
| "already caught by more specific `catch` clauses. If no checked exceptions are found that " + | ||
| "require catching, the generic `catch` block is removed."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.unmodifiableSet(new HashSet<>(Arrays.asList("CWE-396", "RSPEC-S2221"))); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaVisitor<ExecutionContext> getVisitor() { | ||
| return new GenericExceptionCatchVisitor(); | ||
| } | ||
|
|
||
| private static class GenericExceptionCatchVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
|
||
| private static final String JAVA_LANG_EXCEPTION = "java.lang.Exception"; | ||
| private static final String MULTI_CATCH_SEPARATOR = "|"; | ||
| private static final String TRY_CATCH_TEMPLATE = "try {} catch (%s %s) {}"; | ||
|
|
||
| @Override | ||
| public J.Try visitTry(J.Try aTry, ExecutionContext ctx) { | ||
| J.Try t = super.visitTry(aTry, ctx); | ||
|
|
||
| if (hasGenericCatch(t)) { | ||
| Set<JavaType> caughtExceptions = getCaughtExceptions(t); | ||
| Set<JavaType> thrownExceptions = getThrownExceptions(t); | ||
| thrownExceptions.removeAll(caughtExceptions); // Remove exceptions that are already specifically caught | ||
stefanodallapalma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!thrownExceptions.isEmpty()) { | ||
| List<J.Try.Catch> updatedCatches = t.getCatches().stream() | ||
| .map(c -> updateCatchIfGeneric(c, thrownExceptions)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return t.withCatches(updatedCatches); | ||
| } | ||
| } | ||
|
|
||
| return t; | ||
| } | ||
|
|
||
| private static boolean isGenericCatch(J.Try.Catch aCatch) { | ||
| FullyQualified fq = TypeUtils.asFullyQualified(aCatch.getParameter().getType()); | ||
| if (fq != null) { | ||
| String fqn = fq.getFullyQualifiedName(); | ||
| return TypeUtils.fullyQualifiedNamesAreEqual(JAVA_LANG_EXCEPTION, fqn); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean hasGenericCatch(J.Try aTry) { | ||
| for (J.Try.Catch c : aTry.getCatches()) { | ||
| if (isGenericCatch(c)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private Set<JavaType> getCaughtExceptions(J.Try aTry) { | ||
| Set<JavaType> caughtExceptions = new HashSet<>(); | ||
|
|
||
| for (J.Try.Catch c : aTry.getCatches()) { | ||
| if (c.getParameter().getType() != null) { | ||
| caughtExceptions.add(c.getParameter().getType()); | ||
| } | ||
| } | ||
|
|
||
| return caughtExceptions; | ||
| } | ||
|
|
||
| /** | ||
| * Collects all checked exceptions that are explicitly thrown by method invocations | ||
| * and constructor calls within the try block. | ||
| * | ||
| * @param aTry the try block to analyze | ||
| * @return a set of exception types that may be thrown by code in the try block | ||
| */ | ||
| private Set<JavaType> getThrownExceptions(J.Try aTry) { | ||
| Set<JavaType> thrownExceptions = new HashSet<>(); | ||
| new JavaIsoVisitor<Set<JavaType>>() { | ||
| @Override | ||
| public J.NewClass visitNewClass(J.NewClass nc, Set<JavaType> set) { | ||
| if (nc.getConstructorType() != null) { | ||
| set.addAll(nc.getConstructorType().getThrownExceptions()); | ||
| } | ||
| return super.visitNewClass(nc, set); | ||
| } | ||
|
|
||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, Set<JavaType> set) { | ||
| if (mi.getMethodType() != null) { | ||
| set.addAll(mi.getMethodType().getThrownExceptions()); | ||
| } | ||
| return super.visitMethodInvocation(mi, set); | ||
| } | ||
| }.visit(aTry.getBody(), thrownExceptions); | ||
| return thrownExceptions; | ||
| } | ||
|
|
||
| /** | ||
| * Updates a generic catch block (catching java.lang.Exception) to catch only the specific | ||
| * exception types that are actually thrown within the try block. | ||
| * | ||
| * <p>This method transforms generic catch blocks like: | ||
| * <pre>{@code | ||
| * catch (Exception e) { ... } | ||
| * }</pre> | ||
| * | ||
| * into specific single or multi-catch blocks like: | ||
| * <pre>{@code | ||
| * catch (IOException e) { ... } | ||
| * // or | ||
| * catch (IOException | SQLException e) { ... } | ||
| * }</pre> | ||
| * | ||
| * @param aCatch the catch block to potentially update | ||
| * @param thrownExceptions the set of specific exception types thrown in the try block | ||
| * that are not already caught by other catch clauses | ||
| * @return the original catch block if it doesn't catch java.lang.Exception, | ||
| * otherwise a new catch block with specific exception types | ||
| */ | ||
| private J.Try.Catch updateCatchIfGeneric(J.Try.Catch aCatch, Set<JavaType> thrownExceptions) { | ||
| if (!isGenericCatch(aCatch)) { | ||
| return aCatch; | ||
| } | ||
|
|
||
| // Preserve the existing variable name from the original generic catch block | ||
| String variableName = aCatch.getParameter().getTree().getVariables().get(0).getSimpleName(); | ||
|
|
||
| String throwableTypes = thrownExceptions.stream() | ||
| .map(TypeUtils::asFullyQualified) | ||
| .filter(Objects::nonNull) | ||
| .sorted(Comparator.comparing(FullyQualified::getClassName)) | ||
| .map(FullyQualified::getFullyQualifiedName) | ||
| .collect(Collectors.joining(MULTI_CATCH_SEPARATOR)); | ||
|
|
||
| J.Try aTry = getCursor().firstEnclosing(J.Try.class); | ||
| assert aTry != null; | ||
|
|
||
| J.Try tempTry = JavaTemplate.builder(String.format(TRY_CATCH_TEMPLATE, throwableTypes, variableName)) | ||
| .contextSensitive() | ||
| .build() | ||
| .apply( | ||
| new Cursor(getCursor(), aTry), | ||
| aTry.getCoordinates().replace() | ||
| ); | ||
|
|
||
| J.ControlParentheses<J.VariableDeclarations> cp = tempTry.getCatches().get(0).getParameter(); | ||
| doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(cp.getTree())); | ||
| return aCatch.withParameter(cp); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.