Eclipse源代码分析

一、概述
走入Eclipse的内核,看看它到底是怎么工作的?

1、Eclipse源代码 
下载地址:http://download.eclipse.org/eclipse/downloads

2、源代码阅读工具        Source Insight  V3.5
它其实是一个代码编辑软件,因为有强大的代码分析工具,可以很方便地跟踪代码的相关性,所以常用来作为阅读代码的工具。
下载地址:http://sourceinsight.com/down35.html

为了方便代码的分析,我们只提取以下几个插件的代码:
org.eclipse.platform                             org.eclipse.platform_3.1.1.jar
org.eclipse.core.commands              org.eclipse.core.commands_3.1.0.jar
org.eclipse.core.expressions            org.eclipse.core.expressions_3.1.0.jar
org.eclipse.core.runtime                     org.eclipse.core.runtime_3.1.1.jar
org.eclipse.help                                    org.eclipse.help_3.1.0.jar
org.eclipse.jface                                   org.eclipse.jface_3.1.1.jar
org.eclipse.osgi                                    org.eclipse.osgi_3.1.1.jar
org.eclipse.swt.win32.win32.x86      org.eclipse.swt.win32.win32.x86_3.1.1.jar
org.eclipse.swt                                     org.eclipse.swt_3.1.0.jar
org.eclipse.ui.workbench                   org.eclipse.ui.workbench_3.1.1.jar
org.eclipse.ui                                        org.eclipse.ui_3.1.1.jar
org.eclipse.update.configurator        org.eclipse.update.configurator_3.1.0.jar

将这些代码解压缩到一个空目录里,然后导入到Source Insight的Project里。

二、Eclipse启动过程
首先我们从Eclipse的启动过程开始分析。

1、eclipse.exe
它是Eclipse的启动文件,是与平台相关的可执行文件。它的功能比较简单,主要是加载startup.jar文件,代码在Eclipse源代码的/features/org.eclipse.platform.launchers/library目录下,对应多个平台。对于win32平台,你可以直接运行win32目录下的build.bat文件来编译得到它(需要安装C编译器)。

2、startup.jar
这个是Eclipse真正的启动文件,你可以在命令行下运行java -jar startup.jar命令来启动Eclipse。它的入口是org.eclipse.core.launcher.Main,它对应的源代码在org.eclipse.platform/src目录的子目录下的Main.java。我们从main函数往后跟踪,找到basicRun,这个是启动的主要部分。

protected void basicRun(String[] args) throws Exception {
        ......
        setupVMProperties();              //设置VM属性
        processConfiguration();             //读取configuration/config.ini配置文件
        ......
        // need to ensure that getInstallLocation is called at least once to initialize the value.
        // Do this AFTER processing the configuration to allow the configuration to set
        // the install location.  
        getInstallLocation();

// locate boot plugin (may return -dev mode variations)
        URL[] bootPath = getBootPath(bootLocation);

setSecurityPolicy(bootPath);       //设置执行权限

// splash handling is done here, because the default case needs to know
        // the location of the boot plugin we are going to use
        handleSplash(bootPath);

invokeFramework(passThruArgs, bootPath);            //启动Eclipse内核
    }

这个函数前面部分是设置一些属性,最关键的是最后invokeFramework函数,它是启动Eclipse的核心。下面我们看看invokeFramework函数的具体内容。

private void invokeFramework(String[] passThruArgs, URL[] bootPath) 
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, Error, Exception, InvocationTargetException {
        ......
        URLClassLoader loader = new StartupClassLoader(bootPath, parent);

Class clazz = loader.loadClass(STARTER);             //加载   
String STARTER = "org.eclipse.core.runtime.adaptor.EclipseStarter";
        Method method = clazz.getDeclaredMethod("run", new Class[] {String[].class, Runnable.class});          //获得run方法
        ......
        method.invoke(clazz, new Object[] {passThruArgs, endSplashHandler});           //调用run方法
        ......
    }

首先创建加载器loader,它是一个URLClassLoader类型。接着加载类"org.eclipse.core.runtime.adaptor.EclipseStarter",获得其run方法,然后调用此方法。

3、OSGI启动
"org.eclipse.core.runtime.adaptor.EclipseStarter"类的源代码位于/plugins/org.eclipse.osgi/eclipseAdaptor/src/org/eclipse/core/runtime/adaptor。可见它已经位于OSGI包内,它是OSGI的启动类。

public static void startup(String[] args, Runnable endSplashHandler) throws Exception {
  ......
  adaptor = createAdaptor();              //建立适配器
  ......
  OSGi osgi = new OSGi(adaptor);          //建立OSGI对象,这就是我们要找的东西
  ......
  osgi.launch();                        //启动OSGI
  ......
  context = osgi.getBundleContext();        //获得已加载的Bundle的执行上下文
  ......
  Bundle[] startBundles = loadBasicBundles();   //加载Bundle
  setStartLevel(getStartLevel());              //设置启动级别
  ......
 }

4、Eclipse固定菜单的实现类(如Project、Help等菜单)
    org.eclipse.ui.internal.ide包下的WorkbenchActionBuilder.java类中的 protected void fillMenuBar(IMenuManager menuBar)方法,具体实现如下:

