JDT Plug in Developer Guide. Programmer's Guide

Similar documents
Eclipse Platform Technical Overview

Introduction to Eclipse

In order to support developers, there needs to be a number of tools available which may be involved in the ultimate solution.

User s Manual. for. Diagram Consistency and Validation in agenttool III

Just Enough Eclipse What is Eclipse(TM)? Why is it important? What is this tutorial about?

Index. Symbols. /**, symbol, 73 >> symbol, 21

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Teiid Designer User Guide 7.5.0

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Introduction to Eclipse

i2b2 Workbench Developer s Guide: Eclipse Neon & i2b2 Source Code

WebSphere Message Broker. Programming

What s NetBeans? Like Eclipse:

SpringSource Tool Suite M2

Getting Started with Eclipse/Java

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

Advanced User Interface Programming Using the Eclipse Rich Client Platform

Install Guide. Version 1.0 Demonstration. Copyright 2007 MGH

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

EMC Documentum Composer

InfoSphere Data Architect Pluglets

SpringSource Tool Suite 2.7.1

Common Navigator Framework

Adobe ColdFusion Documentation. September 2014

Infor LN Studio Application Development Guide

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

DB2 Stored Procedure and UDF Support in Rational Application Developer V6.01

Using the Plug in Development Environment

AppDev StudioTM 3.2 SAS. Migration Guide

Class Dependency Analyzer CDA Developer Guide

Your password is: firstpw

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, November 2017

Eclipse Introduction. Zeng Yu

ADT: Eclipse development tools for ATL

WPS Workbench. user guide. "To help guide you through using the WPS user interface (Workbench) to create, edit and run programs"

CHAPTER 6. Java Project Configuration

Introduction to Eclipse

Getting Started with Web Services

Creating a new CDC policy using the Database Administration Console

Javac and Eclipse tutorial

EMC Documentum Composer

Scientific Software Development with Eclipse

GNATbench for Eclipse User s Guide

Testing Eclipse plug-ins: Tips & Tricks

The PALOMA Eclipse Plug-in User Manual

Getting the Most from Eclipse

IBM Workplace Client Technology API Toolkit

Design Document CDT Remote Search/Index Framework. Chris Recoskie Vivian Kong Mike Kucera Jason Montojo. IBM Corporation

Getting Started with Web Services

Mobile Application Workbench. SAP Mobile Platform 3.0 SP02

Dynamic Languages Toolkit. Presented by Andrey Tarantsov

FeatureIDE: Development

Extending the JavaScript Development Toolkit

IBM. IBM WebSphere Application Server Migration Toolkit. WebSphere Application Server. Version 9.0 Release

Developer s Guide. Version Copyright 2007 MGH

Regular Forum of Lreis. Speechmaker: Gao Ang

A QUICK OVERVIEW OF THE OMNeT++ IDE

CHAPTER 1INTRODUCTION... 3 CHAPTER 2INSTALLING ECLIPSE...

Before you start with this tutorial, you need to know basic Java programming.

SML - Editor and Validation Scheme

Cupid Documentation. Release 0.2 (ESMF v7) Rocky Dunlap

OMNeT++ IDE Developers Guide. Version 5.2

Wowza IDE 2. User's Guide

At the shell prompt, enter idlde

Using Eclipse Europa - A Tutorial

Teiid Designer User Guide 7.7.0

EMC Documentum Composer

Module 3: Working with C/C++

Notes of the course - Advanced Programming. Barbara Russo

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, January 2018

Developing Eclipse Rich-Client Applications Tutorial

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Version: Copyright World Programming Limited

CHAPTER 6. Organizing Your Development Project. All right, guys! It s time to clean up this town!

SAP Edge Services, cloud edition Edge Services Predictive Analytics Service Guide Version 1803

Module Road Map. 7. Version Control with Subversion Introduction Terminology

DS-5 ARM. Using Eclipse. Version Copyright ARM. All rights reserved. ARM DUI 0480L (ID100912)

IBM. Generic RSE User's Guide. IBM Explorer for z/os. Version 3 Release 0 SC

Express Yourself. What is Eclipse?

Introduction to Programming Using Java (98-388)

1 Overview. 1 Overview. Contents. 1.1 Table of Contents Table of Contents

Plug-ins, RCP and SWT. Sample Content

Software Development Tools. COMP220/COMP285 Sebastian Coope Eclipse and JUnit: Creating and running a JUnit test case

AndeSight. User Manual. Working with AndESLive. Version 1.0

HPE Security Fortify Plugins for Eclipse Software Version: Installation and Usage Guide

ActiveSpaces Transactions. Quick Start Guide. Software Release Published May 25, 2015

Infor LN Studio Administration Guide

SDKs - Eclipse. SENG 403, Tutorial 2

Prerequisites for Eclipse

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

Continuous Testing in Eclipse. David Saff, Michael D. Ernst MIT CSAIL etx 2004, Barcelona, Spain

MC Android Programming

Adapting JDT to the Cloud. Alex Boyko Pivotal Jay Arthanareeswaran - IBM John Arthorne - IBM

TITLE: User Guide for the Graphical Model Editing Framework. MSU Capstone

Eclipse (2/3) Deepak Dhungana Institute for System Engineering and Automation

Table of Contents HOL-SDC-1422

IBM Database Conversion Workbench 3.5

Composer Guide for JavaScript Development

form layout - we will demonstrate how to add your own custom form extensions in to form layout

When Modeling meets Productivity. Sven Efftinge - itemis

Transcription:

JDT Plug in Developer Guide Programmer's Guide

Table of Contents Java Development Tooling overview...1 Java elements and resources...1 Java elements...1 Java elements and their resources...3 Java development process...4 JDT User interface...5 Using the JDT API...7 Manipulating Java code...7 Code modification using Java elements...7 Generating a compilation unit...7 Modifying a compilation unit...7 Generic source code modification...8 Responding to changes in Java elements...8 Compiling Java code...9 Compiling code...9 Problem determination...9 Running a Java program...9 Launching a compiled Java program...10 i

ii

Java Development Tooling overview The Eclipse platform is delivered with a full featured Java integrated development environment (IDE). Java development tooling (JDT) allows users to write, compile, test, debug, and edit programs written in the Java programming language. The JDT makes use of many of the platform extension points and frameworks described in the Platform Plug in Developer Guide. This guide discusses the extension points and API provided by the JDT. We assume that you already understand the concepts of plug ins, extension points, workspace resources, and the workbench UI. We first look at how the JDT tools are structured. It's easiest to think of the JDT as a set of plug ins that add Java specific behavior to the generic platform resource model and contribute Java specific views, editors, and actions to the workbench. Then we will look at how to use the JDT API to accomplish common tasks, such as generating, compiling, and running Java code programmatically. Java elements and resources The Java model is the set of classes that model the objects associated with creating, editing, and building a Java program. The Java model classes are defined in the org.eclipse.jdt.core.* packages. These classes implement Java specific behavior for resources and further decompose Java resources into model elements. Java elements The package org.eclipse.jdt.core defines the classes that model the elements that compose a Java program. The JDT uses an in memory object model to represent the structure of a Java program. This model is hierarchical. Elements of a program can be decomposed into child elements. Java elements are a lot like resource objects. When you work with a Java element, you are actually working with a handle to some underlying model object. You must use the exists() protocol to determine whether the element is actually present in the workspace. The following table summarizes the different kinds of Java elements. Element IJavaModel IJavaProject IPackageFragmentRoot IPackageFragment ICompilationUnit IPackageDeclaration Description Represents the root Java element, corresponding to the workspace. The parent of all Java projects. Represents a Java project in the workspace. (Child of IJavaModel) Represents a set of package fragments, and maps the fragments to an underlying resource which is either a folder, JAR, or ZIP file. (Child of IJavaProject) Represents the portion of the workspace that corresponds to an entire package, or a portion of the package. (Child of IPackageFragmentRoot) Represents a Java source (.java) file. Represents a package declaration in a compilation unit. (Child of ICompilationUnit) 1

IImportContainer IImportDeclaration IType IField IMethod IInitializer IClassFile Represents the collection of package import declarations in a compilation unit. (Child of ICompilationUnit) Represents a single package import declaration. (Child of IImportContainer) Represents either a source type inside a compilation unit, or a binary type inside a class file. Represents a field inside a type. (Child of IType) Represents a method or constructor inside a type. (Child of IType) Represents a static or instance initializer inside a type. (Child of IType) Represents a compiled (binary) type. (Child of IPackageFragment) All Java elements support the IJavaElement interface. Some of the elements are shown in the Packages view. These elements implement the IOpenable interface, since they must be opened before they can be navigated. The figure below shows how these elements are represented in the Packages view. The Java elements that implement IOpenable are created mostly from information found in the underlying resource files. The same elements are represented generically in the resource navigator view. 2

Other elements correspond to the items that make up a Java compilation unit (.java file). The figure below shows a Java compilation unit and a content outliner that displays the source elements in the compilation unit. These elements implement the ISourceReference interface, since they have corresponding source code. (As these elements are selected in the content outliner, their corresponding source code is shown in the Java editor). Java elements and their resources Many of the Java elements correspond to generic resources in the workspace. The class JavaCore is the place to start when you want to create Java elements from a generic resource. The following code snippet shows how to get Java elements from their corresponding resources. 3

private void createjavaelementsfrom(iproject myproject, IFolder myfolder, IFile myfile) { IJavaProject myjavaproject = JavaCore.create(myProject); if (myjavaproject == null) // the project is not configured for Java (has no Java nature) return; // get a package fragment or package fragment root IJavaElement mypackagefragment = JavaCore.create(myFolder); } // get a.java (compilation unit),.class (class file), or //.jar (package fragment root) IJavaElement myjavafile = JavaCore.create(myFile); JavaCore is also used to maintain the Java class path, including locations for finding source code and libraries, and locations for generating output binary (.class) files. When you create a Java project from a plain project, JavaCore will check to see if the project is configured with the Java nature. The JDT plug in uses a project nature to designate a project as having Java behavior. This nature (org.eclipse.jdt.core.javanature) is assigned to a project when the "New Java project" wizard creates a project. If the Java nature is not configured on a project, JavaCore will return null when asked to create the project. What's different about a Java project? Java projects record their classpath in a ".classpath" file and add the Java incremental project builder to the project's build spec. Otherwise, they are just regular projects and can be configured with other natures (and other incremental builders) by plug ins. Plug ins that want to configure projects with Java behavior in addition to their own behavior typically use the "New java project" wizard page to assign the Java nature to the project in addition to their own custom natures or behavior. IJavaModel can be considered the parent of all projects in the workspace that have the Java project nature (and therefore can be treated as an IJavaProject.) Java development process Additional packages in the JDT support other aspects of the Java development process. The following roadmap outlines the major function in the remaining org.eclipse.jdt.core.* packages. org.eclipse.jdt.launching provides classes for launching Java runtimes from the platform. JavaRuntime implements static methods that let you register a vm for a particular project. A family of VM's (such as the JDK) is represented by the IVMInstallType class. IVMInstall represents particular installations within a family. The IVMRunner and VMRunnerResult classes are used to start a particular Java VM, register its processes with the JDT, and return information from the launched program. org.eclipse.jdt.core.search supports searching the workspace Java elements that match a particular description. SearchEngine allows you to set up searches in the workspace. You can search for particular patterns inside a Java element. You can also scope the search according to several predefined search types: searching for declarations of the pattern, implementors of the pattern, references to the pattern, or all occurrences of the pattern. org.eclipse.jdt.core.jdom supports a Java Document Object Model (DOM) that can be used for walking the structure of a Java compilation unit. org.eclipse.jdt.core.eval supports the evaluation of code snippets in a scrapbook or inside the 4

debugger. JDT User interface The org.eclipse.jdt.ui plug in implements the Java specific user interface classes that manipulate Java elements. The following roadmap outlines the major functions in the org.eclipse.jdt.ui package and its sub packages. A Java perspective assembles all of the Java specific UI elements. Users working in the Java perspective use the customized Java views and editors and have access to all of the workbench actions associated with developing Java code. The Java package view (IPackagesViewPart) shows the package structure of the resources in the workspace and includes menu items for Java specific actions. The Java type hierarchy view (ITypeHierarchyViewPart) shows the hierarchy for a Java type in the workspace. A specialized Java editor supports syntax coloring, annotating code with compilation problems, and code assist. A content outliner further decomposes the source file into its underlying elements. A Java.class file editor is provided that uses a content outliner to decompose the binary file into its underlying elements. Specialized wizards are implemented for creating different kinds of Java elements. NewJavaProjectWizardPage is a public wizard class that allows other plug ins to include the Java page when creating their own projects. This wizard page will configure a new project with the Java nature. 5

6

Using the JDT API Given that the JDT supplies a full featured Java IDE, why would you need to use the JDT API? If you are building a plug in that interacts with Java programs or resources as part of its function, you may need to do one or more of the following things: Programmatically manipulate Java resources, such as creating projects, generating Java source code, performing builds, or detecting problems in code. Programmatically launch a Java program from the platform Provide a new type of VM launcher to support a new family of Java runtimes Add new function and extensions to the Java IDE itself If you want to use JDT API in your plug in, you must include the JDT plug ins as prerequisites in your plug in.xml. org.eclipse.jdt.core is the plug in that defines the core Java elements and API. You should always list this plug in as a prerequisite. org.eclipse.jdt.ui is the plug in that defines the Java UI API. You should list this plug in as a prerequisite if you are using the JDT UI classes. org.eclipse.jdt.launching is the plug in that defines the Java launching and runtime support. You should list this plug in as a prerequisite if you need to launch Java virtual machines programmatically. Manipulating Java code Your plug in can use the JDT API to create classes or interfaces, add methods to existing types, or alter the methods for types. The simplest way to alter Java objects is to use the Java element API. More general techniques can be used to work with the raw source code for a Java element. Code modification using Java elements Generating a compilation unit The easiest way to programmatically generate a compilation unit is to use IPackageFragment.createCompilationUnit. You specify the name and contents of the compilation unit. The compilation unit will be created inside the package and the new ICompilationUnit will be returned. A compilation unit can be created generically by creating a file resource whose extension is ".java" in the appropriate folder that corresponds to the package directory. Using generic resource API goes "behind the back" of the Java tooling, so the Java model is not updated until the generic resource change listeners are notified and the JDT listeners update the Java model with the new compilation unit. Modifying a compilation unit Most simple modifications of Java source can be done using the Java element API. For example, you can query a type from a compilation unit. Once you have the IType, you can use protocol such as createfield, createinitializer, createmethod, or createtype to add source code members to the 7

type. The source code and information about the location of the member is supplied in these methods. The ISourceManipulation interface defines common source manipulations for java elements. This includes methods for renaming, moving, copying, or deleting a type's member. Generic source code modification Some source code modifications are not provided via the Java element API. A more general way to edit source code (such as changing the source code for existing elements) is accomplished using the compilation unit's raw source code and the Java DOM. These techniques include the following: // get the source for a compilation unit String contents = mycompilationunit.getbuffer().getcontents(); // Create an editable JDOM myjdom = new DOMFactory(); mydomcompilationunit = myjdom.createcompilationunit(contents, "MyClass"); // Navigate and edit the compilation unit structure using // JDOM node protocol.... // Once modififications have been made to all of the nodes // get the source back from the compilation unit DOM node. String newcontents = mydomcompilationunit.getcontents(); // Set this code back into the compilation unit element mycompilationunit.getbuffer().setcontents(newcontents); // Save the buffer to the file. mycompilationunit.save(); This technique can result in problem markers being associated with incorrect line numbers, since the Java elements were not updated directly. The Java element model does not go any finer than methods and fields. The abstract syntax tree (AST) used by the compiler is not available as API, so the techniques used by the JDT to parse source into programmatic structures are not currently available as API. Responding to changes in Java elements If your plug in needs to know about changes to Java elements after the fact, you can register a Java IElementChangedListener with JavaCore. JavaCore.addElementChangedListener(new MyJavaElementChangeReporter()); Java element change listeners are similar conceptually to resource change listeners (described in tracking resource changes). The following snippet implements a Java element change reporter that prints the element deltas to the system console. public class MyJavaElementChangeReporter implements IElementChangedListener { public void elementchanged(elementchangedevent event) { IJavaElementDelta delta= event.getdelta(); if (delta!= null) { System.out.println("delta received: "); 8

} } } System.out.print(delta); The IJavaElementDelta includes the element that was changed and flags describing the kind of change that occurred. Compiling Java code The JDT plug ins include an incremental and batch Java compiler for building Java.class files from source code. There is no direct API provided by the compiler. It is installed as an incremental builder on Java projects. Compilation is triggered using standard platform build mechanisms. The platform build mechanism is described in detail in resource builders. Compiling code You can programmatically compile the Java source files in a project using the build API. IProgressMonitor myprogressmonitor; myproject.build(incrementalprojectbuilder.incremental_build, myprogressmonitor); For a Java project, this will invoke the Java incremental project builder (along with any other incremental project builders that have been added to the project's build spec). Problem determination The JDT core defines a specialized marker (marker type "org.eclipse.jdt.core.problem") to denote compilation problems. To programmatically discover problems detected by the compiler, the standard platform marker protocol should be used. See resource markers for an overview of using markers. The following snippet finds all Java problem markers in a compilation unit. public IMarker[] findjavaproblemmarkers(icompilationunit cu) throws CoreException { IResource javasourcefile = cu.getunderlyingresource(); IMarker[] markers = javasourcefile.findmarkers(ijavamodelmarker.java_model_problem_marker, true, IResource.DEPTH_INFINITE); } Java problem markers are maintained by the Java incremental project builder and will be removed automatically as problems are resolved and the Java source is recompiled. Running a Java program The JDT API includes facilities for launching a Java program using the VM install that is currently configured by the user for a Java project. 9

Launching a compiled Java program Java programs that have been compiled in a Java project can be run by getting the appropriate IVMRunner for the Java project and running the class by name. The following code snippet shows how the class MyClass inside myjavaproject can be launched. IVMInstall vminstall = JavaRuntime.getVMInstall(myJavaProject); if (vminstall == null) vminstall = JavaRuntime.getDefaultVMInstall(); IVMRunner vmrunner = vminstall.getvmrunner(ilaunchmanager.run_mode); if (vmrunner!= null) { String[] classpath = null; try { classpath = JavaRuntime.computeDefaultRuntimeClassPath(myJavaProject); } catch (CoreException e) { } if (classpath!= null) { VMRunnerConfiguration vmconfig = new VMRunnerConfiguration("MyClass", classpath); VMRunnerResult result = vmrunner.run(vmconfig); } } 10