英文:
IntelliJ Kotlin Classpath: Cannot access class
问题
I cannot access class 'com.****.raptor.gen.models.TextualEntry'. Check your module classpath for missing or conflicting dependencies.
英文:
I'm importing a jar from another library and overriding one of the classes directly in my source code (please don't judge) by declaring it as same class name in same package and rely in classpath priority.
When doing so the code works fine (both with mvn and IntelliJ) and if the class I code into is java then even IntelliJ editor is happy. But if I code the class in kotlin I get:
Cannot access class 'com.****.raptor.gen.models.TextualEntry'. Check your module classpath for missing or conflicting dependencies
I wasn't able to reproduce it with any random library but only with our internal library.
The code I have is more or less this:
pom.xml:
<dependency>
<groupId>com.****.logistics.gpltconfig</groupId>
<artifactId>entities-java</artifactId>
<version>1.0.4-RELEASE</version>
</dependency>
And this java code runs and shows in the editor fine:
CustomsForm customsForm = new CustomsForm();
customsForm.setDeclaredItemPrice(new TextualEntry<>());
System.out.println("done java");
But this kotlin code runs fine but shows in the editor with an error:
val customsForm = CustomsForm()
customsForm.declaredItemPrice = TextualEntry<String>()
println("done kotlin")
Any suggestion on how to do to get rid of the IntelliJ editor error in kotlin?
答案1
得分: 0
Like Tenfour04 - 我想知道你是否故意在你自己的代码库中遮蔽相同的包/类。这非常危险,因为你依赖于某种策略来确保你的实现在编译后的代码中获胜 - 我认为类路径加载器按照声明的顺序搜索,但大多数构建/运行工具不会为你提供声明顺序的选项,一旦你开始构建类似于组合的jar文件(参见Maven Shade),那就不知道了。
但无论如何,回答你的问题...
我曾经在一个复杂的依赖关系图中看到过类似的情况。对我来说问题是这样的:
- 项目1,模块A,打包为版本p1a-1.0.1
- 项目2,依赖于p1a-1.0.1,打包为版本p2-2.0.0
然后
- 项目1将所有版本都迁移到p1xx-1.0.2,因此
- 项目1,模块A,打包为版本p1a-1.0.2
- 项目1,模块B,依赖于项目2,后者依赖于p2-2.0.0,后者又依赖于较旧的p1a-1.0.1
在这种情况下,依赖管理器(我们使用的是Maven)会忽略更深层的过时依赖项,编辑器/运行时一切正常。但是IntelliJ不喜欢它,并在某些情况下会显示基于编辑器的错误。
对我们来说,编辑器错误似乎仅围绕着存在"派生代码"(在我们的情况下是Lombok)的情况,这是因为IntelliJ正在跟踪版本并认为存在冲突。
看起来版本跟踪的智能性也在困扰你。在这个领域接受一个Java类作为你项目中的"桥梁"怎么样...尽管有关"遮蔽"的警告。
切入点
另一种方法是考虑使用面向切面编程来声明一个切入点,AOP框架将包装你想要更改行为的类。参见Spring的AOP实现。
英文:
Like Tenfour04 - I want to know if you are intentionally shadowing the same package/class in your own code base. This is very risky since you are relying on some strategy for your implementation to win in the compiled code - I think the classpath loader searches in declared order, but mostly build/run tools don't give you options to declare ordering and once you go building something like a combined jar (see Maven Shade) then who knows.
But anyway, to answer your question...
I have seen something like this with a complex dependency graph. The problem for me is like this:
- project 1, module A, packaged as version p1a-1.0.1
- project 2, depending on p1a-1.0.1, packaged as version p2-2.0.0
then
- project 1 has moved all versions to p1xx-1.0.2, therefore
- project 1, module A, packaged as version p1a-1.0.2
- project 1, module B, depends on project 2 which depends on p2-2.0.0 that in turn depends on the older p1a-1.0.1
In this case the dependency manager (Maven in our case) ignores the deeper outdated dependency and all is well outside the editor/at run time. But IntelliJ doesn't like it an gives an editor-based error in some cases.
The editor error for us only seems to be around cases where there is "derived code" (in our case Lombok), and it's because IntelliJ is tracking the versions and thinks there is a conflict.
Seems like the version tracking cleverness is hitting you too. What about accepting 1 java class in your project as the "bridge" in this area... notwithstanding those warnings about "shadowing".
Pointcut
A different approach is consider using Aspect Orientated Programming to declare a pointcut where an AOP framework will wrap the class you want to change the behaviour of. See Spring's AOP implementation
答案2
得分: 0
我建议在与原始编写语言相同的语言中编写阴影类。如果是Java,只需在Java中编写它。您的Kotlin编译器将能够正确编译它,与Kotlin源代码一起。
这样,更容易匹配原始类的布局。理论上可以在不同编程语言之间正确执行,但可以轻松避免这种麻烦。
英文:
I recommend coding the shadowing class in the same language it was originally written. If that's Java, just code it in Java. Your Kotlin compiler will pick it up and compile it along with Kotlin sources just fine.
This way, it's much easier to match the layout of the original class. In theory it's possible to do that correctly cross-language, but it's a hassle you can easily avoid.
答案3
得分: 0
It's a bug in IntelliJ.
IntelliJ team are working to solve it.
For the time being there is a workaround.
Put the following text as the first line of that file (even before package
):
@file:Suppress("MISSING_DEPENDENCY_CLASS", "MISSING_DEPENDENCY_SUPERCLASS")
英文:
It's a bug in IntelliJ.
IntelliJ team are working to solve it.
For the time being there is a workaround.
Put the following text as first line of that file (even before package
)
@file:Suppress("MISSING_DEPENDENCY_CLASS", "MISSING_DEPENDENCY_SUPERCLASS")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论