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. Intent----android中的伟大邮差

    在android中,intent就像是一个邮差,辛勤高效的在各个组件之间来回穿梭.我们可以通过它启动一个Activity或者Service,或者是发送给广播组件,又或者是与后台的Service进行通信 ...

  2. SQL plan directives

    SQL plan directives SQL plan directives含有优化器产生优化的执行计划时需要的附加信息和指令. 在sql执行时,如果cardinality估计有错误,数据库就会创建 ...

  3. python -- 装饰器入门

    用例: 统计函数执行需要的时间 假设我们执行的一段代码的运行时间比我们预想的时间要久,而这段代码块有多个函数调用组成,我们有理由相信至少是其中的一个函数调用导致整个代码块产生了瓶颈.我们如何去发现导致 ...

  4. (LeetCode)用两个栈实现一个队列

    LeetCode上面的一道题目.原文例如以下: Implement the following operations of a queue using stacks. push(x) -- Push ...

  5. java设置配置session过期时间的方法

    1) Timeout in the deployment descriptor (web.xml)以分钟为单位 代码如下 复制代码 <web-app ...> <session-co ...

  6. [转]用GSON 五招之内搞定任何JSON数组

    关于GSON的入门级使用,这里就不提了,如有需要可以看这篇博文 <Google Gson的使用方法,实现Json结构的相互转换> ,写的很好,通俗易懂. 我为什么写这篇文章呢?因为前几晚跟 ...

  7. MySQL 示例数据库 employees 详解

    [引子] IT这一行在我看来是比较要求动手能力的,但是人非生而知之:人们身上的技能除了一些本能之外,大多都是通过学习而得到的. 前一段时间一直在整理素材,写一个关于explain 的系列文章:在一开始 ...

  8. 交叉验证(CrossValidation)方法思想简介[zz]

    以下简称交叉验证(Cross Validation)为CV.CV是用来验证分类器的性能一种统计分析方法,基本思想是把在某种意义下将原始数据(dataset)进行分组,一部分做为训练集(train se ...

  9. react-创建组件

    //定义组件class InputControlES6 extends React.Component{ render (){ return (<View style="{sty.co ...

  10. Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子

    Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子 1.1. 何为机器学习?1 1.2. 两类机器学习算法 :监督式学习(Supervised Learning)和非监督式学习( ...