PsiClass 到 java.lang.Class
PsiClass to java.lang.Class
我正在为 IntelliJ IDEA 开发插件。插件如何获取导入到插件正在检查的项目中的库的名称和版本?我有项目的 PsiClass,但无法将其转换为 java.lang.Class。也许有从 PsiElement 获取 ClassLoader 的方法?
super.visitImportStatement(psiImport);
Class importedClass = Class.forName(psiImport.getQualifiedName(), true, psiImport.getClass().getClassLoader());
PsiImport.getClass().GetClassLoader()
- returns class PsiImportStatementImpl 的 ClassLoader 而不是我导入的 class 的 ClassLoader。
IntelliJ 主要对您的代码进行静态分析。事实上,IDE 和您 run/debug 的项目具有完全不同的 class 路径。当您打开一个项目时,您的依赖项不会添加到 IDE class 路径中。相反,IDE 将索引 JAR,这意味着它将自动发现所有声明(classes、方法、接口等)并将它们保存在缓存中以备后用。
当您在编辑器中编写代码时,静态分析工具将利用此索引的内容来验证您的代码并在您尝试使用未知定义时显示错误。
另一方面,当您从项目中 运行 Main class 时,它将生成一个新的 java
进程,该进程有自己的 class 路径。此 class 路径可能包含您的模块中声明的所有依赖项。
知道了这一点,你现在应该明白为什么你不能将一个PsiClass
“转换”成对应的Class
。
回到你原来的问题:
How can plugin get the name and version of libraries that are imported to the project that is being checked by plugin?
您不需要为此访问 Class
个对象。相反,您可以使用 IntelliJ SDK libraries。这是一个例子:
Module mod = ModuleUtil.findModuleForFile(virtualFile,myProject);
ModuleRootManager.getInstance(mod).orderEntries().forEachLibrary(library -> {
// do your thing here with `library`
return true;
});
我正在为 IntelliJ IDEA 开发插件。插件如何获取导入到插件正在检查的项目中的库的名称和版本?我有项目的 PsiClass,但无法将其转换为 java.lang.Class。也许有从 PsiElement 获取 ClassLoader 的方法?
super.visitImportStatement(psiImport);
Class importedClass = Class.forName(psiImport.getQualifiedName(), true, psiImport.getClass().getClassLoader());
PsiImport.getClass().GetClassLoader()
- returns class PsiImportStatementImpl 的 ClassLoader 而不是我导入的 class 的 ClassLoader。
IntelliJ 主要对您的代码进行静态分析。事实上,IDE 和您 run/debug 的项目具有完全不同的 class 路径。当您打开一个项目时,您的依赖项不会添加到 IDE class 路径中。相反,IDE 将索引 JAR,这意味着它将自动发现所有声明(classes、方法、接口等)并将它们保存在缓存中以备后用。
当您在编辑器中编写代码时,静态分析工具将利用此索引的内容来验证您的代码并在您尝试使用未知定义时显示错误。
另一方面,当您从项目中 运行 Main class 时,它将生成一个新的 java
进程,该进程有自己的 class 路径。此 class 路径可能包含您的模块中声明的所有依赖项。
知道了这一点,你现在应该明白为什么你不能将一个PsiClass
“转换”成对应的Class
。
回到你原来的问题:
How can plugin get the name and version of libraries that are imported to the project that is being checked by plugin?
您不需要为此访问 Class
个对象。相反,您可以使用 IntelliJ SDK libraries。这是一个例子:
Module mod = ModuleUtil.findModuleForFile(virtualFile,myProject);
ModuleRootManager.getInstance(mod).orderEntries().forEachLibrary(library -> {
// do your thing here with `library`
return true;
});