杰瑞科技汇

eclipse java api

Of course! The "Eclipse Java API" can refer to a few different things, but it most commonly means the APIs provided by the Eclipse IDE for Java Development. This includes the core APIs for building Eclipse plug-ins (extensibility) and the APIs used by the many tools within the IDE itself.

eclipse java api-图1
(图片来源网络,侵删)

Let's break it down into the most important areas.


The Core: Eclipse Platform APIs (for Extensibility)

This is the original and most powerful meaning. The Eclipse IDE is not just an application; it's a rich platform (the Eclipse Rich Client Platform - RCP) that can be extended to create custom applications and tools. These extensions are built using Eclipse's own APIs.

If you want to:

  • Create a new view in the "Package Explorer".
  • Add a new menu item or wizard.
  • Create a custom editor for a file type (e.g., a .properties file editor with syntax highlighting).
  • Integrate a new version control system.

...you will be using these core Eclipse APIs.

eclipse java api-图2
(图片来源网络,侵删)

Key Packages and Concepts:

Package / Concept Description Common Use Cases
org.eclipse.ui The heart of the user interface. Defines views, editors, perspectives, actions, and menus. Creating new views, adding menu items, defining perspectives.
org.eclipse.jface The widget toolkit for Eclipse, built on top of SWT. Provides higher-level UI components like dialogs, viewers, and wizards. Building custom dialogs, creating tables and trees (TableViewer, TreeViewer), wizards.
org.eclipse.core.runtime The fundamental runtime services. Defines extension points, plug-ins, preferences, and logging. Defining your own plug-in, reading/writing preferences, logging.
org.eclipse.swt The Standard Widget Toolkit. The low-level UI layer that Eclipse uses to create native OS widgets. Creating custom, high-performance UI components when JFace isn't enough.
org.eclipse.jdt.core The Java Development Tools Core API. This is the programmatic interface to the Java model. Analyzing Java code structure (projects, files, classes, methods) without parsing source files.
org.eclipse.jdt.ui The UI components of the JDT. Provides actions, refactoring wizards, and integration with the UI. Creating custom code analysis tools, adding refactoring options, integrating with the "Problems" view.

Where to find the documentation: The definitive source is the Eclipse Platform API Reference: https://help.eclipse.org/latest/topic/org.eclipse.platform.doc.isv/reference/api/


The Tools: JDT (Java Development Tools) APIs

This is what most Java developers use, even if they don't realize it. The JDT is the set of plug-ins that turn the Eclipse Platform into a world-class Java IDE. These APIs allow you to interact with and extend the Java-specific features.

Key Packages and Concepts:

Package / Concept Description Common Use Cases
org.eclipse.jdt.core (As mentioned above) The API for the Java Model. This is your entry point for analyzing any Java element. Finding all classes in a project, getting the methods of a class, resolving references.
org.eclipse.jdt.core.dom The Document Object Model API. This provides a parser that converts Java source code (*.java files) into a tree of objects. Advanced static analysis, code generation, refactoring tools, linters.
org.eclipse.jdt.ui (As mentioned above) The API for the Java UI. Creating custom "Quick Fixes" or "Code Templates", adding custom code inspections.
org.eclipse.jdt.launching APIs for launching and debugging Java applications. Creating custom launch configurations, building test runners, integrating with debuggers.

Where to find the documentation: The JDT API Reference: https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/reference/api/


The Build System: Maven and OSGi

Modern Eclipse projects are built using Maven and the underlying OSGi framework. Understanding these APIs is crucial for any serious Eclipse development.

eclipse java api-图3
(图片来源网络,侵删)
Technology Description Eclipse API Relevance
OSGi (Equinox) The module system and service runtime for Eclipse. Every plug-in is an OSGi bundle. The Bundle and ServiceTracker APIs are fundamental. Managing dependencies between your plug-ins, registering and consuming services (e.g., an IProjectNature).
Maven (Tycho) Tycho is a toolset for building Eclipse plug-ins and OSGi bundles with Maven. It understands the Eclipse metadata (MANIFEST.MF, .project). Building your Eclipse project from the command line, setting up a CI/CD pipeline for your Eclipse plugins.

How to Use These APIs in Your Project

Let's say you want to create a simple plug-in that adds a "Hello World" menu item to the "File" menu.

  1. Create a Plug-in Project:

    • In Eclipse, go to File > New > Project....
    • Select Plug-in Development > Plug-in Project.
    • Give it a name and click "Next".
    • On the "Templates" page, choose "Hello, World" and click "Finish".
  2. Understand the Generated Code:

    • plugin.xml: This is the manifest. It tells Eclipse what your plug-in does. The extension point defines that you are contributing an org.eclipse.ui.commands and an org.eclipse.ui.handlers and an org.eclipse.ui.menus item.
    • Activator.java: This class is started when your plug-in is loaded. It's used to get services.
    • Handler.java: This class contains the logic that runs when your menu item is clicked.
  3. Write the Handler Logic: Open Handler.java. The execute method is called when the action is triggered. You can use other Eclipse APIs here.

    public Object execute(ExecutionEvent event) throws ExecutionException {
        // Get the active window (the main Eclipse window)
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        // Get the active shell (for dialogs)
        Shell shell = window.getShell();
        // Use JFace to show a message box
        MessageDialog.openInformation(
            shell,
            "Hello World", // Title
            "Hello, Eclipse World!"); // Message
        return null;
    }
  4. Run and Test:

    • Right-click your project and select Run As > Eclipse Application.
    • A new Eclipse instance will launch.
    • Go to the File menu. You should see your "Say Hello..." menu item. Click it, and a message box will appear.

Summary and Key Takeaways

API Area Purpose Key Packages Target Audience
Core Platform APIs Building and extending the Eclipse IDE itself. org.eclipse.ui, org.eclipse.jface, org.eclipse.core.runtime Plug-in developers, RCP application developers.
JDT APIs Interacting with and extending Java development features. org.eclipse.jdt.core, org.eclipse.jdt.core.dom, org.eclipse.jdt.ui Java tool developers, static analysis tool authors.
Build & Runtime APIs Managing dependencies and building projects. org.osgi.framework, Tycho/Maven All Eclipse plug-in developers.

For most Java developers, you'll primarily interact with the JDT APIs indirectly through the IDE. If you ever want to automate a task, create a custom code analysis tool, or build a completely new tool inside Eclipse, you'll dive into the Core Platform APIs and JDT APIs.

分享:
扫描分享到社交APP
上一篇
下一篇