Top Description Inners Fields Constructors Methods
com.github.javaparser.utils

public Class SourceRoot

extends Object
Class Inheritance
Imports
com.github.javaparser.JavaParser, .ParseProblemException, .ParseResult, .ParserConfiguration, com.github.javaparser.ast.CompilationUnit, com.github.javaparser.printer.PrettyPrinter, java.io.IOException, java.nio.charset.Charset, java.nio.file.FileVisitResult, .Files, .Path, .SimpleFileVisitor, java.nio.file.attribute.BasicFileAttributes, java.util.ArrayList, .List, .Map, java.util.concurrent.ConcurrentHashMap, .ForkJoinPool, .RecursiveAction, java.util.function.Function, java.util.regex.Pattern, java.util.stream.Collectors

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
private static class
SourceRoot.ParallelParse

Executes a recursive file tree walk using threads.

Field Summary

Modifier and TypeField and Description
private final Map<Path, ParseResult<CompilationUnit>>
private static final Pattern
private ParserConfiguration
private Function<CompilationUnit, String>
private final Path

Constructor Summary

AccessConstructor and Description
public
SourceRoot(Path
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"
root
)

public
SourceRoot(Path
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"
root
,
ParserConfiguration parserConfiguration)

Method Summary

Modifier and TypeMethod and Description
public SourceRoot
add(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
,
String filename, CompilationUnit compilationUnit)

Add a newly created Java file to the cache of this source root.

public SourceRoot
add(CompilationUnit compilationUnit)

Add a newly created Java file to the cache of this source root.

private FileVisitResult
callback(Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback)

public List<ParseResult<CompilationUnit>>
getCache()

The Java files that have been parsed by this source root object, or have been added manually.

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.

public ParserConfiguration
public Function<CompilationUnit, String>
getPrinter()

Get the printing function.

public Path
getRoot()

The path that was passed in the constructor.

private static boolean
private void
logPackage(String startPackage)

public CompilationUnit
parse(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
,
String filename)

Parses a .java files under the source root and returns its CompilationUnit.

