Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Enum values should be compared with "==" #414

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -77,7 +77,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex

String ecfqn = type.getFullyQualifiedName();
if (m.hasModifier(J.Modifier.Type.Public) && m.getReturnTypeExpression() != null &&
JavaType.Primitive.Boolean.equals(m.getReturnTypeExpression().getType()) &&
JavaType.Primitive.Boolean == m.getReturnTypeExpression().getType() &&
new MethodMatcher(ecfqn + " equals(" + ecfqn + ")").matches(m, enclosingClass)) {

if (!service(AnnotationService.class).matches(getCursor(), OVERRIDE_ANNOTATION)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ public J.Switch visitSwitch(J.Switch switch_, P p) {
private boolean isEmptyBlock(Statement blockNode) {
if (blockNode instanceof J.Block) {
J.Block block = (J.Block) blockNode;
if (EmptyBlockStyle.BlockPolicy.STATEMENT.equals(emptyBlockStyle.getBlockPolicy())) {
if (EmptyBlockStyle.BlockPolicy.STATEMENT == emptyBlockStyle.getBlockPolicy()) {
return block.getStatements().isEmpty();
} else if (EmptyBlockStyle.BlockPolicy.TEXT.equals(emptyBlockStyle.getBlockPolicy())) {
} else if (EmptyBlockStyle.BlockPolicy.TEXT == emptyBlockStyle.getBlockPolicy()) {
return block.getStatements().isEmpty() && block.getEnd().getComments().isEmpty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
}
if (variable.getInitializer() instanceof J.Literal && !variableDecls.hasModifier(J.Modifier.Type.Final)) {
J.Literal literalInit = (J.Literal) variable.getInitializer();
if (TypeUtils.asFullyQualified(variable.getType()) != null && JavaType.Primitive.Null.equals(literalInit.getType())) {
if (TypeUtils.asFullyQualified(variable.getType()) != null && JavaType.Primitive.Null == literalInit.getType()) {
v = v.withInitializer(null);
} else if (primitive != null && !Boolean.TRUE.equals(style.getOnlyObjectReferences())) {
switch (primitive) {
Expand All @@ -91,7 +91,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
}
break;
}
} else if (array != null && JavaType.Primitive.Null.equals(literalInit.getType())) {
} else if (array != null && JavaType.Primitive.Null == literalInit.getType()) {
v = v.withInitializer(null)
.withDimensionsAfterName(ListUtils.map(v.getDimensionsAfterName(), (i, dim) ->
i == 0 ? dim.withBefore(Space.EMPTY) : dim));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private boolean hasFinalModifiers(final List<Statement> parameters) {
final List<J.Modifier> modifiers = ((J.VariableDeclarations) p).getModifiers();
return !modifiers.isEmpty() &&
modifiers.stream()
.allMatch(m -> m.getType().equals(J.Modifier.Type.Final));
.allMatch(m -> m.getType() == J.Modifier.Type.Final);
}
return false;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static Set<J.VariableDeclarations.NamedVariable> find(J j, J.VariableDecl
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set<J.VariableDeclarations.NamedVariable> ctx) {
// do not go into static inner classes, interfaces, or enums which have a different name scope
if (!(classDecl.getKind().equals(J.ClassDeclaration.Kind.Type.Class)) || classDecl.hasModifier(J.Modifier.Type.Static)) {
if (classDecl.getKind() != J.ClassDeclaration.Kind.Type.Class || classDecl.hasModifier(J.Modifier.Type.Static)) {
return classDecl;
}
return super.visitClassDeclaration(classDecl, ctx);
Expand Down Expand Up @@ -241,7 +241,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
J.MethodDeclaration md = (J.MethodDeclaration) maybeMethodDecl;

boolean doesSetterReturnItsClass = md.getReturnTypeExpression() != null && TypeUtils.isOfType(targetVariableEnclosingClass.getType(), md.getReturnTypeExpression().getType());
boolean isSetterVoid = md.getReturnTypeExpression() != null && JavaType.Primitive.Void.equals(md.getReturnTypeExpression().getType());
boolean isSetterVoid = md.getReturnTypeExpression() != null && JavaType.Primitive.Void == md.getReturnTypeExpression().getType();
boolean doesMethodNameCorrespondToVariable = md.getSimpleName().startsWith("set") && md.getSimpleName().toLowerCase().endsWith(variable.getSimpleName().toLowerCase());
isIgnorableSetter = doesMethodNameCorrespondToVariable &&
(hiddenFieldStyle.getSetterCanReturnItsClass() ? (doesSetterReturnItsClass || isSetterVoid) : isSetterVoid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private class UtilityClassWithImplicitDefaultConstructorVisitor extends JavaIsoV
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
if (utilityClassMatcher.hasImplicitDefaultConstructor(classDecl) &&
!J.ClassDeclaration.Kind.Type.Enum.equals(classDecl.getKind())) {
J.ClassDeclaration.Kind.Type.Enum != classDecl.getKind()) {
classDecl = JavaTemplate.builder("private #{}() {}")
.contextSensitive()
.build()
Expand Down Expand Up @@ -125,7 +125,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, p);
if (md.getMethodType() == null || !md.isConstructor() ||
(md.hasModifier(J.Modifier.Type.Private) || md.hasModifier(J.Modifier.Type.Protected) || md.getMethodType().getDeclaringType().getKind().equals(JavaType.Class.Kind.Enum))) {
(md.hasModifier(J.Modifier.Type.Private) || md.hasModifier(J.Modifier.Type.Protected) || md.getMethodType().getDeclaringType().getKind() == JavaType.Class.Kind.Enum)) {
return md;
}

Expand Down Expand Up @@ -188,7 +188,7 @@ boolean hasMainMethod(J.ClassDeclaration c) {
md.hasModifier(J.Modifier.Type.Public) &&
md.hasModifier(J.Modifier.Type.Static) &&
md.getReturnTypeExpression() != null &&
JavaType.Primitive.Void.equals(md.getReturnTypeExpression().getType()) &&
JavaType.Primitive.Void == md.getReturnTypeExpression().getType() &&

// note that the matcher for "main(String)" will match on "main(String[]) as expected.
new MethodMatcher(c.getType().getFullyQualifiedName() + " main(String[])")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public J.If visitIf(J.If iff, ExecutionContext ctx) {
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
JavaType.Method methodType = m.getMethodType();
if (m.getBody() != null && methodType != null && JavaType.Primitive.Void.equals(methodType.getReturnType())) {
if (m.getBody() != null && methodType != null && JavaType.Primitive.Void == methodType.getReturnType()) {
return m.withBody(m.getBody().withStatements(ListUtils.mapLast(m.getBody().getStatements(), s -> s instanceof J.Return ? null : s)));
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/openrewrite/staticanalysis/OperatorWrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx) {
(Boolean.TRUE.equals(operatorWrapStyle.getBor()) && op == J.Binary.Type.BitOr) ||
(Boolean.TRUE.equals(operatorWrapStyle.getLand()) && op == J.Binary.Type.And) ||
(Boolean.TRUE.equals(operatorWrapStyle.getLor()) && op == J.Binary.Type.Or)) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (b.getRight().getPrefix().getWhitespace().contains("\n")) {
b = b.getPadding().withOperator(
b.getPadding().getOperator().withBefore(
Expand Down Expand Up @@ -131,7 +131,7 @@ public J.TypeParameter visitTypeParameter(J.TypeParameter typeParam, ExecutionCo
tp.getPadding().getBounds().getPadding().withElements(
ListUtils.map(tp.getPadding().getBounds().getPadding().getElements(),
(index, elemContainer) -> {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (index != typeBoundsSize - 1 && typeParam.getPadding().getBounds() != null) {
JRightPadded<TypeTree> next = typeParam.getPadding().getBounds().getPadding().getElements().get(index + 1);
if (next.getElement().getPrefix().getWhitespace().contains("\n")) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public J.TypeParameter visitTypeParameter(J.TypeParameter typeParam, ExecutionCo
public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, ExecutionContext ctx) {
J.InstanceOf i = super.visitInstanceOf(instanceOf, ctx);
if (Boolean.TRUE.equals(operatorWrapStyle.getLiteralInstanceof())) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (i.getClazz().getPrefix().getWhitespace().contains("\n")) {
i = i.getPadding().withExpr(
i.getPadding().getExpression().withAfter(
Expand Down Expand Up @@ -212,7 +212,7 @@ public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, ExecutionContext ct
public J.Ternary visitTernary(J.Ternary ternary, ExecutionContext ctx) {
J.Ternary t = super.visitTernary(ternary, ctx);
if (Boolean.TRUE.equals(operatorWrapStyle.getQuestion())) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (t.getTruePart().getPrefix().getWhitespace().contains("\n")) {
t = t.getPadding().withTruePart(
t.getPadding().getTruePart().withBefore(
Expand Down Expand Up @@ -243,7 +243,7 @@ public J.Ternary visitTernary(J.Ternary ternary, ExecutionContext ctx) {
}
}
if (Boolean.TRUE.equals(operatorWrapStyle.getColon())) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (t.getPadding().getFalsePart().getElement().getPrefix().getWhitespace().contains("\n")) {
t = t.getPadding().withFalsePart(
t.getPadding().getFalsePart().withBefore(
Expand Down Expand Up @@ -280,7 +280,7 @@ public J.Ternary visitTernary(J.Ternary ternary, ExecutionContext ctx) {
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
J.MemberReference m = super.visitMemberReference(memberRef, ctx);
if (Boolean.TRUE.equals(operatorWrapStyle.getMethodRef())) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (m.getPadding().getReference().getBefore().getWhitespace().contains("\n")) {
m = m.getPadding().withContaining(
m.getPadding().getContaining().withAfter(
Expand Down Expand Up @@ -313,7 +313,7 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Execu
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
J.Assignment a = super.visitAssignment(assignment, ctx);
if (Boolean.TRUE.equals(operatorWrapStyle.getAssign())) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (a.getPadding().getAssignment().getElement().getPrefix().getWhitespace().contains("\n")) {
a = a.getPadding().withAssignment(
a.getPadding().getAssignment().withBefore(
Expand Down Expand Up @@ -361,7 +361,7 @@ public J.AssignmentOperation visitAssignmentOperation(J.AssignmentOperation assi
(Boolean.TRUE.equals(operatorWrapStyle.getBandAssign()) && op == J.AssignmentOperation.Type.BitAnd) ||
(Boolean.TRUE.equals(operatorWrapStyle.getBxorAssign()) && op == J.AssignmentOperation.Type.BitXor) ||
(Boolean.TRUE.equals(operatorWrapStyle.getBorAssign()) && op == J.AssignmentOperation.Type.BitOr)) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (a.getAssignment().getPrefix().getWhitespace().contains("\n")) {
a = a.getPadding().withOperator(
a.getPadding().getOperator().withBefore(
Expand Down Expand Up @@ -394,7 +394,7 @@ public J.AssignmentOperation visitAssignmentOperation(J.AssignmentOperation assi
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
J.VariableDeclarations.NamedVariable v = super.visitVariable(variable, ctx);
if (Boolean.TRUE.equals(operatorWrapStyle.getAssign()) && v.getPadding().getInitializer() != null) {
if (OperatorWrapStyle.WrapOption.NL.equals(operatorWrapStyle.getWrapOption())) {
if (OperatorWrapStyle.WrapOption.NL == operatorWrapStyle.getWrapOption()) {
if (v.getPadding().getInitializer().getElement().getPrefix().getWhitespace().contains("\n")) {
v = v.getPadding().withInitializer(
v.getPadding().getInitializer().withBefore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
String sn = method.getSimpleName();
JavaType rte = method.getReturnTypeExpression().getType();
JavaType.Method t = method.getMethodType();
if (equalsIgnoreCaseExclusive(sn, "hashCode") && JavaType.Primitive.Int.equals(rte) && NO_ARGS.matches(t)) {
if (equalsIgnoreCaseExclusive(sn, "hashCode") && JavaType.Primitive.Int == rte && NO_ARGS.matches(t)) {
doAfterVisit(new ChangeMethodName(MethodMatcher.methodPattern(method), "hashCode", true, false).getVisitor());
} else if ("equal".equalsIgnoreCase(sn) && JavaType.Primitive.Boolean.equals(rte) && OBJECT_ARG.matches(t)) {
} else if ("equal".equalsIgnoreCase(sn) && JavaType.Primitive.Boolean == rte && OBJECT_ARG.matches(t)) {
doAfterVisit(new ChangeMethodName(MethodMatcher.methodPattern(method), "equals", true, false).getVisitor());
} else if (equalsIgnoreCaseExclusive(sn, "toString") && TypeUtils.isString(rte) && NO_ARGS.matches(t)) {
doAfterVisit(new ChangeMethodName(MethodMatcher.methodPattern(method), "toString", true, false).getVisitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations

@Override
public J.Literal visitLiteral(J.Literal literal, Integer integer) {
if (JavaType.Primitive.String.equals(literal.getType()) &&
if (JavaType.Primitive.String == literal.getType() &&
literal.getValue() instanceof String &&
((String) literal.getValue()).length() >= 5) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private static boolean isObjectsEquals(J.MethodInvocation inv) {
}

private static boolean isEqualsBinary(J maybeEqualsBinary) {
return maybeEqualsBinary instanceof J.Binary && ((J.Binary) maybeEqualsBinary).getOperator().equals(Equal);
return maybeEqualsBinary instanceof J.Binary && ((J.Binary) maybeEqualsBinary).getOperator() == Equal;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
n.getBody().getStatements().get(0) instanceof J.MethodDeclaration &&
n.getClazz() != null) {
JavaType.FullyQualified type = TypeUtils.asFullyQualified(n.getClazz().getType());
if (type != null && type.getKind().equals(JavaType.Class.Kind.Interface)) {
if (type != null && type.getKind() == JavaType.Class.Kind.Interface) {
JavaType.Method sam = getSamCompatible(type);
if (sam == null) {
return n;
Expand Down Expand Up @@ -110,7 +110,7 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
}

JavaType returnType = sam.getReturnType();
if (!JavaType.Primitive.Void.equals(returnType)) {
if (JavaType.Primitive.Void != returnType) {
templateBuilder.append("return ").append(valueOfType(returnType)).append(';');
}
templateBuilder.append('}');
Expand Down
Loading