Package com.github.javaparser.utils
Class SourceRoot
- java.lang.Object
-
- com.github.javaparser.utils.SourceRoot
-
public class SourceRoot extends java.lang.ObjectA 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 Classes Modifier and Type Class Description static interfaceSourceRoot.Callbackprivate static classSourceRoot.ParallelParseExecutes a recursive file tree walk using threads.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.nio.file.Path,ParseResult<CompilationUnit>>cacheprivate static java.util.regex.PatternJAVA_IDENTIFIERprivate ParserConfigurationparserConfigurationprivate java.util.function.Function<CompilationUnit,java.lang.String>printerprivate java.nio.file.Pathroot
-
Constructor Summary
Constructors Constructor Description SourceRoot(java.nio.file.Path root)SourceRoot(java.nio.file.Path root, ParserConfiguration parserConfiguration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SourceRootadd(CompilationUnit compilationUnit)Add a newly created Java file to the cache of this source root.SourceRootadd(java.lang.String startPackage, java.lang.String filename, CompilationUnit compilationUnit)Add a newly created Java file to the cache of this source root.private java.nio.file.FileVisitResultcallback(java.nio.file.Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback)java.util.List<ParseResult<CompilationUnit>>getCache()The Java files that have been parsed by this source root object, or have been added manually.java.util.List<CompilationUnit>getCompilationUnits()The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been added manually.ParserConfigurationgetParserConfiguration()java.util.function.Function<CompilationUnit,java.lang.String>getPrinter()Get the printing function.java.nio.file.PathgetRoot()The path that was passed in the constructor.(package private) booleanisSensibleDirectoryToEnter(java.nio.file.Path dir)private voidlogPackage(java.lang.String startPackage)SourceRootparse(java.lang.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.SourceRootparse(java.lang.String startPackage, SourceRoot.Callback callback)CompilationUnitparse(java.lang.String startPackage, java.lang.String filename)Parses a .java files under the source root and returns its CompilationUnit.SourceRootparse(java.lang.String startPackage, java.lang.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.SourceRootparse(java.lang.String startPackage, java.lang.String filename, SourceRoot.Callback callback)Parses the provided .java file and passes it to the callback.SourceRootparseParallelized(SourceRoot.Callback callback)Tries to parse all .java files recursively using multiple threads, and passes them one by one to the callback.SourceRootparseParallelized(java.lang.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.SourceRootparseParallelized(java.lang.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.private SourceRootsave(CompilationUnit cu, java.nio.file.Path path)Save the given compilation unit to the given path.private SourceRootsave(CompilationUnit cu, java.nio.file.Path path, java.nio.charset.Charset encoding)Save the given compilation unit to the given path.SourceRootsaveAll()Save all previously parsed files back to where they were found.SourceRootsaveAll(java.nio.charset.Charset encoding)Save all previously parsed files back to where they were found, with the given encoding.SourceRootsaveAll(java.nio.file.Path root)Save all previously parsed files back to a new path.SourceRootsaveAll(java.nio.file.Path root, java.nio.charset.Charset encoding)Save all previously parsed files back to a new path.SourceRootsetParserConfiguration(ParserConfiguration parserConfiguration)Set the parser configuration that is used for parsing when no configuration is passed to a method.SourceRootsetPrinter(java.util.function.Function<CompilationUnit,java.lang.String> printer)Set the printing function that transforms compilation units into a string to save.java.lang.StringtoString()java.util.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.java.util.List<ParseResult<CompilationUnit>>tryToParse(java.lang.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(java.lang.String startPackage, java.lang.String filename)Tries to parse a .java files under the source root and returns the ParseResult.ParseResult<CompilationUnit>tryToParse(java.lang.String startPackage, java.lang.String filename, ParserConfiguration configuration)Tries to parse a .java files under the source root and returns the ParseResult.java.util.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.java.util.List<ParseResult<CompilationUnit>>tryToParseParallelized(java.lang.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 Detail
-
root
private final java.nio.file.Path root
-
cache
private final java.util.Map<java.nio.file.Path,ParseResult<CompilationUnit>> cache
-
parserConfiguration
private ParserConfiguration parserConfiguration
-
printer
private java.util.function.Function<CompilationUnit,java.lang.String> printer
-
JAVA_IDENTIFIER
private static final java.util.regex.Pattern JAVA_IDENTIFIER
-
-
Constructor Detail
-
SourceRoot
public SourceRoot(java.nio.file.Path root)
- 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
public SourceRoot(java.nio.file.Path root, ParserConfiguration parserConfiguration)- 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 Detail
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(java.lang.String startPackage, java.lang.String filename, ParserConfiguration configuration) throws java.io.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:
java.io.IOException
-
tryToParse
public ParseResult<CompilationUnit> tryToParse(java.lang.String startPackage, java.lang.String filename) throws java.io.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:
java.io.IOException
-
tryToParse
public java.util.List<ParseResult<CompilationUnit>> tryToParse(java.lang.String startPackage) throws java.io.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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
java.io.IOException
-
isSensibleDirectoryToEnter
boolean isSensibleDirectoryToEnter(java.nio.file.Path dir) throws java.io.IOException- Throws:
java.io.IOException
-
tryToParse
public java.util.List<ParseResult<CompilationUnit>> tryToParse() throws java.io.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:
java.io.IOException
-
tryToParseParallelized
public java.util.List<ParseResult<CompilationUnit>> tryToParseParallelized(java.lang.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. 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
public java.util.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. 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
public CompilationUnit parse(java.lang.String startPackage, java.lang.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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.- Throws:
ParseProblemException- when something went wrong.
-
callback
private java.nio.file.FileVisitResult callback(java.nio.file.Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback) throws java.io.IOException- Throws:
java.io.IOException
-
parse
public SourceRoot parse(java.lang.String startPackage, java.lang.String filename, ParserConfiguration configuration, SourceRoot.Callback callback) throws java.io.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:
java.io.IOException
-
parse
public SourceRoot parse(java.lang.String startPackage, java.lang.String filename, SourceRoot.Callback callback) throws java.io.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:
java.io.IOException
-
parse
public SourceRoot parse(java.lang.String startPackage, ParserConfiguration configuration, SourceRoot.Callback callback) throws java.io.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:
java.io.IOException
-
parse
public SourceRoot parse(java.lang.String startPackage, SourceRoot.Callback callback) throws java.io.IOException
- Throws:
java.io.IOException
-
logPackage
private void logPackage(java.lang.String startPackage)
-
parseParallelized
public SourceRoot parseParallelized(java.lang.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(java.lang.String startPackage, SourceRoot.Callback callback) throws java.io.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:
java.io.IOException
-
parseParallelized
public SourceRoot parseParallelized(SourceRoot.Callback callback) throws java.io.IOException
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:
java.io.IOException
-
add
public SourceRoot add(java.lang.String startPackage, java.lang.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.- Parameters:
startPackage- files in this package and deeper are parsed. Pass "" to parse all files.
-
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.
-
save
private SourceRoot save(CompilationUnit cu, java.nio.file.Path path)
Save the given compilation unit to the given path.- Parameters:
cu- the compilation unitpath- the path of the java file
-
save
private SourceRoot save(CompilationUnit cu, java.nio.file.Path path, java.nio.charset.Charset encoding)
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
public SourceRoot saveAll(java.nio.file.Path root, java.nio.charset.Charset encoding)
Save all previously parsed files back to a new path.- Parameters:
root- the root of the java packagesencoding- the encoding to use while saving the file
-
saveAll
public SourceRoot saveAll(java.nio.file.Path root)
Save all previously parsed files back to a new path.- Parameters:
root- the root of the java packages
-
saveAll
public SourceRoot saveAll()
Save all previously parsed files back to where they were found.
-
saveAll
public SourceRoot saveAll(java.nio.charset.Charset encoding)
Save all previously parsed files back to where they were found, with the given encoding.- Parameters:
encoding- the encoding to use.
-
getCache
public java.util.List<ParseResult<CompilationUnit>> getCache()
The Java files that have been parsed by this source root object, or have been added manually.
-
getCompilationUnits
public java.util.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 java.nio.file.Path getRoot()
The path that was passed in the constructor.
-
getParserConfiguration
public ParserConfiguration getParserConfiguration()
-
setParserConfiguration
public SourceRoot setParserConfiguration(ParserConfiguration parserConfiguration)
Set the parser configuration that is used for parsing when no configuration is passed to a method.
-
setPrinter
public SourceRoot setPrinter(java.util.function.Function<CompilationUnit,java.lang.String> printer)
Set the printing function that transforms compilation units into a string to save.
-
getPrinter
public java.util.function.Function<CompilationUnit,java.lang.String> getPrinter()
Get the printing function.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-