Search in sources :

Example 56 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class Issue128 method loopOnStaticallyImportedType.

@Test
public void loopOnStaticallyImportedType() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue128/foo/Issue128");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("test").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    assertEquals(false, javaParserFacade.solve(methodCallExpr).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 57 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserTypeSolver method parseDirectory.

private List<CompilationUnit> parseDirectory(File srcDirectory) {
    try {
        return parsedDirectories.get(srcDirectory.getAbsolutePath(), () -> {
            List<CompilationUnit> units = new ArrayList<>();
            File[] files = srcDirectory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.getName().toLowerCase().endsWith(".java")) {
                        Optional<CompilationUnit> unit = parse(file);
                        if (unit.isPresent()) {
                            units.add(unit.get());
                        }
                    }
                }
            }
            return units;
        });
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 58 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParser method parse.

public static CompilationUnit parse(final Reader reader, boolean considerComments) {
    try {
        String code = SourcesHelper.readerToString(reader);
        Reader reader1 = SourcesHelper.stringToReader(code);
        CompilationUnit cu = new ASTParser(reader1).CompilationUnit();
        if (considerComments) {
            insertComments(cu, code);
        }
        return cu;
    } catch (IOException ioe) {
        throw new ParseException(ioe.getMessage());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Reader(java.io.Reader) StringReader(java.io.StringReader) IOException(java.io.IOException) ParseException(com.github.javaparser.ParseException) ASTParser(com.github.javaparser.ASTParser)

Example 59 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParser method parse.

/**
 * Parses the Java code contained in the {@link InputStream} and returns a
 * {@link CompilationUnit} that represents it.
 *
 * @param in
 *            {@link InputStream} containing Java source code
 * @param encoding
 *            encoding of the source code
 * @return CompilationUnit representing the Java source code
 * @throws ParseException
 *             if the source code has parser errors
 */
public static CompilationUnit parse(final InputStream in, final String encoding, boolean considerComments) {
    try {
        String code = SourcesHelper.streamToString(in, encoding);
        InputStream in1 = SourcesHelper.stringToStream(code, encoding);
        CompilationUnit cu = new ASTParser(in1, encoding).CompilationUnit();
        if (considerComments) {
            insertComments(cu, code);
        }
        return cu;
    } catch (IOException ioe) {
        throw new ParseException(ioe.getMessage());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ParseException(com.github.javaparser.ParseException) ASTParser(com.github.javaparser.ASTParser)

Example 60 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class ParseErrorRecoveryTest method compilationUnitRecovery.

@Test
public void compilationUnitRecovery() {
    CompilationUnit cu = parser.parse(ParseStart.COMPILATION_UNIT, provider("XXX")).getResult().get();
    assertEquals(UNPARSABLE, cu.getParsed());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Test(org.junit.Test)

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