Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

FAQ How do I manipulate Java code?

Revision as of 16:38, 14 March 2006 by Claffra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

JDT offers a number of mechanisms for manipulating Java programs. It can be a daunting task to figure out which of these options best suits your needs. Each mechanism has different capabilities and trade-offs, depending on exactly what you want to do. Here is a quick rundown of what’s available.


The first option is to use the Java model API. This API is intended primarily for browsing and manipulating Java projects on a macro scale. For example, if you want to create or browse Java projects, packages, or libraries, the Java model is the way to go. The finest granularity the Java model supports is the principal structure of Java types. You can browse the method and field signatures of a type, but you cannot manipulate the bodies of methods or any source file comments. The Java model is typically not used for modifying individual Java files.


The Java document object model (JDOM), is used for manipulating an individual Java file, also known as a compilation unit. JDOM also supports only manipulation of the principal structure of Java files but has more power than the Java model for modifying files. In particular, JDOM lets you modify the source characters for each method and field in a file. For performing manipulation of the principal structure only, such as adding methods and changing parameter or return types, JDOM is the way to go.


Last but not least is the abstract syntax tree (AST) API. By creating an AST on a Java file, you have ultimate control over browsing and modifying a Java program, including modification of method bodies and source comments. For complex analysis or modification of Java files, the AST is the best choice. Support for modifying and writing ASTs was introduced in Eclipse 3.0. Prior to 3.0, you could use the AST for analyzing Java types but had to use the JDOM for source modification.


Of course, you can modify Java files without using any of these facilities. You can obtain a raw character buffer on the file contents and perform arbitrary transformations yourself. Regardless of what mechanism you use to modify Java files, you should always use a working copy to do so.


See Also:

FAQ_What_is_a_working_copy?

FAQ_What_is_a_JDOM?

FAQ_What_is_an_AST?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top