Package com.github.javaparser.utils
Class SourceRoot
- java.lang.Object
-
- com.github.javaparser.utils.SourceRoot
-
public class SourceRoot extends Object
A collection of Java source files located in one directory and its subdirectories on the file system. Files can be parsed and written back one by one or all together. Note that the internal cache used is thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
SourceRoot.Callback
-
Constructor Summary
Constructors Constructor Description SourceRoot(Path root)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SourceRoot
add(CompilationUnit compilationUnit)
Add a newly created Java file to the cache of this source root.SourceRoot
add(String pkg, String filename, CompilationUnit compilationUnit)
Add a newly created Java file to the cache of this source root.List<ParseResult<CompilationUnit>>
getCache()
The Java files that have been parsed by this source root object, or have been added manually.List<CompilationUnit>
getCompilationUnits()
The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been added manually.JavaParser
getJavaParser()
Function<CompilationUnit,String>
getPrinter()
Get the printing function.Path
getRoot()
The path that was passed in the constructor.SourceRoot
parse(String startPackage, JavaParser javaParser, SourceRoot.Callback callback)
Tries to parse all .java files in a package recursively and passes them one by one to the callback.CompilationUnit
parse(String pkg, String filename)
Parses a .java files under the source root and returns its CompilationUnit.SourceRoot
parseParallelized(String startPackage, JavaParser javaParser, SourceRoot.Callback callback)
Tries to parse all .java files in a package recursively using multiple threads, and passes them one by one to the callback.SourceRoot
saveAll()
Save all previously parsed files back to where they were found.SourceRoot
saveAll(Path root)
Save all previously parsed files back to a new path.SourceRoot
setJavaParser(JavaParser javaParser)
Set the parser that is used for parsing by default.SourceRoot
setPrinter(Function<CompilationUnit,String> printer)
Set the printing function that transforms compilation units into a string to save.List<ParseResult<CompilationUnit>>
tryToParse()
Tries to parse all .java files under the source root recursively, and returns all files ever parsed with this source root.List<ParseResult<CompilationUnit>>
tryToParse(String startPackage)
Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source root.ParseResult<CompilationUnit>
tryToParse(String pkg, String filename)
Tries to parse a .java files under the source root and returns the ParseResult.ParseResult<CompilationUnit>
tryToParse(String pkg, String filename, JavaParser javaParser)
Tries to parse a .java files under the source root and returns the ParseResult.List<ParseResult<CompilationUnit>>
tryToParseParallelized()
Tries to parse all .java files under the source root recursively using multiple threads, and returns all files ever parsed with this source root.List<ParseResult<CompilationUnit>>
tryToParseParallelized(String startPackage)
Tries to parse all .java files in a package recursively using multiple threads, and returns all files ever parsed with this source root.
-
-
-
Constructor Detail
-
SourceRoot
public SourceRoot(Path root)
-
-
Method Detail
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(String pkg, String filename, JavaParser javaParser) throws IOException
Tries to parse a .java files under the source root and returns the ParseResult. It keeps track of the parsed file so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(String pkg, String filename) throws IOException
Tries to parse a .java files under the source root and returns the ParseResult. It keeps track of the parsed file so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
tryToParse
public List<ParseResult<CompilationUnit>> tryToParse(String startPackage) throws IOException
Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source root. It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
tryToParse
public List<ParseResult<CompilationUnit>> tryToParse() throws IOException
Tries to parse all .java files under the source root recursively, and returns all files ever parsed with this source root. It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
tryToParseParallelized
public List<ParseResult<CompilationUnit>> tryToParseParallelized(String startPackage) throws IOException
Tries to parse all .java files in a package recursively using multiple threads, and returns all files ever parsed with this source root. A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in that directory. Note that to ensure thread safety, a new parser instance is created for every file with the internal parser's (i.e.setJavaParser(com.github.javaparser.JavaParser)
) configuration. It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
tryToParseParallelized
public List<ParseResult<CompilationUnit>> tryToParseParallelized() throws IOException
Tries to parse all .java files under the source root recursively using multiple threads, and returns all files ever parsed with this source root. A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in that directory. Note that to ensure thread safety, a new parser instance is created for every file with the internal parser's (i.e.setJavaParser(com.github.javaparser.JavaParser)
) configuration. It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
IOException
-
parse
public CompilationUnit parse(String pkg, String filename)
Parses a .java files under the source root and returns its CompilationUnit. It keeps track of the parsed file so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use the parse method with a callback.- Throws:
ParseProblemException
- when something went wrong.
-
parse
public SourceRoot parse(String startPackage, JavaParser javaParser, SourceRoot.Callback callback) throws IOException
Tries to parse all .java files in a package recursively and passes them one by one to the callback. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Throws:
IOException
-
parseParallelized
public SourceRoot parseParallelized(String startPackage, JavaParser javaParser, SourceRoot.Callback callback) throws IOException
Tries to parse all .java files in a package recursively using multiple threads, and passes them one by one to the callback. A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in that directory. Note that the providedSourceRoot.Callback
code must be made thread-safe. Note that to ensure thread safety, a new parser instance is created for every file with the providedJavaParser
's configuration. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Throws:
IOException
-
add
public SourceRoot add(String pkg, String filename, CompilationUnit compilationUnit)
Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called.
-
add
public SourceRoot add(CompilationUnit compilationUnit)
Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called. It needs to have its path set.
-
saveAll
public SourceRoot saveAll(Path root)
Save all previously parsed files back to a new path.
-
saveAll
public SourceRoot saveAll()
Save all previously parsed files back to where they were found.
-
getCache
public List<ParseResult<CompilationUnit>> getCache()
The Java files that have been parsed by this source root object, or have been added manually.
-
getCompilationUnits
public List<CompilationUnit> getCompilationUnits()
The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been added manually.
-
getRoot
public Path getRoot()
The path that was passed in the constructor.
-
getJavaParser
public JavaParser getJavaParser()
-
setJavaParser
public SourceRoot setJavaParser(JavaParser javaParser)
Set the parser that is used for parsing by default.
-
setPrinter
public SourceRoot setPrinter(Function<CompilationUnit,String> printer)
Set the printing function that transforms compilation units into a string to save.
-
getPrinter
public Function<CompilationUnit,String> getPrinter()
Get the printing function.
-
-