Skip to content

Commit

Permalink
refactor: Enum values should be compared with "==" (#4811)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne authored Dec 25, 2024
1 parent 15740e4 commit 5b14eb0
Show file tree
Hide file tree
Showing 36 changed files with 77 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Ki
private List<Reference> findMatchesInternal(Reference.Matcher matcher, Reference.@Nullable Kind kind) {
List<Reference> list = new ArrayList<>();
for (Reference ref : references) {
if ((kind == null || ref.getKind().equals(kind)) && ref.matches(matcher) ) {
if ((kind == null || ref.getKind() == kind) && ref.matches(matcher) ) {
list.add(ref);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private Collection<Map<String, Object>> loadResources(ResourceType resourceType)
for (Object resource : yaml.loadAll(yamlSource)) {
if (resource instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> resourceMap = (Map<String, Object>) resource;
if (resourceType.equals(ResourceType.fromSpec((String) resourceMap.get("type")))) {
if (resourceType == ResourceType.fromSpec((String) resourceMap.get("type"))) {
resources.add(resourceMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private boolean matchesComparator(@Nullable String managedVersion, String reques
if (managedVersion == null) {
return false;
}
if (comparator.equals(Comparator.ANY)) {
if (comparator == Comparator.ANY) {
return true;
}
if (!isExact(managedVersion)) {
Expand All @@ -301,11 +301,11 @@ private boolean matchesComparator(@Nullable String managedVersion, String reques
int comparison = new LatestIntegration(null)
.compare(null, managedVersion, requestedVersion);
if (comparison < 0) {
return comparator.equals(Comparator.LT) || comparator.equals(Comparator.LTE);
return comparator == Comparator.LT || comparator == Comparator.LTE;
} else if (comparison > 0) {
return comparator.equals(Comparator.GT) || comparator.equals(Comparator.GTE);
return comparator == Comparator.GT || comparator == Comparator.GTE;
} else {
return comparator.equals(Comparator.EQ) || comparator.equals(Comparator.LTE) || comparator.equals(Comparator.GTE);
return comparator == Comparator.EQ || comparator == Comparator.LTE || comparator == Comparator.GTE;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ public void visitBlockStatement(BlockStatement block) {
J expr = visit(statement);
if (i == blockStatements.size() - 1 && (expr instanceof Expression)) {
if (parent instanceof ClosureExpression || (parent instanceof MethodNode &&
!JavaType.Primitive.Void.equals(typeMapping.type(((MethodNode) parent).getReturnType())))) {
JavaType.Primitive.Void != typeMapping.type(((MethodNode) parent).getReturnType()))) {
expr = new J.Return(randomId(), expr.getPrefix(), Markers.EMPTY,
expr.withPrefix(EMPTY));
expr = expr.withMarkers(expr.getMarkers().add(new ImplicitReturn(randomId())));
Expand Down
10 changes: 5 additions & 5 deletions rewrite-hcl/src/main/java/org/openrewrite/hcl/HclTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public <H extends Hcl> H apply(Cursor scope, HclCoordinates coordinates, Object.
@Override
public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
Hcl.ConfigFile c = (Hcl.ConfigFile) super.visitConfigFile(configFile, p);
if (loc.equals(Space.Location.CONFIG_FILE_EOF)) {
if (loc == Space.Location.CONFIG_FILE_EOF) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));

if (coordinates.getComparator() != null) {
Expand Down Expand Up @@ -100,7 +100,7 @@ public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
)
);
}
} else if (loc.equals(Space.Location.CONFIG_FILE)) {
} else if (loc == Space.Location.CONFIG_FILE) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));
c = c.withBody(
ListUtils.concatAll(
Expand All @@ -116,7 +116,7 @@ public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
@Override
public Hcl visitBlock(Hcl.Block block, Integer p) {
Hcl.Block b = (Hcl.Block) super.visitBlock(block, p);
if (loc.equals(Space.Location.BLOCK_CLOSE)) {
if (loc == Space.Location.BLOCK_CLOSE) {
if (b.isScope(insertionPoint)) {
List<BodyContent> gen = substitutions.unsubstitute(templateParser.parseBodyContent(substitutedTemplate));

Expand Down Expand Up @@ -145,7 +145,7 @@ public Hcl visitBlock(Hcl.Block block, Integer p) {
.orElse(SpacesStyle.DEFAULT)).visit(b, p, getCursor().getParentOrThrow());
assert b != null;
}
} else if (loc.equals(Space.Location.BLOCK)) {
} else if (loc == Space.Location.BLOCK) {
if (b.isScope(insertionPoint)) {
b = (Hcl.Block) autoFormat(templateParser.parseBodyContent(substitutedTemplate).get(0), p,
getCursor().getParentOrThrow());
Expand All @@ -158,7 +158,7 @@ public Hcl visitBlock(Hcl.Block block, Integer p) {
public Hcl visitExpression(Expression expression, Integer p) {
Hcl e = super.visitExpression(expression, p);

if (loc.equals(Space.Location.EXPRESSION_PREFIX)) {
if (loc == Space.Location.EXPRESSION_PREFIX) {
if (e.isScope(insertionPoint)) {
e = templateParser.parseExpression(substitutedTemplate).withPrefix(expression.getPrefix());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Hcl.Attribute visitAttribute(Hcl.Attribute attribute, P p) {
if (parent instanceof Hcl.Block || parent instanceof Hcl.ObjectValue) {
List<Hcl.Attribute> siblingAttributes = getSiblingAttributes(parent);

if (attribute.getType().equals(Hcl.Attribute.Type.Assignment)) {
if (attribute.getType() == Hcl.Attribute.Type.Assignment) {
HclLeftPadded<Hcl.Attribute.Type> type = a.getPadding().getType();

if (Boolean.TRUE.equals(style.getBodyContent().getColumnarAlignment())) {
Expand Down Expand Up @@ -106,7 +106,7 @@ private List<Hcl.Attribute> attributesInGroup(List<Hcl.Attribute> siblings, Hcl.
boolean groupFound = false;
Hcl.Attribute perviousSibling = null;
for (Hcl.Attribute sibling : siblings) {
if (sibling.getType().equals(Hcl.Attribute.Type.Assignment)) {
if (sibling.getType() == Hcl.Attribute.Type.Assignment) {
boolean siblingPrefixHasNewLines = sibling.getPrefix().getWhitespace().split("\r\n|\r|\n").length > 2;
boolean siblingIsMultiline = sibling.getValue().print(getCursor()).split("\r\n|\r|\n").length > 2;
boolean previousSiblingIsMultiline = perviousSibling != null && perviousSibling.getValue().print(getCursor()).split("\r\n|\r|\n").length > 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public BracketsVisitor(BracketsStyle style, @Nullable Tree stopAfter) {

@Override
public Space visitSpace(Space space, Space.Location loc, P p) {
if (loc.equals(Space.Location.BLOCK_CLOSE) && !space.getLastWhitespace().contains("\n")) {
if (loc == Space.Location.BLOCK_CLOSE && !space.getLastWhitespace().contains("\n")) {
return space.withLastWhitespace("\n");
}

if (loc.equals(Space.Location.BLOCK_OPEN) && !space.getWhitespace().equals(" ")) {
if (loc == Space.Location.BLOCK_OPEN && !space.getWhitespace().equals(" ")) {
return space.withWhitespace(" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Space visitSpace(Space space, Space.Location loc, P p) {
IndentType indentType = getCursor().getParentOrThrow().getNearestMessage("indentType", IndentType.ALIGN);

// block spaces are always aligned to their parent
boolean alignBlockToParent = loc.equals(Space.Location.BLOCK_CLOSE) || loc.equals(Space.Location.OBJECT_VALUE_ATTRIBUTE_SUFFIX);
boolean alignBlockToParent = loc == Space.Location.BLOCK_CLOSE || loc == Space.Location.OBJECT_VALUE_ATTRIBUTE_SUFFIX;

if (alignBlockToParent) {
indentType = IndentType.ALIGN;
Expand Down Expand Up @@ -266,16 +266,16 @@ private Space indentTo(Space space, int column, Space.Location spaceLocation) {
space = space.withComments(ListUtils.map(space.getComments(), c -> {
// The suffix of the last element in the comment list sets the whitespace for the end of the block.
// The column for comments that come before the last element are incremented.
int incrementBy = spaceLocation.equals(Space.Location.BLOCK_CLOSE) && !c.equals(lastElement) ? style.getIndentSize() : 0;
int incrementBy = spaceLocation == Space.Location.BLOCK_CLOSE && !c.equals(lastElement) ? style.getIndentSize() : 0;
return c.getStyle() == Comment.Style.INLINE ?
indentMultilineComment(c, column + incrementBy) :
indentSingleLineComment(c, column + incrementBy);
}));

// Prevent formatting trailing comments, the whitespace in a trailing comment won't have a new line character.
// Compilation unit prefixes are an exception, since they do not exist in a block.
if (space.getWhitespace().contains("\n") || spaceLocation.equals(Space.Location.CONFIG_FILE)) {
int incrementBy = spaceLocation.equals(Space.Location.BLOCK_CLOSE) ? style.getIndentSize() : 0;
if (space.getWhitespace().contains("\n") || spaceLocation == Space.Location.CONFIG_FILE) {
int incrementBy = spaceLocation == Space.Location.BLOCK_CLOSE ? style.getIndentSize() : 0;
int indent = getLengthOfWhitespace(space.getWhitespace());
if (indent != (column + incrementBy)) {
int shift = column + incrementBy - indent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Hcl visitAttribute(Hcl.Attribute attribute, PrintOutputCapture<P> p) {
beforeSyntax(attribute, Space.Location.ATTRIBUTE, p);
visit(attribute.getName(), p);
visitSpace(attribute.getPadding().getType().getBefore(), Space.Location.ATTRIBUTE_ASSIGNMENT, p);
p.append(attribute.getType().equals(Hcl.Attribute.Type.Assignment) ? "=" : ":");
p.append(attribute.getType() == Hcl.Attribute.Type.Assignment ? "=" : ":");
visit(attribute.getValue(), p);
if (attribute.getComma() != null) {
visitSpace(attribute.getComma().getPrefix(), Space.Location.OBJECT_VALUE_ATTRIBUTE_COMMA, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class HclCoordinates implements Coordinates {
Comparator<? extends Hcl> comparator;

public boolean isReplacement() {
return Mode.REPLACEMENT.equals(mode);
return Mode.REPLACEMENT == mode;
}

public enum Mode {
Expand Down
4 changes: 2 additions & 2 deletions rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Space.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static Space format(String formatting) {
for (char c : charArray) {
switch (c) {
case '#':
if (Comment.Style.LINE_SLASH.equals(inLineSlashOrHashComment)) {
if (Comment.Style.LINE_SLASH == inLineSlashOrHashComment) {
comment.append(c);
} else {
if (inSingleLineComment) {
Expand All @@ -169,7 +169,7 @@ public static Space format(String formatting) {
}
break;
case '/':
if (Comment.Style.LINE_HASH.equals(inLineSlashOrHashComment)) {
if (Comment.Style.LINE_HASH == inLineSlashOrHashComment) {
comment.append(c);
} else {
if (inSingleLineComment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Kind getKind() {
@Override
public boolean isNameCompatible(String simpleName, Kind kind) {
String baseName = simpleName + kind.extension;
return kind.equals(getKind()) &&
return kind == getKind() &&
path.getFileName().toString().equals(baseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ public J visitLiteral(LiteralTree node, Space fmt) {
singletonList(new J.Literal.UnicodeEscape(1,
valueSource.substring(3, valueSource.length() - 1))), type);
}
} else if (JavaType.Primitive.String.equals(type)) {
} else if (JavaType.Primitive.String == type) {
StringBuilder valueSourceWithoutSurrogates = new StringBuilder();
List<J.Literal.UnicodeEscape> unicodeEscapes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Kind getKind() {
@Override
public boolean isNameCompatible(String simpleName, Kind kind) {
String baseName = simpleName + kind.extension;
return kind.equals(getKind()) &&
return kind == getKind() &&
path.getFileName().toString().equals(baseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ public J visitLiteral(LiteralTree node, Space fmt) {
singletonList(new J.Literal.UnicodeEscape(1,
valueSource.substring(3, valueSource.length() - 1))), type);
}
} else if (JavaType.Primitive.String.equals(type)) {
} else if (JavaType.Primitive.String == type) {
StringBuilder valueSourceWithoutSurrogates = new StringBuilder();
List<J.Literal.UnicodeEscape> unicodeEscapes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Kind getKind() {
@Override
public boolean isNameCompatible(String simpleName, Kind kind) {
String baseName = simpleName + kind.extension;
return kind.equals(getKind()) &&
return kind == getKind() &&
path.getFileName().toString().equals(baseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public J visitLiteral(LiteralTree node, Space fmt) {
singletonList(new J.Literal.UnicodeEscape(1,
valueSource.substring(3, valueSource.length() - 1))), type);
}
} else if (JavaType.Primitive.String.equals(type)) {
} else if (JavaType.Primitive.String == type) {
StringBuilder valueSourceWithoutSurrogates = new StringBuilder();
List<J.Literal.UnicodeEscape> unicodeEscapes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Kind getKind() {
@Override
public boolean isNameCompatible(String simpleName, Kind kind) {
String baseName = simpleName + kind.extension;
return kind.equals(getKind()) &&
return kind == getKind() &&
path.getFileName().toString().equals(baseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public String inferBinaryName(Location location, JavaFileObject file) {

@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
if (StandardLocation.CLASS_PATH.equals(location)) {
if (StandardLocation.CLASS_PATH == location) {
Iterable<JavaFileObject> listed = super.list(location, packageName, kinds, recurse);
return Stream.concat(
classByteClasspath.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ public J visitLiteral(LiteralTree node, Space fmt) {
singletonList(new J.Literal.UnicodeEscape(1,
valueSource.substring(3, valueSource.length() - 1))), type);
}
} else if (JavaType.Primitive.String.equals(type)) {
} else if (JavaType.Primitive.String == type) {
StringBuilder valueSourceWithoutSurrogates = new StringBuilder();
List<J.Literal.UnicodeEscape> unicodeEscapes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public J visitClassDeclaration(ClassDeclaration classDecl, PrintOutputCapture<P>
visitContainer("<", classDecl.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, ",", ">", p);
visitContainer("(", classDecl.getPadding().getPrimaryConstructor(), JContainer.Location.RECORD_STATE_VECTOR, ",", ")", p);
visitLeftPadded("extends", classDecl.getPadding().getExtends(), JLeftPadded.Location.EXTENDS, p);
visitContainer(classDecl.getKind().equals(ClassDeclaration.Kind.Type.Interface) ? "extends" : "implements",
visitContainer(classDecl.getKind() == ClassDeclaration.Kind.Type.Interface ? "extends" : "implements",
classDecl.getPadding().getImplements(), JContainer.Location.IMPLEMENTS, ",", null, p);
visitContainer("permits", classDecl.getPadding().getPermits(), JContainer.Location.PERMITS, ",", null, p);
visit(classDecl.getBody(), p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ protected boolean isInSameNameScope(Cursor base, Cursor child) {
Object childScope = it.next();
if (childScope instanceof J.ClassDeclaration) {
J.ClassDeclaration childClass = (J.ClassDeclaration) childScope;
if (!(childClass.getKind().equals(J.ClassDeclaration.Kind.Type.Class)) ||
if (childClass.getKind() != J.ClassDeclaration.Kind.Type.Class ||
childClass.hasModifier(J.Modifier.Type.Static)) {
//Short circuit the search if a terminating element is encountered.
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PackageMatcher(@Nullable String targetPackage, boolean recursive) {

@Override
public boolean matchesReference(Reference reference) {
if (reference.getKind().equals(Reference.Kind.TYPE) || reference.getKind().equals(Reference.Kind.PACKAGE)) {
if (reference.getKind() == Reference.Kind.TYPE || reference.getKind() == Reference.Kind.PACKAGE) {
String recursivePackageNamePrefix = targetPackage + ".";
if (reference.getValue().equals(targetPackage) || recursive && reference.getValue().startsWith(recursivePackageNamePrefix)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static boolean isPlainIdentifier(MethodSignatureParser.TargetTypePattern

@Override
public boolean matchesReference(Reference reference) {
return reference.getKind().equals(Reference.Kind.TYPE) && matchesTargetTypeName(reference.getValue());
return reference.getKind() == Reference.Kind.TYPE && matchesTargetTypeName(reference.getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static String generateVariableName(String baseName, Cursor scope, Generat
Set<String> namesInScope = findNamesInScope(scope);
// Generate a new name to prevent namespace shadowing.
String newName = baseName;
if (GenerationStrategy.INCREMENT_NUMBER.equals(strategy)) {
if (GenerationStrategy.INCREMENT_NUMBER == strategy) {
StringBuilder postFix = new StringBuilder();
char[] charArray = baseName.toCharArray();
for (int i = charArray.length - 1; i >= 0; i--) {
Expand Down
Loading

0 comments on commit 5b14eb0

Please sign in to comment.