<commit_before>/*
 * Copyright (C) 2019-2020 <NAME>
 * Copyright 2000-2019 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.compat.testFramework

import com.intellij.mock.MockApplication
import com.intellij.mock.MockProjectEx
import com.intellij.openapi.components.ComponentManager
import com.intellij.openapi.extensions.*
import com.intellij.openapi.extensions.impl.ExtensionsAreaImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import org.picocontainer.MutablePicoContainer
import java.lang.reflect.Modifier

abstract class PlatformLiteFixture : com.intellij.testFramework.UsefulTestCase() {
    // region Application

    private var myApp: MockApplication? = null

    fun initApplication(): MockApplication {
        val app = MockApplication.setUp(testRootDisposable)
        myApp = app
        return app
    }

    // endregion
    // region Project

    private var myProjectEx: MockProjectEx? = null
    protected var myProject: MockProjectEx
        get() = myProjectEx!!
        set(value) {
            myProjectEx = value
        }

    val project: Project get() = myProject

    // endregion
    // region JUnit

    @Throws(Exception::class)
    override fun tearDown() {
        myProjectEx = null
        try {
            super.tearDown()
        } finally {
            clearFields(this)
        }
    }

    // endregion
    // region Registering Application Services

    fun <T> registerApplicationService(aClass: Class<T>, `object`: T) {
        myApp!!.registerService(aClass, `object`, testRootDisposable)
    }

    @Suppress("UNCHECKED_CAST")
    fun registerApplicationService(className: String) {
        try {
            val aClass = Class.forName(className) as Class<Any>
            val `object` = aClass.getConstructor().newInstance()
            registerApplicationService(aClass, `object`)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    // endregion
    // region Registering Project Services

    fun <T> registerProjectService(aClass: Class<T>, `object`: T) {
        myProject.registerService(aClass, `object`)
    }

    // endregion
    // region Registering Component Instances

    fun <T> registerComponentInstance(container: MutablePicoContainer, key: Class<T>, implementation: T): T {
        val old = container.getComponentInstance(key)
        container.unregisterComponent(key)
        container.registerComponentInstance(key, implementation)

        @Suppress("UNCHECKED_CAST")
        return old as T
    }

    fun <T> registerComponentInstance(container: ComponentManager, key: Class<T>, implementation: T): T {
        return registerComponentInstance(container.picoContainer as MutablePicoContainer, key, implementation)
    }

    // endregion
    // region Registering Extension Points

    fun <T> registerExtensionPoint(extensionPointName: ExtensionPointName<T>, aClass: Class<T>) {
        registerExtensionPoint(Extensions.getRootArea(), extensionPointName, aClass)
    }

    fun registerExtensionPoint(epClassName: String, epField: String, aClass: Class<*>? = null) {
        registerExtensionPoint(Extensions.getRootArea(), epClassName, epField, aClass)
    }

    fun <T> registerExtensionPoint(
        area: ExtensionsArea,
        extensionPointName: ExtensionPointName<T>,
        aClass: Class<out T>
    ) {
        if (!area.hasExtensionPoint(extensionPointName)) {
            val kind =
                if (aClass.isInterface || aClass.modifiers and Modifier.ABSTRACT != 0)
                    ExtensionPoint.Kind.INTERFACE
                else
                    ExtensionPoint.Kind.BEAN_CLASS
            val impl = area as ExtensionsAreaImpl
            impl.registerExtensionPoint(extensionPointName, aClass.name, kind, testRootDisposable)
            Disposer.register(myProject, com.intellij.openapi.Disposable {
                area.unregisterExtensionPoint(extensionPointName.name)
            })
        }
    }

    fun registerExtensionPoint(area: ExtensionsArea, epClassName: String, epField: String, aClass: Class<*>? = null) {
        try {
            val epClass = Class.forName(epClassName)
            val epname = epClass.getDeclaredField(epField)
            val register = PlatformLiteFixture::class.java.getDeclaredMethod(
                "registerExtensionPoint", ExtensionsArea::class.java, ExtensionPointName::class.java, Class::class.java
            )
            epname.isAccessible = true
            register.invoke(this, area, epname.get(null), aClass ?: epClass)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage", "SameParameterValue")
    fun <T> registerExtensionPoint(
        area: AreaInstance,
        extensionPointName: ExtensionPointName<T>,
        aClass: Class<out T>
    ) {
        registerExtensionPoint(area.extensionArea, extensionPointName, aClass)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun registerExtensionPoint(area: AreaInstance, epClassName: String, epField: String, aClass: Class<*>? = null) {
        registerExtensionPoint(area.extensionArea, epClassName, epField, aClass)
    }

    // endregion
    // region Registering Extensions

    private fun <T : Any> registerExtension(area: ExtensionsArea, name: ExtensionPointName<T>, extension: T) {
        area.getExtensionPoint<T>(name.name).registerExtension(extension, testRootDisposable)
    }

    fun <T : Any> registerExtension(extensionPointName: ExtensionPointName<T>, extension: T) {
        registerExtension(Extensions.getRootArea(), extensionPointName, extension)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun <T : Any> registerExtension(area: AreaInstance, name: ExtensionPointName<T>, extension: T) {
        registerExtension(area.extensionArea, name, extension)
    }

    private fun <T : Any> registerExtension(
        area: ExtensionsArea,
        epClassName: String,
        epField: String,
        extension: T
    ) {
        try {
            val epClass = Class.forName(epClassName)
            val epname = epClass.getDeclaredField(epField)
            @Suppress("UNCHECKED_CAST")
            registerExtension(area, epname.get(null) as ExtensionPointName<T>, extension)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    fun <T : Any> registerExtension(epClassName: String, epField: String, extension: T) {
        registerExtension(Extensions.getRootArea(), epClassName, epField, extension)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun <T : Any> registerExtension(
        area: AreaInstance,
        epClassName: String,
        epField: String,
        extension: T
    ) {
        registerExtension(area.extensionArea, epClassName, epField, extension)
    }

    // endregion
}
<commit_msg>Work around the endpoint cleanup exceptions that can occur on IntelliJ 2020.1.<commit_after>/*
 * Copyright (C) 2019-2020 <NAME>
 * Copyright 2000-2019 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.compat.testFramework

import com.intellij.mock.MockApplication
import com.intellij.mock.MockProjectEx
import com.intellij.openapi.components.ComponentManager
import com.intellij.openapi.extensions.*
import com.intellij.openapi.extensions.impl.ExtensionsAreaImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import org.picocontainer.MutablePicoContainer
import java.lang.reflect.Modifier

abstract class PlatformLiteFixture : com.intellij.testFramework.UsefulTestCase() {
    // region Application

    private var myApp: MockApplication? = null

    fun initApplication(): MockApplication {
        val app = MockApplication.setUp(testRootDisposable)
        myApp = app
        return app
    }

    // endregion
    // region Project

    private var myProjectEx: MockProjectEx? = null
    protected var myProject: MockProjectEx
        get() = myProjectEx!!
        set(value) {
            myProjectEx = value
        }

    val project: Project get() = myProject

    // endregion
    // region JUnit

    @Throws(Exception::class)
    override fun tearDown() {
        myProjectEx = null
        try {
            super.tearDown()
        } catch (e: Throwable) {
            // IntelliJ 2020.1 can throw an error in CodeStyleSettingsManager.getCurrentSettings
            // when trying to clean up the registered endpoints. Log that error, but don't make
            // it fail the test.
            LOG.warn(e)
        } finally {
            clearFields(this)
        }
    }

    // endregion
    // region Registering Application Services

    fun <T> registerApplicationService(aClass: Class<T>, `object`: T) {
        myApp!!.registerService(aClass, `object`, testRootDisposable)
    }

    @Suppress("UNCHECKED_CAST")
    fun registerApplicationService(className: String) {
        try {
            val aClass = Class.forName(className) as Class<Any>
            val `object` = aClass.getConstructor().newInstance()
            registerApplicationService(aClass, `object`)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    // endregion
    // region Registering Project Services

    fun <T> registerProjectService(aClass: Class<T>, `object`: T) {
        myProject.registerService(aClass, `object`)
    }

    // endregion
    // region Registering Component Instances

    fun <T> registerComponentInstance(container: MutablePicoContainer, key: Class<T>, implementation: T): T {
        val old = container.getComponentInstance(key)
        container.unregisterComponent(key)
        container.registerComponentInstance(key, implementation)

        @Suppress("UNCHECKED_CAST")
        return old as T
    }

    fun <T> registerComponentInstance(container: ComponentManager, key: Class<T>, implementation: T): T {
        return registerComponentInstance(container.picoContainer as MutablePicoContainer, key, implementation)
    }

    // endregion
    // region Registering Extension Points

    fun <T> registerExtensionPoint(extensionPointName: ExtensionPointName<T>, aClass: Class<T>) {
        registerExtensionPoint(Extensions.getRootArea(), extensionPointName, aClass)
    }

    fun registerExtensionPoint(epClassName: String, epField: String, aClass: Class<*>? = null) {
        registerExtensionPoint(Extensions.getRootArea(), epClassName, epField, aClass)
    }

    fun <T> registerExtensionPoint(
        area: ExtensionsArea,
        extensionPointName: ExtensionPointName<T>,
        aClass: Class<out T>
    ) {
        if (!area.hasExtensionPoint(extensionPointName)) {
            val kind =
                if (aClass.isInterface || aClass.modifiers and Modifier.ABSTRACT != 0)
                    ExtensionPoint.Kind.INTERFACE
                else
                    ExtensionPoint.Kind.BEAN_CLASS
            val impl = area as ExtensionsAreaImpl
            impl.registerExtensionPoint(extensionPointName, aClass.name, kind, testRootDisposable)
            Disposer.register(myProject, com.intellij.openapi.Disposable {
                area.unregisterExtensionPoint(extensionPointName.name)
            })
        }
    }

    fun registerExtensionPoint(area: ExtensionsArea, epClassName: String, epField: String, aClass: Class<*>? = null) {
        try {
            val epClass = Class.forName(epClassName)
            val epname = epClass.getDeclaredField(epField)
            val register = PlatformLiteFixture::class.java.getDeclaredMethod(
                "registerExtensionPoint", ExtensionsArea::class.java, ExtensionPointName::class.java, Class::class.java
            )
            epname.isAccessible = true
            register.invoke(this, area, epname.get(null), aClass ?: epClass)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage", "SameParameterValue")
    fun <T> registerExtensionPoint(
        area: AreaInstance,
        extensionPointName: ExtensionPointName<T>,
        aClass: Class<out T>
    ) {
        registerExtensionPoint(area.extensionArea, extensionPointName, aClass)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun registerExtensionPoint(area: AreaInstance, epClassName: String, epField: String, aClass: Class<*>? = null) {
        registerExtensionPoint(area.extensionArea, epClassName, epField, aClass)
    }

    // endregion
    // region Registering Extensions

    private fun <T : Any> registerExtension(area: ExtensionsArea, name: ExtensionPointName<T>, extension: T) {
        area.getExtensionPoint<T>(name.name).registerExtension(extension, testRootDisposable)
    }

    fun <T : Any> registerExtension(extensionPointName: ExtensionPointName<T>, extension: T) {
        registerExtension(Extensions.getRootArea(), extensionPointName, extension)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun <T : Any> registerExtension(area: AreaInstance, name: ExtensionPointName<T>, extension: T) {
        registerExtension(area.extensionArea, name, extension)
    }

    private fun <T : Any> registerExtension(
        area: ExtensionsArea,
        epClassName: String,
        epField: String,
        extension: T
    ) {
        try {
            val epClass = Class.forName(epClassName)
            val epname = epClass.getDeclaredField(epField)
            @Suppress("UNCHECKED_CAST")
            registerExtension(area, epname.get(null) as ExtensionPointName<T>, extension)
        } catch (e: Exception) {
            // Don't register the extension point, as the associated class is not found.
        }
    }

    fun <T : Any> registerExtension(epClassName: String, epField: String, extension: T) {
        registerExtension(Extensions.getRootArea(), epClassName, epField, extension)
    }

    // IntelliJ >= 2019.3 deprecates Extensions#getArea
    @Suppress("UnstableApiUsage")
    fun <T : Any> registerExtension(
        area: AreaInstance,
        epClassName: String,
        epField: String,
        extension: T
    ) {
        registerExtension(area.extensionArea, epClassName, epField, extension)
    }

    // endregion
}

{'fraction_non_alphanumeric': 0.09241257416992804, 'fraction_numerical': 0.005807347557126625, 'mean_word_length': 2.9060650887573964, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528147', 'n_tokens_mistral': 4346, 'n_tokens_neox': 4192, 'n_words': 1089}