Java Code Examples for com.github.javaparser.ParseResult#isSuccessful()

The following examples show how to use com.github.javaparser.ParseResult#isSuccessful() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyShellCallback.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private String getNewJavaFile(String newFileSource, String existingFileFullPath) throws FileNotFoundException {
    JavaParser javaParser = new JavaParser();
    ParseResult<CompilationUnit> newCompilationUnitParse = javaParser.parse(newFileSource);
    CompilationUnit newCompilationUnit;
    if (newCompilationUnitParse.isSuccessful() && newCompilationUnitParse.getResult().isPresent()) {
        newCompilationUnit = newCompilationUnitParse.getResult().get();
    } else {
        log.error("解析 newFileSource 失败, {}", newCompilationUnitParse.getProblem(0).toString());
        return newFileSource;
    }

    ParseResult<CompilationUnit> existingCompilationUnitParse = javaParser.parse(new File(existingFileFullPath));
    CompilationUnit existingCompilationUnit;
    if (existingCompilationUnitParse.isSuccessful() && existingCompilationUnitParse.getResult().isPresent()) {
        existingCompilationUnit = existingCompilationUnitParse.getResult().get();
    } else {
        log.error("解析 existingFileFullPath 失败, {}", existingCompilationUnitParse.getProblem(0).toString());
        return newFileSource;
    }
    return mergerFile(newCompilationUnit, existingCompilationUnit);
}
 
Example 2
Source File: Apigcc.java    From apigcc with MIT License 6 votes vote down vote up
/**
 * 解析源代码
 *
 * @return
 */
public Project parse() {
    for (Path source : this.context.getSources()) {
        SourceRoot root = new SourceRoot(source, parserConfiguration);
        try {
            for (ParseResult<CompilationUnit> result : root.tryToParse()) {
                if (result.isSuccessful() && result.getResult().isPresent()) {
                    result.getResult().get().accept(visitorParser, project);
                }
            }
        } catch (IOException e) {
            log.warn("parse root {} error {}", source, e.getMessage());
        }
    }
    return project;
}