Skip to content

Commit

Permalink
refactor: Common static analysis issues
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Mar 12, 2024
1 parent 2bdf2c6 commit f54ac1e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public String methodType(String methodName) {
void fileField() {
IrProperty property = getCompiledSource().getDeclarations().stream()
.filter(it -> it instanceof IrProperty && "field".equals(((IrProperty) it).getName().asString()))
.map(it -> (IrProperty) it).findFirst().orElseThrow();
.map(IrProperty.class::cast).findFirst().orElseThrow();
assertThat(typeMapper().variableType(property).toString())
.isEqualTo("org.openrewrite.kotlin.KotlinTypeGoatKt{name=field,type=kotlin.Int}");
}
Expand All @@ -181,7 +181,7 @@ void fileField() {
void fileFunction() {
IrFunction function = getCompiledSource().getDeclarations().stream()
.filter(it -> it instanceof IrFunction && "function".equals(((IrFunction) it).getName().asString()))
.map(it -> (IrFunction) it).findFirst().orElseThrow();
.map(IrFunction.class::cast).findFirst().orElseThrow();
assertThat(typeMapper().methodDeclarationType(function).toString())
.isEqualTo("org.openrewrite.kotlin.KotlinTypeGoatKt{name=function,return=kotlin.Unit,parameters=[org.openrewrite.kotlin.C]}");
}
Expand Down
33 changes: 15 additions & 18 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public JavaType.Method methodType(String methodName) {

public K.Property getProperty(String fieldName) {
return goatClassDeclaration.getClassDeclaration().getBody().getStatements().stream()
.filter(it -> it instanceof K.Property)
.filter(K.Property.class::isInstance)
.map(K.Property.class::cast)
.filter(mv -> mv.getVariableDeclarations().getVariables().stream().anyMatch(v -> v.getSimpleName().equals(fieldName)))
.findFirst()
Expand All @@ -110,15 +110,15 @@ void fieldType() {
assertThat(id.getType()).isInstanceOf(JavaType.Class.class);
assertThat(id.getType().toString()).isEqualTo("kotlin.Int");

J.MethodDeclaration getter = property.getAccessors().getElements().stream().filter(x -> x.getName().getSimpleName().equals("get")).findFirst().orElse(null);
J.MethodDeclaration getter = property.getAccessors().getElements().stream().filter(x -> "get".equals(x.getName().getSimpleName())).findFirst().orElse(null);
JavaType.FullyQualified declaringType = getter.getMethodType().getDeclaringType();
assertThat(declaringType.getFullyQualifiedName()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat");
assertThat(getter.getMethodType().getName()).isEqualTo("get");
assertThat(getter.getMethodType().getReturnType()).isEqualTo(id.getType());
assertThat(getter.getName().getType()).isEqualTo(getter.getMethodType());
assertThat(getter.getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=get,return=kotlin.Int,parameters=[]}");

J.MethodDeclaration setter = property.getAccessors().getElements().stream().filter(x -> x.getName().getSimpleName().equals("set")).findFirst().orElse(null);
J.MethodDeclaration setter = property.getAccessors().getElements().stream().filter(x -> "set".equals(x.getName().getSimpleName())).findFirst().orElse(null);
declaringType = setter.getMethodType().getDeclaringType();
assertThat(declaringType.getFullyQualifiedName()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat");
assertThat(setter.getMethodType().getName()).isEqualTo("set");
Expand All @@ -129,7 +129,7 @@ void fieldType() {
@Test
void fileField() {
J.VariableDeclarations.NamedVariable nv = cu.getStatements().stream()
.filter(it -> it instanceof J.VariableDeclarations)
.filter(J.VariableDeclarations.class::isInstance)
.flatMap(it -> ((J.VariableDeclarations) it).getVariables().stream())
.filter(it -> "field".equals(it.getSimpleName())).findFirst().orElseThrow();

Expand All @@ -142,7 +142,7 @@ void fileField() {
@Test
void fileFunction() {
J.MethodDeclaration md = cu.getStatements().stream()
.filter(it -> it instanceof J.MethodDeclaration)
.filter(J.MethodDeclaration.class::isInstance)
.map(J.MethodDeclaration.class::cast)
.filter(it -> "function".equals(it.getSimpleName())).findFirst().orElseThrow();

Expand Down Expand Up @@ -199,7 +199,7 @@ void parameterized() {
.map(J.MethodDeclaration.class::cast).findFirst().orElseThrow();
assertThat(md.getMethodType().toString())
.isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat{name=parameterized,return=org.openrewrite.kotlin.PT<org.openrewrite.kotlin.C>,parameters=[org.openrewrite.kotlin.PT<org.openrewrite.kotlin.C>]}");
J.VariableDeclarations vd = ((J.VariableDeclarations) md.getParameters().get(0));
J.VariableDeclarations vd = (J.VariableDeclarations) md.getParameters().get(0);
assertThat(vd.getTypeExpression().getType().toString())
.isEqualTo("org.openrewrite.kotlin.PT<org.openrewrite.kotlin.C>");
assertThat(((J.ParameterizedType) vd.getTypeExpression()).getClazz().getType().toString())
Expand Down Expand Up @@ -1267,7 +1267,7 @@ void annotationOnKotlinConeType(String input, String type) {
new KotlinIsoVisitor<Integer>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, Integer integer) {
assertThat(String.valueOf(annotation.getType().toString())).isEqualTo(type);
assertThat(annotation.getType().toString()).isEqualTo(type);
found.set(true);
return super.visitAnnotation(annotation, integer);
}
Expand Down Expand Up @@ -1316,11 +1316,11 @@ class Test {
new KotlinIsoVisitor<Integer>() {
@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Integer integer) {
if (identifier.getSimpleName().equals("lang")) {
if ("lang".equals(identifier.getSimpleName())) {
assertThat(identifier.getType()).isNull();
isFieldTargetNull.set(true);
}
if (identifier.getSimpleName().equals("StringBuilder")) {
if ("StringBuilder".equals(identifier.getSimpleName())) {
assertThat(identifier.getType().toString()).isEqualTo("java.lang.StringBuilder");
isStringBuilderTyped.set(true);
}
Expand Down Expand Up @@ -1419,15 +1419,12 @@ fun bar(b: Array<out Number>) {}
new KotlinIsoVisitor<Integer>() {
@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Integer integer) {
switch (identifier.getSimpleName()) {
case "String" -> {
assertThat(identifier.getType().toString()).isEqualTo("kotlin.String");
count.incrementAndGet();
}
case "Number" -> {
assertThat(identifier.getType().toString()).isEqualTo("kotlin.Number");
count.incrementAndGet();
}
if ("String".equals(identifier.getSimpleName())) {
assertThat(identifier.getType().toString()).isEqualTo("kotlin.String");
count.incrementAndGet();
} else if ("Number".equals(identifier.getSimpleName())) {
assertThat(identifier.getType().toString()).isEqualTo("kotlin.Number");
count.incrementAndGet();
}
return super.visitIdentifier(identifier, integer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class A{val zero:Int=0
void variableDeclarationsInClass2() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand Down Expand Up @@ -312,7 +312,7 @@ void noSpaceAferAnnotation() {
rewriteRun(
spec -> spec.parser(KotlinParser.builder().classpath("junit-jupiter-api"))
.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand All @@ -332,7 +332,7 @@ fun testA() {
void classConstructor() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand All @@ -351,7 +351,7 @@ class BaseProjectionNode (
void spaceAfterPublic() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand All @@ -369,7 +369,7 @@ public fun me() {
void statementWithCommentInPrefix() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand All @@ -387,7 +387,7 @@ fun x() {
void compilationUnitDeclarations() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand All @@ -404,7 +404,7 @@ class Test
void blockWithImplicitReturn() {
rewriteRun(
spec -> spec.recipes(
toRecipe(() -> new MinimumViableSpacingVisitor<>())
toRecipe(MinimumViableSpacingVisitor::new)
),
kotlin(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RemoveTrailingWhitespaceTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(toRecipe(() -> new RemoveTrailingWhitespaceVisitor<>()));
spec.recipe(toRecipe(RemoveTrailingWhitespaceVisitor::new));
}

@DocumentExample
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/org/openrewrite/kotlin/style/AutodetectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,15 @@ class Test {

assertThat(importLayout.getLayout().get(1))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("java\\..+"));
.matches(b -> "java\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(2))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("javax\\..+"));
.matches(b -> "javax\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(3))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("kotlin\\..+"));
.matches(b -> "kotlin\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(4)).isInstanceOf(ImportLayoutStyle.Block.AllAliases.class);
}
Expand Down Expand Up @@ -551,15 +551,15 @@ class Test {

assertThat(importLayout.getLayout().get(1))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("kotlin\\..+"));
.matches(b -> "kotlin\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(2))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("javax\\..+"));
.matches(b -> "javax\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(3))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("java\\..+"));
.matches(b -> "java\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(4)).isInstanceOf(ImportLayoutStyle.Block.AllOthers.class);
}
Expand Down Expand Up @@ -594,17 +594,17 @@ class Test {

assertThat(importLayout.getLayout().get(0))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("java\\..+"));
.matches(b -> "java\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(1))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("kotlin\\..+"));
.matches(b -> "kotlin\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(2)).isInstanceOf(ImportLayoutStyle.Block.AllOthers.class);

assertThat(importLayout.getLayout().get(3))
.isInstanceOf(ImportLayoutStyle.Block.ImportPackage.class)
.matches(b -> ((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString().equals("javax\\..+"));
.matches(b -> "javax\\..+".equals(((ImportLayoutStyle.Block.ImportPackage) b).getPackageWildcard().toString()));

assertThat(importLayout.getLayout().get(4)).isInstanceOf(ImportLayoutStyle.Block.AllAliases.class);
}
Expand Down

0 comments on commit f54ac1e

Please sign in to comment.