当前位置: 首页 > 知识库问答 >
问题:

如何以编程方式为Intellij Idea创建自定义Kotlin检查

太叔炎彬
2023-03-14

链接到我下面的示例:

  • 文档页面:https://www.jetbrains.org/intellij/sdk/docs/tutorials/code_inspections.html
  • 示例项目:https://github.com/jetbrains/intellij-sdk-docs/tree/master/code_samples/comparing_references_inspection

这是检查的提供者。

package com.magicbytes.kotlin.inspections;

import com.intellij.codeInspection.InspectionToolProvider;
import com.magicbytes.kotlin.inspections.ExampleInspection;
import org.jetbrains.annotations.NotNull;

public class ExampleProvider implements InspectionToolProvider {
    @NotNull
    @Override
    public Class[] getInspectionClasses() {
        return new Class[]{ExampleInspection.class};
    }
}
package com.magicbytes.kotlin.inspections;

import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection;

public class ExampleInspection extends AbstractKotlinInspection {
    @Nls
    @NotNull
    @Override
    public String getDisplayName() {
        return "Kotlin Test";
    }
}
plugins {
    id 'java'
    id 'org.jetbrains.intellij' version '0.3.2'
}

group 'com.magicbytes'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

intellij {
    version '2018.1.4'
    plugins 'kotlin'
}
<idea-plugin>
    <id>com.magicbytes.kotlin.inspections</id>
    <name>Plugin display name here</name>
    <description></description>

    <depends>org.jetbrains.kotlin</depends>

    <extensions defaultExtensionNs="com.intellij">
        <!-- Add your extensions here -->
        <inspectionToolProvider implementation="com.magicbytes.kotlin.inspections.ExampleProvider"/>
    </extensions>

    <actions>
    </actions>
</idea-plugin>

我得到的例外..

 java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
com.intellij.openapi.extensions.impl.PicoPluginExtensionInitializationException: java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getComponentInstance(ExtensionComponentAdapter.java:96)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getExtension(ExtensionComponentAdapter.java:119)
    at com.intellij.openapi.extensions.impl.ExtensionPointImpl.processAdapters(ExtensionPointImpl.java:246)
    at com.intellij.openapi.extensions.impl.ExtensionPointImpl.getExtensions(ExtensionPointImpl.java:191)
    at com.intellij.openapi.extensions.Extensions.getExtensions(Extensions.java:102)
    at com.intellij.openapi.extensions.Extensions.getExtensions(Extensions.java:89)
    at com.intellij.openapi.extensions.ExtensionPointName.getExtensions(ExtensionPointName.java:50)
    at com.intellij.codeInspection.ex.InspectionToolRegistrar.ensureInitialized(InspectionToolRegistrar.java:54)
    at com.intellij.codeInspection.ex.InspectionToolRegistrar.createTools(InspectionToolRegistrar.java:127)
    at com.intellij.codeInspection.ex.InspectionSearchableOptionContributor.processOptions(InspectionSearchableOptionContributor.java:34)
    at com.intellij.ide.ui.search.SearchableOptionPreloader.preload(SearchableOptionPreloader.java:49)
    at com.intellij.openapi.application.Preloader.lambda$null$0(Preloader.java:74)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:543)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:488)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:94)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
    at com.intellij.openapi.application.Preloader.lambda$initComponent$1(Preloader.java:72)
    at com.intellij.util.concurrency.BoundedTaskExecutor$2.run(BoundedTaskExecutor.java:212)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.loadImplementationClass(ExtensionComponentAdapter.java:161)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getComponentImplementation(ExtensionComponentAdapter.java:66)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getComponentInstance(ExtensionComponentAdapter.java:73)
    ... 21 more
Caused by: java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
    at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:63)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.loadImplementationClass(ExtensionComponentAdapter.java:158)
    ... 23 more
Caused by:

java.lang.RuntimeException: java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.loadImplementationClass(ExtensionComponentAdapter.java:161)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getComponentImplementation(ExtensionComponentAdapter.java:66)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getComponentInstance(ExtensionComponentAdapter.java:73)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.getExtension(ExtensionComponentAdapter.java:119)
    at com.intellij.openapi.extensions.impl.ExtensionPointImpl.processAdapters(ExtensionPointImpl.java:246)
    at com.intellij.openapi.extensions.impl.ExtensionPointImpl.getExtensions(ExtensionPointImpl.java:191)
    at com.intellij.openapi.extensions.Extensions.getExtensions(Extensions.java:102)
    at com.intellij.openapi.extensions.Extensions.getExtensions(Extensions.java:89)
    at com.intellij.openapi.extensions.ExtensionPointName.getExtensions(ExtensionPointName.java:50)
    at com.intellij.codeInspection.ex.InspectionToolRegistrar.ensureInitialized(InspectionToolRegistrar.java:54)
    at com.intellij.codeInspection.ex.InspectionToolRegistrar.createTools(InspectionToolRegistrar.java:127)
    at com.intellij.codeInspection.ex.InspectionSearchableOptionContributor.processOptions(InspectionSearchableOptionContributor.java:34)
    at com.intellij.ide.ui.search.SearchableOptionPreloader.preload(SearchableOptionPreloader.java:49)
    at com.intellij.openapi.application.Preloader.lambda$null$0(Preloader.java:74)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:543)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:488)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:94)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
    at com.intellij.openapi.application.Preloader.lambda$initComponent$1(Preloader.java:72)
    at com.intellij.util.concurrency.BoundedTaskExecutor$2.run(BoundedTaskExecutor.java:212)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: ExampleProvider PluginClassLoader[com.magicbytes.kotlin.inspections, 1.0-SNAPSHOT] com.intellij.ide.plugins.cl.PluginClassLoader@6f989177
    at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:63)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.intellij.openapi.extensions.impl.ExtensionComponentAdapter.loadImplementationClass(ExtensionComponentAdapter.java:158)
    ... 23 more

