Search in sources :

Example 26 with CompilationUnit

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;
}
Also used : CodeGenerationUtils.fileInPackageRelativePath(com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath) Path(java.nio.file.Path) CodeGenerationUtils.packageAbsolutePath(com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ParseResult(com.github.javaparser.ParseResult)

Example 27 with CompilationUnit

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;
}
Also used : CodeGenerationUtils.fileInPackageRelativePath(com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath) Path(java.nio.file.Path) CodeGenerationUtils.packageAbsolutePath(com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ParseResult(com.github.javaparser.ParseResult)

Example 28 with CompilationUnit

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;
}
Also used : CodeGenerationUtils.fileInPackageRelativePath(com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath) Path(java.nio.file.Path) CodeGenerationUtils.packageAbsolutePath(com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath) CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser)

Example 29 with CompilationUnit

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));
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Navigator.getParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) Node(com.github.javaparser.ast.Node) File(java.io.File)

Example 30 with CompilationUnit

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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)489 Test (org.junit.Test)304 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)160 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)140 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)128 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)101 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)70 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)66 Context (com.github.javaparser.symbolsolver.core.resolution.Context)62 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)55 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 File (java.io.File)39 Expression (com.github.javaparser.ast.expr.Expression)38 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 MethodUsage (com.github.javaparser.resolution.MethodUsage)34 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)33 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)29 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)29 ArrayList (java.util.ArrayList)29