Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@

@Incubating(since = "7.23.0")
public class RemoveRedundantTypeCast extends Recipe {
private static final String REMOVE_UNNECESSARY_PARENTHESES = "removeUnnecessaryParentheses";

@Override
public String getDisplayName() {
return "Remove redundant casts";
}

@Override
public String getDescription() {
return "Removes unnecessary type casts. Does not currently check casts in lambdas, class constructors, and method invocations.";
return "Removes unnecessary type casts. Does not currently check casts in lambdas and class constructors.";
}

@Override
Expand Down Expand Up @@ -68,8 +70,14 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {

J parentValue = parent.getValue();

J.TypeCast visitedTypeCast = (J.TypeCast) visited;
JavaType expressionType = visitedTypeCast.getExpression().getType();
JavaType castType = visitedTypeCast.getType();

JavaType targetType = null;
if (parentValue instanceof J.VariableDeclarations) {
if (castType.equals(expressionType)) {
targetType = castType;
} else if (parentValue instanceof J.VariableDeclarations) {
targetType = ((J.VariableDeclarations) parentValue).getVariables().get(0).getType();
} else if (parentValue instanceof MethodCall) {
MethodCall methodCall = (MethodCall) parentValue;
Expand Down Expand Up @@ -98,10 +106,6 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
}
}

J.TypeCast visitedTypeCast = (J.TypeCast) visited;
JavaType expressionType = visitedTypeCast.getExpression().getType();
JavaType castType = visitedTypeCast.getType();

if (targetType == null) {
return visitedTypeCast;
}
Expand Down Expand Up @@ -135,11 +139,24 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
if (fullyQualified != null) {
maybeRemoveImport(fullyQualified.getFullyQualifiedName());
}
Cursor directParent = getCursor().getParent();
if (directParent != null && directParent.getParent() != null && directParent.getParent().getValue() instanceof J.Parentheses) {
directParent.getParent().putMessage(REMOVE_UNNECESSARY_PARENTHESES, true);
}
return visitedTypeCast.getExpression().withPrefix(visitedTypeCast.getPrefix());
}
return visitedTypeCast;
}

@Override
public <T extends J> J visitParentheses(J.Parentheses<T> parens, ExecutionContext executionContext) {
J.Parentheses<T> parentheses = (J.Parentheses<T>) super.visitParentheses(parens, executionContext);
Comment thread
jevanlingen marked this conversation as resolved.
Outdated
if (getCursor().getMessage(REMOVE_UNNECESSARY_PARENTHESES, false)) {
return parentheses.getTree();
}
return parentheses;
}

private JavaType getParameterType(JavaType.Method method, int arg) {
List<JavaType> parameterTypes = method.getParameterTypes();
if (parameterTypes.size() > arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,48 @@ public Bar baz() {
)
);
}

@Test
void chainedMethods() {
// language=java
rewriteRun(
java(
"""
class Bar {
String getName() {
return "The bar";
}
}
"""
),
java(
"""
class Foo {
public void getBarName() {
String.format(((Bar) getBar()).getName());
((Bar) getBar()).getName();
(((Bar) getBar())).getName();
}

private Bar getBar() {
return new Bar();
}
}
""",
"""
class Foo {
public void getBarName() {
String.format(getBar().getName());
getBar().getName();
(getBar()).getName();
}

private Bar getBar() {
return new Bar();
}
}
"""
)
);
}
}
Loading