Class JavaSourceCodeFile.HANDLER

  • All Implemented Interfaces:
    com.github.javaparser.ast.visitor.VoidVisitor<java.lang.Void>
    Enclosing class:
    JavaSourceCodeFile

    public static class JavaSourceCodeFile.HANDLER
    extends com.github.javaparser.ast.visitor.VoidVisitorAdapter<java.lang.Void>
    Handler for Java Parser



    • Constructor Summary

      Constructors 
      Constructor
      HANDLER​(JavaSourceCodeFile javaSourceCodeFile, String javaSourceCodeFileName)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method
      boolean isContainedByCorrectCIET​(java.util.Optional<com.github.javaparser.ast.Node> p)
      void visit​(com.github.javaparser.ast.body.ConstructorDeclaration cd, java.lang.Void arg)
      void visit​(com.github.javaparser.ast.body.FieldDeclaration fd, java.lang.Void arg)
      void visit​(com.github.javaparser.ast.body.MethodDeclaration md, java.lang.Void arg)
      • Methods inherited from class com.github.javaparser.ast.visitor.VoidVisitorAdapter

        visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HANDLER

        public HANDLER​(JavaSourceCodeFile javaSourceCodeFile,
                       java.lang.String javaSourceCodeFileName)
        Constructs an instance of this class.
        Parameters:
        javaSourceCodeFile - This is just an instance of the enclosing class 'JavaSourceCodeFile'. This class has a 'parent' class in its inheritance tree that deals with maintaining three lists. The three lists that are supported by that parent class hold all of the following: methods, constructors, and fields. This work is done in ancestor-class 'ParsedFile' primarily because there are two versions of parsing the methods, classes into lists of String's: '.java' files (this class), and '.html' files, which is done by 'JavaDocHTMLFile'.
        javaSourceCodeFileName - This is the name of the file. The only reason that it is required here is because when exceptions are handled, it is easier to append the name of the file that was being parsed when the exception was thrown.
    • Method Detail

      • isContainedByCorrectCIET

        public boolean isContainedByCorrectCIET​
                    (java.util.Optional<com.github.javaparser.ast.Node> p)
        
        The Tree-Traversal algorithm / tree-walker for a JavaParser AST will visit all of the Methods, Constructors, and Enumerated-Types - even ones defined in inner classes.

        When parsing a Java Source-Code file, it is easier to keep them separated by CIET (class, interface, or enumerated-type). This method is used to REJECT any method that is not defined in the appropriate class inside of a java source code file. Which CIET is the "correct" or "appropriate" CIET is defined in the Constructor where the user specifies whether he wants the main-class parsed, or one of the inner classes parsed.
        Parameters:
        p - This is the (Optional) parent-node for a MethodDeclaration, FieldDeclaration, or ConstructorDeclaration
        Returns:
        This will return TRUE if the parent/containing CIET where this method, Field or Constructor is defined is the right parent CIET based on the inputs to the JavaSourceCodeFile Constructor where the user is expected to provide a CIET name.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
         StringBuilder foundCIETName = new StringBuilder();
        
         // This crawls "upwards" in the JavaParser AST for the Class, Interface or Enumerated
         // Type that contains the declared variable
         for (Node n; p.isPresent(); p = n.getParentNode())
         {
             String isInnerClass = (foundCIETName.length() > 0) ? "." : "";
             n = p.get();
            
             if (n instanceof ClassOrInterfaceDeclaration)   // 'ClassOrInterfaceDeclaration' is a JavaParser class
                 foundCIETName.insert
                     (0, ((ClassOrInterfaceDeclaration) n).getName().asString() + isInnerClass);
        
             else if (n instanceof EnumDeclaration)          // 'EnumDeclaration' is a JavaParser class
                 foundCIETName.insert
                     (0, ((EnumDeclaration) n).getName().asString() + isInnerClass);
         }
        
         // See above note about the 'name' and including the Generic Variable-Types in the
         // name.  The above getName().asString() from the JavaParser code does not include the
         // Generic Type Parameters in the name of the containing class.  Therefore, in this
         // comparison, they are left out.  There is no way a class could contain an inner-class
         // with the same-name, but different generic-type parameters.  It would be illegal.
         return foundCIETName.toString().equals(javaSourceCodeFile.nameWithoutGenericStuff);
        
      • visit

        public void visit​(com.github.javaparser.ast.body.MethodDeclaration md,
                          java.lang.Void arg)
        Handles 'JavaParser' events during a parse of a Java-Source Code File whenever a 'Method' definition has been identified during its parse-passes.

        'MethodDeclaration' is type: com.github.javaparser.ast.body.MethodDeclaration
        Specified by:
        visit in interface com.github.javaparser.ast.visitor.VoidVisitor<java.lang.Void>
        Overrides:
        visit in class com.github.javaparser.ast.visitor.VoidVisitorAdapter<java.lang.Void>
        Parameters:
        md - This parameter is passed from JavaParser to this method, and it contains the information needed to build a local instance of class 'Method'. Class 'Method' is a pared down, simplified, version of class MethodDeclaration. It has been reduced to a list of strings that can be used to help upgrade java-doc '.html' files.
        arg - This parameter is provided by Java-Parser. This handler does not make use of it.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
         // Many java source-code files have inner-class, interfaces and enumerated-types.
         // Make sure to skip any 'MethodDeclarations' that are defined in a different CIET or
         // inner-CIET than requested at startup.
         if (! isContainedByCorrectCIET(md.getParentNode())) return;
        
         try 
             { javaSourceCodeFile.addMethod(new Method(md, false)); }
             // local class "Method" (and its class-inheritance tree deals with
             // "MethodDeclaration")
         catch (Exception e)
         {
             String signature = null;
             try
                 { signature = md.getDeclarationAsString(); }
             catch (Throwable t)
             {
                 throw new SourceCodeParseException(
                     "There was an error while parsing a method defined in Java-Source Code " +
                     "File [" + javaSourceCodeFileName + "].  " +
                     "The signature of the method being parsed could not be computed or " +
                     "provided, sorry.  Please see this exception's 'getCause()' method for " +
                     "more details.", javaSourceCodeFileName, null
                 );
             }
             throw new SourceCodeParseException(
                 "There was an error while parsing a method defined in Java-Source Code " +
                 "File [" + javaSourceCodeFileName + "].  The signature of the method being " +
                 "parsed was:\n" + signature + "\nPlease see this exception's 'getCause()' " +
                 "method for more details.", javaSourceCodeFileName, signature
             );
         }
        
      • visit

        public void visit​
                    (com.github.javaparser.ast.body.ConstructorDeclaration cd,
                     java.lang.Void arg)
        
        Handles 'JavaParser' events during a parse of a Java-Source Code File whenever a 'Constructor' definition has been identified during its parse-passes.

        ConstructorDeclaration is type: com.github.javaparser.ast.body.ConstructorDeclaration
        Specified by:
        visit in interface com.github.javaparser.ast.visitor.VoidVisitor<java.lang.Void>
        Overrides:
        visit in class com.github.javaparser.ast.visitor.VoidVisitorAdapter<java.lang.Void>
        Parameters:
        cd - This parameter is passed from JavaParser to this method, and it contains the information needed to build a local instance of class 'Constructor'. 'Constructor' is a pared down, simplified, version of JavaParser class ConstructorDeclaration. It has been reduced to a list of String's that can be used to help upgrade java-doc '.html' files.
        arg - This parameter is provided by Java-Parser. This handler does not make use of it.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
         // Many java source-code files have inner-class, interfaces and enumerated-types.
         // Make sure to skip any 'ConstructorDeclarations' that are defined in a different
         // CIET or inner-CIET than requested at startup.
         if (! isContainedByCorrectCIET(cd.getParentNode())) return;
        
         try 
             { javaSourceCodeFile.addConstructor(new Constructor(cd, false)); } 
             // local class "Constructor" (and its class-inheritance tree deals with
             // "ConstructorDeclaration")
         catch (Exception e1)
         {
             String signature = null;
             try
                 { signature = cd.getDeclarationAsString(); }
             catch (Exception e2)
             {
                 throw new SourceCodeParseException(
                     "There was an error while parsing a constructor defined in Java-Source " +
                     "Code File [" + javaSourceCodeFileName + "].  The signature of the " +
                     "constructor being parsed could not be computed or provided, sorry.  " +
                     "Please see this exception's 'getCause()' method for more details.",
                     e1, javaSourceCodeFileName, null
                 );
             }
             throw new SourceCodeParseException(
                 "There was an error while parsing a constructor defined in Java-Source " +
                 "Code File [" + javaSourceCodeFileName + "].  The signature of the " +
                 "constructor being parsed was:\n" + signature + "\nPlease see this "+
                 "exception's 'getCause()' method for more details.",
                 e1, javaSourceCodeFileName, signature
             );
         }
        
      • visit

        public void visit​(com.github.javaparser.ast.body.FieldDeclaration fd,
                          java.lang.Void arg)
        Handles 'JavaParser' events during a parse of a Java-Source Code File whenever a 'Field' definition has been identified during its parse-passes.

        'FieldDeclaration' is type: com.github.javaparser.ast.body.FieldDeclaration

        'VariableDeclarator' is type: com.github.javaparser.ast.body.VariableDeclarator

        NOTE: One Field Declaration may declare multiple Variable Declarations
        Specified by:
        visit in interface com.github.javaparser.ast.visitor.VoidVisitor<java.lang.Void>
        Overrides:
        visit in class com.github.javaparser.ast.visitor.VoidVisitorAdapter<java.lang.Void>
        Parameters:
        fd - This parameter is passed from JavaParser to this method, and it contains the information needed to build a local instance of class 'Field'. Class 'Field' is a pared down, simplified, version of JavaParser class FieldDeclaration. It has been reduced to a list of String's that can be used to help upgrade java-doc '.html' files.
        arg - This parameter is provided by Java-Parser. This handler does not make use of it.
        Code:
        Exact Method Body:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
         // Many java source-code files have inner-class, interfaces and enumerated-types.
         // Make sure to skip and 'FieldDeclarations' that are defined in a different CIET or
         // inner-CIET than requested at startup.
         if (! isContainedByCorrectCIET(fd.getParentNode())) return;
        
         // "FieldDeclaration" is of type: com.github.javaparser.ast.body.FieldDeclaration;
         String declaration = null;
         try 
         {
             String[] modifiers = Declaration.MODIFIERS_LIST(fd.getModifiers());
        
             String comment = fd.hasJavaDocComment() 
                                 ? fd.getJavadocComment().get().toString()
                                 : null;
        
             declaration = LexicalPreservingPrinter.print
                             (LexicalPreservingPrinter.setup(fd.removeComment()));
            
             // "VariableDeclarator" is of type: 
             // import com.github.javaparser.ast.body.VariableDeclarator;
             for (VariableDeclarator vd : fd.getVariables())
                 javaSourceCodeFile.addField(new Field(fd, vd, declaration, modifiers, comment));
         }
         catch (Exception e1)
         {
             if (declaration == null)
                 throw new SourceCodeParseException(
                     "There was an error while parsing a constructor defined in Java-Source " +
                     "Code File [" + javaSourceCodeFileName + "].  The signature of the " +
                     "constructor being parsed could not be computed or provided, sorry.  " +
                     "Please see this exception's 'getCause()' method for more details.",
                     e1, javaSourceCodeFileName, null
                 );
             else 
                 throw new SourceCodeParseException(
                     "There was an error while parsing a constructor defined in Java-Source " +
                     "Code File [" + javaSourceCodeFileName + "].  The signature of the " +
                     "constructor being parsed was:\n" + declaration + "\nPlease see this " +
                     "exception's 'getCause()' method for more details.",
                     e1, javaSourceCodeFileName, declaration
                 );
         }