Class StaticJavaParser


  • public final class StaticJavaParser
    extends Object
    A simpler, static API than JavaParser.
    • Method Detail

      • getParserConfiguration

        public static ParserConfiguration getParserConfiguration()
        Get the configuration for the parse... methods.
      • setConfiguration

        public static void setConfiguration​(@NotNull
                                            ParserConfiguration configuration)
        Set the configuration for the static parse... methods. This is a STATIC field, so modifying it will directly change how all static parse... methods work!
      • parseResource

        public static CompilationUnit parseResource​(@NotNull
                                                    String path)
                                             throws IOException
        Parses the Java code contained in a resource and returns a CompilationUnit that represents it.
        Parameters:
        path - path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource
        Returns:
        CompilationUnit representing the Java source code
        Throws:
        ParseProblemException - if the source code has parser errors
        IOException - the path could not be accessed
      • parseResource

        @Deprecated
        public static CompilationUnit parseResource​(@NotNull
                                                    String path,
                                                    @NotNull
                                                    Charset encoding)
                                             throws IOException
        Deprecated.
        set the encoding in the ParserConfiguration
        Parses the Java code contained in a resource and returns a CompilationUnit that represents it.
        Parameters:
        path - path to a resource containing Java source code. As resource is accessed through a class loader, a leading "/" is not allowed in pathToResource
        encoding - encoding of the source code
        Returns:
        CompilationUnit representing the Java source code
        Throws:
        ParseProblemException - if the source code has parser errors
        IOException - the path could not be accessed
      • parse

        public static CompilationUnit parse​(@NotNull
                                            Reader reader)
        Parses Java code from a Reader and returns a CompilationUnit that represents it.
        Parameters:
        reader - the reader containing Java source code. It will be closed after parsing.
        Returns:
        CompilationUnit representing the Java source code
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseBlock

        public static BlockStmt parseBlock​(@NotNull
                                           String blockStatement)
        Parses the Java block contained in a String and returns a BlockStmt that represents it.
        Parameters:
        blockStatement - String containing Java block code
        Returns:
        BlockStmt representing the Java block
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseStatement

        public static Statement parseStatement​(@NotNull
                                               String statement)
        Parses the Java statement contained in a String and returns a Statement that represents it.
        Parameters:
        statement - String containing Java statement code
        Returns:
        Statement representing the Java statement
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseExpression

        public static <T extends Expression> T parseExpression​(@NotNull
                                                               String expression)
        Parses the Java expression contained in a String and returns a Expression that represents it.
        Parameters:
        expression - String containing Java expression
        Returns:
        Expression representing the Java expression
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseAnnotation

        public static AnnotationExpr parseAnnotation​(@NotNull
                                                     String annotation)
        Parses the Java annotation contained in a String and returns a AnnotationExpr that represents it.
        Parameters:
        annotation - String containing Java annotation
        Returns:
        AnnotationExpr representing the Java annotation
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseAnnotationBodyDeclaration

        public static BodyDeclaration<?> parseAnnotationBodyDeclaration​(@NotNull
                                                                        String body)
        Parses the Java annotation body declaration(e.g fields or methods) contained in a String and returns a BodyDeclaration that represents it.
        Parameters:
        body - String containing Java body declaration
        Returns:
        BodyDeclaration representing the Java annotation
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseBodyDeclaration

        public static BodyDeclaration<?> parseBodyDeclaration​(@NotNull
                                                              String body)
        Parses a Java class or interface body declaration(e.g fields or methods) and returns a BodyDeclaration that represents it.
        Parameters:
        body - the body of a class or interface
        Returns:
        BodyDeclaration representing the Java interface body
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseClassOrInterfaceType

        public static ClassOrInterfaceType parseClassOrInterfaceType​(@NotNull
                                                                     String type)
        Parses a Java class or interface type name and returns a ClassOrInterfaceType that represents it.
        Parameters:
        type - the type name like a.b.c.X or Y
        Returns:
        ClassOrInterfaceType representing the type
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseType

        public static Type parseType​(@NotNull
                                     String type)
        Parses a Java type name and returns a Type that represents it.
        Parameters:
        type - the type name like a.b.c.X, Y, or int
        Returns:
        ClassOrInterfaceType representing the type
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseVariableDeclarationExpr

        public static VariableDeclarationExpr parseVariableDeclarationExpr​(@NotNull
                                                                           String declaration)
        Parses a variable declaration expression and returns a VariableDeclarationExpr that represents it.
        Parameters:
        declaration - a variable declaration like int x=2;
        Returns:
        VariableDeclarationExpr representing the type
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseJavadoc

        public static Javadoc parseJavadoc​(@NotNull
                                           String content)
        Parses the content of a JavadocComment and returns a Javadoc that represents it.
        Parameters:
        content - a variable declaration like content of my javadoc\n * second line\n * third line
        Returns:
        Javadoc representing the content of the comment
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseExplicitConstructorInvocationStmt

        public static ExplicitConstructorInvocationStmt parseExplicitConstructorInvocationStmt​(@NotNull
                                                                                               String statement)
        Parses the this(...) and super(...) statements that may occur at the start of a constructor.
        Parameters:
        statement - a statement like super("hello");
        Returns:
        the AST for the statement.
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseName

        public static Name parseName​(@NotNull
                                     String qualifiedName)
        Parses a qualified name (one that can have "."s in it) and returns it as a Name.
        Parameters:
        qualifiedName - a name like "com.laamella.parameter_source"
        Returns:
        the AST for the name
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseSimpleName

        public static SimpleName parseSimpleName​(@NotNull
                                                 String name)
        Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.
        Parameters:
        name - a name like "parameter_source"
        Returns:
        the AST for the name
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseParameter

        public static Parameter parseParameter​(@NotNull
                                               String parameter)
        Parses a single parameter (a type and a name) and returns it as a Parameter.
        Parameters:
        parameter - a parameter like "int[] x"
        Returns:
        the AST for the parameter
        Throws:
        ParseProblemException - if the source code has parser errors
      • parsePackageDeclaration

        public static PackageDeclaration parsePackageDeclaration​(@NotNull
                                                                 String packageDeclaration)
        Parses a package declaration and returns it as a PackageDeclaration.
        Parameters:
        packageDeclaration - a declaration like "package com.microsoft.java;"
        Returns:
        the AST for the parameter
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseTypeDeclaration

        public static TypeDeclaration<?> parseTypeDeclaration​(@NotNull
                                                              String typeDeclaration)
        Parses a type declaration and returns it as a TypeDeclaration.
        Parameters:
        typeDeclaration - a declaration like "class X {}"
        Returns:
        the AST for the type declaration
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseModuleDeclaration

        public static ModuleDeclaration parseModuleDeclaration​(@NotNull
                                                               String moduleDeclaration)
        Parses a module declaration and returns it as a ModuleDeclaration.
        Parameters:
        moduleDeclaration - a declaration like "module X {}"
        Returns:
        the AST for the module declaration
        Throws:
        ParseProblemException - if the source code has parser errors
        See Also:
        ModuleDeclaration
      • parseModuleDirective

        public static ModuleDirective parseModuleDirective​(@NotNull
                                                           String moduleDirective)
        Parses a module directive and returns it as a ModuleDirective.
        Parameters:
        moduleDirective - a directive like "opens C;"
        Returns:
        the AST for the module directive
        Throws:
        ParseProblemException - if the source code has parser errors
        See Also:
        ModuleDirective
      • parseTypeParameter

        public static TypeParameter parseTypeParameter​(@NotNull
                                                       String typeParameter)
        Parses a type parameter and returns it as a TypeParameter
        Parameters:
        typeParameter - a parameter like "T extends Serializable"
        Returns:
        the AST for the type parameter
        Throws:
        ParseProblemException - if the source code has parser errors
      • parseMethodDeclaration

        public static MethodDeclaration parseMethodDeclaration​(@NotNull
                                                               String methodDeclaration)
        Parses a method declaration and returns it as a MethodDeclaration.
        Parameters:
        methodDeclaration - a method declaration like "void foo() {}"
        Returns:
        the AST for the method declaration
        Throws:
        ParseProblemException - if the source code has parser errors
        See Also:
        MethodDeclaration
      • parseArrayInitializerExpr

        public static ArrayInitializerExpr parseArrayInitializerExpr​(@NotNull
                                                                     String arrayInitializerExpr)
        Parses an array initializer expression and returns it as ArrayInitializerExpr.
        Parameters:
        arrayInitializerExpr - an array initializer like "{1,2,3}"
        Returns:
        the AST for the array initializer expression
        Throws:
        ParseProblemException - if the source code has parser errors
        See Also:
        ArrayInitializerExpr