Search in sources :

Example 1 with ASTParser

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

the class JavaParser method parseExpression.

/**
 * Parses the Java expression contained in a {@link String} and returns a
 * {@link Expression} that represents it.
 *
 * @param expression
 *            {@link String} containing Java expression
 * @return Expression representing the Java expression
 * @throws ParseException
 *             if the source code has parser errors
 */
public static Expression parseExpression(final String expression) {
    StringReader sr = new StringReader(expression);
    Expression e = new ASTParser(sr).Expression();
    sr.close();
    return e;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StringReader(java.io.StringReader) ASTParser(com.github.javaparser.ASTParser)

Example 2 with ASTParser

use of com.github.javaparser.ASTParser 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 3 with ASTParser

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

the class JavaParser method parseBlock.

/**
 * Parses the Java block contained in a {@link String} and returns a
 * {@link BlockStmt} that represents it.
 *
 * @param blockStatement
 *            {@link String} containing Java block code
 * @return BlockStmt representing the Java block
 * @throws ParseException
 *             if the source code has parser errors
 */
public static BlockStmt parseBlock(final String blockStatement) {
    StringReader sr = new StringReader(blockStatement);
    BlockStmt result = new ASTParser(sr).Block();
    sr.close();
    return result;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringReader(java.io.StringReader) ASTParser(com.github.javaparser.ASTParser)

Example 4 with ASTParser

use of com.github.javaparser.ASTParser 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 5 with ASTParser

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

the class JavaParser method parseStatement.

/**
 * Parses the Java statement contained in a {@link String} and returns a
 * {@link Statement} that represents it.
 *
 * @param statement
 *            {@link String} containing Java statement code
 * @return Statement representing the Java statement
 * @throws ParseException
 *             if the source code has parser errors
 */
public static Statement parseStatement(final String statement) {
    StringReader sr = new StringReader(statement);
    Statement stmt = new ASTParser(sr).Statement();
    sr.close();
    return stmt;
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) StringReader(java.io.StringReader) ASTParser(com.github.javaparser.ASTParser)

Aggregations

ASTParser (com.github.javaparser.ASTParser)8 StringReader (java.io.StringReader)7 ParseException (com.github.javaparser.ParseException)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 IOException (java.io.IOException)2 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1