1.
用interface
来定义系统对外提供的服务,有抽象类来做扩展。尽量用interface
作为参数类型。
2. ThreadLocal类,定义了一个变量的本地副本,与原有变量隔离,作用类似static变量,只是不共享。可用set添加变量,get去获取变量。变量类型不限制。
3. Eclipse plug in开发中可以实现IRuntimeClasspathProvider接口。可以提供用launch configuration去获得unresolved和resolved classpath。开发人员可以在resolveClasspath方法中加入自定义的classpath。实现类需要注册在extension point中。
/**
*Computesandreturnsanunresolvedclasspathforthegivenlaunchconfiguration.
*Variableandcontainerentriesarenotresolved.
*
*@paramconfigurationlaunchconfiguration
*@returnunresolvedpath
*@exceptionCoreExceptionifunabletocomputeapath
*/
public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException;
/**
*Returnstheresolvedpathcorrespondingtothegivenpath,inthecontextofthe
*givenlaunchconfiguration.Variableandcontainerentriesareresolved.Thereturned
*(resolved)pathneednothavethesamenumberofentriesasthegiven(unresolved)
*path.
*
*@paramentriesentriestoresolve
*@paramconfigurationlaunchconfigurationcontexttoresolvein
*@returnresolvedpath
*@exceptionCoreExceptionifunabletoresolveapath
*/
public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException;
A provider extension is defined in plugin.xml
. Following is an example definition of a runtime classpath provider extension.
<extension point="org.eclipse.jdt.launching.classpathProviders">
<classpathProvider
id="com.example.ExampleClasspathProvider"
class="com.example.ExampleClasspathProviderImpl"
</classpathProvider>
</extension>
4. plug in 开发中可以用JavaRuntime 去得到运行环境的信息。
IRuntimeClasspathProvider provider = JavaRuntime.getClasspathProvider(configuration);
其中configuration是ILaunchConfiguration类型的。
4. 在Eclipse plug in开发中获取文件。两种解决办法:1.从plug in实例中读取文件的URL,然后用FileLocator把这个URL转化成文件路径;2.直接利用FileLocator的find方法。
方法1
//filepath 是需要定位的文件
String filepath = "/bin/resources/test.jar";
//instance 是当前plug in的实例
URL url = instance.getBundle().getEntry(filepath);
String path = null;
try {
path = FileLocator.resolve(url).getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnnew Path(path);
}
方法2
String filepath = "/bin/resources/test.jar";
URL url = FileLocator.find(instance.getBundle(),new Path(filepath),null);
try {
path = FileLocator.resolve(url).getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5. 可以利用JarOutputStream来写jar包。必须为JarOutputStream 实例创建至少一个Entry,可以调用putNextEntry方法。
Manifest mf = new Manifest();
JarOutputStream jar = new JarOutputStream(new FileOutputStream("MainTest.jar"),mf);
Properties properties = new Properties();
jar.putNextEntry(new ZipEntry("MainTest.property"));
properties.store(jar, "this is a test");
jar.close();
6. 得到IJavaModle
IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace()
.getRoot());
IJavaProject[] projects = model.getJavaProjects();
IPackageFragmentRoot[] roots = projects[i] .getPackageFragmentRoots();
然后可以依次得到对应elements