Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* Copyright 2026 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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.*;
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;

import java.time.Duration;
import java.util.List;
import java.util.Set;

import static java.util.Collections.singleton;

@EqualsAndHashCode(callSuper = false)
@Value
public class RemoveMethodsOnlyCallSuper extends Recipe {

String displayName = "Remove methods that only call super";

String description = "Methods that override a parent method but only call `super` with the same arguments are redundant and should be removed.";

Set<String> tags = singleton("RSPEC-S1185");

Duration estimatedEffortPerOccurrence = Duration.ofMinutes(2);

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.not(new KotlinFileChecker<>()), new JavaVisitor<ExecutionContext>() {
@Override
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration md = (J.MethodDeclaration) super.visitMethodDeclaration(method, ctx);
JavaType.Method methodType = md.getMethodType();
if (md.isConstructor() || methodType == null || !TypeUtils.isOverride(methodType)) {
return md;
}

J.Block body = md.getBody();
if (body == null || body.getStatements().size() != 1) {
return md;
}

J.MethodInvocation superCall = extractSuperCall(body.getStatements().get(0));
if (superCall == null) {
return md;
}

// Verify the call target is "super"
Expression select = superCall.getSelect();
if (!(select instanceof J.Identifier) || !"super".equals(((J.Identifier) select).getSimpleName())) {
return md;
}

// Method name must match
if (!superCall.getSimpleName().equals(md.getSimpleName())) {
return md;
}

// Arguments must match parameters in same order
if (!argumentsMatchParameters(md.getParameters(), superCall.getArguments())) {
return md;
}

// Skip if method has annotations other than @Override
for (J.Annotation annotation : md.getLeadingAnnotations()) {
JavaType annotationType = annotation.getAnnotationType().getType();
if (annotationType == null || !TypeUtils.isOfClassType(annotationType, "java.lang.Override")) {
return md;
}
}

// Skip if method has Javadoc comments
for (Comment comment : md.getPrefix().getComments()) {
if (comment instanceof Javadoc) {
return md;
}
}

// Skip if method is final (prevents further overriding)
if (methodType.hasFlags(Flag.Final)) {
return md;
}

// Skip if method widens visibility compared to the overridden method
if (widensVisibility(methodType)) {
return md;
}

//noinspection DataFlowIssue
return null;
}

private J.@Nullable MethodInvocation extractSuperCall(Statement statement) {
if (statement instanceof J.MethodInvocation) {
return (J.MethodInvocation) statement;
}
if (statement instanceof J.Return) {
Expression expr = ((J.Return) statement).getExpression();
if (expr instanceof J.MethodInvocation) {
return (J.MethodInvocation) expr;
}
}
return null;
}

private boolean argumentsMatchParameters(List<Statement> parameters, List<Expression> arguments) {
int argIndex = 0;
int paramCount = 0;
for (Statement param : parameters) {
if (!(param instanceof J.VariableDeclarations)) {
continue;
}
paramCount++;
J.VariableDeclarations varDecls = (J.VariableDeclarations) param;
if (varDecls.getVariables().size() != 1) {
return false;
}
String paramName = varDecls.getVariables().get(0).getSimpleName();

// Find next non-empty argument
while (argIndex < arguments.size() && arguments.get(argIndex) instanceof J.Empty) {
argIndex++;
}
if (argIndex >= arguments.size()) {
return false;
}

Expression arg = arguments.get(argIndex);
if (!(arg instanceof J.Identifier) || !paramName.equals(((J.Identifier) arg).getSimpleName())) {
return false;
}
argIndex++;
}

// Verify no extra non-empty arguments remain
while (argIndex < arguments.size()) {
if (!(arguments.get(argIndex) instanceof J.Empty)) {
return false;
}
argIndex++;
}
return paramCount > 0 || arguments.isEmpty() || arguments.stream().allMatch(a -> a instanceof J.Empty);
}

private boolean widensVisibility(JavaType.Method methodType) {
int childVisibility = visibilityRank(methodType);
JavaType.FullyQualified declaringType = methodType.getDeclaringType();

// Check supertype methods
JavaType.FullyQualified supertype = declaringType.getSupertype();
if (supertype != null) {
for (JavaType.Method parentMethod : supertype.getMethods()) {
if (isMatchingMethod(methodType, parentMethod) && childVisibility > visibilityRank(parentMethod)) {
return true;
}
}
}

// Check interface default methods
for (JavaType.FullyQualified iface : declaringType.getInterfaces()) {
for (JavaType.Method ifaceMethod : iface.getMethods()) {
if (isMatchingMethod(methodType, ifaceMethod) && childVisibility > visibilityRank(ifaceMethod)) {
return true;
}
}
}

return false;
}

private boolean isMatchingMethod(JavaType.Method child, JavaType.Method parent) {
if (!child.getName().equals(parent.getName())) {
return false;
}
List<JavaType> childParams = child.getParameterTypes();
List<JavaType> parentParams = parent.getParameterTypes();
if (childParams.size() != parentParams.size()) {
return false;
}
for (int i = 0; i < childParams.size(); i++) {
if (!TypeUtils.isOfType(childParams.get(i), parentParams.get(i))) {
return false;
}
}
return true;
}

private int visibilityRank(JavaType.Method method) {
if (method.hasFlags(Flag.Public)) {
return 3;
}
if (method.hasFlags(Flag.Protected)) {
return 2;
}
if (method.hasFlags(Flag.Private)) {
return 0;
}
return 1; // package-private
}
});
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/rewrite/recipes.csv
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Stray semicolons are typically typos or remnants of refactoring and can mislead
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveHashCodeCallsFromArrayInstances,`hashCode()` should not be called on array instances,"Replace `hashCode()` calls on arrays with `Arrays.hashCode()` because the results from `hashCode()` are not helpful. Arrays inherit `hashCode()` from `Object`, which returns an identity-based value unrelated to the array contents, so two arrays with identical elements will produce different hash codes.",1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveInstanceOfPatternMatch,Removes from code Java 14's `instanceof` pattern matching,Adds an explicit variable declaration at the beginning of `if` statement instead of `instanceof` pattern matching.,1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveJavaDocAuthorTag,Remove author tags from JavaDocs,Removes author tags from JavaDocs to reduce code maintenance.,1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveMethodsOnlyCallSuper,Remove methods that only call super,Methods that override a parent method but only call `super` with the same arguments are redundant and should be removed.,1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveRedundantNullCheckBeforeInstanceof,Remove redundant null checks before instanceof,Removes redundant null checks before instanceof operations since instanceof returns false for null. Removing the extra check simplifies the conditional and makes the null-safety guarantee of `instanceof` more visible to readers.,1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveRedundantNullCheckBeforeLiteralEquals,Remove redundant null checks before literal equals,"Removes redundant null checks before `equals()` comparisons when the receiver is a literal string, since literals can never be null and `equals()` returns false for null arguments.",1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.RemoveRedundantTypeCast,Remove redundant casts,"Removes unnecessary type casts. Does not currently check casts in lambdas and class constructors. Redundant casts add visual noise and can obscure the actual type relationships in the code, making it harder to follow the data flow.",1,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
Expand Down Expand Up @@ -198,7 +199,7 @@ maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanaly
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.UseMapContainsKey,Use `Map#containsKey`,`map.keySet().contains(a)` can be simplified to `map.containsKey(a)`.,2,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNull,Replace `org.apache.commons.lang3.Validate#notNull` with `Objects#requireNonNull`,Replace `org.apache.commons.lang3.Validate.notNull(..)` with `Objects.requireNonNull(..)`.,5,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.ReplaceValidateNotNullHavingSingleArgWithObjectsRequireNonNull,Replace `org.apache.commons.lang3.Validate#notNull` with `Objects#requireNonNull`,Replace `org.apache.commons.lang3.Validate.notNull(Object)` with `Objects.requireNonNull(Object)`.,3,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.CodeCleanup,Code cleanup,"Automatically cleanup code, e.g. remove unnecessary parentheses, simplify expressions.",25,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.CodeCleanup,Code cleanup,"Automatically cleanup code, e.g. remove unnecessary parentheses, simplify expressions.",26,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.ReplaceThreadRunWithThreadStart,Replace calls to `Thread.run()` with `Thread.start()`,`Thread.run()` should not be called directly.,2,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.CommonDeclarationSiteTypeVariances,Properly use declaration-site type variance for well-known types,"When using a method parameter like `Function<IN, OUT>`, it should rather be `Function<? super IN, ? extends OUT>`. This recipe checks for method parameters of well-known types.",2,,Static analysis and remediation,,Remediations for issues identified by SAST tools.,
maven,org.openrewrite.recipe:rewrite-static-analysis,org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType,Move annotation to type instead of field,"Annotations that could be applied to either a field or a type are better applied to the type, because similar annotations may be more restrictive, leading to compile errors like 'scoping construct cannot be annotated with type-use annotation' when migrating later.",1,Java,Static analysis and remediation,,Remediations for issues identified by SAST tools.,"[{""name"":""annotationType"",""type"":""String"",""displayName"":""Annotation type"",""description"":""The type of annotation to move."",""example"":""org.openrewrite..*""}]"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ recipeList:
- org.openrewrite.staticanalysis.UnnecessaryParentheses
- org.openrewrite.staticanalysis.ReplaceThreadRunWithThreadStart
- org.openrewrite.staticanalysis.ChainStringBuilderAppendCalls
- org.openrewrite.staticanalysis.RemoveMethodsOnlyCallSuper
- org.openrewrite.staticanalysis.ReplaceStringBuilderWithString
- org.openrewrite.java.ShortenFullyQualifiedTypeReferences
- org.openrewrite.java.SimplifySingleElementAnnotation
Expand Down
Loading
Loading