Jump to: navigation, search

Difference between revisions of "FAQ How do I manipulate Java code?"

 
m
 
Line 1: Line 1:
''
 
 
 
JDT offers a number of mechanisms for manipulating
 
JDT offers a number of mechanisms for manipulating
 
Java programs.  It can be a daunting task to figure out
 
Java programs.  It can be a daunting task to figure out
Line 6: Line 4:
 
different capabilities and trade-offs, depending on exactly
 
different capabilities and trade-offs, depending on exactly
 
what you want to do. Here is a quick rundown of what’s available.
 
what you want to do. Here is a quick rundown of what’s available.
 
  
 
The first option is to use the <i>Java model</i> API. This
 
The first option is to use the <i>Java model</i> API. This
Line 18: Line 15:
 
The Java model is typically not used for modifying individual Java
 
The Java model is typically not used for modifying individual Java
 
files.
 
files.
 
  
 
The Java document object model (JDOM), is used for manipulating an
 
The Java document object model (JDOM), is used for manipulating an
Line 28: Line 24:
 
only, such as adding methods and changing parameter or return types,  
 
only, such as adding methods and changing parameter or return types,  
 
JDOM is the way to go.
 
JDOM is the way to go.
 
  
 
Last but not least is the <i>abstract syntax tree (AST)</i> API. By creating
 
Last but not least is the <i>abstract syntax tree (AST)</i> API. By creating
Line 37: Line 32:
 
to 3.0, you could use the AST for analyzing Java types but had to use the  
 
to 3.0, you could use the AST for analyzing Java types but had to use the  
 
JDOM for source modification.
 
JDOM for source modification.
 
  
 
Of course, you can modify Java files without using any of these  
 
Of course, you can modify Java files without using any of these  
Line 44: Line 38:
 
you use to modify Java files, you should always use a <i>working
 
you use to modify Java files, you should always use a <i>working
 
copy</i> to do so.
 
copy</i> to do so.
 
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ What is a working copy?]]
 +
*[[FAQ What is a JDOM?]]
 +
*[[FAQ What is an AST?]]
  
[[FAQ_What_is_a_working_copy%3F]]
+
{{Template:FAQ_Tagline}}
 
+
[[FAQ_What_is_a_JDOM%3F]]
+
 
+
[[FAQ_What_is_an_AST%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Latest revision as of 22:44, 14 June 2006

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:


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.