此文说明org.eclipse.core.runtime下的几个常用类,他们为插件开发提供了便捷的方法.

 

*Assert,参数校验

---在插件开发中,经常需要对参数做校验,如NullPointerException,IllegalArgumentException异常等.一般的代码方式如if(arg==null) throw new RuntimeException("msg").

简化这类代码的方式使用org.eclipse.core.runtime.Assert,如上改写成Assert.isNotNull(arg,"msg").

---org.eclipse.jface.util.Assert与runtime中的Assert功能一样,只是被废弃了.

---和java的assert关键字不同在于,java的assert抛出Error异常.

 

*Path,处理文件路径,绝对或相对路径

---fromOSString和new Path(pathString);

构造path;并且在windows操作符时,若pathString有'\\',则转换为'/';

---String getFileExtension(),文件后缀

---File toFile(),转换为文件

Path方法测试

 

*FileLocator,查找bundle中的文件

---File getBundleFile(Bundle bundle),获取bundle文件

getBundleFile源码

这里提供了一种获取bundle内文件的方法:url =bundle.getEntry()—>url =resolve(url)--->new File(url.getPath()).

---URL find(Bundle bundle, IPath path, Map override),返回bundle中内容的url,协议bundleentry

path是相对bundle的Path,如new Path("META-INF/MANIFEST.MF");支持一些特殊标识使用,

$nl$ - for language specific information
$os$ - for operating system specific information
$ws$ - for windowing system specific information

如new Path("$nl$/plugin.properties"),特殊标识的含义请见注释说明.

override的作用如其名用于替换默认的环境信息;在overriade=null或没有这个特殊标志定义时,对前面的特殊标志解释采用默认环境的设置,否则overrdie中记录的信息优先级高于默认值.

注意:

a.查询范围是bundle和bundle的所有fragment.

---URL[] findEntries(Bundle bundle, IPath path, Map override)

从bundle和它的fragment中查找对应的path,并返回url.

---InputStream openStream(Bundle bundle, IPath file, boolean substituteArgs)

获取bundle中对应文件的InputSteam.

substituteArgs为true表示支持特殊标识,false则表示不支持特殊标识.

从源码看

openStream源码

 

---URL resolve(URL url)和URL toFileURL(URL url),协议转换

2者利用URLConverter服务实现将bundleentry:之类转换为file:等格式,转换后就可将此url作为其他类的参数了,如File(url.getPath());

osgi的URLConverter服务还没研究过,暂不论.

posted on 2010-10-26 13:42  戴忠  阅读(5495)  评论(0编辑  收藏  举报