Skip to content

Commit

Permalink
Simplify 'isK2' check
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka committed Nov 26, 2024
1 parent 8222d77 commit e3c264c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
import java.util.Collections;

@SuppressWarnings("ALL")
public class K1J {
public class K1 {

private K1() {
// Utility class
}

public static void configureK1AnalysisApiServices(KotlinCoreEnvironment env) {
MockApplication application = env.getProjectEnvironment().getEnvironment().getApplication();
Expand Down Expand Up @@ -88,48 +92,50 @@ public static void configureK1AnalysisApiServices(KotlinCoreEnvironment env) {
)
);
}
}

class AnalysisApiFe10ServiceRegistrar extends AnalysisApiSimpleServiceRegistrar {
public static final AnalysisApiSimpleServiceRegistrar INSTANCE = new AnalysisApiFe10ServiceRegistrar();
private static final String PLUGIN_RELATIVE_PATH = "/META-INF/analysis-api/analysis-api-fe10.xml";

private AnalysisApiFe10ServiceRegistrar() {
}

public void registerApplicationServices(MockApplication application) {
PluginStructureProvider.INSTANCE.registerApplicationServices(application, PLUGIN_RELATIVE_PATH);
application.registerService(
KtFe10ReferenceResolutionHelper.class,
DummyKtFe10ReferenceResolutionHelper.INSTANCE
);
private static class AnalysisApiFe10ServiceRegistrar extends AnalysisApiSimpleServiceRegistrar {
public static final AnalysisApiSimpleServiceRegistrar INSTANCE = new AnalysisApiFe10ServiceRegistrar();
private static final String PLUGIN_RELATIVE_PATH = "/META-INF/analysis-api/analysis-api-fe10.xml";

private AnalysisApiFe10ServiceRegistrar() {
}

final var applicationArea = application.getExtensionArea();
if (!applicationArea.hasExtensionPoint(ClassTypePointerFactory.EP_NAME)) {
CoreApplicationEnvironment.registerApplicationExtensionPoint(
ClassTypePointerFactory.EP_NAME,
ClassTypePointerFactory.class
@Override
public void registerApplicationServices(MockApplication application) {
PluginStructureProvider.INSTANCE.registerApplicationServices(application, PLUGIN_RELATIVE_PATH);
application.registerService(
KtFe10ReferenceResolutionHelper.class,
DummyKtFe10ReferenceResolutionHelper.INSTANCE
);
applicationArea
.getExtensionPoint(ClassTypePointerFactory.EP_NAME)
.registerExtension(new PsiClassReferenceTypePointerFactory(), application);

final var applicationArea = application.getExtensionArea();
if (!applicationArea.hasExtensionPoint(ClassTypePointerFactory.EP_NAME)) {
CoreApplicationEnvironment.registerApplicationExtensionPoint(
ClassTypePointerFactory.EP_NAME,
ClassTypePointerFactory.class
);
applicationArea
.getExtensionPoint(ClassTypePointerFactory.EP_NAME)
.registerExtension(new PsiClassReferenceTypePointerFactory(), application);
}
}
}

public void registerProjectExtensionPoints(MockProject project) {
AnalysisHandlerExtension.Companion.registerExtensionPoint(project);
PluginStructureProvider.INSTANCE.registerProjectExtensionPoints(project, PLUGIN_RELATIVE_PATH);
}
@Override
public void registerProjectExtensionPoints(MockProject project) {
AnalysisHandlerExtension.Companion.registerExtensionPoint(project);
PluginStructureProvider.INSTANCE.registerProjectExtensionPoints(project, PLUGIN_RELATIVE_PATH);
}

public void registerProjectServices(MockProject project) {
PluginStructureProvider.INSTANCE.registerProjectServices(project, PLUGIN_RELATIVE_PATH);
PluginStructureProvider.INSTANCE.registerProjectListeners(project, PLUGIN_RELATIVE_PATH);
}
@Override
public void registerProjectServices(MockProject project) {
PluginStructureProvider.INSTANCE.registerProjectServices(project, PLUGIN_RELATIVE_PATH);
PluginStructureProvider.INSTANCE.registerProjectListeners(project, PLUGIN_RELATIVE_PATH);
}

public void registerProjectModelServices(MockProject project, Disposable disposable) {
project.registerService(Fe10AnalysisFacade.class, new CliFe10AnalysisFacade());
AnalysisHandlerExtension.Companion.registerExtension(project, new KaFe10AnalysisHandlerExtension());
@Override
public void registerProjectModelServices(MockProject project, Disposable disposable) {
project.registerService(Fe10AnalysisFacade.class, new CliFe10AnalysisFacade());
AnalysisHandlerExtension.Companion.registerExtension(project, new KaFe10AnalysisHandlerExtension());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import org.jetbrains.kotlin.util.slicedMap.WritableSlice
import org.sonarsource.kotlin.api.frontend.K1J.configureK1AnalysisApiServices
import org.sonarsource.kotlin.api.frontend.K1.configureK1AnalysisApiServices
import java.io.File

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,13 @@
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.analysis.api.KaIdeApi
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.descriptors.VariableAccessorDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
import org.jetbrains.kotlin.descriptors.accessors
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtArrayAccessExpression
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtImportList
import org.jetbrains.kotlin.psi.KtOperationReferenceExpression
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.KtPrefixExpression
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi2ir.deparenthesize
Expand All @@ -53,8 +40,8 @@ import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.analyze
import org.sonarsource.kotlin.checks.UnnecessaryImportsHelper.isK2

private const val KA_FIR_SESSION = "org.jetbrains.kotlin.analysis.api.fir.KaFirSession"
private const val MESSAGE_UNUSED = "Remove this unused import."
private const val MESSAGE_REDUNDANT = "Remove this redundant import."
private val DELEGATES_IMPORTED_NAMES = setOf("getValue", "setValue", "provideDelegate")
Expand All @@ -68,7 +55,7 @@ class UnnecessaryImportsCheck : AbstractCheck() {
override fun visitKtFile(file: KtFile, context: KotlinFileContext) {

analyze {
if (isK2(this)) {
if (isK2()) {
val analyzeImportsToOptimize = analyzeImportsToOptimize(file)

file.importDirectives.mapNotNull { import -> import.importedFqName?.let { import to it } }
Expand Down Expand Up @@ -285,3 +272,6 @@ private fun KtReferenceExpression.importableSimpleName() =
is KtSimpleNameExpression -> getReferencedName()
else -> null
}

fun KaSession.isK2(): Boolean =
KA_FIR_SESSION == javaClass.name

This file was deleted.

0 comments on commit e3c264c

Please sign in to comment.