public SourceRoot
parse(String
The package containing the file
startPackage
,
String
The name of the file
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.

public SourceRoot
parse(String startPackage, String filename, SourceRoot.Callback callback)

Parses the provided .java file and passes it to the callback.

public SourceRoot
parse(String
files in this package and deeper are parsed. Pass "" to parse all files.
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.

public SourceRoot
parse(String startPackage, SourceRoot.Callback callback)

public SourceRoot
parseParallelized(String
files in this package and deeper are parsed. Pass "" to parse all files.
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.

public SourceRoot
parseParallelized(String
files in this package and deeper are parsed. Pass "" to parse all files.
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.

public SourceRoot
parseParallelized(SourceRoot.Callback callback)

Tries to parse all .java files recursively using multiple threads, and passes them one by one to the callback.

private SourceRoot
save(CompilationUnit
the compilation unit
cu
,
Path
the path of the java file
path
)

Save the given compilation unit to the given path.

private SourceRoot
save(CompilationUnit
the compilation unit
cu
,
Path
the path of the java file
path
,
Charset
the encoding to use while saving the file
encoding
)

Save the given compilation unit to the given path.

public SourceRoot
saveAll(Path
the root of the java packages
root
,
Charset
the encoding to use while saving the file
encoding
)

Save all previously parsed files back to a new path.

public SourceRoot
saveAll(Path
the root of the java packages
root
)

Save all previously parsed files back to a new path.

public SourceRoot
saveAll()

Save all previously parsed files back to where they were found.

public SourceRoot
saveAll(Charset
the encoding to use.
encoding
)

Save all previously parsed files back to where they were found, with the given encoding.

public SourceRoot
setParserConfiguration(ParserConfiguration parserConfiguration)

Set the parser configuration that is used for parsing when no configuration is passed to a method.

public SourceRoot
setPrinter(Function<CompilationUnit, String> printer)

Set the printing function that transforms compilation units into a string to save.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public ParseResult<CompilationUnit>
tryToParse(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
,
String filename, ParserConfiguration configuration)

Tries to parse a .java files under the source root and returns the ParseResult.

public ParseResult<CompilationUnit>
tryToParse(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
,
String filename)

Tries to parse a .java files under the source root and returns the ParseResult.

public List<ParseResult<CompilationUnit>>
tryToParse(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
)

Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source root.

public 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.

public List<ParseResult<CompilationUnit>>
tryToParseParallelized(String
files in this package and deeper are parsed. Pass "" to parse all files.
startPackage
)

Tries to parse all .java files in a package recursively using multiple threads, and returns all files ever parsed with this source root.

public 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cacheback to summary
private final Map<Path, ParseResult<CompilationUnit>> cache
JAVA_IDENTIFIERback to summary
private static final Pattern JAVA_IDENTIFIER
parserConfigurationback to summary
private ParserConfiguration parserConfiguration
printerback to summary
private Function<CompilationUnit, String> printer
rootback to summary
private final Path root

Constructor Detail

SourceRootback to summary
public SourceRoot(Path root)
Parameters
root:Path

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"

SourceRootback to summary
public SourceRoot(Path root, ParserConfiguration parserConfiguration)
Parameters
root:Path

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

addback to summary
public SourceRoot add(String startPackage, 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:String

files in this package and deeper are parsed. Pass "" to parse all files.

addback to summary
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.

callbackback to summary
private FileVisitResult callback(Path absolutePath, ParserConfiguration configuration, SourceRoot.Callback callback) throws IOException
getCacheback to summary
public List<ParseResult<CompilationUnit>> getCache()

The Java files that have been parsed by this source root object, or have been added manually.

getCompilationUnitsback to summary
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.

getParserConfigurationback to summary
public ParserConfiguration getParserConfiguration()
getPrinterback to summary
public Function<CompilationUnit, String> getPrinter()

Get the printing function.

getRootback to summary
public Path getRoot()

The path that was passed in the constructor.

isSensibleDirectoryToEnterback to summary
private static boolean isSensibleDirectoryToEnter(Path dir) throws IOException
logPackageback to summary
private void logPackage(String startPackage)
parseback to summary
public CompilationUnit parse(String startPackage, 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:String

files in this package and deeper are parsed. Pass "" to parse all files.

Exceptions
ParseProblemException:
when something went wrong.
parseback to summary
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:String

The package containing the file

filename:String

The name of the file

parseback to summary
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.

parseback to summary
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:String

files in this package and deeper are parsed. Pass "" to parse all files.

parseback to summary
public SourceRoot parse(String startPackage, SourceRoot.Callback callback) throws IOException
parseParallelizedback to summary
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 provided Callback code must be made thread-safe. Note that to ensure thread safety, a new parser instance is created for every file with the provided ParserConfiguration. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work.

Parameters
startPackage:String

files in this package and deeper are parsed. Pass "" to parse all files.

parseParallelizedback to summary
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 provided Callback code 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:String

files in this package and deeper are parsed. Pass "" to parse all files.

parseParallelizedback to summary
public SourceRoot parseParallelized(SourceRoot.Callback callback) throws 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 provided Callback code 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.

saveback to summary
private SourceRoot save(CompilationUnit cu, Path path)

Save the given compilation unit to the given path.

Parameters
cu:CompilationUnit

the compilation unit

path:Path

the path of the java file

saveback to summary
private SourceRoot save(CompilationUnit cu, Path path, Charset encoding)

Save the given compilation unit to the given path.

Parameters
cu:CompilationUnit

the compilation unit

path:Path

the path of the java file

encoding:Charset

the encoding to use while saving the file

saveAllback to summary
public SourceRoot saveAll(Path root, Charset encoding)

Save all previously parsed files back to a new path.

Parameters
root:Path

the root of the java packages

encoding:Charset

the encoding to use while saving the file

saveAllback to summary
public SourceRoot saveAll(Path root)

Save all previously parsed files back to a new path.

Parameters
root:Path

the root of the java packages

saveAllback to summary
public SourceRoot saveAll()

Save all previously parsed files back to where they were found.

saveAllback to summary
public SourceRoot saveAll(Charset encoding)

Save all previously parsed files back to where they were found, with the given encoding.

Parameters
encoding:Charset

the encoding to use.

setParserConfigurationback to summary
public SourceRoot setParserConfiguration(ParserConfiguration parserConfiguration)

Set the parser configuration that is used for parsing when no configuration is passed to a method.

setPrinterback to summary
public SourceRoot setPrinter(Function<CompilationUnit, String> printer)

Set the printing function that transforms compilation units into a string to save.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
tryToParseback to summary
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:String

files in this package and deeper are parsed. Pass "" to parse all files.

tryToParseback to summary
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:String

files in this package and deeper are parsed. Pass "" to parse all files.

tryToParseback to summary
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.

Parameters
startPackage:String

files in this package and deeper are parsed. Pass "" to parse all files.

tryToParseback to summary
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.

tryToParseParallelizedback to summary
public 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. 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:String

files in this package and deeper are parsed. Pass "" to parse all files.

tryToParseParallelizedback to summary
public 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.

com.github.javaparser.utils back to summary

public Interface SourceRoot.Callback

Annotations
@FunctionalInterface

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Method Summary

Modifier and TypeMethod and Description
public SourceRoot.Callback.Result
process(Path
the path to the file that was parsed, relative to the source root path.
localPath
,
Path
the absolute path to the file that was parsed.
absolutePath
,
ParseResult<CompilationUnit>
the result of of parsing the file.
result
)

Method Detail

processback to summary
public SourceRoot.Callback.Result process(Path localPath, Path absolutePath, ParseResult<CompilationUnit> result)
Parameters
localPath:Path

the path to the file that was parsed, relative to the source root path.

absolutePath:Path

the absolute path to the file that was parsed.

result:ParseResult<CompilationUnit>

the result of of parsing the file.

com.github.javaparser.utils back to summary

public final Enum SourceRoot.Callback.Result

extends Enum<SourceRoot.Callback.Result>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final SourceRoot.Callback.Result
public static final SourceRoot.Callback.Result
public static final SourceRoot.Callback.Result

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static SourceRoot.Callback.Result
public static SourceRoot.Callback.Result[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DONT_SAVEback to summary
public static final SourceRoot.Callback.Result DONT_SAVE
SAVEback to summary
public static final SourceRoot.Callback.Result SAVE
TERMINATEback to summary
public static final SourceRoot.Callback.Result TERMINATE

Constructor Detail

Resultback to summary
private Result()

Method Detail

valueOfback to summary
public static SourceRoot.Callback.Result valueOf(String name)
valuesback to summary
public static SourceRoot.Callback.Result[] values()
com.github.javaparser.utils back to summary

private Class SourceRoot.ParallelParse

extends RecursiveAction
Class Inheritance

Executes a recursive file tree walk using threads. A new thread is invoked for each new directory discovered during the walk. For each file visited, the user-provided VisitFileCallback is called with the current path and file attributes. Any shared resources accessed in a VisitFileCallback should be made thread-safe.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface

Field Summary

Modifier and TypeField and Description
private final SourceRoot.ParallelParse.VisitFileCallback
private final Path
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected void
compute()

Implements abstract java.util.concurrent.RecursiveAction.compute.

The main computation performed by this task.
Inherited from java.util.concurrent.RecursiveAction:
execgetRawResultsetRawResult

Field Detail

callbackback to summary
private final SourceRoot.ParallelParse.VisitFileCallback callback
pathback to summary
private final Path path
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.RecursiveAction.serialVersionUID.

Constructor Detail

ParallelParseback to summary
pack-priv ParallelParse(Path path, SourceRoot.ParallelParse.VisitFileCallback callback)

Method Detail

computeback to summary
protected void compute()

Implements abstract java.util.concurrent.RecursiveAction.compute.

Doc from java.util.concurrent.RecursiveAction.compute.

The main computation performed by this task.

Annotations
@Override
com.github.javaparser.utils back to summary

pack-priv Interface SourceRoot.ParallelParse.VisitFileCallback


Method Summary

Modifier and TypeMethod and Description
public FileVisitResult

Method Detail

processback to summary
public FileVisitResult process(Path file, BasicFileAttributes attrs)