Eclipse插件开发

转自: http://www.blogjava.net/zjibo/archive/2008/04/10/191925.html
1. interface 来定义系统对外提供的服务,有抽象类来做扩展。尽量用interface 作为参数类型。

2. ThreadLocal类,定义了一个变量的本地副本,与原有变量隔离,作用类似static变量,只是不共享。可用set添加变量,get去获取变量。变量类型不限制。

3. Eclipse plug in开发中可以实现IRuntimeClasspathProvider接口。可以提供用launch configuration去获得unresolvedresolved classpath。开发人员可以在resolveClasspath方法中加入自定义的classpath。实现类需要注册在extension point中。

      /**

       *Computes and returns an unresolved classpath for the given launch configuration.

       *Variable and containerentries are not resolved.

       *

       * @paramconfiguration launch configuration

       * @returnunresolved path

       * @exceptionCoreException if unable to compute a path

       */

      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);

其中configurationILaunchConfiguration类型的。

4. Eclipse plug in开发中获取文件。两种解决办法:1.plug in实例中读取文件的URL,然后用FileLocator把这个URL转化成文件路径;2.直接利用FileLocatorfind方法。

方法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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值