Skip to content

Commit

Permalink
refactor: Common static analysis issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kmccarp and TeamModerne committed Mar 28, 2024
1 parent ac109fa commit e4a5dbd
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 107 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ private boolean isFieldAccess(J.Identifier ident) {
Tree value = getCursor().getParentTreeCursor().getValue();
return value instanceof J.FieldAccess
&& (ident == ((J.FieldAccess) value).getName() ||
ident == ((J.FieldAccess) value).getTarget() && !((J.FieldAccess) value).getSimpleName().equals("class"));
ident == ((J.FieldAccess) value).getTarget() && !"class".equals(((J.FieldAccess) value).getSimpleName()));
}

private boolean isBeingDeclared(J.Identifier ident) {
Expand Down Expand Up @@ -557,7 +557,7 @@ private boolean isAnnotationField(J.Identifier ident) {
}

private boolean isValidated(J.Identifier i) {
J j = getCursor().dropParentUntil(it -> it instanceof J).getValue();
J j = getCursor().dropParentUntil(J.class::isInstance).getValue();
// TODO: replace with AnnotationUseSite tree.
return !(j instanceof K.Return);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static class Builder extends Parser.Builder {
private final List<NamedStyles> styles = new ArrayList<>();
private String moduleName = "main";
private KotlinLanguageLevel languageLevel = KotlinLanguageLevel.KOTLIN_1_9;
private boolean isKotlinScript = false;
private boolean isKotlinScript;

public Builder() {
super(K.CompilationUnit.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Set;
import java.util.function.Consumer;

public class KotlinTemplate extends JavaTemplate {
public final class KotlinTemplate extends JavaTemplate {
private KotlinTemplate(boolean contextSensitive, KotlinParser.Builder parser, String code, Set<String> imports, Consumer<String> onAfterVariableSubstitution, Consumer<String> onBeforeParseTemplate) {
super(
code,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/kotlin/KotlinVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public <T> JLeftPadded<T> visitLeftPadded(@Nullable JLeftPadded<T> left, KLeftPa
return null;
}

return (before == left.getBefore() && t == left.getElement()) ? left : new JLeftPadded<>(before, t, left.getMarkers());
return before == left.getBefore() && t == left.getElement() ? left : new JLeftPadded<>(before, t, left.getMarkers());
}

public <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, KRightPadded.Location loc, P p) {
Expand All @@ -480,7 +480,7 @@ public <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, KRi

Space after = visitSpace(right.getAfter(), loc.getAfterLocation(), p);
Markers markers = visitMarkers(right.getMarkers(), p);
return (after == right.getAfter() && t == right.getElement() && markers == right.getMarkers()) ?
return after == right.getAfter() && t == right.getElement() && markers == right.getMarkers() ?
right : new JRightPadded<>(t, after, markers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AutoFormatVisitor(@Nullable Tree stopAfter) {

@Override
public J visit(@Nullable Tree tree, P p, Cursor cursor) {
JavaSourceFile cu = (tree instanceof JavaSourceFile) ?
JavaSourceFile cu = tree instanceof JavaSourceFile ?
(JavaSourceFile) tree :
cursor.firstEnclosingOrThrow(JavaSourceFile.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
j = firstStatement ?
(hasImports ? minimumLines(j, minimumBlankLines_AfterImports) : j) :
// Apply `style.getMinimum().getAroundClass()` to classes declared in the SourceFile.
(topLevelStatements.contains(j)) ? minimumLines(j, minimumBlankLines_AroundClass) : j;
topLevelStatements.contains(j) ? minimumLines(j, minimumBlankLines_AroundClass) : j;

// style.getKeepMaximum().getInDeclarations() also sets the maximum new lines of class declaration prefixes.
j = firstStatement ?
(hasImports ? keepMaximumLines(j, Math.max(style.getKeepMaximum().getInDeclarations(), minimumBlankLines_AfterImports)) : j) :
(topLevelStatements.contains(j)) ? keepMaximumLines(j, style.getKeepMaximum().getInDeclarations()) : j;
topLevelStatements.contains(j) ? keepMaximumLines(j, style.getKeepMaximum().getInDeclarations()) : j;

if (!hasImports && firstStatement) {
if (cu.getPackageDeclaration() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
}

c = c.withBody(c.getBody().withStatements(ListUtils.map(c.getBody().getStatements(),
(i, st) -> (i != 0) ? st.withPrefix(addNewline(st.getPrefix())) : st)));
(i, st) -> i != 0 ? st.withPrefix(addNewline(st.getPrefix())) : st)));

return c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P
List<JRightPadded<Statement>> rps = jc.getPadding().getElements();
if (rps.size() > 1) {
int range = rps.size() - 1;
rps = ListUtils.map(rps, (index, rp) -> (index < range) ? spaceAfter(rp, style.getOther().getBeforeComma()) : rp);
rps = ListUtils.map(rps, (index, rp) -> index < range ? spaceAfter(rp, style.getOther().getBeforeComma()) : rp);
m = m.getPadding().withParameters(jc.getPadding().withElements(rps));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public J visit(@Nullable Tree tree, P p, Cursor parent) {
}
}
Iterator<Object> itr = parent.getPath(J.class::isInstance);
J next = (itr.hasNext()) ? (J) itr.next() : null;
J next = itr.hasNext() ? (J) itr.next() : null;
if (next != null) {
preVisit(next, p);
}
Expand All @@ -90,13 +90,7 @@ public J visit(@Nullable Tree tree, P p, Cursor parent) {
@Override
@Nullable
public J preVisit(@Nullable J tree, P p) {
if (tree instanceof JavaSourceFile ||
tree instanceof J.Package ||
tree instanceof J.Import ||
tree instanceof J.ClassDeclaration ||
tree instanceof J.Label ||
tree instanceof J.DoWhileLoop ||
tree instanceof J.ArrayDimension) {
if (tree instanceof JavaSourceFile) {
getCursor().putMessage("indentType", IndentType.ALIGN);
} else if (tree instanceof J.Block && tree.getMarkers().findFirst(SingleExpressionBlock.class).isPresent()) {
getCursor().putMessage("indentType", wrappingStyle.getExpressionBodyFunctions().getUseContinuationIndent() ? IndentType.CONTINUATION_INDENT : IndentType.INDENT);
Expand Down Expand Up @@ -419,7 +413,7 @@ public <T> JRightPadded<T> visitRightPadded(@Nullable JRightPadded<T> right, JRi
}

setCursor(getCursor().getParent());
return (after == right.getAfter() && t == right.getElement()) ? right : new JRightPadded<>(t, after, right.getMarkers());
return after == right.getAfter() && t == right.getElement() ? right : new JRightPadded<>(t, after, right.getMarkers());
}

@SuppressWarnings("NullableProblems")
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public J visitBinary(J.Binary binary, PrintOutputCapture<P> p) {
keyword = "ushr";
break;
case Or:
keyword = (binary.getMarkers().findFirst(LogicalComma.class).isPresent()) ? "," : "||";
keyword = binary.getMarkers().findFirst(LogicalComma.class).isPresent() ? "," : "||";
break;
case And:
keyword = "&&";
Expand Down Expand Up @@ -656,8 +656,8 @@ private J.ClassDeclaration visitClassDeclaration0(J.ClassDeclaration classDecl,
visit(classDecl.getAnnotations().getKind().getAnnotations(), p);
visitSpace(classDecl.getAnnotations().getKind().getPrefix(), Space.Location.CLASS_KIND, p);

KObject KObject = classDecl.getMarkers().findFirst(KObject.class).orElse(null);
if (KObject != null) {
KObject kObject = classDecl.getMarkers().findFirst(KObject.class).orElse(null);
if (kObject != null) {
p.append("object");
if (!classDecl.getName().getMarkers().findFirst(Implicit.class).isPresent()) {
visit(classDecl.getName(), p);
Expand Down Expand Up @@ -979,7 +979,7 @@ public J visitMethodInvocation(J.MethodInvocation method, PrintOutputCapture<P>
p.append("?");
}

if (!method.getName().getSimpleName().equals("<empty>") &&
if (!"<empty>".equals(method.getName().getSimpleName()) &&
!method.getName().getMarkers().findFirst(Implicit.class).isPresent()) {
p.append(".");
}
Expand Down Expand Up @@ -1179,7 +1179,7 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOu
p.append("val");
}

if (m.getType() == J.Modifier.Type.LanguageExtension && m.getKeyword() != null && m.getKeyword().equals("typealias")) {
if (m.getType() == J.Modifier.Type.LanguageExtension && m.getKeyword() != null && "typealias".equals(m.getKeyword())) {
isTypeAlias = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public J visitParenthesizedExpression(KtParenthesizedExpression expression, Exec
assert expression.getExpression() != null;

PsiElement rPar = expression.getLastChild();
if (rPar == null || !(")".equals(rPar.getText()))) {
if (rPar == null || !")".equals(rPar.getText())) {
throw new UnsupportedOperationException("TODO");
}

Expand Down Expand Up @@ -1937,8 +1937,9 @@ private J.AssignmentOperation.Type mapAssignmentOperationType(KtOperationReferen
return J.AssignmentOperation.Type.Division;
} else if (elementType == KtTokens.PERCEQ) {
return J.AssignmentOperation.Type.Modulo;
} else
} else {
return null;
}
}

@Override
Expand Down Expand Up @@ -2260,7 +2261,7 @@ private J visitClass0(KtClass klass, ExecutionContext data) {
(JavaType.FullyQualified) type(klass)
);

return (typeConstraints != null) ? new K.ClassDeclaration(randomId(), Markers.EMPTY, classDeclaration, typeConstraints) : classDeclaration;
return typeConstraints != null ? new K.ClassDeclaration(randomId(), Markers.EMPTY, classDeclaration, typeConstraints) : classDeclaration;
}

@Override
Expand Down Expand Up @@ -2750,7 +2751,7 @@ private J visitNamedFunction0(KtNamedFunction function, ExecutionContext data) {
type
));

return (typeConstraints == null) ? methodDeclaration : new K.MethodDeclaration(randomId(), Markers.EMPTY, methodDeclaration, typeConstraints);
return typeConstraints == null ? methodDeclaration : new K.MethodDeclaration(randomId(), Markers.EMPTY, methodDeclaration, typeConstraints);
}

private List<JRightPadded<J.TypeParameter>> mapTypeParameters(KtTypeParameterList list, ExecutionContext data) {
Expand Down Expand Up @@ -3353,34 +3354,35 @@ private J.Binary.Type mapJBinaryType(KtOperationReferenceExpression operationRef
}
}

if (elementType == KtTokens.PLUS)
if (elementType == KtTokens.PLUS) {
return J.Binary.Type.Addition;
else if (elementType == KtTokens.MINUS)
} else if (elementType == KtTokens.MINUS) {
return J.Binary.Type.Subtraction;
else if (elementType == KtTokens.MUL)
} else if (elementType == KtTokens.MUL) {
return J.Binary.Type.Multiplication;
else if (elementType == KtTokens.DIV)
} else if (elementType == KtTokens.DIV) {
return J.Binary.Type.Division;
else if (elementType == KtTokens.EQEQ)
} else if (elementType == KtTokens.EQEQ) {
return J.Binary.Type.Equal;
else if (elementType == KtTokens.EXCLEQ)
} else if (elementType == KtTokens.EXCLEQ) {
return J.Binary.Type.NotEqual;
else if (elementType == KtTokens.GT)
} else if (elementType == KtTokens.GT) {
return J.Binary.Type.GreaterThan;
else if (elementType == KtTokens.GTEQ)
} else if (elementType == KtTokens.GTEQ) {
return J.Binary.Type.GreaterThanOrEqual;
else if (elementType == KtTokens.LT)
} else if (elementType == KtTokens.LT) {
return J.Binary.Type.LessThan;
else if (elementType == KtTokens.LTEQ)
} else if (elementType == KtTokens.LTEQ) {
return J.Binary.Type.LessThanOrEqual;
else if (elementType == KtTokens.PERC)
} else if (elementType == KtTokens.PERC) {
return J.Binary.Type.Modulo;
else if (elementType == KtTokens.ANDAND)
} else if (elementType == KtTokens.ANDAND) {
return J.Binary.Type.And;
else if (elementType == KtTokens.OROR)
} else if (elementType == KtTokens.OROR) {
return J.Binary.Type.Or;
else
} else {
return null;
}
}

@Nullable
Expand Down Expand Up @@ -3959,7 +3961,7 @@ private static int findLastRPAR(List<PsiElement> elements, int end) {
}

private static boolean isCRLF(ASTNode node) {
return node instanceof PsiErrorElementImpl && node.getText().equals("\r");
return node instanceof PsiErrorElementImpl && "\r".equals(node.getText());
}

private String nodeRangeText(@Nullable ASTNode first, @Nullable ASTNode last) {
Expand Down Expand Up @@ -4262,7 +4264,7 @@ private Space kdocToSpace(KDoc kDoc) {
if (it instanceof PsiWhiteSpace) {
text = replaceCRLF((PsiWhiteSpace) it);
} else if (it instanceof KDocSection) {
text = KDocSectionToString((KDocSection) it);
text = kDocSectionToString((KDocSection) it);
}

sb.append(text);
Expand All @@ -4275,7 +4277,7 @@ private Space kdocToSpace(KDoc kDoc) {
return Space.build("", comments);
}

private String KDocSectionToString(KDocSection kDocSection) {
private String kDocSectionToString(KDocSection kDocSection) {
StringBuilder sb = new StringBuilder();
Iterator<PsiElement> iterator = PsiUtilsKt.getAllChildren(kDocSection).iterator();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class PsiTreePrinter {
private static final KotlinIrTypeMapping irTypeMapping = new KotlinIrTypeMapping(new JavaTypeCache());

// Set to true to print types and verify, otherwise just verify the parse to print idempotent.
private final static boolean printTypes = true;
private static final boolean printTypes = true;

private final List<StringBuilder> outputLines;

Expand Down Expand Up @@ -581,9 +581,9 @@ public static String firElementToString(FirElement firElement) {
} else if (firElement instanceof FirWhenBranch) {
FirWhenBranch whenBranch = (FirWhenBranch) firElement;
return "when(" + firElementToString(whenBranch.getCondition()) + ")" + " -> " + firElementToString(whenBranch.getResult());
} else if (firElement.getClass().getSimpleName().equals("FirElseIfTrueCondition")) {
} else if ("FirElseIfTrueCondition".equals(firElement.getClass().getSimpleName())) {
return PsiElementAssociations.Companion.printElement(firElement);
} else if (firElement.getClass().getSimpleName().equals("FirSingleExpressionBlock")) {
} else if ("FirSingleExpressionBlock".equals(firElement.getClass().getSimpleName())) {
return PsiElementAssociations.Companion.printElement(firElement);
}
return "";
Expand Down
Loading

0 comments on commit e4a5dbd

Please sign in to comment.