Class Navigator


  • public final class Navigator
    extends Object
    This class can be used to easily retrieve nodes from a JavaParser AST.
    Author:
    Federico Tomassetti
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandClass​(com.github.javaparser.ast.CompilationUnit cu, String qualifiedName)  
      static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandClassOrInterface​(com.github.javaparser.ast.CompilationUnit compilationUnit, String qualifiedName)  
      static com.github.javaparser.ast.body.ConstructorDeclaration demandConstructor​(com.github.javaparser.ast.body.TypeDeclaration<?> td, int index)
      Returns the (i+1)'th constructor of the given type declaration, in textual order.
      static com.github.javaparser.ast.body.EnumDeclaration demandEnum​(com.github.javaparser.ast.CompilationUnit cu, String qualifiedName)  
      static com.github.javaparser.ast.body.VariableDeclarator demandField​(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration cd, String name)  
      static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandInterface​(com.github.javaparser.ast.CompilationUnit cu, String qualifiedName)  
      static com.github.javaparser.ast.body.MethodDeclaration demandMethod​(com.github.javaparser.ast.body.TypeDeclaration<?> cd, String name)  
      static Optional<com.github.javaparser.ast.body.VariableDeclarator> demandVariableDeclaration​(com.github.javaparser.ast.Node node, String name)  
      static <N extends com.github.javaparser.ast.Node>
      List<N>
      findAllNodesOfGivenClass​(com.github.javaparser.ast.Node node, Class<N> clazz)
      Deprecated.
      use Node.findAll instead
      static <N extends com.github.javaparser.ast.Node>
      Optional<N>
      findAncestor​(com.github.javaparser.ast.Node node, Class<N> clazz)
      Deprecated.
      use Node.findAncestor instead
      static Optional<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCall​(com.github.javaparser.ast.Node node, String methodName)  
      static Optional<com.github.javaparser.ast.expr.NameExpr> findNameExpression​(com.github.javaparser.ast.Node node, String name)  
      static <N extends com.github.javaparser.ast.Node>
      N
      findNodeOfGivenClass​(com.github.javaparser.ast.Node node, Class<N> clazz)  
      static com.github.javaparser.ast.stmt.ReturnStmt findReturnStmt​(com.github.javaparser.ast.body.MethodDeclaration method)  
      static Optional<com.github.javaparser.ast.expr.SimpleName> findSimpleName​(com.github.javaparser.ast.Node node, String name)  
      static com.github.javaparser.ast.stmt.SwitchStmt findSwitch​(com.github.javaparser.ast.Node node)  
      static Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> findType​(com.github.javaparser.ast.body.TypeDeclaration<?> td, String qualifiedName)
      Looks among the type declared in the TypeDeclaration for one having the specified name.
      static Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> findType​(com.github.javaparser.ast.CompilationUnit cu, String qualifiedName)
      Looks among the type declared in the Compilation Unit for one having the specified name.
      static com.github.javaparser.ast.Node getParentNode​(com.github.javaparser.ast.Node node)
      Deprecated.
      use Node.getParentNode
      static com.github.javaparser.ast.Node requireParentNode​(com.github.javaparser.ast.Node node)  
    • Method Detail

      • getParentNode

        @Deprecated
        public static com.github.javaparser.ast.Node getParentNode​(com.github.javaparser.ast.Node node)
        Deprecated.
        use Node.getParentNode
      • requireParentNode

        public static com.github.javaparser.ast.Node requireParentNode​(com.github.javaparser.ast.Node node)
      • findType

        public static Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> findType​(com.github.javaparser.ast.CompilationUnit cu,
                                                                                           String qualifiedName)
        Looks among the type declared in the Compilation Unit for one having the specified name. The name can be qualified with respect to the compilation unit. For example, if the compilation unit is in package a.b; and it contains two top level classes named C and D, with class E being defined inside D then the qualifiedName that can be resolved are "C", "D", and "D.E".
      • findType

        public static Optional<com.github.javaparser.ast.body.TypeDeclaration<?>> findType​(com.github.javaparser.ast.body.TypeDeclaration<?> td,
                                                                                           String qualifiedName)
        Looks among the type declared in the TypeDeclaration for one having the specified name. The name can be qualified with respect to the TypeDeclaration. For example, if the class declarationd defines class D and class D contains an internal class named E then the qualifiedName that can be resolved are "D", and "D.E".
      • demandClass

        public static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandClass​(com.github.javaparser.ast.CompilationUnit cu,
                                                                                             String qualifiedName)
      • demandInterface

        public static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandInterface​(com.github.javaparser.ast.CompilationUnit cu,
                                                                                                 String qualifiedName)
      • demandEnum

        public static com.github.javaparser.ast.body.EnumDeclaration demandEnum​(com.github.javaparser.ast.CompilationUnit cu,
                                                                                String qualifiedName)
      • demandMethod

        public static com.github.javaparser.ast.body.MethodDeclaration demandMethod​(com.github.javaparser.ast.body.TypeDeclaration<?> cd,
                                                                                    String name)
      • demandConstructor

        public static com.github.javaparser.ast.body.ConstructorDeclaration demandConstructor​(com.github.javaparser.ast.body.TypeDeclaration<?> td,
                                                                                              int index)
        Returns the (i+1)'th constructor of the given type declaration, in textual order. The constructor that appears first has the index 0, the second one the index 1, and so on.
        Parameters:
        td - The type declaration to search in. Note that only classes and enums have constructors.
        index - The index of the desired constructor.
        Returns:
        The desired ConstructorDeclaration if it was found, and null otherwise.
      • demandField

        public static com.github.javaparser.ast.body.VariableDeclarator demandField​(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration cd,
                                                                                    String name)
      • findNameExpression

        public static Optional<com.github.javaparser.ast.expr.NameExpr> findNameExpression​(com.github.javaparser.ast.Node node,
                                                                                           String name)
      • findSimpleName

        public static Optional<com.github.javaparser.ast.expr.SimpleName> findSimpleName​(com.github.javaparser.ast.Node node,
                                                                                         String name)
      • findMethodCall

        public static Optional<com.github.javaparser.ast.expr.MethodCallExpr> findMethodCall​(com.github.javaparser.ast.Node node,
                                                                                             String methodName)
      • demandVariableDeclaration

        public static Optional<com.github.javaparser.ast.body.VariableDeclarator> demandVariableDeclaration​(com.github.javaparser.ast.Node node,
                                                                                                            String name)
      • demandClassOrInterface

        public static com.github.javaparser.ast.body.ClassOrInterfaceDeclaration demandClassOrInterface​(com.github.javaparser.ast.CompilationUnit compilationUnit,
                                                                                                        String qualifiedName)
      • findSwitch

        public static com.github.javaparser.ast.stmt.SwitchStmt findSwitch​(com.github.javaparser.ast.Node node)
      • findNodeOfGivenClass

        public static <N extends com.github.javaparser.ast.Node> N findNodeOfGivenClass​(com.github.javaparser.ast.Node node,
                                                                                        Class<N> clazz)
      • findAllNodesOfGivenClass

        @Deprecated
        public static <N extends com.github.javaparser.ast.Node> List<N> findAllNodesOfGivenClass​(com.github.javaparser.ast.Node node,
                                                                                                  Class<N> clazz)
        Deprecated.
        use Node.findAll instead
      • findReturnStmt

        public static com.github.javaparser.ast.stmt.ReturnStmt findReturnStmt​(com.github.javaparser.ast.body.MethodDeclaration method)
      • findAncestor

        @Deprecated
        public static <N extends com.github.javaparser.ast.Node> Optional<N> findAncestor​(com.github.javaparser.ast.Node node,
                                                                                          Class<N> clazz)
        Deprecated.
        use Node.findAncestor instead