天天看點

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

若轉載請注明出處!若有疑問,請回複交流!

繼續閱讀