Search in sources :

Example 6 with ASTParser

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

the class JavaParser method parseAnnotation.

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

Example 7 with ASTParser

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

the class JavaParser method parseImport.

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

Example 8 with ASTParser

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

the class JavaParser method parseBodyDeclaration.

/**
 * Parses the Java body declaration(e.g fields or methods) contained in a
 * {@link String} and returns a {@link BodyDeclaration} that represents it.
 *
 * @param body
 *            {@link String} containing Java body declaration
 * @return BodyDeclaration representing the Java annotation
 * @throws ParseException
 *             if the source code has parser errors
 */
public static BodyDeclaration parseBodyDeclaration(final String body) {
    StringReader sr = new StringReader(body);
    BodyDeclaration bd = new ASTParser(sr).AnnotationBodyDeclaration();
    sr.close();
    return bd;
}
Also used : StringReader(java.io.StringReader) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) 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