Search in sources :

Example 1 with ParserConfiguration

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

the class CommentParsingSteps method givenTheClassWithEncoding.

@When("read sample \"$sampleName\" using encoding \"$encoding\"")
public void givenTheClassWithEncoding(String sampleName, String encoding) throws IOException {
    sourceUnderTest = null;
    ParseResult<CompilationUnit> parseResult = new JavaParser(new ParserConfiguration()).parse(COMPILATION_UNIT, provider(getSampleStream(sampleName), Charset.forName(encoding)));
    commentsCollection = parseResult.getCommentsCollection().orElse(new CommentsCollection());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser) ParserConfiguration(com.github.javaparser.ParserConfiguration) When(org.jbehave.core.annotations.When)

Example 2 with ParserConfiguration

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

the class JavaParserAPIIntegrationTest method parameterDeclarationResolve.

@Test
public void parameterDeclarationResolve() throws IOException {
    File f = adaptPath(new File("src/test/test_sourcecode/javaparser_new_src/javaparser-core/com/github/javaparser/ast/CompilationUnit.java"));
    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();
    ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) cu.getType(0);
    assertEquals("CompilationUnit", classDeclaration.getNameAsString());
    MethodDeclaration methodDeclaration = classDeclaration.getMethodsByName("setComments").get(0);
    Parameter declaration = methodDeclaration.getParameter(0);
    ResolvedParameterDeclaration resolvedDeclaration = declaration.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 3 with ParserConfiguration

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

the class JavaParserAPIIntegrationTest method parseWithSymbolResolution.

private CompilationUnit parseWithSymbolResolution(File f) throws IOException {
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
    return new JavaParser(parserConfiguration).parse(ParseStart.COMPILATION_UNIT, new StreamProvider(new FileInputStream(f))).getResult().get();
}
Also used : JavaParser(com.github.javaparser.JavaParser) StreamProvider(com.github.javaparser.StreamProvider) FileInputStream(java.io.FileInputStream) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 4 with ParserConfiguration

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

the class Issue1364 method setup.

@Before
public void setup() {
    ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
    fakeObject.setName(new SimpleName("java.lang.Object"));
    TypeSolver typeSolver = new TypeSolver() {

        @Override
        public TypeSolver getParent() {
            return null;
        }

        @Override
        public void setParent(TypeSolver parent) {
        }

        @Override
        public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
            if ("java.lang.Object".equals(name)) {
                // custom handling
                return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
            }
            return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
        }
    };
    ParserConfiguration config = new ParserConfiguration();
    config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
    javaParser = new JavaParser(config);
}
Also used : JavaParser(com.github.javaparser.JavaParser) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) SimpleName(com.github.javaparser.ast.expr.SimpleName) ParserConfiguration(com.github.javaparser.ParserConfiguration) Before(org.junit.Before)

Example 5 with ParserConfiguration

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

the class TypeTest method primitiveTypeArgumentLenientValidator.

@Test
public void primitiveTypeArgumentLenientValidator() {
    ParserConfiguration config = new ParserConfiguration().setLanguageLevel(RAW);
    config.getPostProcessors().add(new Java5Validator() {

        {
            remove(noPrimitiveGenericArguments);
        }
    }.postProcessor());
    ParseResult<VariableDeclarationExpr> result = new JavaParser(config).parse(VARIABLE_DECLARATION_EXPR, provider("List<long> x"));
    assertTrue(result.isSuccessful());
    VariableDeclarationExpr decl = result.getResult().get();
    assertEquals("List<long>", decl.getVariable(0).getType().asString());
}
Also used : JavaParser(com.github.javaparser.JavaParser) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) JavaParser.parseVariableDeclarationExpr(com.github.javaparser.JavaParser.parseVariableDeclarationExpr) Java5Validator(com.github.javaparser.ast.validator.Java5Validator) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.Test)

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