protected void fillMenuBar(IMenuManager menuBar) {
        menuBar.add(createFileMenu());                                    //在菜单栏增加File菜单
        menuBar.add(createEditMenu());
        menuBar.add(createNavigateMenu());
        menuBar.add(createProjectMenu());
        menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        menuBar.add(createWindowMenu());
        menuBar.add(createHelpMenu());
    }

如果想去掉File菜单下的Move项可以注掉private MenuManager createFileMenu()方法中的以下语句:
    // menu.add(moveAction);

Eclipse源代码分析的更多相关文章

  1. MonkeyRunner源代码分析之启动

    在工作中由于要追求完毕目标的效率,所以很多其它是强调实战.注重招式.关注怎么去用各种框架来实现目的.可是假设一味仅仅是注重招式.缺少对原理这个内功的了解,相信自己非常难对各种框架有更深入的理解. 从几 ...

  2. android-plugmgr源代码分析

    android-plugmgr是一个Android插件加载框架,它最大的特点就是对插件不需要进行任何约束.关于这个类库的介绍见作者博客,市面上也有一些插件加载框架,但是感觉没有这个好.在这篇文章中,我 ...

  3. Twitter Storm源代码分析之ZooKeeper中的目录结构

    徐明明博客:Twitter Storm源代码分析之ZooKeeper中的目录结构 我们知道Twitter Storm的所有的状态信息都是保存在Zookeeper里面,nimbus通过在zookeepe ...

  4. 转:SDL2源代码分析

    1:初始化(SDL_Init()) SDL简介 有关SDL的简介在<最简单的视音频播放示例7:SDL2播放RGB/YUV>以及<最简单的视音频播放示例9:SDL2播放PCM>中 ...

  5. 转:RTMPDump源代码分析

    0: 主要函数调用分析 rtmpdump 是一个用来处理 RTMP 流媒体的开源工具包,支持 rtmp://, rtmpt://, rtmpe://, rtmpte://, and rtmps://. ...

  6. 转:ffdshow 源代码分析

    ffdshow神奇的功能:视频播放时显示运动矢量和QP FFDShow可以称得上是全能的解码.编码器.最初FFDShow只是mpeg视频解码器,不过现在他能做到的远不止于此.它能够解码的视频格式已经远 ...

  7. UiAutomator源代码分析之UiAutomatorBridge框架

    上一篇文章<UIAutomator源代码分析之启动和执行>我们描写叙述了uitautomator从命令行执行到载入測试用例执行測试的整个流程.过程中我们也描写叙述了UiAutomatorB ...

  8. MyBatis架构设计及源代码分析系列(一):MyBatis架构

    如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处. 一.概述 MyBatis并不是一个完整的ORM框架,其官方首页是这么介绍自己 The MyBa ...

  9. hostapd源代码分析(三):管理帧的收发和处理

    hostapd源代码分析(三):管理帧的收发和处理 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004379 这篇文章我来讲解一下h ...

随机推荐

  1. Database Vault Administrator的使用

    第一次安装Database Vault的时候,先安装好了Database Vault.然后才安装的EM.发现,根本无法訪问<span><span>Database Vault ...

  2. 【BZOJ2395】【Balkan 2011】Timeismoney 最小乘积生成树

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  3. Thinking In Design Pattern——MVP模式演绎

    原文<Thinking In Design Pattern——MVP模式演绎>不知为何丢失了,故重新整理了一遍. 目录 What Is MVP Domain Model StubRepos ...

  4. 王勇详谈 Linux Deepin 背后的故事

    (Linux Deepin最近发布了12.12版本.其也许是国内第一款比较优秀的桌面Linux系统.在此向致力于研发国产OS系统的猿人们表示敬意.虽然Deepin只是基于Ubuntu在桌面应用和UI方 ...

  5. Android Studio主题设置、颜色背景配置

    打开http://color-themes.com/有很多样式可供选择 导入方式 下载主题—xxx.jar 注意:如果我们下载下来的jar名字如果有空格,一定要把空格去掉,同时文件路径中不要含有中文 ...

  6. JVM 入门三板斧

    一个JVM实例只存在一个堆内存,堆内存的大小是可以调节的.类加载器读取了类文件后,需要把类.方法.常变量放到堆内存中,保存所有引用类型的真实信息,以方便执行器执行,堆内存分为三部分: Young Ge ...

  7. Appium 脚本代码中启动appium server

    import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.Appiu ...

  8. mysql数据导入遇到的timestamp类型问题

    今天准备把最新的表导入自己以前的机子上做临时开发,在数据库导入的时候遇到一个问题:Incorrect table definition; there can be only one TIMESTAMP ...

  9. IOS团队开发之——CocoaPods 第三方库管理工具

    使用前需要下载ruby 的gem 命令镜像,mac 下自带有.但一般不用,直接访问国外网站有限制. 下面安装 http://ruby.taobao.org/ http://blog.devtang.c ...

  10. ubuntu for win10 里运行apache+php

    一直想试试ubuntu for win10中运行网站测试一下,弄了好久,今天终于基本弄明白了, ubuntu for win10里的IP就是外面WIN10的IP,在里面建立网站了可以直接在外面WIN1 ...