[英]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. 从选择中获得的许多对象没有实现
IProject
或IJavaProject
类的东西,而是它们是一些内部类,更适合于代码想要呈现视图或编辑器的方式。
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
,而是使用IAdapterManager
的registerAdapters
方法声明一个完全不同的类,该类将从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.