-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Fix-PMD-rule-LiteralsFirstInComparisons-for-…
…compareTo-and-contentEquals
- Loading branch information
Showing
12 changed files
with
376 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip | ||
distributionSha256Sum=7a00d51fb93147819aab76024feece20b6b84e420694101f276be952e08bef03 | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip | ||
distributionSha256Sum=8d97a97984f6cbd2b85fe4c60a743440a347544bf18818048e611f5288d46c94 | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/openrewrite/staticanalysis/AbstractClassPublicConstructor.java
This file contains 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,66 @@ | ||
/* | ||
* 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.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.ListUtils; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static org.openrewrite.java.tree.J.Modifier.Type.*; | ||
|
||
public class AbstractClassPublicConstructor extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Constructors of an `abstract` class should not be declared `public`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Constructors of `abstract` classes can only be called in constructors of their subclasses. " + | ||
"Therefore the visibility of `public` constructors are reduced to `protected`."; | ||
} | ||
|
||
@Override | ||
public Set<String> getTags() { | ||
return Collections.singleton("RSPEC-S5993"); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); | ||
if (cd.hasModifier(Abstract)) { | ||
return cd.withBody(cd.getBody().withStatements(ListUtils.map(cd.getBody().getStatements(), st -> { | ||
if (st instanceof J.MethodDeclaration && ((J.MethodDeclaration) st).isConstructor()) { | ||
return ((J.MethodDeclaration) st).withModifiers(ListUtils.map(((J.MethodDeclaration) st).getModifiers(), | ||
mod -> mod.getType() == Public ? mod.withType(Protected) : mod)); | ||
} | ||
return st; | ||
}))); | ||
} | ||
return cd; | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
103 changes: 103 additions & 0 deletions
103
src/test/java/org/openrewrite/staticanalysis/AbstractClassPublicConstructorTest.java
This file contains 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,103 @@ | ||
/* | ||
* 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.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.Issue; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
@SuppressWarnings("java:S2699") | ||
class AbstractClassPublicConstructorTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new AbstractClassPublicConstructor()); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void replacePublicByProtected() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
abstract class Test { | ||
public Test() { | ||
} | ||
} | ||
""", | ||
""" | ||
abstract class Test { | ||
protected Test() { | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noReplaceOnNonAbstractClass() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
class Test { | ||
public Test() { | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noReplaceOnPackageProtectedConstructor() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
abstract class Test { | ||
Test() { | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/449") | ||
@Test | ||
void noReplaceOnNestedClasses() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
abstract class Test { | ||
static class Nested { | ||
public Nested() { | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.