Skip to content

Commit

Permalink
refactor: Common static analysis issues
Browse files Browse the repository at this point in the history
Co-authored-by: Moderne <team@moderne.io>
  • Loading branch information
jkschneider and TeamModerne committed Mar 5, 2023
1 parent 617c064 commit b5da895
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.util.function.Consumer;

public class Assertions {
public final class Assertions {
private Assertions() {
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable
VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
GlobalSearchScope globalScope = GlobalSearchScope.allScope(project);
JvmPackagePartProvider packagePartProvider = environment.createPackagePartProvider(globalScope);
Function<GlobalSearchScope, JvmPackagePartProvider> packagePartProviderFunction = (globalSearchScope) -> packagePartProvider;
Function<GlobalSearchScope, JvmPackagePartProvider> packagePartProviderFunction = globalSearchScope -> packagePartProvider;
VfsBasedProjectEnvironment projectEnvironment = new VfsBasedProjectEnvironment(
project,
fileSystem,
Expand All @@ -241,8 +241,8 @@ Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable

List<ContentRoot> contentRoots = compilerConfiguration.get(CONTENT_ROOTS);
List<KotlinSourceRoot> roots = contentRoots == null ? emptyList() : contentRoots.stream()
.filter(it -> it instanceof KotlinSourceRoot)
.map(it -> (KotlinSourceRoot) it).collect(toList());
.filter(KotlinSourceRoot.class::isInstance)
.map(KotlinSourceRoot.class::cast).collect(toList());

Function2<VirtualFile, Boolean, Unit> sortFiles = (virtualFile, isCommon) -> {
KtVirtualFileSourceFile file = new KtVirtualFileSourceFile(virtualFile);
Expand Down Expand Up @@ -363,7 +363,7 @@ public static class Builder extends Parser.Builder {
private Collection<Path> classpath = JavaParser.runtimeClasspath();

private JavaTypeCache typeCache = new JavaTypeCache();
private boolean logCompilationWarningsAndErrors = false;
private boolean logCompilationWarningsAndErrors;
private final List<NamedStyles> styles = new ArrayList<>();
private String moduleName = "main";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ private List<JavaType.FullyQualified> listAnnotations(List<FirAnnotation> firAnn
for (FirExpression argument : ((FirAnnotationCall) annotation).getArgumentList().getArguments()) {
if (argument instanceof FirPropertyAccessExpression) {
FirPropertyAccessExpression accessExpression = (FirPropertyAccessExpression) argument;
FirBasedSymbol<?> callRefSymbol = (((FirResolvedNamedReference) accessExpression.getCalleeReference()).getResolvedSymbol());
FirBasedSymbol<?> callRefSymbol = ((FirResolvedNamedReference) accessExpression.getCalleeReference()).getResolvedSymbol();
if (callRefSymbol instanceof FirEnumEntrySymbol) {
FirEnumEntrySymbol enumEntrySymbol = (FirEnumEntrySymbol) callRefSymbol;
if ("kotlin.annotation.AnnotationRetention$SOURCE".equals(convertKotlinFqToJavaFq(enumEntrySymbol.getCallableId().toString()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public String signature(@Nullable Object type, @Nullable FirBasedSymbol<?> owner
}

if (type instanceof FirClass) {
return ((FirClass) type).getTypeParameters().size() > 0 ? parameterizedSignature(type) : classSignature(type);
return !((FirClass) type).getTypeParameters().isEmpty() ? parameterizedSignature(type) : classSignature(type);
} else if (type instanceof FirFunction) {
return methodDeclarationSignature(((FirFunction) type).getSymbol());
} else if (type instanceof FirVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public class KotlinParserVisitor extends FirDefaultVisitor<J, ExecutionContext>
private final KotlinTypeMapping typeMapping;
private final ExecutionContext ctx;
private final FirSession firSession;
private int cursor = 0;
private int cursor;

// Associate top-level function and property declarations to the file.
@Nullable
private FirFile currentFile = null;
private FirFile currentFile;

private static final Pattern whitespaceSuffixPattern = Pattern.compile("\\s*[^\\s]+(\\s*)");

Expand Down Expand Up @@ -169,7 +169,7 @@ public J visitAnnotationCall(FirAnnotationCall annotationCall, ExecutionContext

J.Identifier name = (J.Identifier) visitElement(annotationCall.getCalleeReference(), ctx);
JContainer<Expression> args = null;
if (annotationCall.getArgumentList().getArguments().size() > 0) {
if (!annotationCall.getArgumentList().getArguments().isEmpty()) {
Space argsPrefix = sourceBefore("(");
List<JRightPadded<Expression>> expressions;
if (annotationCall.getArgumentList().getArguments().size() == 1) {
Expand Down Expand Up @@ -2328,11 +2328,11 @@ public J visitVariableAssignment(FirVariableAssignment variableAssignment, Execu
}

if (!(variableAssignment.getRValue() instanceof FirFunctionCall) ||
(((FirFunctionCall) variableAssignment.getRValue())).getArgumentList().getArguments().size() != 1) {
((FirFunctionCall) variableAssignment.getRValue()).getArgumentList().getArguments().size() != 1) {
throw new IllegalArgumentException("Unexpected compound assignment.");
}

FirElement rhs = (((FirFunctionCall) variableAssignment.getRValue())).getArgumentList().getArguments().get(0);
FirElement rhs = ((FirFunctionCall) variableAssignment.getRValue()).getArgumentList().getArguments().get(0);
return new J.AssignmentOperation(
randomId(),
prefix,
Expand Down Expand Up @@ -3119,31 +3119,26 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
if (source.length() - untilDelim.length() > delimIndex + 1) {
char c1 = source.charAt(delimIndex);
char c2 = source.charAt(delimIndex + 1);
switch (c1) {
case '/':
switch (c2) {
case '/':
inSingleLineComment = true;
delimIndex++;
break;
case '*':
inMultiLineComment = true;
delimIndex++;
break;
}
break;
case '*':
if (c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
}
break;
if (c1 == '/') {
if (c2 == '/') {
inSingleLineComment = true;
delimIndex++;
} else if (c2 == '*') {
inMultiLineComment = true;
delimIndex++;
}
} else if (c1 == '*') {
if (c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
}
}
}

if (!inMultiLineComment && !inSingleLineComment) {
if (stop != null && source.charAt(delimIndex) == stop)
return -1; // reached stop word before finding the delimiter
if (stop != null && source.charAt(delimIndex) == stop) {
return -1;
} // reached stop word before finding the delimiter

if (source.startsWith(untilDelim, delimIndex)) {
break; // found it!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void genericIntersectionType() {
public void enumTypeA() {
JavaType.Class clazz = (JavaType.Class) firstMethodParameter("enumTypeA");
JavaType.Method type = clazz.getMethods().stream()
.filter(m -> m.getName().equals("<constructor>"))
.filter(m -> "<constructor>".equals(m.getName()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No constructor found"));
assertThat(type.toString()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat$EnumTypeA{name=<constructor>,return=org.openrewrite.kotlin.KotlinTypeGoat$EnumTypeA,parameters=[]}");
Expand All @@ -194,7 +194,7 @@ public void enumTypeA() {
public void enumTypeB() {
JavaType.Class clazz = (JavaType.Class) firstMethodParameter("enumTypeB");
JavaType.Method type = clazz.getMethods().stream()
.filter(m -> m.getName().equals("<constructor>"))
.filter(m -> "<constructor>".equals(m.getName()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No constructor found"));
assertThat(type.toString()).isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat$EnumTypeB{name=<constructor>,return=org.openrewrite.kotlin.KotlinTypeGoat$EnumTypeB,parameters=[org.openrewrite.kotlin.KotlinTypeGoat$TypeA]}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class KotlinTypeSignatureBuilderTest {
private static final String goat = StringUtils.readFully(KotlinTypeSignatureBuilderTest.class.getResourceAsStream("/KotlinTypeGoat.kt"));

private static Disposable disposable = Disposer.newDisposable();
private static Map<FirSession, List<CompiledKotlinSource>> cu = null;
private static Map<FirSession, List<CompiledKotlinSource>> cu;

@BeforeAll
public static void beforeAll() {
Expand Down Expand Up @@ -72,21 +72,21 @@ private FirFile getCompiledSource() {

public String constructorSignature() {
return signatureBuilder().methodDeclarationSignature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(it -> it instanceof FirConstructor)
.map(it -> (FirFunction) it)
.filter(FirConstructor.class::isInstance)
.map(FirFunction.class::cast)
.findFirst()
.orElseThrow()
.getSymbol());
}

public Object innerClassSignature(String innerClassSimpleName) {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(it -> it instanceof FirRegularClass)
.map(it -> (FirRegularClass) it)
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.filter(it -> innerClassSimpleName.equals(it.getName().asString()))
.findFirst()
.orElseThrow()
Expand All @@ -95,10 +95,10 @@ public Object innerClassSignature(String innerClassSimpleName) {

public String fieldSignature(String field) {
return signatureBuilder().variableSignature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(it -> it instanceof FirProperty)
.map(it -> (FirProperty) it)
.filter(FirProperty.class::isInstance)
.map(FirProperty.class::cast)
.filter(it -> field.equals(it.getName().asString()))
.findFirst()
.orElseThrow()
Expand All @@ -107,10 +107,10 @@ public String fieldSignature(String field) {

public Object firstMethodParameterSignature(String methodName) {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(it -> it instanceof FirSimpleFunction)
.map(it -> (FirSimpleFunction) it)
.filter(FirSimpleFunction.class::isInstance)
.map(FirSimpleFunction.class::cast)
.filter(it -> methodName.equals(it.getName().asString()))
.findFirst()
.orElseThrow()
Expand All @@ -121,7 +121,7 @@ public Object firstMethodParameterSignature(String methodName) {

public Object lastClassTypeParameter() {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.findFirst()
.orElseThrow()
.getTypeParameters()
Expand All @@ -130,10 +130,10 @@ public Object lastClassTypeParameter() {

public String methodSignature(String methodName) {
return signatureBuilder().methodDeclarationSignature(getCompiledSource().getDeclarations().stream()
.map(it -> (FirRegularClass) it)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(it -> it instanceof FirSimpleFunction)
.map(it -> (FirSimpleFunction) it)
.filter(FirSimpleFunction.class::isInstance)
.map(FirSimpleFunction.class::cast)
.filter(it -> methodName.equals(it.getName().asString()))
.findFirst()
.orElseThrow()
Expand Down

0 comments on commit b5da895

Please sign in to comment.