Skip to content

Commit

Permalink
Try Kotlin 2.1.0-RC2
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka committed Nov 25, 2024
1 parent aa6621d commit 8222d77
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 106 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ group=org.sonarsource.kotlin
version=2.21-SNAPSHOT
description=Code Analyzer for Kotlin
projectTitle=Kotlin
kotlinVersion=2.0.20
kotlinVersion=2.1.0-RC2
org.gradle.jvmargs=-Xmx4096M
1 change: 1 addition & 0 deletions sonar-kotlin-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation(libs.staxmate)
implementation(libs.gson)
implementation(libs.sonar.analyzer.commons.recognizers)
implementation("com.github.ben-manes.caffeine:caffeine:2.9.3")

testImplementation(testLibs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,107 +19,5 @@
*/
package org.sonarsource.kotlin.api.frontend

import com.intellij.core.CoreApplicationEnvironment
import com.intellij.mock.MockApplication
import com.intellij.mock.MockProject
import com.intellij.openapi.Disposable
import com.intellij.psi.ClassTypePointerFactory
import com.intellij.psi.impl.smartPointers.PsiClassReferenceTypePointerFactory
import org.jetbrains.kotlin.analysis.api.KaAnalysisNonPublicApi
import org.jetbrains.kotlin.analysis.api.descriptors.CliFe10AnalysisFacade
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisFacade
import org.jetbrains.kotlin.analysis.api.descriptors.KaFe10AnalysisHandlerExtension
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeTokenProvider
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenProvider
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinGlobalModificationService
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationTrackerFactory
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinByModulesResolutionScopeProvider
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneGlobalModificationService
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneModificationTrackerFactory
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.AnalysisApiSimpleServiceRegistrar
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.PluginStructureProvider
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.references.fe10.base.DummyKtFe10ReferenceResolutionHelper
import org.jetbrains.kotlin.references.fe10.base.KtFe10ReferenceResolutionHelper
import org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension

@Deprecated("")
annotation class K1only(val comments: String = "")

internal fun configureK1AnalysisApiServices(env: KotlinCoreEnvironment) {
val application = env.projectEnvironment.environment.application
if (application.getServiceIfCreated(KtFe10ReferenceResolutionHelper::class.java) == null) {
AnalysisApiFe10ServiceRegistrar.registerApplicationServices(application)
}
val project = env.projectEnvironment.project
AnalysisApiFe10ServiceRegistrar.registerProjectServices(project)
AnalysisApiFe10ServiceRegistrar.registerProjectModelServices(
project,
env.projectEnvironment.parentDisposable
)

project.registerService(
KotlinModificationTrackerFactory::class.java,
KotlinStandaloneModificationTrackerFactory::class.java,
)
project.registerService(
KotlinGlobalModificationService::class.java,
KotlinStandaloneGlobalModificationService::class.java,
)
project.registerService(
KotlinLifetimeTokenProvider::class.java,
KotlinAlwaysAccessibleLifetimeTokenProvider::class.java,
)
project.registerService(
KotlinResolutionScopeProvider::class.java,
KotlinByModulesResolutionScopeProvider::class.java,
);
project.registerService(
KotlinProjectStructureProvider::class.java,
KtModuleProviderByCompilerConfiguration.build(
env.projectEnvironment,
env.configuration,
listOf()
)
)
}

@OptIn(KaAnalysisNonPublicApi::class)
private object AnalysisApiFe10ServiceRegistrar : AnalysisApiSimpleServiceRegistrar() {
private const val PLUGIN_RELATIVE_PATH = "/META-INF/analysis-api/analysis-api-fe10.xml"

override fun registerApplicationServices(application: MockApplication) {
PluginStructureProvider.registerApplicationServices(application, PLUGIN_RELATIVE_PATH)
application.registerService(
KtFe10ReferenceResolutionHelper::class.java,
DummyKtFe10ReferenceResolutionHelper,
)
val applicationArea = application.extensionArea
if (!applicationArea.hasExtensionPoint(ClassTypePointerFactory.EP_NAME)) {
CoreApplicationEnvironment.registerApplicationExtensionPoint(
ClassTypePointerFactory.EP_NAME,
ClassTypePointerFactory::class.java,
)
applicationArea
.getExtensionPoint(ClassTypePointerFactory.EP_NAME)
.registerExtension(PsiClassReferenceTypePointerFactory(), application)
}
}

override fun registerProjectExtensionPoints(project: MockProject) {
AnalysisHandlerExtension.registerExtensionPoint(project)
PluginStructureProvider.registerProjectExtensionPoints(project, PLUGIN_RELATIVE_PATH)
}

override fun registerProjectServices(project: MockProject) {
PluginStructureProvider.registerProjectServices(project, PLUGIN_RELATIVE_PATH)
PluginStructureProvider.registerProjectListeners(project, PLUGIN_RELATIVE_PATH)
}

override fun registerProjectModelServices(project: MockProject, disposable: Disposable) {
project.apply { registerService(Fe10AnalysisFacade::class.java, CliFe10AnalysisFacade()) }
AnalysisHandlerExtension.registerExtension(project, KaFe10AnalysisHandlerExtension())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* SonarSource Kotlin
* Copyright (C) 2018-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.kotlin.api.frontend;


import com.intellij.core.CoreApplicationEnvironment;
import com.intellij.mock.MockApplication;
import com.intellij.mock.MockProject;
import com.intellij.openapi.Disposable;
import com.intellij.psi.ClassTypePointerFactory;
import com.intellij.psi.impl.smartPointers.PsiClassReferenceTypePointerFactory;
import org.jetbrains.kotlin.analysis.api.descriptors.CliFe10AnalysisFacade;
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisFacade;
import org.jetbrains.kotlin.analysis.api.descriptors.KaFe10AnalysisHandlerExtension;
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeTokenFactory;
import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinLifetimeTokenFactory;
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinGlobalModificationService;
import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationTrackerFactory;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinByModulesResolutionScopeProvider;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider;
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider;
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneGlobalModificationService;
import org.jetbrains.kotlin.analysis.api.standalone.base.modification.KotlinStandaloneModificationTrackerFactory;
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.AnalysisApiSimpleServiceRegistrar;
import org.jetbrains.kotlin.analysis.api.standalone.base.projectStructure.PluginStructureProvider;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.references.fe10.base.DummyKtFe10ReferenceResolutionHelper;
import org.jetbrains.kotlin.references.fe10.base.KtFe10ReferenceResolutionHelper;
import org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension;

import java.util.Collections;

@SuppressWarnings("ALL")
public class K1J {

public static void configureK1AnalysisApiServices(KotlinCoreEnvironment env) {
MockApplication application = env.getProjectEnvironment().getEnvironment().getApplication();

if (application.getServiceIfCreated(KtFe10ReferenceResolutionHelper.class) == null) {
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerApplicationServices(application);
}
final var project = env.getProjectEnvironment().getProject();
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerProjectServices(project);
AnalysisApiFe10ServiceRegistrar.INSTANCE.registerProjectModelServices(
project,
env.getProjectEnvironment().getParentDisposable()
);

project.registerService(
KotlinModificationTrackerFactory.class,
KotlinStandaloneModificationTrackerFactory.class
);
project.registerService(
KotlinGlobalModificationService.class,
KotlinStandaloneGlobalModificationService.class
);
project.registerService(
KotlinLifetimeTokenFactory.class,
KotlinAlwaysAccessibleLifetimeTokenFactory.class
);
project.registerService(
KotlinResolutionScopeProvider.class,
KotlinByModulesResolutionScopeProvider.class
);
project.registerService(
KotlinProjectStructureProvider.class,
KtModuleProviderByCompilerConfiguration.build(
env.getProjectEnvironment(),
env.getConfiguration(),
Collections.emptyList()
)
);
}
}

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
);


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);
}

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());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +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 java.io.File

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

