Class SourceRoot
java.lang.Object
com.github.javaparser.utils.SourceRoot
A collection of Java source files located in one directory and its subdirectories on the file system. The root directory
corresponds to the root of the package structure of the source files within. Files can be parsed and written back one
by one or all together. Note that the internal cache used is thread-safe.
- methods called "tryToParse..." will return their result inside a "ParseResult", which supports parse successes and failures.
- methods called "parse..." will return "CompilationUnit"s. If a file fails to parse, an exception is thrown.
- methods ending in "...Parallelized" will speed up parsing by using multiple threads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceprivate static classExecutes a recursive file tree walk using threads. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<Path, ParseResult<CompilationUnit>> private static final Patternprivate ParserConfigurationprivate Function<CompilationUnit, String> private final Path -
Constructor Summary
ConstructorsConstructorDescriptionSourceRoot(Path root) SourceRoot(Path root, ParserConfiguration parserConfiguration) -
Method Summary
Modifier and TypeMethodDescriptionadd(CompilationUnit compilationUnit) Add a newly created Java file to the cache of this source root.add(String startPackage, String filename, CompilationUnit compilationUnit) Add a newly created Java file to the cache of this source root.private FileVisitResultcallback(Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback) getCache()The Java files that have been parsed by this source root object, or have been added manually.The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been added manually.Get the printing function.getRoot()The path that was passed in the constructor.(package private) booleanprivate voidlogPackage(String startPackage) parse(String startPackage, ParserConfiguration configuration, SourceRoot.Callback callback) Tries to parse all .java files in a package recursively and passes them one by one to the callback.parse(String startPackage, SourceRoot.Callback callback) Parses a .java files under the source root and returns its CompilationUnit.parse(String startPackage, String filename, ParserConfiguration configuration, SourceRoot.Callback callback) Locates the .java file with the provided package and file name, parses it and passes it to the callback.parse(String startPackage, String filename, SourceRoot.Callback callback) Parses the provided .java file and passes it to the callback.parseParallelized(SourceRoot.Callback callback) Tries to parse all .java files recursively using multiple threads, and passes them one by one to the callback.parseParallelized(String startPackage, ParserConfiguration configuration, 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.parseParallelized(String startPackage, 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.(package private) PathresolvePath(Path newRoot, Path cachedPath) Resolves and normalizes a cached path using java.nio.file.Path.resolve semantics.private SourceRootsave(CompilationUnit cu, Path path) Save the given compilation unit to the given path.private SourceRootsave(CompilationUnit cu, Path path, Charset encoding) Save the given compilation unit to the given path.saveAll()Save all previously parsed files back to where they were found.Save all previously parsed files back to where they were found, with the given encoding.Save all previously parsed files back to a new path.Saves all cached compilation units to the specified root directory.setParserConfiguration(ParserConfiguration parserConfiguration) Set the parser configuration that is used for parsing when no configuration is passed to a method.setPrinter(Function<CompilationUnit, String> printer) Set the printing function that transforms compilation units into a string to save.toString()Tries to parse all .java files under the source root recursively, and returns all files ever parsed with this source root.tryToParse(String startPackage) Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source root.tryToParse(String startPackage, String filename) Tries to parse a .java files under the source root and returns the ParseResult.tryToParse(String startPackage, String filename, ParserConfiguration configuration) Tries to parse a .java files under the source root and returns the ParseResult.Tries to parse all .java files under the source root recursively using multiple threads, and returns all files ever parsed with this source root.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.
-
Field Details
-
root
-
cache
-
parserConfiguration
-
printer
-
JAVA_IDENTIFIER
-
-
Constructor Details
-
SourceRoot
- Parameters:
root- the root directory of a set of source files. It corresponds to the root of the package structure of the source files within, like "javaparser/javaparser-core/src/main/java"
-
SourceRoot
- Parameters:
root- the root directory of a set of source files. It corresponds to the root of the package structure of the source files within, like "javaparser/javaparser-core/src/main/java"
-
-
Method Details
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(String startPackage, String filename, ParserConfiguration configuration) 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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
IOException
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(String startPackage, 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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
IOException
-
tryToParse
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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
IOException
-
isSensibleDirectoryToEnter
- Throws:
IOException
-
tryToParse
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
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.setParserConfiguration(ParserConfiguration)) 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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.
-
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. 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 (i.e.setParserConfiguration(ParserConfiguration)) 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. -
parse
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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
ParseProblemException- when something went wrong.
-
callback
private FileVisitResult callback(Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback) throws IOException - Throws:
IOException
-
parse
public SourceRoot parse(String startPackage, String filename, ParserConfiguration configuration, SourceRoot.Callback callback) throws IOException Locates the .java file with the provided package and file name, parses it and passes it to the callback. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Parameters:
startPackage- The package containing the filefilename- The name of the file- Throws:
IOException
-
parse
public SourceRoot parse(String startPackage, String filename, SourceRoot.Callback callback) throws IOException Parses the provided .java file and passes it to the callback. In comparison to the other parse methods, this makes is much more memory efficient., but saveAll() won't work.- Throws:
IOException
-
parse
public SourceRoot parse(String startPackage, ParserConfiguration configuration, 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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
IOException
-
parse
- Throws:
IOException
-
logPackage
-
parseParallelized
public SourceRoot parseParallelized(String startPackage, ParserConfiguration configuration, 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. 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.Callbackcode must be made thread-safe. Note that to ensure thread safety, a new parser instance is created for every file with the providedParserConfiguration. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.
-
parseParallelized
public SourceRoot parseParallelized(String startPackage, 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.Callbackcode must be made thread-safe. Note that to ensure thread safety, a new parser instance is created for every file. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
IOException
-
parseParallelized
Tries to parse all .java files 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.Callbackcode must be made thread-safe. Note that to ensure thread safety, a new parser instance is created for every file. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.- Throws:
IOException
-
add
Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.
-
add
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. -
save
Save the given compilation unit to the given path.- Parameters:
cu- the compilation unitpath- the path of the java file
-
save
Save the given compilation unit to the given path.- Parameters:
cu- the compilation unitpath- the path of the java fileencoding- the encoding to use while saving the file
-
saveAll
Saves all cached compilation units to the specified root directory. Path resolution follows java.nio.file.Path.resolve semantics: - Relative paths are resolved against the provided root. - Absolute paths remain unchanged (only normalized).- Parameters:
root- the destination root directoryencoding- the character encoding used when writing files- Returns:
- this SourceRoot instance
- Throws:
NullPointerException- if root is null
-
resolvePath
Resolves and normalizes a cached path using java.nio.file.Path.resolve semantics. Rules: - If cachedPath is relative, it is resolved under newRoot. - If cachedPath is absolute, it is returned unchanged (only normalized).- Parameters:
newRoot- the root directory used for resolutioncachedPath- the original cached path (relative or absolute)- Returns:
- the resolved and normalized target path
- Throws:
NullPointerException- if either argument is null
-
saveAll
Save all previously parsed files back to a new path.- Parameters:
root- the root of the java packages
-
saveAll
Save all previously parsed files back to where they were found. -
saveAll
Save all previously parsed files back to where they were found, with the given encoding.- Parameters:
encoding- the encoding to use.
-
getCache
The Java files that have been parsed by this source root object, or have been added manually. -
getCompilationUnits
The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been added manually. -
getRoot
The path that was passed in the constructor. -
getParserConfiguration
-
setParserConfiguration
Set the parser configuration that is used for parsing when no configuration is passed to a method. -
setPrinter
Set the printing function that transforms compilation units into a string to save. -
getPrinter
Get the printing function. -
toString
-