简体   繁体   English

Eclipse插件-使用适配器而不是强制转换有什么好处?

[英]Eclipse plugin - what is the benefit of using Adapter instead of a cast?

If you get a selection of projects as IStructuredSelection, use .toArray() to get their Object[]-content, then why does casting each Object to IAdaptable and then simply casting again to IJavaProject work fine for me and I get all the Classes/Interfaces with their methods.... 如果您选择了一个项目作为IStructuredSelection,请使用.toArray()来获取其Object []内容,那么为什么将每个Object强制转换为IAdaptable,然后再次将其强制转换为IJavaProject,对我来说还是可以的,我得到了所有的Classs /接口及其方法...

But when using (IJavaProject) adaptable.getAdapter(IJavaProject.class) instead, some interface methods get lost? 但是当改用(IJavaProject)Adaptable.getAdapter(IJavaProject.class)时,某些接口方法会丢失吗?

What exactly does the Adapter do (other than "Returns an object which is an instance of the given class parameter...") and why does everyone recommend to use it instead of a cast? 适配器到底要做什么(除了“返回给定类参数实例的对象...”),为什么每个人都建议使用它而不是强制转换? Here is the relevant code 这是相关的代码

    IAdaptable adaptable = (IAdaptable) selectedObject;
    IJavaProject javaProject = (IJavaProject) adaptable.getAdapter(IJavaProject.class);
    final IProject iProject = javaProject.getProject();
    iProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    final IJavaProject javaRefreshedProject = JavaCore.create(iProject);
    //processing further using ASTParser and CompilationUnit

Many objects you get from a selection do not implement things like IProject or IJavaProject instead they are some internal class that is more suited to the way the code wants to present a view or editor. 从选择中获得的许多对象没有实现IProjectIJavaProject类的东西,而是它们是一些内部类,更适合于代码想要呈现视图或编辑器的方式。

In this case you cannot use a cast because the selection object does not implement the interface you want. 在这种情况下,您不能使用强制类型转换,因为选择对象无法实现所需的接口。

The user interface objects may implement IAdaptable which allows you to retrieve the underlying class using object.getAdapter(...) . 用户界面对象可以实现IAdaptable ,它允许您使用object.getAdapter(...)检索基础类。

However not all objects implement IAdaptable themselves, instead they use the registerAdapters method of the IAdapterManager to declare a completely different class that converts from the UI object to the underlying object. 但是,并非所有对象都自己实现IAdaptable ,而是使用IAdapterManagerregisterAdapters方法声明一个完全不同的类,该类将从UI对象转换为基础对象。 In this case you use 在这种情况下,您使用

IProject project = (IProject)Platform.getAdapterManager().getAdapter(object, IProject.class);

You can use a utility method like the following to deal with all possibilities: 您可以使用以下实用程序方法来处理所有可能性:

public static <AdapterType> AdapterType adapt(Object adaptableObject, Class<AdapterType> adapterType)
{
  if (adaptableObject == null)
    return null;

  // Is the object the desired type?

  if (adapterType.isInstance(adaptableObject))
    return adapterType.cast(adaptableObject);

  // Does object adapt to the type?

  if (adaptableObject instanceof IAdaptable)
   {
     AdapterType result = adapterType.cast(((IAdaptable)adaptableObject).getAdapter(adapterType));
     if (result != null)
       return result;
   }

  // Try the platform adapter manager

  return adapterType.cast(Platform.getAdapterManager().getAdapter(adaptableObject, adapterType));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对ArrayList使用Iterator而不是ListIterator有什么好处? - What is the benefit of using Iterator instead of ListIterator for ArrayList? 使用fromValue函数代替java枚举valueOf有什么好处? - What is benefit from using fromValue function instead of valueOf, java enums? 使用Class有什么好处 <?> 代替类作为方法的参数类型? - What is the benefit of using Class<?> instead of Class as a method parameter type? 使用taglib有什么好处? - What the benefit of using taglib? 在具有JDO持久性的POJO上使用Email GAE数据存储类型而不是字符串类型有什么好处? - What is the benefit of using Email GAE Datastore type instead of String type on POJOs with JDO persistence? 对于Servlet 3.x,使用基于Java的配置而不是web.xml有什么好处? - What is benefit of using java based config instead of web.xml for servlet 3.x Eclipse 3.4 Glassfish插件(服务器适配器) - Eclipse 3.4 Glassfish plugin (server adapter) 使用新的Button(this)有什么好处? - what's the benefit of using new Button(this)? 在Java中使用或创建递归函数有什么好处? - what is the benefit of using or creating recursive functions in java? 在Java中使用HashMap的主要好处是什么? - What is the main benefit of using HashMap in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM