Eclipse CDT扩展AdapterFactory [英] Eclipse CDT extend AdapterFactory

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

问题描述

我尝试覆盖CDT ResumeAtLine,MoveToLine,RunToLine的功能。因此,我创建了一个自定义的SuspendResumeAdapterFactory,但它没有加载,但编译并运行没有错误。我可能需要一个自定义的适应性类型?



这是我的 plugin.xml 的内容。

 < extension point =org.eclipse.core.runtime.adapters> 
< factory
class =my.package.CustomSuspendResumeAdapterFactory
adaptableType =org.eclipse.cdt.dsf.ui.viewmodel.IVMContext>
< adapter type =org.eclipse.debug.core.model.ISuspendResume/>
< / factory>
< / extension>

这里我的 CustomSuspendResumeAdapterFactory 从内存不是100%确定语法是否正确,但我认为应该清楚看到我想做什么。

  package my.package; 

import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.MoveToLine;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.ResumeAtLine;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.RunToLine;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.ISuspendResume;

public class CustomSuspendResumeAdapterFactory实现IAdapterFactory {

static class SuspendResume实现ISuspendResume,IAdaptable {

private final CustomRunToLine fRunToLine;
private final CustomMoveToLine fMoveToLine;
private final CustomResumeAtLine fResumeAtLine;

SuspendResume(IExecutionDMContext execCtx){
fRunToLine = new CustomRunToLine(execCtx);
fMoveToLine = new CustomMoveToLine(execCtx);
fResumeAtLine = new CustomResumeAtLine(execCtx);
}

@SuppressWarnings(unchecked)
@Override
public< T> T getAdapter(Class< T>适配器){
if(adapter.isInstance(RunToLine.class)){
System.out.println(CUSTOM RUNTOLINE);
return(T)fRunToLine;
}
if(adapter.isInstance(MoveToLine.class)){
System.out.println(CUSTOM MOVETOLINE);
return(T)fMoveToLine;
}
if(adapter.isInstance(ResumeToLine.class)){
System.out.println(CUSTOM RESUMEATLINE);
return(T)fResumeAtLine;
}
返回null;
}

@Override
public boolean canResume(){return false; }
@Override
public boolean canSuspend(){return false; }
//必须返回true,因为平台
// RunToLineActionDelegate才会启用
//动作(如果我们被挂起)
@Override
public boolean isSuspended ){return true;
@Override
public void resume()throws DebugException {}
@Override
public void suspend()throws DebugException {}
}

@SuppressWarnings(unchecked)
@Override
public< T> (getAtt (
((IDMVMContext)adaptableObject).getDMContext(),
IExecutionDMContext.class);
//如果我们正在处理一个线程,而不是容器
if(execDmc!= null&&!(execDmc)),那么RunToLine,MoveToLine或
// ResumeAtLine才有意义instanceof IContainerDMContext)){
return(T)new SuspendResume(execDmc);
}
}
}
返回null;
}

@Override
public Class<?> [] getAdapterList(){
return new Class [] {ISuspendResume.class}
}
}


解决方案

为什么您的代码不运行



您提供了一个新的适配器工厂,可以转换已处理的对象类型。即您的plugin.xml表示您可以将 IVMContext 转换为 ISuspendResume 。但DSF插件已经提供了这样一个适配器工厂。如果您有一个新的目标类型(如IMySpecialRunToLine),您可以为此安装一个工厂,它将需要 IVMContext 并将其转换为 IMySpecialRunToLine )。



尽管如此,


I try to override the functionality of CDT ResumeAtLine, MoveToLine, RunToLine. For this reason I created a custom SuspendResumeAdapterFactory but it isn't loaded but compiles and runs without error. Do I maybe need a custom adaptableType too?

Here is the content of my plugin.xml.

  <extension point="org.eclipse.core.runtime.adapters">
      <factory 
            class="my.package.CustomSuspendResumeAdapterFactory" 
            adaptableType="org.eclipse.cdt.dsf.ui.viewmodel.IVMContext">
         <adapter type="org.eclipse.debug.core.model.ISuspendResume"/>
      </factory>
   </extension> 

