< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java

Print this page




 473             case NEWCLASS: {
 474                 JCNewClass node = (JCNewClass)tree;
 475                 if (node.encl != null)
 476                     return getStartPos(node.encl);
 477                 break;
 478             }
 479             case VARDEF: {
 480                 JCVariableDecl node = (JCVariableDecl)tree;
 481                 if (node.startPos != Position.NOPOS) {
 482                     return node.startPos;
 483                 } else if (node.mods.pos != Position.NOPOS) {
 484                     return node.mods.pos;
 485                 } else if (node.vartype == null || node.vartype.pos == Position.NOPOS) {
 486                     //if there's no type (partially typed lambda parameter)
 487                     //simply return node position
 488                     return node.pos;
 489                 } else {
 490                     return getStartPos(node.vartype);
 491                 }
 492             }




 493             case ERRONEOUS: {
 494                 JCErroneous node = (JCErroneous)tree;
 495                 if (node.errs != null && node.errs.nonEmpty())
 496                     return getStartPos(node.errs.head);
 497             }
 498         }
 499         return tree.pos;
 500     }
 501 
 502     /** The end position of given tree, given  a table of end positions generated by the parser
 503      */
 504     public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
 505         if (tree == null)
 506             return Position.NOPOS;
 507 
 508         if (endPosTable == null) {
 509             // fall back on limited info in the tree
 510             return endPos(tree);
 511         }
 512 


 557                 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
 558             case SYNCHRONIZED:
 559                 return getEndPos(((JCSynchronized) tree).body, endPosTable);
 560             case TOPLEVEL:
 561                 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
 562             case TRY: {
 563                 JCTry node = (JCTry)tree;
 564                 if (node.finalizer != null) {
 565                     return getEndPos(node.finalizer, endPosTable);
 566                 } else if (!node.catchers.isEmpty()) {
 567                     return getEndPos(node.catchers.last(), endPosTable);
 568                 } else {
 569                     return getEndPos(node.body, endPosTable);
 570                 }
 571             }
 572             case WILDCARD:
 573                 return getEndPos(((JCWildcard) tree).inner, endPosTable);
 574             case TYPECAST:
 575                 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
 576             case TYPETEST:
 577                 return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
 578             case WHILELOOP:
 579                 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
 580             case ANNOTATED_TYPE:
 581                 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
 582             case ERRONEOUS: {
 583                 JCErroneous node = (JCErroneous)tree;
 584                 if (node.errs != null && node.errs.nonEmpty())
 585                     return getEndPos(node.errs.last(), endPosTable);
 586             }
 587         }
 588         return Position.NOPOS;
 589     }
 590 
 591 
 592     /** A DiagnosticPosition with the preferred position set to the
 593      *  end position of given tree, if it is a block with
 594      *  defined endpos.
 595      */
 596     public static DiagnosticPosition diagEndPos(final JCTree tree) {
 597         final int endPos = TreeInfo.endPos(tree);


 830         case VARDEF:
 831             return ((JCVariableDecl) node).sym;
 832         case IDENT:
 833             return ((JCIdent) node).sym;
 834         case SELECT:
 835             return ((JCFieldAccess) node).sym;
 836         case REFERENCE:
 837             return ((JCMemberReference) node).sym;
 838         case NEWCLASS:
 839             return ((JCNewClass) node).constructor;
 840         case APPLY:
 841             return symbolFor(((JCMethodInvocation) node).meth);
 842         case TYPEAPPLY:
 843             return symbolFor(((JCTypeApply) node).clazz);
 844         case ANNOTATION:
 845         case TYPE_ANNOTATION:
 846         case TYPEPARAMETER:
 847             if (node.type != null)
 848                 return node.type.tsym;
 849             return null;


 850         default:
 851             return null;
 852         }
 853     }
 854 
 855     public static boolean isDeclaration(JCTree node) {
 856         node = skipParens(node);
 857         switch (node.getTag()) {
 858         case PACKAGEDEF:
 859         case CLASSDEF:
 860         case METHODDEF:
 861         case VARDEF:
 862             return true;
 863         default:
 864             return false;
 865         }
 866     }
 867 
 868     /** If this tree is an identifier or a field, return its symbol,
 869      *  otherwise return null.


1208         return finder.foundTypeAnno;
1209     }
1210 
1211     public static boolean isModuleInfo(JCCompilationUnit tree) {
1212         return tree.sourcefile.isNameCompatible("module-info", JavaFileObject.Kind.SOURCE)
1213                 && tree.getModuleDecl() != null;
1214     }
1215 
1216     public static JCModuleDecl getModule(JCCompilationUnit t) {
1217         if (t.defs.nonEmpty()) {
1218             JCTree def = t.defs.head;
1219             if (def.hasTag(MODULEDEF))
1220                 return (JCModuleDecl) def;
1221         }
1222         return null;
1223     }
1224 
1225     public static boolean isPackageInfo(JCCompilationUnit tree) {
1226         return tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
1227     }

1228 }


 473             case NEWCLASS: {
 474                 JCNewClass node = (JCNewClass)tree;
 475                 if (node.encl != null)
 476                     return getStartPos(node.encl);
 477                 break;
 478             }
 479             case VARDEF: {
 480                 JCVariableDecl node = (JCVariableDecl)tree;
 481                 if (node.startPos != Position.NOPOS) {
 482                     return node.startPos;
 483                 } else if (node.mods.pos != Position.NOPOS) {
 484                     return node.mods.pos;
 485                 } else if (node.vartype == null || node.vartype.pos == Position.NOPOS) {
 486                     //if there's no type (partially typed lambda parameter)
 487                     //simply return node position
 488                     return node.pos;
 489                 } else {
 490                     return getStartPos(node.vartype);
 491                 }
 492             }
 493             case BINDINGPATTERN: {
 494                 JCBindingPattern node = (JCBindingPattern)tree;
 495                 return getStartPos(node.vartype);
 496             }
 497             case ERRONEOUS: {
 498                 JCErroneous node = (JCErroneous)tree;
 499                 if (node.errs != null && node.errs.nonEmpty())
 500                     return getStartPos(node.errs.head);
 501             }
 502         }
 503         return tree.pos;
 504     }
 505 
 506     /** The end position of given tree, given  a table of end positions generated by the parser
 507      */
 508     public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
 509         if (tree == null)
 510             return Position.NOPOS;
 511 
 512         if (endPosTable == null) {
 513             // fall back on limited info in the tree
 514             return endPos(tree);
 515         }
 516 


 561                 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
 562             case SYNCHRONIZED:
 563                 return getEndPos(((JCSynchronized) tree).body, endPosTable);
 564             case TOPLEVEL:
 565                 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
 566             case TRY: {
 567                 JCTry node = (JCTry)tree;
 568                 if (node.finalizer != null) {
 569                     return getEndPos(node.finalizer, endPosTable);
 570                 } else if (!node.catchers.isEmpty()) {
 571                     return getEndPos(node.catchers.last(), endPosTable);
 572                 } else {
 573                     return getEndPos(node.body, endPosTable);
 574                 }
 575             }
 576             case WILDCARD:
 577                 return getEndPos(((JCWildcard) tree).inner, endPosTable);
 578             case TYPECAST:
 579                 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
 580             case TYPETEST:
 581                 return getEndPos(((JCInstanceOf) tree).pattern, endPosTable);
 582             case WHILELOOP:
 583                 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
 584             case ANNOTATED_TYPE:
 585                 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
 586             case ERRONEOUS: {
 587                 JCErroneous node = (JCErroneous)tree;
 588                 if (node.errs != null && node.errs.nonEmpty())
 589                     return getEndPos(node.errs.last(), endPosTable);
 590             }
 591         }
 592         return Position.NOPOS;
 593     }
 594 
 595 
 596     /** A DiagnosticPosition with the preferred position set to the
 597      *  end position of given tree, if it is a block with
 598      *  defined endpos.
 599      */
 600     public static DiagnosticPosition diagEndPos(final JCTree tree) {
 601         final int endPos = TreeInfo.endPos(tree);


 834         case VARDEF:
 835             return ((JCVariableDecl) node).sym;
 836         case IDENT:
 837             return ((JCIdent) node).sym;
 838         case SELECT:
 839             return ((JCFieldAccess) node).sym;
 840         case REFERENCE:
 841             return ((JCMemberReference) node).sym;
 842         case NEWCLASS:
 843             return ((JCNewClass) node).constructor;
 844         case APPLY:
 845             return symbolFor(((JCMethodInvocation) node).meth);
 846         case TYPEAPPLY:
 847             return symbolFor(((JCTypeApply) node).clazz);
 848         case ANNOTATION:
 849         case TYPE_ANNOTATION:
 850         case TYPEPARAMETER:
 851             if (node.type != null)
 852                 return node.type.tsym;
 853             return null;
 854         case BINDINGPATTERN:
 855             return ((JCBindingPattern) node).symbol;
 856         default:
 857             return null;
 858         }
 859     }
 860 
 861     public static boolean isDeclaration(JCTree node) {
 862         node = skipParens(node);
 863         switch (node.getTag()) {
 864         case PACKAGEDEF:
 865         case CLASSDEF:
 866         case METHODDEF:
 867         case VARDEF:
 868             return true;
 869         default:
 870             return false;
 871         }
 872     }
 873 
 874     /** If this tree is an identifier or a field, return its symbol,
 875      *  otherwise return null.


1214         return finder.foundTypeAnno;
1215     }
1216 
1217     public static boolean isModuleInfo(JCCompilationUnit tree) {
1218         return tree.sourcefile.isNameCompatible("module-info", JavaFileObject.Kind.SOURCE)
1219                 && tree.getModuleDecl() != null;
1220     }
1221 
1222     public static JCModuleDecl getModule(JCCompilationUnit t) {
1223         if (t.defs.nonEmpty()) {
1224             JCTree def = t.defs.head;
1225             if (def.hasTag(MODULEDEF))
1226                 return (JCModuleDecl) def;
1227         }
1228         return null;
1229     }
1230 
1231     public static boolean isPackageInfo(JCCompilationUnit tree) {
1232         return tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
1233     }
1234 
1235 }
< prev index next >