Eclipse CDT ASTRewrite NPE [英] Eclipse CDT ASTRewrite NPE

查看:71
本文介绍了Eclipse CDT ASTRewrite NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Eclipse 插件,它应该重写一些 .c 源文件.我正在使用 Eclipse CDT AST 解析器,但在尝试将调整保存到 AST 时遇到了 NPE.我从活动工作区的项目中获取要解析的 IFile(_file).执行下面的代码时,我面临着下面的 NPE.NPE 被抛出:Change c = rw.rewriteAST();

MapdefinedSymbols = new HashMap();String[] includePaths = new String[0];IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);IParserLogService 日志 = 新的 DefaultLogService();IncludeFileContentProvider emptyIncludes = IncludeFileContentProvider.getEmptyFilesProvider();int opts = 8;最终 IASTTranslationUnit tu = GCCLanguage.getDefault().getASTTranslationUnit(FileContent.create(_file), info, emptyIncludes, null, opts, log);最终 ASTRewrite rw = ASTRewrite.create(tu);tu.accept(新的ASTVisitor(真){@覆盖公共 int 访问(IASTStatement 语句){如果(测试){测试 = 假;rw.insertBefore(statement.getParent(), statement, rw.createLiteralNode(int i = 3;"), null);}返回 PROCESS_CONTINUE;}});更改 c = rw.rewriteAST();c.perform(new NullProgressMonitor());

<块引用>

java.lang.NullPointerException在 org.eclipse.cdt.internal.formatter.ChangeFormatter.formatChangedCode(ChangeFormatter.java:95)在 org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:119)在 org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:107)在 org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteAST(ASTRewriteAnalyzer.java:29)在 org.eclipse.cdt.core.dom.rewrite.ASTRewrite.rewriteAST(ASTRewrite.java:213)在 sheaperdexecuter.AstParser.parse(AstParser.java:155)在 sheaperdexecuter.parts.SheaperdView$3.mouseDown(SheaperdView.java:140)在 org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:196)在 org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)在 org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)在 org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)在 org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)在 org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)在 org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)在 org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)在 org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)在 org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)在 org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)在 org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)在 org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)在 org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)在 org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)在 org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.base/java.lang.reflect.Method.invoke(Method.java:566)在 org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)在 org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)在 org.eclipse.equinox.launcher.Main.run(Main.java:1461)在 org.eclipse.equinox.launcher.Main.main(Main.java:1434)

这里提到了一个几乎相同的问题:

添加依赖项后,NPE 消失了,我可以访问工作区相关模型等的正确实例.

我希望这对某人有所帮助,因为我花了几个小时才获得带有 CDT 引导的 Eclipse 插件.

I am developing a Eclipse plugin which should rewrite some .c source files. I am using the Eclipse CDT AST parser, but I am facing a NPE when trying to save the adjustments to the AST. I obtain the IFile(_file) to parse from a project of the active workspace. When executing the code below I am facing a NPE below. NPE is thrown at: Change c = rw.rewriteAST();

Map<String, String> definedSymbols = new HashMap<String, String>();
String[] includePaths = new String[0];
IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);
IParserLogService log = new DefaultLogService();
IncludeFileContentProvider emptyIncludes = IncludeFileContentProvider.getEmptyFilesProvider();
int opts = 8;
            
final IASTTranslationUnit tu =  GCCLanguage.getDefault().getASTTranslationUnit(FileContent.create(_file), info, emptyIncludes, null, opts, log);
final ASTRewrite rw = ASTRewrite.create(tu);
tu.accept(new ASTVisitor(true){
                
        @Override
        public int visit(IASTStatement statement){
            if(test) {
                test = false;
                rw.insertBefore(statement.getParent(), statement, rw.createLiteralNode("int i = 3;"), null);
            }
            return PROCESS_CONTINUE;
        }
});

Change c = rw.rewriteAST();
c.perform(new NullProgressMonitor());

java.lang.NullPointerException at org.eclipse.cdt.internal.formatter.ChangeFormatter.formatChangedCode(ChangeFormatter.java:95) at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:119) at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:107) at org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteAST(ASTRewriteAnalyzer.java:29) at org.eclipse.cdt.core.dom.rewrite.ASTRewrite.rewriteAST(ASTRewrite.java:213) at sheaperdexecuter.AstParser.parse(AstParser.java:155) at sheaperdexecuter.parts.SheaperdView$3.mouseDown(SheaperdView.java:140) at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:196) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89) at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155) at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590) at org.eclipse.equinox.launcher.Main.run(Main.java:1461) at org.eclipse.equinox.launcher.Main.main(Main.java:1434)

A nearly identical problem is mention here: Eclipse CDT ASTRewrite not working The answer to that question is to make sure that the program is running on an osgi framework. In my case the program is a eclipse plugin but I am still facing this NPE.

Can there by a problem with CDT? I am adding CDT via jar (org.eclipse.cdt.core_6.9.0.201909091953.jar) using the Runtime configuration tab of the plugin. (I would have added it to the Dependencies tab, but it is not available there)

//EDIT:

I have researched further in the CDT source code and in my case the CCorePlugin seems to not be initialized. If I call CCorePlugin.getDefault(); I get null returned. I think this causes the exception mentioned above. Do I need to bootstrap the CCorePlugin in any way?

解决方案

The problem was due to the fact that I added CDT via .jar using the Runtime configuration tab of the plugin.

My calls to the CDT environment (CCorePlugin, CoreModel, etc.) ended up not being related to the CDT running in the executing eclipse. Therefore I got NPE's on CoreModel and stuff.

I solved the problem by cloning the CDT source repo (https://git.eclipse.org/r/cdt/org.eclipse.cdt.git) and importing the projects to my Eclipse Committers IDE. (As explained here: https://wiki.eclipse.org/Getting_started_with_CDT_development#Setting_up_the_Sources)

Afterwards I was able to add CDT to the Dependencies configuration tab of the plugin:

After the dependency has been added the NPE's were gone and I could access the correct Instances of the workspace related models etc..

I hope this helps someone as it took me some hours to get the Eclipse plugin with CDT bootstrapped.

这篇关于Eclipse CDT ASTRewrite NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