共有1个答案

长孙阳嘉
2023-03-14

在导入GitHub repo并在本地构建之后,我发现./build/libs/kotlin.inspections-1.0-snapshot.jar只包含META-INF,但没有类。我怀疑这是因为您的源代码位于./src/main/src之下,而不是像Gradle项目那样位于./src/main/java之下,所以它只是假设您根本没有代码。

将内部src目录重命名为java后,错误变为

class com.magicbytes.kotlin.inspections.ExampleInspection: group display name should be overridden or configured via XML class com.magicbytes.kotlin.inspections.ExampleInspection
java.lang.Throwable: class com.magicbytes.kotlin.inspections.ExampleInspection: group display name should be overridden or configured via XML class com.magicbytes.kotlin.inspections.ExampleInspection
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:123)
    at com.intellij.codeInspection.InspectionProfileEntry.getGroupDisplayName(InspectionProfileEntry.java:230)
    at com.intellij.codeInspection.InspectionProfileEntry.getGroupPath(InspectionProfileEntry.java:239)
    at com.intellij.codeInspection.ex.InspectionToolWrapper.getGroupPath(InspectionToolWrapper.java:161)
    at com.intellij.codeInspection.ex.InspectionSearchableOptionContributor.processOptions(InspectionSearchableOptionContributor.java:40)
    at com.intellij.ide.ui.search.SearchableOptionPreloader.preload(SearchableOptionPreloader.java:49)
    at com.intellij.openapi.application.Preloader.lambda$null$0(Preloader.java:74)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:580)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:525)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:85)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
    at com.intellij.openapi.application.Preloader.lambda$initComponent$1(Preloader.java:72)
    at com.intellij.util.concurrency.BoundedTaskExecutor$2.run(BoundedTaskExecutor.java:212)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这更好,看起来像是一个单独的问题,因为您的代码违反了一些约定。

 类似资料:
  • 我正尝试使用以下命令以编程方式创建:

  • 问题内容: 是否有免费的库可根据图像序列创建MPEG(或任何其他简单的视频格式)? 它也必须在Linux上运行,并且最好具有Python绑定。 问题答案: 我知道有一个mencoder(是mplayer项目的一部分)和ffmpeg,它们都可以做到这一点。

  • 我有一个充满自定义视图的应用程序。当我尝试以编程方式创建FAB时,它会抛出一个错误 原因:java.lang.IllegalArgumentException:您需要在设计库中使用theme.AppCompat主题(或后代)。

  • 我在尝试完全以编程方式进行此布局时遇到了问题,每次进程运行结束时都没有成功,应用程序崩溃。 XML im试图以编程方式创建的部分是: 我试图用以下kotlin代码重新创建它: 这就是我在logcat中遇到的错误 我仍在研究它可能是什么,我怀疑它可能是错误的LayoutParams,但如何以编程方式正确设置这些资源还不是很清楚。 提前谢谢。 PD:如果我发现了什么,我会更新。 欢迎任何帮助! 更新1

  • 我有一个在本地系统帐户下作为Windows服务运行的自托管WCF服务器。我正在尝试用C#以编程方式创建一个自签名证书,用于使用消息级安全性的Net.tcpendpoint。 我可以在资源管理器中看到这个1.43KB的文件。如果我查看propertiesSecurity,我会看到SYSTEM和Administrators都具有完全控制权。 在研究这个错误时,我看到了许多关于私钥丢失或权限不正确的答案

  • 问题内容: 概要 我正在开发一个Web应用程序以学习Django(python 3.4和Django 1.6.10)。该Web应用程序具有复杂且经常更新的工作流程。我决定集成Django-Viewflow库(https://github.com/viewflow/viewflow/),因为这似乎是处理工作流且不将工作流逻辑与应用程序模型合并的一种非常方便的方法。 在这种情况下,我创建了一个工作流以