Skip to content

Commit

Permalink
refactor: Enum values should be compared with "==" (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne authored Dec 25, 2024
1 parent 9993a67 commit ea60250
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (ASSERT_EQUALS.matches(mi) && isBooleanLiteral(mi) &&
JavaType.Primitive.Boolean.equals(mi.getArguments().get(1).getType())) {
JavaType.Primitive.Boolean == mi.getArguments().get(1).getType()) {
StringBuilder sb = new StringBuilder();
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource()) ?
"assertTrue" : "assertFalse";
Expand Down Expand Up @@ -98,7 +98,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private boolean isBooleanLiteral(J.MethodInvocation method) {
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Literal) {
J.Literal literal = (J.Literal) method.getArguments().get(0);
return JavaType.Primitive.Boolean.equals(literal.getType());
return JavaType.Primitive.Boolean == literal.getType();
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private boolean isUnaryOperatorNot(J.MethodInvocation method) {
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Unary) {
J.Unary unary = (J.Unary) method.getArguments().get(0);
return unary.getOperator().equals(J.Unary.Type.Not);
return unary.getOperator() == J.Unary.Type.Not;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private boolean isBooleanLiteral(J.MethodInvocation method) {
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Literal) {
J.Literal literal = (J.Literal) method.getArguments().get(0);
return JavaType.Primitive.Boolean.equals(literal.getType());
return JavaType.Primitive.Boolean == literal.getType();
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private boolean isEqualBinary(J.MethodInvocation method) {
J.Binary binary = (J.Binary) firstArgument;
J.Binary.Type operator = binary.getOperator();

if (!operator.equals(J.Binary.Type.Equal)) {
if (operator != J.Binary.Type.Equal) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private boolean isUnaryOperatorNot(J.MethodInvocation method) {
if (!method.getArguments().isEmpty() && method.getArguments().get(0) instanceof J.Unary) {
J.Unary unary = (J.Unary) method.getArguments().get(0);
return unary.getOperator().equals(J.Unary.Type.Not);
return unary.getOperator() == J.Unary.Type.Not;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
.imports(NESTED)
.build()
.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
cd.getModifiers().removeIf(modifier -> modifier.getType().equals(J.Modifier.Type.Static));
cd.getModifiers().removeIf(modifier -> modifier.getType() == J.Modifier.Type.Static);
return maybeAutoFormat(classDecl, cd, ctx);
}
return cd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex

// If the method is an init-method then add a static modifier if necessary
if (initMethods.contains(m.getSimpleName()) || initMethodReferences.containsValue(m.getSimpleName())) {
if (m.getModifiers().stream().noneMatch(it -> J.Modifier.Type.Static.equals(it.getType()))) {
if (m.getModifiers().stream().noneMatch(it -> J.Modifier.Type.Static == it.getType())) {
J.Modifier staticModifier = new J.Modifier(Tree.randomId(), Space.format(" "), Markers.EMPTY, null, J.Modifier.Type.Static, new ArrayList<>());
m = maybeAutoFormat(m, m.withModifiers(ListUtils.concat(m.getModifiers(), staticModifier)), ctx, getCursor().getParentTreeCursor());
}
Expand Down

0 comments on commit ea60250

Please sign in to comment.