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;
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
|
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.Env;
35 import com.sun.tools.javac.tree.JCTree.*;
36 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
37 import com.sun.tools.javac.util.*;
38 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
39
40 import static com.sun.tools.javac.code.Flags.*;
41 import static com.sun.tools.javac.code.Kinds.Kind.*;
42 import com.sun.tools.javac.code.Symbol.VarSymbol;
43 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
44 import static com.sun.tools.javac.code.TypeTag.BOT;
45 import static com.sun.tools.javac.tree.JCTree.Tag.*;
46 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
47 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
48
49 import javax.lang.model.element.ElementKind;
50 import javax.tools.JavaFileObject;
51
52 import java.util.function.Function;
53 import java.util.function.Predicate;
178 return isThisQualifier(((JCFieldAccess)tree).selected);
179 default:
180 return false;
181 }
182 }
183
184 /** Check if the given tree is an explicit reference to the 'this' instance of the
185 * class currently being compiled. This is true if tree is:
186 * - An unqualified 'this' identifier
187 * - A 'super' identifier qualified by a class name whose type is 'currentClass' or a supertype
188 * - A 'this' identifier qualified by a class name whose type is 'currentClass' or a supertype
189 * but also NOT an enclosing outer class of 'currentClass'.
190 */
191 public static boolean isExplicitThisReference(Types types, Type.ClassType currentClass, JCTree tree) {
192 switch (tree.getTag()) {
193 case PARENS:
194 return isExplicitThisReference(types, currentClass, skipParens(tree));
195 case IDENT: {
196 JCIdent ident = (JCIdent)tree;
197 Names names = ident.name.table.names;
198 return ident.name == names._this && tree.type.tsym == currentClass.tsym ||
199 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
|