import org.jetbrains.kotlin.analysis.api.KaIdeApi
import org.jetbrains.kotlin.analysis.api.descriptors.KaFe10Session
import org.jetbrains.kotlin.descriptors.VariableAccessorDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
import org.jetbrains.kotlin.descriptors.accessors
Expand Down Expand Up @@ -54,6 +53,7 @@ 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 MESSAGE_UNUSED = "Remove this unused import."
private const val MESSAGE_REDUNDANT = "Remove this redundant import."
Expand All @@ -68,7 +68,7 @@ class UnnecessaryImportsCheck : AbstractCheck() {
override fun visitKtFile(file: KtFile, context: KotlinFileContext) {

analyze {
if (this !is KaFe10Session) {
if (isK2(this)) {
val analyzeImportsToOptimize = analyzeImportsToOptimize(file)

file.importDirectives.mapNotNull { import -> import.importedFqName?.let { import to it } }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SonarSource Kotlin
* Copyright (C) 2018-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.kotlin.checks;

import org.jetbrains.kotlin.analysis.api.KaSession;
import org.jetbrains.kotlin.analysis.api.fir.KaFirSession;

public class UnnecessaryImportsHelper {
public static boolean isK2(KaSession session) {
return session instanceof KaFirSession;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class WorkaroundForJarMinimization {
org.jetbrains.kotlin.analysis.api.descriptors.KaFe10SessionProvider.class,
org.jetbrains.kotlin.references.fe10.base.KtFe10KotlinReferenceProviderContributor.class,
org.jetbrains.kotlin.analysis.api.descriptors.references.ReadWriteAccessCheckerDescriptorsImpl.class,
org.jetbrains.kotlin.analysis.api.descriptors.modification.KaFe10SourceModificationService.class,
org.jetbrains.kotlin.analysis.api.fir.KaFirSession.class,

/** Used to have proper named groups behavior in regular expressions */
kotlin.internal.jdk8.JDK8PlatformImplementations.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import org.junit.jupiter.api.Test
internal class WorkaroundForJarMinimizationTest {
@Test
fun minimizing_class_count() {
assertThat(WorkaroundForJarMinimization.CLASSES_TO_KEEP_WHEN_MINIMIZING_JAR).hasSize(16)
assertThat(WorkaroundForJarMinimization.CLASSES_TO_KEEP_WHEN_MINIMIZING_JAR).hasSize(18)
}
}

0 comments on commit 8222d77

Please sign in to comment.