Package com.thoughtworks.qdox
Class JavaProjectBuilder
- java.lang.Object
-
- com.thoughtworks.qdox.JavaProjectBuilder
-
public class JavaProjectBuilder extends java.lang.Object
This is the improved version of the JavaDocBuilder of QDox 1.x, which has the following tasks:- Provide adders for all kind of resources, such as classloaders, java files and source directories
- Provide setters to enable the debug-mode for the Lexer and Parser (which are used when parsing sourcefiles) and the encoding
- Provide getter for retrieving Java Object Models from these libraries, such as JavaSources, JavaClasses and JavaPackages
- Provide a method to search through all the parsed JavaClasses
- Provide store and load methods for the JavaProjectBuilder
- Provide the option to set an ErrorHandler
SortedClassLibraryBuilder
, which means it doesn't matter in which order you add the resources, first all sources and sourcefolders, followed by the classloaders. Another implementation for the ClassLibraryBuilder is theOrderedClassLibraryBuilder
, which preserves the order in which resources are added. By creating a new JavaProjectBuilder with your own ClassLibraryBuilder you can decide which loading strategy should be used.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description JavaProjectBuilder()
Default constructor, which will use theSortedClassLibraryBuilder
implementation and add the default classloadersJavaProjectBuilder(ClassLibraryBuilder classLibraryBuilder)
Custom constructor, so another resource loading strategy can be defined
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addClassLoader(java.lang.ClassLoader classLoader)
Add theClassLoader
to this JavaProjectBuilderJavaSource
addSource(java.io.File file)
Add a java source from a file to this JavaProjectBuilderJavaSource
addSource(java.io.Reader reader)
Add a java source from a Reader to this JavaProjectBuilderJavaSource
addSource(java.net.URL url)
Add a java source from a URL to this JavaProjectBuilderJavaModule
addSourceFolder(java.io.File sourceFolder)
Add a sourcefolder to this javaprojectbuilder, but don't parse any file.void
addSourceTree(java.io.File directory)
Add all java files of thedirectory
recursivelyvoid
addSourceTree(java.io.File directory, FileVisitor errorHandler)
Add all java files of thedirectory
recursivelyJavaClass
getClassByName(java.lang.String name)
Try to retrieve aJavaClass
by its name.java.util.Collection<JavaClass>
getClasses()
Retrieve all classes which were added by sourcesjava.util.Collection<JavaModule>
getModules()
JavaPackage
getPackageByName(java.lang.String name)
Try to retrieve aJavaPackage
by its name.java.util.Collection<JavaPackage>
getPackages()
Retrieve all packages which were added by sources.java.util.Collection<JavaSource>
getSources()
Get all the sources added.static JavaProjectBuilder
load(java.io.File file)
Note that after loading JavaDocBuilder classloaders need to be re-added.void
save(java.io.File file)
Persist the classLibraryBuilder to a filejava.util.Collection<JavaClass>
search(Searcher searcher)
JavaProjectBuilder
setDebugLexer(boolean debugLexer)
Enable the debugmode for the LexerJavaProjectBuilder
setDebugParser(boolean debugParser)
Enable the debugmode for the ParserJavaProjectBuilder
setEncoding(java.lang.String encoding)
Sets the encoding when using Files or URL's to parse.JavaProjectBuilder
setErrorHandler(ErrorHandler errorHandler)
Sets the errorHandler which will be triggered when a parse exception occurs.
-
-
-
Constructor Detail
-
JavaProjectBuilder
public JavaProjectBuilder()
Default constructor, which will use theSortedClassLibraryBuilder
implementation and add the default classloaders
-
JavaProjectBuilder
public JavaProjectBuilder(ClassLibraryBuilder classLibraryBuilder)
Custom constructor, so another resource loading strategy can be defined- Parameters:
classLibraryBuilder
- custom implementation ofClassLibraryBuilder
-
-
Method Detail
-
setDebugLexer
public JavaProjectBuilder setDebugLexer(boolean debugLexer)
Enable the debugmode for the Lexer- Parameters:
debugLexer
-true
to enable,false
to disable- Returns:
- this javaProjectBuilder itself
-
setDebugParser
public JavaProjectBuilder setDebugParser(boolean debugParser)
Enable the debugmode for the Parser- Parameters:
debugParser
-true
to enable,false
to disable- Returns:
- this javaProjectBuilder itself
-
setEncoding
public JavaProjectBuilder setEncoding(java.lang.String encoding)
Sets the encoding when using Files or URL's to parse.- Parameters:
encoding
- the encoding to use forFile
orURL
- Returns:
- this javaProjectBuilder itself
-
setErrorHandler
public JavaProjectBuilder setErrorHandler(ErrorHandler errorHandler)
Sets the errorHandler which will be triggered when a parse exception occurs.- Parameters:
errorHandler
- the errorHandler- Returns:
- this javaProjectBuilder itself
-
addSource
public JavaSource addSource(java.io.File file) throws java.io.IOException
Add a java source from a file to this JavaProjectBuilder- Parameters:
file
- a java file- Returns:
- the
JavaSource
of the parsed file - Throws:
java.io.IOException
- if file is a directory or can't be read
-
addSource
public JavaSource addSource(java.net.URL url) throws java.io.IOException
Add a java source from a URL to this JavaProjectBuilder- Parameters:
url
- the url- Returns:
- the
JavaSource
of the parsed file - Throws:
java.io.IOException
- if the url can't be read
-
addSource
public JavaSource addSource(java.io.Reader reader)
Add a java source from a Reader to this JavaProjectBuilder- Parameters:
reader
- the reader- Returns:
- the
JavaSource
of the parsed reader content
-
addSourceFolder
public JavaModule addSourceFolder(java.io.File sourceFolder)
Add a sourcefolder to this javaprojectbuilder, but don't parse any file. This is a lazy parser. Only if a JavaClass is called it will be searched by matching the package with the folder structure and the classname with the filename- Parameters:
sourceFolder
- the sourcefolder to add- Returns:
- the
JavaModule
when the sourceFolder has a module-info, otherwisenull
- See Also:
addSourceTree(File)
-
addSourceTree
public void addSourceTree(java.io.File directory)
Add all java files of thedirectory
recursively- Parameters:
directory
- the directory from which all java files should be parsed.
-
addSourceTree
public void addSourceTree(java.io.File directory, FileVisitor errorHandler)
Add all java files of thedirectory
recursively- Parameters:
directory
- the directory from which all java files should be parsed.errorHandler
- a fileVisitor which will be triggered when anIOException
occurs.
-
addClassLoader
public void addClassLoader(java.lang.ClassLoader classLoader)
Add theClassLoader
to this JavaProjectBuilder- Parameters:
classLoader
- the classloader to add
-
getClassByName
public JavaClass getClassByName(java.lang.String name)
Try to retrieve aJavaClass
by its name.- Parameters:
name
- the fully qualified name of the class- Returns:
- the matching JavaClass, otherwise
null
-
getSources
public java.util.Collection<JavaSource> getSources()
Get all the sources added. This will only contain the sources added as sourcefile, sourcetree or sourcefolder.- Returns:
- a list of sources
- See Also:
addSource(File)
,addSource(Reader)
,addSourceFolder(File)
,addSourceTree(File)
-
getClasses
public java.util.Collection<JavaClass> getClasses()
Retrieve all classes which were added by sources- Returns:
- a list of javaclasses, never
null
- See Also:
addSource(File)
,addSource(Reader)
,addSourceFolder(File)
,addSourceTree(File)
-
getPackageByName
public JavaPackage getPackageByName(java.lang.String name)
Try to retrieve aJavaPackage
by its name.- Parameters:
name
- the package name- Returns:
- the matching JavaPackage, otherwise
null
-
getPackages
public java.util.Collection<JavaPackage> getPackages()
Retrieve all packages which were added by sources.- Returns:
- a list of packages, never
null
- See Also:
addSource(File)
,addSource(Reader)
,addSourceFolder(File)
,addSourceTree(File)
-
getModules
public java.util.Collection<JavaModule> getModules()
-
save
public void save(java.io.File file) throws java.io.IOException
Persist the classLibraryBuilder to a file- Parameters:
file
- the file to serialize to- Throws:
java.io.IOException
- Any exception thrown by the underlying OutputStream
-
load
public static JavaProjectBuilder load(java.io.File file) throws java.io.IOException
Note that after loading JavaDocBuilder classloaders need to be re-added.- Parameters:
file
- the file to load- Returns:
- the deserialized project builder
- Throws:
java.io.IOException
- when file could not be deserialized
-
-