JavaDocBuilder
is the entry point to
QDox. It is responsible for parsing source code, resolving imports and storing
the data.
To create it, all you need to do is call the default constructor.
JavaDocBuilder builder = new JavaDocBuilder();
Java source code can then be added to the
JavaDocBuilder
.
Source can either beread one file at a time (using a java.io.Reader) or an entire source tree
can be added recursively.
// Reading a single source file. builder.addSource(new FileReader("MyFile.java")); // Reading from another kind of input stream. builder.addSource(new StringReader("package test; public class Hello {}")); // Adding all .java files in a source tree (recursively). builder.addSourceTree(new File("mysrcdir"));
In order to resolve classes that have been imported using a wildcard (e.g. import java.util.*;
), the
ClassLibrary
must be aware of other classes used in the project.
ClassLibrary has 3 ways to resolve classes:
The first two are automaticly set by JavaDocBuilder. In most cases this shall be sufficient, however in some situations you may want resolve the full classes in external libraries.
To resolve classes from different ClassLoaders (e.g. 3rd party Jar files), the
addClassLoader()
method must be called on the ClassLibrary.
// Get the ClassLibrary ClassLibrary lib = builder.getClassLibrary(); // Add a custom ClassLoader lib.addClassLoader(myCustomClassLoader); // Ant example : add the <classpath> element's contents lib.addClassLoader(new AntClassLoader(getProject(), classpath));
It is important that additional ClassLoaders are added before any source files are parsed.
Now the files have been parsed, move on to navigating the model.