And here my CustomSuspendResumeAdapterFactory this class is reconstructed from memory not 100% sure if the syntax is correct, but I think it should be clear to see what I want to do.

package my.package;

import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.MoveToLine;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.ResumeAtLine;
import org.eclipse.cdt.dsf.debug.internal.ui.actions.RunToLine;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.ISuspendResume;

public class CustomSuspendResumeAdapterFactory implements IAdapterFactory {

    static class SuspendResume implements ISuspendResume, IAdaptable {

        private final CustomRunToLine fRunToLine;
        private final CustomMoveToLine fMoveToLine;
        private final CustomResumeAtLine fResumeAtLine;

        SuspendResume(IExecutionDMContext execCtx) {
            fRunToLine = new CustomRunToLine(execCtx);
            fMoveToLine = new CustomMoveToLine(execCtx);
            fResumeAtLine = new CustomResumeAtLine(execCtx);
        }

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getAdapter(Class<T> adapter) {
            if (adapter.isInstance(RunToLine.class)) {
                System.out.println("CUSTOM RUNTOLINE");
                return (T)fRunToLine;
            }
            if (adapter.isInstance(MoveToLine.class)) {
                System.out.println("CUSTOM MOVETOLINE");
                return (T)fMoveToLine;
            }
            if (adapter.isInstance(ResumeToLine.class)) {
                System.out.println("CUSTOM RESUMEATLINE");
                return (T)fResumeAtLine;
            }
            return null;
        }

        @Override
        public boolean canResume() { return false; }
        @Override
        public boolean canSuspend() { return false; }
        // This must return true because the platform
        // RunToLineActionDelegate will only enable the
        // action if we are suspended
        @Override
        public boolean isSuspended() { return true; }
        @Override
        public void resume() throws DebugException {}
        @Override
        public void suspend() throws DebugException {}
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
        if (ISuspendResume.class.equals(adapterType)) {
            if (adaptableObject instanceof IDMVMContext) {
                IExecutionDMContext execDmc = DMContexts.getAncestorOfType(
                    ((IDMVMContext)adaptableObject).getDMContext(),
                    IExecutionDMContext.class);
                // It only makes sense to RunToLine, MoveToLine or
                // ResumeAtLine if we are dealing with a thread, not a container
                if (execDmc != null && !(execDmc instanceof IContainerDMContext)) {
                    return (T)new SuspendResume(execDmc);
                }
            }
        }
        return null;
    }

    @Override
    public Class<?>[] getAdapterList() {
        return new Class[] { ISuspendResume.class };
    }
}

解决方案

Why your code is not run

You have provided a new adapter factory that converts object types that are already handled. i.e. your plugin.xml says you can convert IVMContext to ISuspendResume. But the DSF plug-in already provides such an adapter factory. If you have a new target type (like IMySpecialRunToLine) you could install a factory for that, it would take IVMContext and convert it to a IMySpecialRunToLine).

Although dated, the Eclipse Corner Article on Adapter Pattern may be useful if this is a new concept.

How to do custom Run To Line implementation

If you want to provide different implementation of Run To Line, you need to provide your own version of org.eclipse.cdt.dsf.debug.service.IRunControl2.runToLine(IExecutionDMContext, String, int, boolean, RequestMonitor). The org.eclipse.cdt.dsf.debug.internal.ui.actions.RunToLine class is simply glue to connect UI features (such as buttons/etc some provided directly, some by the core eclipse debug) to the DSF backend. i.e. if you look at what RunToLine does, all it actually does is get the IRunControl2 service and call runToLine on it.

The way to provider your own implementation of IRunControl2 is override org.eclipse.cdt.dsf.gdb.service.GdbDebugServicesFactory.createRunControlService(DsfSession) and provide your own GdbDebugServicesFactory in your custom launch delegate by overriding org.eclipse.cdt.dsf.gdb.launching.GdbLaunchDelegate.newServiceFactory(ILaunchConfiguration, String)

RunToLine will be triggered when the user select Run To Line from the popup menu in the editor, as per this screenshot:

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

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