langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java
changeset 24069 dfb8f11542fc
parent 22702 1297fbaf34fa
child 24404 cf534ffbc9d8
equal deleted inserted replaced
24068:d8a1180faaa9 24069:dfb8f11542fc
    87 
    87 
    88         /** Toplevel nodes, of type TopLevel, representing entire source files.
    88         /** Toplevel nodes, of type TopLevel, representing entire source files.
    89         */
    89         */
    90         TOPLEVEL,
    90         TOPLEVEL,
    91 
    91 
       
    92         /** Package level definitions.
       
    93          */
       
    94         PACKAGEDEF,
       
    95 
    92         /** Import clauses, of type Import.
    96         /** Import clauses, of type Import.
    93          */
    97          */
    94         IMPORT,
    98         IMPORT,
    95 
    99 
    96         /** Class definitions, of type ClassDef.
   100         /** Class definitions, of type ClassDef.
   476 
   480 
   477     /**
   481     /**
   478      * Everything in one source file is kept in a {@linkplain JCCompilationUnit} structure.
   482      * Everything in one source file is kept in a {@linkplain JCCompilationUnit} structure.
   479      */
   483      */
   480     public static class JCCompilationUnit extends JCTree implements CompilationUnitTree {
   484     public static class JCCompilationUnit extends JCTree implements CompilationUnitTree {
   481         public List<JCAnnotation> packageAnnotations;
       
   482         /** The tree representing the package clause. */
       
   483         public JCExpression pid;
       
   484         /** All definitions in this file (ClassDef, Import, and Skip) */
   485         /** All definitions in this file (ClassDef, Import, and Skip) */
   485         public List<JCTree> defs;
   486         public List<JCTree> defs;
   486         /* The source file name. */
   487         /* The source file name. */
   487         public JavaFileObject sourcefile;
   488         public JavaFileObject sourcefile;
   488         /** The package to which this compilation unit belongs. */
   489         /** The package to which this compilation unit belongs. */
   497          * nodes they refer to. defined only if option -s is set. */
   498          * nodes they refer to. defined only if option -s is set. */
   498         public DocCommentTable docComments = null;
   499         public DocCommentTable docComments = null;
   499         /* An object encapsulating ending positions of source ranges indexed by
   500         /* An object encapsulating ending positions of source ranges indexed by
   500          * the tree nodes they belong to. Defined only if option -Xjcov is set. */
   501          * the tree nodes they belong to. Defined only if option -Xjcov is set. */
   501         public EndPosTable endPositions = null;
   502         public EndPosTable endPositions = null;
   502         protected JCCompilationUnit(List<JCAnnotation> packageAnnotations,
   503         protected JCCompilationUnit(List<JCTree> defs) {
   503                         JCExpression pid,
       
   504                         List<JCTree> defs,
       
   505                         JavaFileObject sourcefile,
       
   506                         PackageSymbol packge,
       
   507                         ImportScope namedImportScope,
       
   508                         StarImportScope starImportScope) {
       
   509             this.packageAnnotations = packageAnnotations;
       
   510             this.pid = pid;
       
   511             this.defs = defs;
   504             this.defs = defs;
   512             this.sourcefile = sourcefile;
       
   513             this.packge = packge;
       
   514             this.namedImportScope = namedImportScope;
       
   515             this.starImportScope = starImportScope;
       
   516         }
   505         }
   517         @Override
   506         @Override
   518         public void accept(Visitor v) { v.visitTopLevel(this); }
   507         public void accept(Visitor v) { v.visitTopLevel(this); }
   519 
   508 
   520         public Kind getKind() { return Kind.COMPILATION_UNIT; }
   509         public Kind getKind() { return Kind.COMPILATION_UNIT; }
       
   510 
       
   511         public JCPackageDecl getPackage() {
       
   512             // PackageDecl must be the first entry if it exists
       
   513             if (!defs.isEmpty() && defs.head.hasTag(PACKAGEDEF))
       
   514                 return (JCPackageDecl)defs.head;
       
   515             return null;
       
   516         }
   521         public List<JCAnnotation> getPackageAnnotations() {
   517         public List<JCAnnotation> getPackageAnnotations() {
   522             return packageAnnotations;
   518             JCPackageDecl pd = getPackage();
   523         }
   519             return pd != null ? pd.getAnnotations() : List.<JCAnnotation>nil();
       
   520         }
       
   521         public ExpressionTree getPackageName() {
       
   522             JCPackageDecl pd = getPackage();
       
   523             return pd != null ? pd.getPackageName() : null;
       
   524         }
       
   525 
   524         public List<JCImport> getImports() {
   526         public List<JCImport> getImports() {
   525             ListBuffer<JCImport> imports = new ListBuffer<>();
   527             ListBuffer<JCImport> imports = new ListBuffer<>();
   526             for (JCTree tree : defs) {
   528             for (JCTree tree : defs) {
   527                 if (tree.hasTag(IMPORT))
   529                 if (tree.hasTag(IMPORT))
   528                     imports.append((JCImport)tree);
   530                     imports.append((JCImport)tree);
   529                 else if (!tree.hasTag(SKIP))
   531                 else if (!tree.hasTag(PACKAGEDEF) && !tree.hasTag(SKIP))
   530                     break;
   532                     break;
   531             }
   533             }
   532             return imports.toList();
   534             return imports.toList();
   533         }
   535         }
   534         public JCExpression getPackageName() { return pid; }
       
   535         public JavaFileObject getSourceFile() {
   536         public JavaFileObject getSourceFile() {
   536             return sourcefile;
   537             return sourcefile;
   537         }
   538         }
   538         public Position.LineMap getLineMap() {
   539         public Position.LineMap getLineMap() {
   539             return lineMap;
   540             return lineMap;
   540         }
   541         }
   541         public List<JCTree> getTypeDecls() {
   542         public List<JCTree> getTypeDecls() {
   542             List<JCTree> typeDefs;
   543             List<JCTree> typeDefs;
   543             for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail)
   544             for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail)
   544                 if (!typeDefs.head.hasTag(IMPORT))
   545                 if (!typeDefs.head.hasTag(PACKAGEDEF) && !typeDefs.head.hasTag(IMPORT))
   545                     break;
   546                     break;
   546             return typeDefs;
   547             return typeDefs;
   547         }
   548         }
   548         @Override
   549         @Override
   549         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
   550         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
   551         }
   552         }
   552 
   553 
   553         @Override
   554         @Override
   554         public Tag getTag() {
   555         public Tag getTag() {
   555             return TOPLEVEL;
   556             return TOPLEVEL;
       
   557         }
       
   558     }
       
   559 
       
   560     /**
       
   561      * Package definition.
       
   562      */
       
   563     public static class JCPackageDecl extends JCTree implements PackageTree {
       
   564         public List<JCAnnotation> annotations;
       
   565         /** The tree representing the package clause. */
       
   566         public JCExpression pid;
       
   567         public PackageSymbol packge;
       
   568         public JCPackageDecl(List<JCAnnotation> annotations, JCExpression pid) {
       
   569             this.annotations = annotations;
       
   570             this.pid = pid;
       
   571         }
       
   572         @Override
       
   573         public void accept(Visitor v) { v.visitPackageDef(this); }
       
   574         public Kind getKind() {
       
   575             return Kind.PACKAGE;
       
   576         }
       
   577         public List<JCAnnotation> getAnnotations() {
       
   578             return annotations;
       
   579         }
       
   580         public JCExpression getPackageName() {
       
   581             return pid;
       
   582         }
       
   583         @Override
       
   584         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
       
   585             return v.visitPackage(this, d);
       
   586         }
       
   587         @Override
       
   588         public Tag getTag() {
       
   589             return PACKAGEDEF;
   556         }
   590         }
   557     }
   591     }
   558 
   592 
   559     /**
   593     /**
   560      * An import clause.
   594      * An import clause.
  2436     }
  2470     }
  2437 
  2471 
  2438     /** An interface for tree factories
  2472     /** An interface for tree factories
  2439      */
  2473      */
  2440     public interface Factory {
  2474     public interface Factory {
  2441         JCCompilationUnit TopLevel(List<JCAnnotation> packageAnnotations,
  2475         JCCompilationUnit TopLevel(List<JCTree> defs);
  2442                                    JCExpression pid,
  2476         JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
  2443                                    List<JCTree> defs);
  2477                                   JCExpression pid);
  2444         JCImport Import(JCTree qualid, boolean staticImport);
  2478         JCImport Import(JCTree qualid, boolean staticImport);
  2445         JCClassDecl ClassDef(JCModifiers mods,
  2479         JCClassDecl ClassDef(JCModifiers mods,
  2446                           Name name,
  2480                           Name name,
  2447                           List<JCTypeParameter> typarams,
  2481                           List<JCTypeParameter> typarams,
  2448                           JCExpression extending,
  2482                           JCExpression extending,
  2526 
  2560 
  2527     /** A generic visitor class for trees.
  2561     /** A generic visitor class for trees.
  2528      */
  2562      */
  2529     public static abstract class Visitor {
  2563     public static abstract class Visitor {
  2530         public void visitTopLevel(JCCompilationUnit that)    { visitTree(that); }
  2564         public void visitTopLevel(JCCompilationUnit that)    { visitTree(that); }
       
  2565         public void visitPackageDef(JCPackageDecl that)      { visitTree(that); }
  2531         public void visitImport(JCImport that)               { visitTree(that); }
  2566         public void visitImport(JCImport that)               { visitTree(that); }
  2532         public void visitClassDef(JCClassDecl that)          { visitTree(that); }
  2567         public void visitClassDef(JCClassDecl that)          { visitTree(that); }
  2533         public void visitMethodDef(JCMethodDecl that)        { visitTree(that); }
  2568         public void visitMethodDef(JCMethodDecl that)        { visitTree(that); }
  2534         public void visitVarDef(JCVariableDecl that)         { visitTree(that); }
  2569         public void visitVarDef(JCVariableDecl that)         { visitTree(that); }
  2535         public void visitSkip(JCSkip that)                   { visitTree(that); }
  2570         public void visitSkip(JCSkip that)                   { visitTree(that); }