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.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;
164 default:
165 return false;
166 }
167 }
168
169 /** Is this tree an identifier, possibly qualified by 'this'?
170 */
171 public static boolean isIdentOrThisDotIdent(JCTree tree) {
172 switch (tree.getTag()) {
173 case PARENS:
174 return isIdentOrThisDotIdent(skipParens(tree));
175 case IDENT:
176 return true;
177 case SELECT:
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 || ident.name == names._super;
199 }
200 case SELECT: {
201 JCFieldAccess select = (JCFieldAccess)tree;
202 Type selectedType = types.erasure(select.selected.type);
203 if (!selectedType.hasTag(TypeTag.CLASS))
204 return false;
205 Symbol.ClassSymbol currentClassSym = (Symbol.ClassSymbol)((Type.ClassType)types.erasure(currentClass)).tsym;
206 Symbol.ClassSymbol selectedClassSym = (Symbol.ClassSymbol)((Type.ClassType)selectedType).tsym;
207 Names names = select.name.table.names;
208 return currentClassSym.isSubClass(selectedClassSym, types) &&
209 (select.name == names._super ||
210 (select.name == names._this &&
211 (currentClassSym == selectedClassSym ||
212 !currentClassSym.isEnclosedBy(selectedClassSym))));
213 }
214 default:
215 return false;
216 }
217 }
218
219 /** Is this a call to super?
220 */
221 public static boolean isSuperCall(JCTree tree) {
222 Name name = calledMethodName(tree);
223 if (name != null) {
224 Names names = name.table.names;
225 return name==names._super;
226 } 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;
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 {
|