< prev index next >

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

Print this page




  81             return false;
  82         }
  83     }
  84 
  85     public static boolean isReceiverParam(JCTree tree) {
  86         if (tree.hasTag(VARDEF)) {
  87             return ((JCVariableDecl)tree).nameexpr != null;
  88         } else {
  89             return false;
  90         }
  91     }
  92 
  93     /** Is there a constructor declaration in the given list of trees?
  94      */
  95     public static boolean hasConstructors(List<JCTree> trees) {
  96         for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
  97             if (isConstructor(l.head)) return true;
  98         return false;
  99     }
 100 



















 101     public static boolean isMultiCatch(JCCatch catchClause) {
 102         return catchClause.param.vartype.hasTag(TYPEUNION);
 103     }
 104 
 105     /** Is statement an initializer for a synthetic field?
 106      */
 107     public static boolean isSyntheticInit(JCTree stat) {
 108         if (stat.hasTag(EXEC)) {
 109             JCExpressionStatement exec = (JCExpressionStatement)stat;
 110             if (exec.expr.hasTag(ASSIGN)) {
 111                 JCAssign assign = (JCAssign)exec.expr;
 112                 if (assign.lhs.hasTag(SELECT)) {
 113                     JCFieldAccess select = (JCFieldAccess)assign.lhs;
 114                     if (select.sym != null &&
 115                         (select.sym.flags() & SYNTHETIC) != 0) {
 116                         Name selected = name(select.selected);
 117                         if (selected != null && selected == selected.table.names._this)
 118                             return true;
 119                     }
 120                 }


 173                 return true;
 174             case SELECT:
 175                 return isThisQualifier(((JCFieldAccess)tree).selected);
 176             default:
 177                 return false;
 178         }
 179     }
 180 
 181     /** Is this a call to super?
 182      */
 183     public static boolean isSuperCall(JCTree tree) {
 184         Name name = calledMethodName(tree);
 185         if (name != null) {
 186             Names names = name.table.names;
 187             return name==names._super;
 188         } else {
 189             return false;
 190         }
 191     }
 192 

















 193     /** Is this a constructor whose first (non-synthetic) statement is not
 194      *  of the form this(...)?
 195      */
 196     public static boolean isInitialConstructor(JCTree tree) {
 197         JCMethodInvocation app = firstConstructorCall(tree);
 198         if (app == null) return false;
 199         Name meth = name(app.meth);
 200         return meth == null || meth != meth.table.names._this;
 201     }
 202 
 203     /** Return the first call in a constructor definition. */
 204     public static JCMethodInvocation firstConstructorCall(JCTree tree) {
 205         if (!tree.hasTag(METHODDEF)) return null;
 206         JCMethodDecl md = (JCMethodDecl) tree;
 207         Names names = md.name.table.names;
 208         if (md.name != names.init) return null;
 209         if (md.body == null) return null;
 210         List<JCStatement> stats = md.body.stats;
 211         // Synthetic initializations can appear before the super call.
 212         while (stats.nonEmpty() && isSyntheticInit(stats.head))


 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.
 870      */
 871     public static Symbol symbol(JCTree tree) {
 872         tree = skipParens(tree);
 873         switch (tree.getTag()) {
 874         case IDENT:
 875             return ((JCIdent) tree).sym;
 876         case SELECT:
 877             return ((JCFieldAccess) tree).sym;
 878         case TYPEAPPLY:
 879             return symbol(((JCTypeApply) tree).clazz);
 880         case ANNOTATED_TYPE:
 881             return symbol(((JCAnnotatedType) tree).underlyingType);
 882         case REFERENCE:
 883             return ((JCMemberReference) tree).sym;
 884         default:
 885             return null;
 886         }
 887     }
 888 


















 889     /** Return true if this is a nonstatic selection. */
 890     public static boolean nonstaticSelect(JCTree tree) {
 891         tree = skipParens(tree);
 892         if (!tree.hasTag(SELECT)) return false;
 893         JCFieldAccess s = (JCFieldAccess) tree;
 894         Symbol e = symbol(s.selected);
 895         return e == null || (e.kind != PCK && e.kind != TYP);
 896     }
 897 
 898     /** If this tree is an identifier or a field, set its symbol, otherwise skip.
 899      */
 900     public static void setSymbol(JCTree tree, Symbol sym) {
 901         tree = skipParens(tree);
 902         switch (tree.getTag()) {
 903         case IDENT:
 904             ((JCIdent) tree).sym = sym; break;
 905         case SELECT:
 906             ((JCFieldAccess) tree).sym = sym; break;
 907         default:
 908         }


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 }


  81             return false;
  82         }
  83     }
  84 
  85     public static boolean isReceiverParam(JCTree tree) {
  86         if (tree.hasTag(VARDEF)) {
  87             return ((JCVariableDecl)tree).nameexpr != null;
  88         } else {
  89             return false;
  90         }
  91     }
  92 
  93     /** Is there a constructor declaration in the given list of trees?
  94      */
  95     public static boolean hasConstructors(List<JCTree> trees) {
  96         for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
  97             if (isConstructor(l.head)) return true;
  98         return false;
  99     }
 100 
 101     /** Is there a constructor invocation in the given list of trees?
 102      */
 103     public static Name getConstructorInvocationName(List<? extends JCTree> trees, Names names, boolean isRecord) {
 104         for (JCTree tree : trees) {
 105             if (tree.hasTag(EXEC)) {
 106                 JCExpressionStatement stat = (JCExpressionStatement)tree;
 107                 if (stat.expr.hasTag(APPLY)) {
 108                     JCMethodInvocation apply = (JCMethodInvocation)stat.expr;
 109                     Name methName = TreeInfo.name(apply.meth);
 110                     if (methName == names._this ||
 111                         methName == names._super) {
 112                         return methName;
 113                     }
 114                 }
 115             }
 116         }
 117         return names.empty;
 118     }
 119 
 120     public static boolean isMultiCatch(JCCatch catchClause) {
 121         return catchClause.param.vartype.hasTag(TYPEUNION);
 122     }
 123 
 124     /** Is statement an initializer for a synthetic field?
 125      */
 126     public static boolean isSyntheticInit(JCTree stat) {
 127         if (stat.hasTag(EXEC)) {
 128             JCExpressionStatement exec = (JCExpressionStatement)stat;
 129             if (exec.expr.hasTag(ASSIGN)) {
 130                 JCAssign assign = (JCAssign)exec.expr;
 131                 if (assign.lhs.hasTag(SELECT)) {
 132                     JCFieldAccess select = (JCFieldAccess)assign.lhs;
 133                     if (select.sym != null &&
 134                         (select.sym.flags() & SYNTHETIC) != 0) {
 135                         Name selected = name(select.selected);
 136                         if (selected != null && selected == selected.table.names._this)
 137                             return true;
 138                     }
 139                 }


 192                 return true;
 193             case SELECT:
 194                 return isThisQualifier(((JCFieldAccess)tree).selected);
 195             default:
 196                 return false;
 197         }
 198     }
 199 
 200     /** Is this a call to super?
 201      */
 202     public static boolean isSuperCall(JCTree tree) {
 203         Name name = calledMethodName(tree);
 204         if (name != null) {
 205             Names names = name.table.names;
 206             return name==names._super;
 207         } else {
 208             return false;
 209         }
 210     }
 211 
 212     public static List<JCVariableDecl> recordFields(JCClassDecl tree) {
 213         return tree.defs.stream()
 214                 .filter(t -> t.hasTag(VARDEF))
 215                 .map(t -> (JCVariableDecl)t)
 216                 .filter(vd -> (vd.getModifiers().flags & (Flags.RECORD)) == RECORD)
 217                 .collect(List.collector());
 218     }
 219 
 220     public static List<Type> recordFieldTypes(JCClassDecl tree) {
 221         return tree.defs.stream()
 222                 .filter(t -> t.hasTag(VARDEF))
 223                 .map(t -> (JCVariableDecl)t)
 224                 .filter(vd -> (vd.getModifiers().flags & (Flags.RECORD)) == RECORD)
 225                 .map(vd -> vd.type)
 226                 .collect(List.collector());
 227     }
 228 
 229     /** Is this a constructor whose first (non-synthetic) statement is not
 230      *  of the form this(...)?
 231      */
 232     public static boolean isInitialConstructor(JCTree tree) {
 233         JCMethodInvocation app = firstConstructorCall(tree);
 234         if (app == null) return false;
 235         Name meth = name(app.meth);
 236         return meth == null || meth != meth.table.names._this;
 237     }
 238 
 239     /** Return the first call in a constructor definition. */
 240     public static JCMethodInvocation firstConstructorCall(JCTree tree) {
 241         if (!tree.hasTag(METHODDEF)) return null;
 242         JCMethodDecl md = (JCMethodDecl) tree;
 243         Names names = md.name.table.names;
 244         if (md.name != names.init) return null;
 245         if (md.body == null) return null;
 246         List<JCStatement> stats = md.body.stats;
 247         // Synthetic initializations can appear before the super call.
 248         while (stats.nonEmpty() && isSyntheticInit(stats.head))


 509             case NEWCLASS: {
 510                 JCNewClass node = (JCNewClass)tree;
 511                 if (node.encl != null)
 512                     return getStartPos(node.encl);
 513                 break;
 514             }
 515             case VARDEF: {
 516                 JCVariableDecl node = (JCVariableDecl)tree;
 517                 if (node.startPos != Position.NOPOS) {
 518                     return node.startPos;
 519                 } else if (node.mods.pos != Position.NOPOS) {
 520                     return node.mods.pos;
 521                 } else if (node.vartype == null || node.vartype.pos == Position.NOPOS) {
 522                     //if there's no type (partially typed lambda parameter)
 523                     //simply return node position
 524                     return node.pos;
 525                 } else {
 526                     return getStartPos(node.vartype);
 527                 }
 528             }
 529             case BINDINGPATTERN: {
 530                 JCBindingPattern node = (JCBindingPattern)tree;
 531                 return getStartPos(node.vartype);
 532             }
 533             case ERRONEOUS: {
 534                 JCErroneous node = (JCErroneous)tree;
 535                 if (node.errs != null && node.errs.nonEmpty())
 536                     return getStartPos(node.errs.head);
 537             }
 538         }
 539         return tree.pos;
 540     }
 541 
 542     /** The end position of given tree, given  a table of end positions generated by the parser
 543      */
 544     public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
 545         if (tree == null)
 546             return Position.NOPOS;
 547 
 548         if (endPosTable == null) {
 549             // fall back on limited info in the tree
 550             return endPos(tree);
 551         }
 552 


 597                 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
 598             case SYNCHRONIZED:
 599                 return getEndPos(((JCSynchronized) tree).body, endPosTable);
 600             case TOPLEVEL:
 601                 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
 602             case TRY: {
 603                 JCTry node = (JCTry)tree;
 604                 if (node.finalizer != null) {
 605                     return getEndPos(node.finalizer, endPosTable);
 606                 } else if (!node.catchers.isEmpty()) {
 607                     return getEndPos(node.catchers.last(), endPosTable);
 608                 } else {
 609                     return getEndPos(node.body, endPosTable);
 610                 }
 611             }
 612             case WILDCARD:
 613                 return getEndPos(((JCWildcard) tree).inner, endPosTable);
 614             case TYPECAST:
 615                 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
 616             case TYPETEST:
 617                 return getEndPos(((JCInstanceOf) tree).pattern, endPosTable);
 618             case WHILELOOP:
 619                 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
 620             case ANNOTATED_TYPE:
 621                 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
 622             case ERRONEOUS: {
 623                 JCErroneous node = (JCErroneous)tree;
 624                 if (node.errs != null && node.errs.nonEmpty())
 625                     return getEndPos(node.errs.last(), endPosTable);
 626             }
 627         }
 628         return Position.NOPOS;
 629     }
 630 
 631 
 632     /** A DiagnosticPosition with the preferred position set to the
 633      *  end position of given tree, if it is a block with
 634      *  defined endpos.
 635      */
 636     public static DiagnosticPosition diagEndPos(final JCTree tree) {
 637         final int endPos = TreeInfo.endPos(tree);


 870         case VARDEF:
 871             return ((JCVariableDecl) node).sym;
 872         case IDENT:
 873             return ((JCIdent) node).sym;
 874         case SELECT:
 875             return ((JCFieldAccess) node).sym;
 876         case REFERENCE:
 877             return ((JCMemberReference) node).sym;
 878         case NEWCLASS:
 879             return ((JCNewClass) node).constructor;
 880         case APPLY:
 881             return symbolFor(((JCMethodInvocation) node).meth);
 882         case TYPEAPPLY:
 883             return symbolFor(((JCTypeApply) node).clazz);
 884         case ANNOTATION:
 885         case TYPE_ANNOTATION:
 886         case TYPEPARAMETER:
 887             if (node.type != null)
 888                 return node.type.tsym;
 889             return null;
 890         case BINDINGPATTERN:
 891             return ((JCBindingPattern) node).symbol;
 892         default:
 893             return null;
 894         }
 895     }
 896 
 897     public static boolean isDeclaration(JCTree node) {
 898         node = skipParens(node);
 899         switch (node.getTag()) {
 900         case PACKAGEDEF:
 901         case CLASSDEF:
 902         case METHODDEF:
 903         case VARDEF:
 904             return true;
 905         default:
 906             return false;
 907         }
 908     }
 909 
 910     /** If this tree is an identifier or a field, return its symbol,
 911      *  otherwise return null.
 912      */
 913     public static Symbol symbol(JCTree tree) {
 914         tree = skipParens(tree);
 915         switch (tree.getTag()) {
 916         case IDENT:
 917             return ((JCIdent) tree).sym;
 918         case SELECT:
 919             return ((JCFieldAccess) tree).sym;
 920         case TYPEAPPLY:
 921             return symbol(((JCTypeApply) tree).clazz);
 922         case ANNOTATED_TYPE:
 923             return symbol(((JCAnnotatedType) tree).underlyingType);
 924         case REFERENCE:
 925             return ((JCMemberReference) tree).sym;
 926         default:
 927             return null;
 928         }
 929     }
 930 
 931     /** If this tree has a modifiers field, return it otherwise return null
 932      */
 933     public static JCModifiers getModifiers(JCTree tree) {
 934         tree = skipParens(tree);
 935         switch (tree.getTag()) {
 936             case VARDEF:
 937                 return ((JCVariableDecl) tree).mods;
 938             case METHODDEF:
 939                 return ((JCMethodDecl) tree).mods;
 940             case CLASSDEF:
 941                 return ((JCClassDecl) tree).mods;
 942             case MODULEDEF:
 943                 return ((JCModuleDecl) tree).mods;
 944             default:
 945                 return null;
 946         }
 947     }
 948 
 949     /** Return true if this is a nonstatic selection. */
 950     public static boolean nonstaticSelect(JCTree tree) {
 951         tree = skipParens(tree);
 952         if (!tree.hasTag(SELECT)) return false;
 953         JCFieldAccess s = (JCFieldAccess) tree;
 954         Symbol e = symbol(s.selected);
 955         return e == null || (e.kind != PCK && e.kind != TYP);
 956     }
 957 
 958     /** If this tree is an identifier or a field, set its symbol, otherwise skip.
 959      */
 960     public static void setSymbol(JCTree tree, Symbol sym) {
 961         tree = skipParens(tree);
 962         switch (tree.getTag()) {
 963         case IDENT:
 964             ((JCIdent) tree).sym = sym; break;
 965         case SELECT:
 966             ((JCFieldAccess) tree).sym = sym; break;
 967         default:
 968         }


1268         return finder.foundTypeAnno;
1269     }
1270 
1271     public static boolean isModuleInfo(JCCompilationUnit tree) {
1272         return tree.sourcefile.isNameCompatible("module-info", JavaFileObject.Kind.SOURCE)
1273                 && tree.getModuleDecl() != null;
1274     }
1275 
1276     public static JCModuleDecl getModule(JCCompilationUnit t) {
1277         if (t.defs.nonEmpty()) {
1278             JCTree def = t.defs.head;
1279             if (def.hasTag(MODULEDEF))
1280                 return (JCModuleDecl) def;
1281         }
1282         return null;
1283     }
1284 
1285     public static boolean isPackageInfo(JCCompilationUnit tree) {
1286         return tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
1287     }
1288 
1289 }
< prev index next >