Skip to content

Commit

Permalink
Merge branch 'main' into test-ci
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java
  • Loading branch information
Vincent Potucek committed Dec 27, 2024
2 parents f267f9a + f7469d1 commit a6d2f7c
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 96 deletions.
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 @@ -47,23 +47,19 @@
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> {

private static final String JAVA_LANG_STRING = "java.lang.String ";
private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING +
"equals(java.lang.Object)");
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING +
"equalsIgnoreCase(java.lang.String)");
private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + "equals(java.lang.Object)");
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "equalsIgnoreCase(java.lang.String)");
private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + "compareTo(java.lang.String)");
private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING +
"compareToIgnoreCase(java.lang.String)");
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + "contentEquals(java.lang" +
".CharSequence)");
private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "compareToIgnoreCase(java.lang.String)");
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + "contentEquals(java.lang.CharSequence)");

EqualsAvoidsNullStyle style;

@Override
public J visitMethodInvocation(J.MethodInvocation method, P p) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, p);
if (m.getSelect() != null && !(m.getSelect() instanceof J.Literal) &&
isStringComparisonMethod(m) && hasCompatibleArgument(m)) {
isStringComparisonMethod(m) && hasCompatibleArgument(m)) {

maybeHandleParentBinary(m);

Expand Down Expand Up @@ -95,22 +91,20 @@ private boolean hasCompatibleArgument(J.MethodInvocation m) {

private boolean isStringComparisonMethod(J.MethodInvocation methodInvocation) {
return EQUALS.matches(methodInvocation) ||
!style.getIgnoreEqualsIgnoreCase() &&
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
COMPARE_TO.matches(methodInvocation) ||
COMPARE_TO_IGNORE_CASE.matches(methodInvocation) ||
CONTENT_EQUALS.matches(methodInvocation);
!style.getIgnoreEqualsIgnoreCase() &&
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
COMPARE_TO.matches(methodInvocation) ||
COMPARE_TO_IGNORE_CASE.matches(methodInvocation) ||
CONTENT_EQUALS.matches(methodInvocation);
}

private void maybeHandleParentBinary(J.MethodInvocation m) {
P parent = getCursor().getParentTreeCursor().getValue();
if (parent instanceof J.Binary) {
if (((J.Binary) parent).getOperator() == J.Binary.Type.And && ((J.Binary) parent).getLeft() instanceof J.Binary) {
J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft();
if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(),
requireNonNull(m.getSelect())) ||
isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(),
requireNonNull(m.getSelect()))) {
if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) ||
isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
doAfterVisit(new RemoveUnnecessaryNullCheck<>((J.Binary) parent));
}
}
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
Loading

0 comments on commit a6d2f7c

Please sign in to comment.