use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class SourceRoot method 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.
*/
public SourceRoot add(CompilationUnit compilationUnit) {
assertNotNull(compilationUnit);
if (compilationUnit.getStorage().isPresent()) {
final Path path = compilationUnit.getStorage().get().getPath();
Log.trace("Adding new file %s", path);
final ParseResult<CompilationUnit> parseResult = new ParseResult<>(compilationUnit, new ArrayList<>(), null, null);
cache.put(path, parseResult);
} else {
throw new AssertionError("Files added with this method should have their path set.");
}
return this;
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class SourceRoot method add.
/**
* Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called.
*
* @param startPackage files in this package and deeper are parsed. Pass "" to parse all files.
*/
public SourceRoot add(String startPackage, String filename, CompilationUnit compilationUnit) {
assertNotNull(startPackage);
assertNotNull(filename);
assertNotNull(compilationUnit);
Log.trace("Adding new file %s.%s", startPackage, filename);
final Path path = fileInPackageRelativePath(startPackage, filename);
final ParseResult<CompilationUnit> parseResult = new ParseResult<>(compilationUnit, new ArrayList<>(), null, null);
cache.put(path, parseResult);
return this;
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class SourceRoot method tryToParse.
/**
* 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.
*
* @param startPackage files in this package and deeper are parsed. Pass "" to parse all files.
*/
public ParseResult<CompilationUnit> tryToParse(String startPackage, String filename, ParserConfiguration configuration) throws IOException {
assertNotNull(startPackage);
assertNotNull(filename);
final Path relativePath = fileInPackageRelativePath(startPackage, filename);
if (cache.containsKey(relativePath)) {
Log.trace("Retrieving cached %s", relativePath);
return cache.get(relativePath);
}
final Path path = root.resolve(relativePath);
Log.trace("Parsing %s", path);
final ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(path));
result.getResult().ifPresent(cu -> cu.setStorage(path));
cache.put(relativePath, result);
return result;
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class SourceFileInfoExtractor method solve.
public void solve(File file) throws IOException, ParseException {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
solve(f);
}
} else {
if (file.getName().endsWith(".java")) {
if (printFileName) {
out.println("- parsing " + file.getAbsolutePath());
}
CompilationUnit cu = JavaParser.parse(file);
List<Node> nodes = collectAllNodes(cu);
nodes.forEach(n -> solve(n));
}
}
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class SourceZip method parse.
/**
* Tries to parse all '.java' files in the ZIP located at this <i>SourceZip</i>'s path and returns the parse
* results in a list.
*
* @return A list of path-compilation unit pairs.
*
* @throws IOException If an error occurs while trying to parse the given source.
*/
public SourceZip parse(Callback callback) throws IOException {
Log.info("Parsing zip at \"%s\"", zipPath);
JavaParser javaParser = new JavaParser(parserConfiguration);
try (ZipFile zipFile = new ZipFile(zipPath.toFile())) {
for (ZipEntry entry : Collections.list(zipFile.entries())) {
if (!entry.isDirectory() && entry.getName().endsWith(".java")) {
Log.info("Parsing zip entry \"%s\"", entry.getName());
final ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(zipFile.getInputStream(entry)));
callback.process(Paths.get(entry.getName()), result);
}
}
}
return this;
}
Aggregations