1.
用interface
來(lái)定義系統(tǒng)對(duì)外提供的服務(wù),有抽象類來(lái)做擴(kuò)展。盡量用interface
作為參數(shù)類型。
2. ThreadLocal類,定義了一個(gè)變量的本地副本,與原有變量隔離,作用類似static變量,只是不共享。可用set添加變量,get去獲取變量。變量類型不限制。
3. Eclipse plug in開(kāi)發(fā)中可以實(shí)現(xiàn)IRuntimeClasspathProvider接口。可以提供用launch configuration去獲得unresolved和resolved classpath。開(kāi)發(fā)人員可以在resolveClasspath方法中加入自定義的classpath。實(shí)現(xiàn)類需要注冊(cè)在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 開(kāi)發(fā)中可以用JavaRuntime 去得到運(yùn)行環(huán)境的信息。
IRuntimeClasspathProvider provider = JavaRuntime.getClasspathProvider(configuration);
其中configuration是ILaunchConfiguration類型的。
4. 在Eclipse plug in開(kāi)發(fā)中獲取文件。兩種解決辦法:1.從plug in實(shí)例中讀取文件的URL,然后用FileLocator把這個(gè)URL轉(zhuǎn)化成文件路徑;2.直接利用FileLocator的find方法。
方法1
//filepath 是需要定位的文件
String filepath = "/bin/resources/test.jar";
//instance 是當(dāng)前plug in的實(shí)例
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來(lái)寫(xiě)jar包。必須為JarOutputStream 實(shí)例創(chuàng)建至少一個(gè)Entry,可以調(diào)用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();
然后可以依次得到對(duì)應(yīng)elements