< prev index next >

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

Print this page

   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.tree;
  27 
  28 
  29 
  30 import com.sun.source.tree.Tree;
  31 import com.sun.source.util.TreePath;
  32 import com.sun.tools.javac.code.*;
  33 import com.sun.tools.javac.code.Symbol.RecordComponent;
  34 import com.sun.tools.javac.comp.AttrContext;
  35 import com.sun.tools.javac.comp.Env;
  36 import com.sun.tools.javac.tree.JCTree.*;
  37 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  38 import com.sun.tools.javac.util.*;
  39 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  40 
  41 import static com.sun.tools.javac.code.Flags.*;
  42 import static com.sun.tools.javac.code.Kinds.Kind.*;
  43 import com.sun.tools.javac.code.Symbol.VarSymbol;
  44 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
  45 import static com.sun.tools.javac.code.TypeTag.BOT;
  46 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  47 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
  48 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
  49 
  50 import javax.lang.model.element.ElementKind;
  51 import javax.tools.JavaFileObject;
  52 
  53 import java.util.function.Function;
  54 import java.util.function.Predicate;

 165             default:
 166                 return false;
 167         }
 168     }
 169 
 170     /** Is this tree an identifier, possibly qualified by 'this'?
 171      */
 172     public static boolean isIdentOrThisDotIdent(JCTree tree) {
 173         switch (tree.getTag()) {
 174             case PARENS:
 175                 return isIdentOrThisDotIdent(skipParens(tree));
 176             case IDENT:
 177                 return true;
 178             case SELECT:
 179                 return isThisQualifier(((JCFieldAccess)tree).selected);
 180             default:
 181                 return false;
 182         }
 183     }
 184 






























 185     /** Check if the given tree is an explicit reference to the 'this' instance of the
 186      *  class currently being compiled. This is true if tree is:
 187      *  - An unqualified 'this' identifier
 188      *  - A 'super' identifier qualified by a class name whose type is 'currentClass' or a supertype
 189      *  - A 'this' identifier qualified by a class name whose type is 'currentClass' or a supertype
 190      *    but also NOT an enclosing outer class of 'currentClass'.
 191      */
 192     public static boolean isExplicitThisReference(Types types, Type.ClassType currentClass, JCTree tree) {

 193         switch (tree.getTag()) {
 194             case PARENS:
 195                 return isExplicitThisReference(types, currentClass, skipParens(tree));
 196             case IDENT: {
 197                 JCIdent ident = (JCIdent)tree;
 198                 Names names = ident.name.table.names;
 199                 return ident.name == names._this || ident.name == names._super;



 200             }
 201             case SELECT: {
 202                 JCFieldAccess select = (JCFieldAccess)tree;
 203                 Type selectedType = types.erasure(select.selected.type);
 204                 if (!selectedType.hasTag(TypeTag.CLASS))
 205                     return false;
 206                 Symbol.ClassSymbol currentClassSym = (Symbol.ClassSymbol)((Type.ClassType)types.erasure(currentClass)).tsym;
 207                 Symbol.ClassSymbol selectedClassSym = (Symbol.ClassSymbol)((Type.ClassType)selectedType).tsym;
 208                 Names names = select.name.table.names;
 209                 return currentClassSym.isSubClass(selectedClassSym, types) &&
 210                         (select.name == names._super ||
 211                         (select.name == names._this &&
 212                             (currentClassSym == selectedClassSym ||
 213                             !currentClassSym.isEnclosedBy(selectedClassSym))));
 214             }
 215             default:
 216                 return false;
 217         }
 218     }
 219 
 220     /** Is this a call to super?
 221      */
 222     public static boolean isSuperCall(JCTree tree) {
 223         Name name = calledMethodName(tree);
 224         if (name != null) {
 225             Names names = name.table.names;
 226             return name==names._super;
 227         } else {

   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.tree;
  27 


  28 import com.sun.source.tree.Tree;
  29 import com.sun.source.util.TreePath;
  30 import com.sun.tools.javac.code.*;
  31 import com.sun.tools.javac.code.Symbol.RecordComponent;

  32 import com.sun.tools.javac.comp.Env;
  33 import com.sun.tools.javac.tree.JCTree.*;
  34 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  35 import com.sun.tools.javac.util.*;
  36 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  37 
  38 import static com.sun.tools.javac.code.Flags.*;
  39 import static com.sun.tools.javac.code.Kinds.Kind.*;
  40 import com.sun.tools.javac.code.Symbol.VarSymbol;
  41 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
  42 import static com.sun.tools.javac.code.TypeTag.BOT;
  43 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  44 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
  45 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
  46 
  47 import javax.lang.model.element.ElementKind;
  48 import javax.tools.JavaFileObject;
  49 
  50 import java.util.function.Function;
  51 import java.util.function.Predicate;

 162             default:
 163                 return false;
 164         }
 165     }
 166 
 167     /** Is this tree an identifier, possibly qualified by 'this'?
 168      */
 169     public static boolean isIdentOrThisDotIdent(JCTree tree) {
 170         switch (tree.getTag()) {
 171             case PARENS:
 172                 return isIdentOrThisDotIdent(skipParens(tree));
 173             case IDENT:
 174                 return true;
 175             case SELECT:
 176                 return isThisQualifier(((JCFieldAccess)tree).selected);
 177             default:
 178                 return false;
 179         }
 180     }
 181 
 182     /** Is this tree `super`, or `Ident.super`?
 183      */
 184     public static boolean isSuperOrSelectorDotSuper(JCTree tree) {
 185         switch (tree.getTag()) {
 186             case PARENS:
 187                 return isSuperOrSelectorDotSuper(skipParens(tree));
 188             case IDENT:
 189                 return ((JCIdent)tree).name == ((JCIdent)tree).name.table.names._super;
 190             case SELECT:
 191                 return ((JCFieldAccess)tree).name == ((JCFieldAccess)tree).name.table.names._super;
 192             default:
 193                 return false;
 194         }
 195     }
 196 
 197     /** Is this tree `this`, or `Ident.this`?
 198      */
 199     public static boolean isThisOrSelectorDotThis(JCTree tree) {
 200         switch (tree.getTag()) {
 201             case PARENS:
 202                 return isThisOrSelectorDotThis(skipParens(tree));
 203             case IDENT:
 204                 return ((JCIdent)tree).name == ((JCIdent)tree).name.table.names._this;
 205             case SELECT:
 206                 return ((JCFieldAccess)tree).name == ((JCFieldAccess)tree).name.table.names._this;
 207             default:
 208                 return false;
 209         }
 210     }
 211 
 212     /** Check if the given tree is an explicit reference to the 'this' instance of the
 213      *  class currently being compiled. This is true if tree is:
 214      *  - An unqualified 'this' identifier
 215      *  - A 'super' identifier qualified by a class name whose type is 'currentClass' or a supertype
 216      *  - A 'this' identifier qualified by a class name whose type is 'currentClass' or a supertype
 217      *    but also NOT an enclosing outer class of 'currentClass'.
 218      */
 219     public static boolean isExplicitThisReference(Types types, Type.ClassType currentClass, JCTree tree) {
 220         Symbol.ClassSymbol currentClassSym = (Symbol.ClassSymbol) types.erasure(currentClass).tsym;
 221         switch (tree.getTag()) {
 222             case PARENS:
 223                 return isExplicitThisReference(types, currentClass, skipParens(tree));
 224             case IDENT: {
 225                 JCIdent ident = (JCIdent)tree;
 226                 Names names = ident.name.table.names;
 227                 return ident.name == names._this && tree.type.tsym == currentClass.tsym ||
 228                        ident.name == names._super &&
 229                                (tree.type.tsym == currentClass.tsym ||
 230                                 currentClassSym.isSubClass(tree.type.tsym, types));
 231             }
 232             case SELECT: {
 233                 JCFieldAccess select = (JCFieldAccess)tree;
 234                 Type selectedType = types.erasure(select.selected.type);
 235                 if (!selectedType.hasTag(TypeTag.CLASS))
 236                     return false;
 237                 Symbol.ClassSymbol selectedClassSym = (Symbol.ClassSymbol)(selectedType).tsym;

 238                 Names names = select.name.table.names;
 239                 return currentClassSym.isSubClass(selectedClassSym, types) &&
 240                         (select.name == names._super ||
 241                         (select.name == names._this &&
 242                             (currentClassSym == selectedClassSym ||
 243                             !currentClassSym.isEnclosedBy(selectedClassSym))));
 244             }
 245             default:
 246                 return false;
 247         }
 248     }
 249 
 250     /** Is this a call to super?
 251      */
 252     public static boolean isSuperCall(JCTree tree) {
 253         Name name = calledMethodName(tree);
 254         if (name != null) {
 255             Names names = name.table.names;
 256             return name==names._super;
 257         } else {
< prev index next >