JDT
Das Java Development Toolkit (JDT) stellt die Plugins für Eclipse bereit, die diese Plattform zu einer vollwertigen Java Entwicklungsumgebung machen.
JDT AST
Über JDT kann auf den abstrakten Syntaxbaum (AST) von Java Programmen zugegriffen werden. Damit kann der Source Code manipuliert werden und Code-Analysen durchgeführt werden.
Tutorials:
- http://www.eclipse.org/articles/Article-JavaCodeManipulation_AST/index.html
- http://www-128.ibm.com/developerworks/opensource/library/os-ast/?ca=dgr-lnxw97ASTParser (für den alten JLS2)
How does code look like in the JDT AST?
PACKAGE_DECLARATION
is our own package. It consists of one or more
QUALIFIED_NAME
, all the names with at least one dot. A qualified name
itself consists of simple names for each name between the dots.
E.g. java.io.File
has the qualified names java.io.File
and java.io
,
then (in the AST) the simple names java
, io
, and File
.
Same for IMPORT_DECLARATIONS
. Imports with wildcards (java.io.*
) have
FQNs without the last dot and wildcard, i.e. java.io
.
The class itself is a TYPE_DECLARATION
. Then come the modifiers, then
the class name as simple name.
Same for METHOD_DECLARATION
s.
Assignments are both, in that order, an EXPRESSION_STATEMENT
and an
ASSIGNMENT
.
Instantiations with new() are *not* method calls, they are
CLASS_INSTANCE_CREATION
s.
Invocations of methods with no class/package qualifiers (correct
term?), i.e. foo()
not bar.foo()
, are a METHOD_INVOCATION
followed
immediately by *exactly one* simple name. These are "own" methods!
Invocations of methods on an object (foo.bar()
) are METHOD_INVOCATION
s
followed by two simple names. getFullyQualifiedName()
is of no use
here, it's only the methode name itself. One has to analyse the first
simple name.
Even parameters in method invocations are simple names! So one.two.three()
looks the same in the AST as
one.two(three)
. You also cannot tell it from the traversal direction,
it's "advance" in both cases. To recognize them, you can use the bindings.
The Javadoc preceding a method declaration is inside the
METHOD_DECLARATION
in the AST!
In method declarations, the parameters with their type, like the
MyClass c
in foo(MyClass c)
, are SINGLE_VARIABLE_DECLARATION
s.
Conditionals like IF_STATEMENT
are the whole thing, including the
body. the conditional itself - the whole one, if it has several parts
- is an INFIX_EXPRESSION
. Apparently, you cannot figure out what is
actually tested except using string matching. Operators like && or >
have no representation in the AST. (Not even as string or something,
they only appear in the INFIX_EXPRESSION
itself.)
For loops with iterators are a FOR_STATEMENT
followed by a
var. declaration (the Iterator it
= foo.iterator()
)
INSTANCE_OF
nodes have two leaves, the first for the variable as simple
name, the second for the type as simple type.