Java 何时使用IAdaptable?
给定一个类型为A的对象,并希望在可能的情况下将其转换为类型B,何时适合使用以下各项Java 何时使用IAdaptable?,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,给定一个类型为A的对象,并希望在可能的情况下将其转换为类型B,何时适合使用以下各项 直接转换和/或检查的实例 if(a instanceof B) { B b = (B)a; // ... } 通过IAdaptable.getAdapter进行转换 // assuming A implements/extends IAdaptable B b = (B)a.getAdapter(B.class); if(b != null) { // ... } 当A无法隐式转换
检查的实例
if(a instanceof B) {
B b = (B)a;
// ...
}
IAdaptable.getAdapter进行转换
// assuming A implements/extends IAdaptable
B b = (B)a.getAdapter(B.class);
if(b != null) {
// ...
}
A
无法隐式转换为“IAdaptable”时,通过IAdaptable
进行的转换
B b = (a instanceof IAdaptable ? (B)((IAdaptable)a).getAdapter(B.class) : a instanceof B ? (B)a : null);
if(b != null) {
// ...
}
B b = (B)Platform.getAdapterManager().getAdapter(a, B.class);
if(b != null) {
// ...
}
这很难给出一般规则 当您从Eclipse项目视图中获得类似于当前选择的内容时,对象是一个用户界面,而不是底层对象(例如项目或文件)<代码>实例of将不起作用 从用户界面对象到基础对象的转换通常使用
IAdapterFactory
完成,它指定一个单独的工厂类来进行转换。在这种情况下,必须使用Platform.getAdapterManager().getAdapter
当一个对象实现IAdaptable
时,您必须查看文档或源代码,看看它也支持哪些类
我不认为案例3会发生
我经常使用此代码处理大多数事情:
public final class AdapterUtil
{
/**
* Get adapter for an object.
* This version checks first if the object is already the correct type.
* Next it checks the object is adaptable (not done by the Platform adapter manager).
* Finally the Platform adapter manager is called.
*
* @param adaptableObject Object to examine
* @param adapterType Adapter type class
* @return The adapted object or <code>null</code>
*/
public static <AdapterType> AdapterType adapt(Object adaptableObject, Class<AdapterType> adapterType)
{
// 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));
}
}
注意:较新版本的Eclipse有一个
org.Eclipse.core.runtime.Adapters
类,该类具有类似的adapt
方法。这很难给出一般规则
当您从Eclipse项目视图中获得类似于当前选择的内容时,对象是一个用户界面,而不是底层对象(例如项目或文件)<代码>实例of将不起作用
从用户界面对象到基础对象的转换通常使用IAdapterFactory
完成,它指定一个单独的工厂类来进行转换。在这种情况下,必须使用Platform.getAdapterManager().getAdapter
当一个对象实现IAdaptable
时,您必须查看文档或源代码,看看它也支持哪些类
我不认为案例3会发生
我经常使用此代码处理大多数事情:
public final class AdapterUtil
{
/**
* Get adapter for an object.
* This version checks first if the object is already the correct type.
* Next it checks the object is adaptable (not done by the Platform adapter manager).
* Finally the Platform adapter manager is called.
*
* @param adaptableObject Object to examine
* @param adapterType Adapter type class
* @return The adapted object or <code>null</code>
*/
public static <AdapterType> AdapterType adapt(Object adaptableObject, Class<AdapterType> adapterType)
{
// 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));
}
}
注意:较新版本的Eclipse有一个
org.Eclipse.core.runtime.Adapters
类,该类具有类似的adapt
方法。此代码实际上遵循案例3,而案例2-对象
不能隐式转换为IAdaptable
,因此,它使用instanceof
进行运行时检查。与案例2相比,该代码实际上更符合案例3-对象不能隐式转换为IAdaptable
,因此它使用instanceof
进行运行时检查。可能阅读会有所帮助。可能阅读会有所帮助。