Search in sources :

Example 16 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class TryStmtTest method parse9.

private <T> T parse9(String code) {
    JavaParser parser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_9));
    ParseResult<Statement> result = parser.parse(ParseStart.STATEMENT, provider(code));
    assertTrue(result.toString(), result.isSuccessful());
    return (T) result.getResult().get();
}
Also used : JavaParser(com.github.javaparser.JavaParser) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 17 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class BulkParseTest method parseJdkSrcZip.

private void parseJdkSrcZip() throws IOException {
    // This is where Ubuntu stores the contents of package openjdk-8-src
    Path path = Paths.get("/usr/lib/jvm/openjdk-9/src.zip");
    bulkTest(new SourceZip(path), "openjdk_src_zip_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_9));
}
Also used : Path(java.nio.file.Path) SourceZip(com.github.javaparser.utils.SourceZip) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 18 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class BulkParseTest method parseOpenJdkLangToolsRepository.

private void parseOpenJdkLangToolsRepository() throws IOException {
    Path workdir = CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve(Paths.get(temporaryDirectory(), "javaparser_bulkparsetest"));
    workdir.toFile().mkdirs();
    Path openJdkZipPath = workdir.resolve("langtools.zip");
    if (Files.notExists(openJdkZipPath)) {
        Log.info("Downloading JDK langtools");
        /* Found by choosing a tag here: http://hg.openjdk.java.net/jdk9/jdk9/langtools/tags
             then copying the "zip" link to the line below: */
        download(new URL("http://hg.openjdk.java.net/jdk10/jdk10/langtools/archive/19293ea3999f.zip"), openJdkZipPath);
    }
    bulkTest(new SourceZip(openJdkZipPath), "openjdk_src_repo_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_10));
}
Also used : Path(java.nio.file.Path) SourceZip(com.github.javaparser.utils.SourceZip) URL(java.net.URL) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 19 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class JavaParserAPIIntegrationTest method annotationMemberDeclarationResolve.

@Test
public void annotationMemberDeclarationResolve() throws IOException {
    File f = adaptPath(new File("src/test/resources/Annotations.java.txt"));
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
    CompilationUnit cu = new JavaParser(parserConfiguration).parse(ParseStart.COMPILATION_UNIT, new StreamProvider(new FileInputStream(f))).getResult().get();
    AnnotationDeclaration declaration = (AnnotationDeclaration) cu.getType(2);
    assertEquals("MyAnnotationWithFields", declaration.getNameAsString());
    AnnotationMemberDeclaration memberDeclaration = (AnnotationMemberDeclaration) declaration.getMember(0);
    ResolvedAnnotationMemberDeclaration resolvedDeclaration = memberDeclaration.resolve();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser) StreamProvider(com.github.javaparser.StreamProvider) File(java.io.File) FileInputStream(java.io.FileInputStream) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.Test)

Example 20 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project drools by kiegroup.

the class DrlxCompiler method toPackageDescr.

public PackageDescr toPackageDescr(Resource resource) throws IOException {
    ParseStart<CompilationUnit> context = ParseStart.DRLX_COMPILATION_UNIT;
    MvelParser mvelParser = new MvelParser(new ParserConfiguration(), false);
    ParseResult<CompilationUnit> result = mvelParser.parse(context, provider(resource.getReader()));
    if (result.isSuccessful()) {
        DrlxVisitor drlxCompiler = new DrlxVisitor();
        drlxCompiler.visit(result.getResult().get(), null);
        PackageDescr pkg = drlxCompiler.getPackageDescr();
        if (pkg == null) {
            this.results.add(new ParserError(resource, "Parser returned a null Package", 0, 0));
            return null;
        } else {
            pkg.setResource(resource);
            return pkg;
        }
    } else {
        for (Problem problem : result.getProblems()) {
            TokenRange tokenRange = problem.getLocation().get();
            Range range = tokenRange.getBegin().getRange().get();
            int lineCount = range.getLineCount();
            this.results.add(new ParserError(problem.getMessage(), lineCount, -1));
        }
        return null;
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MvelParser(org.drools.mvel.parser.MvelParser) ParserError(org.drools.drl.parser.ParserError) Problem(com.github.javaparser.Problem) TokenRange(com.github.javaparser.TokenRange) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Range(com.github.javaparser.Range) TokenRange(com.github.javaparser.TokenRange) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Aggregations

ParserConfiguration (com.github.javaparser.ParserConfiguration)24 JavaParser (com.github.javaparser.JavaParser)14 CompilationUnit (com.github.javaparser.ast.CompilationUnit)11 Test (org.junit.Test)8 ParseProblemException (com.github.javaparser.ParseProblemException)6 Expression (com.github.javaparser.ast.expr.Expression)4 Path (java.nio.file.Path)4 StreamProvider (com.github.javaparser.StreamProvider)3 ParseResult (com.github.javaparser.ParseResult)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 JavaSymbolSolver (com.github.javaparser.symbolsolver.JavaSymbolSolver)2 ClassLoaderTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver)2 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)2 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)2 SourceZip (com.github.javaparser.utils.SourceZip)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2