Unnecessary throws
org.openrewrite.staticanalysis.UnnecessaryThrowsArtifactorg.openrewrite.recipe:rewrite-static-analysisRemove unnecessary throws declarations. This recipe will only remove unused, checked exceptions if:
- The declaring class or the method declaration is final.
- The method declaration is static or private.
- The method overrides a method declaration in a super class and the super class does not throw the exception.
- The method is public and the exception is not documented via a JavaDoc as a @throws tag.
The throws declaration is retained on overridable methods (package-private and protected methods on non-final classes), and on public methods overridden within the same source file, so that a subclass override which does throw the exception keeps compiling. Overrides in other source files cannot be detected without a scanning recipe and are therefore not accounted for.
A throws declaration is also retained when the method body contains a call whose type does not fully resolve, because such a call may throw the exception.
When a throws declaration is removed, any @throws or @exception JavaDoc tag documenting that exception is removed along with it, so that the documentation does not describe an exception the method no longer declares.
Declaring exceptions that are never thrown misleads callers into writing unnecessary error-handling code and obscures the method's true behavior.
Add to your build
~/.gradle/gradle.properties
codeGenomeUsername=you@example.com
codeGenomeToken=YOUR_DOWNLOAD_TOKEN
build.gradle.kts
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("org.openrewrite.staticanalysis.UnnecessaryThrows")
}
repositories {
mavenCentral()
maven {
url = uri("https://artifacts.codegenomeproject.org/maven")
credentials {
username = providers.gradleProperty("codeGenomeUsername").get()
password = providers.gradleProperty("codeGenomeToken").get()
}
}
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.44.0")
}
Then run
./gradlew rewriteRunUsage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.staticanalysis.UnnecessaryThrowsIf the recipe isn’t available locally, install it with:
mod config recipes jar install org.openrewrite.recipe:rewrite-static-analysis:RELEASEData tables
Structured output this recipe can produce.
- Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats