Fri, 12 Nov 2010 12:33:52 +0000
6999067: cast for invokeExact call gets redundant cast to <type> warnings
Summary: Xlint:cast should not report cast used in order to specify target type in polymorphic signature calls
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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 */
26 package com.sun.tools.javac.comp;
28 import java.util.*;
29 import javax.tools.JavaFileObject;
30 import javax.tools.JavaFileManager;
32 import com.sun.tools.javac.code.*;
33 import com.sun.tools.javac.jvm.*;
34 import com.sun.tools.javac.tree.*;
35 import com.sun.tools.javac.util.*;
36 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
37 import com.sun.tools.javac.util.List;
39 import com.sun.tools.javac.code.Type.*;
40 import com.sun.tools.javac.code.Symbol.*;
41 import com.sun.tools.javac.main.RecognizedOptions.PkgInfo;
42 import com.sun.tools.javac.tree.JCTree.*;
44 import static com.sun.tools.javac.code.Flags.*;
45 import static com.sun.tools.javac.code.Kinds.*;
47 /** This class enters symbols for all encountered definitions into
48 * the symbol table. The pass consists of two phases, organized as
49 * follows:
50 *
51 * <p>In the first phase, all class symbols are intered into their
52 * enclosing scope, descending recursively down the tree for classes
53 * which are members of other classes. The class symbols are given a
54 * MemberEnter object as completer.
55 *
56 * <p>In the second phase classes are completed using
57 * MemberEnter.complete(). Completion might occur on demand, but
58 * any classes that are not completed that way will be eventually
59 * completed by processing the `uncompleted' queue. Completion
60 * entails (1) determination of a class's parameters, supertype and
61 * interfaces, as well as (2) entering all symbols defined in the
62 * class into its scope, with the exception of class symbols which
63 * have been entered in phase 1. (2) depends on (1) having been
64 * completed for a class and all its superclasses and enclosing
65 * classes. That's why, after doing (1), we put classes in a
66 * `halfcompleted' queue. Only when we have performed (1) for a class
67 * and all it's superclasses and enclosing classes, we proceed to
68 * (2).
69 *
70 * <p>Whereas the first phase is organized as a sweep through all
71 * compiled syntax trees, the second phase is demand. Members of a
72 * class are entered when the contents of a class are first
73 * accessed. This is accomplished by installing completer objects in
74 * class symbols for compiled classes which invoke the member-enter
75 * phase for the corresponding class tree.
76 *
77 * <p>Classes migrate from one phase to the next via queues:
78 *
79 * <pre>
80 * class enter -> (Enter.uncompleted) --> member enter (1)
81 * -> (MemberEnter.halfcompleted) --> member enter (2)
82 * -> (Todo) --> attribute
83 * (only for toplevel classes)
84 * </pre>
85 *
86 * <p><b>This is NOT part of any supported API.
87 * If you write code that depends on this, you do so at your own risk.
88 * This code and its internal interfaces are subject to change or
89 * deletion without notice.</b>
90 */
91 public class Enter extends JCTree.Visitor {
92 protected static final Context.Key<Enter> enterKey =
93 new Context.Key<Enter>();
95 Log log;
96 Symtab syms;
97 Scope.ScopeCounter scopeCounter;
98 Check chk;
99 TreeMaker make;
100 ClassReader reader;
101 Annotate annotate;
102 MemberEnter memberEnter;
103 Types types;
104 Lint lint;
105 Names names;
106 JavaFileManager fileManager;
107 PkgInfo pkginfoOpt;
109 private final Todo todo;
111 public static Enter instance(Context context) {
112 Enter instance = context.get(enterKey);
113 if (instance == null)
114 instance = new Enter(context);
115 return instance;
116 }
118 protected Enter(Context context) {
119 context.put(enterKey, this);
121 log = Log.instance(context);
122 reader = ClassReader.instance(context);
123 make = TreeMaker.instance(context);
124 syms = Symtab.instance(context);
125 scopeCounter = Scope.ScopeCounter.instance(context);
126 chk = Check.instance(context);
127 memberEnter = MemberEnter.instance(context);
128 types = Types.instance(context);
129 annotate = Annotate.instance(context);
130 lint = Lint.instance(context);
131 names = Names.instance(context);
133 predefClassDef = make.ClassDef(
134 make.Modifiers(PUBLIC),
135 syms.predefClass.name, null, null, null, null);
136 predefClassDef.sym = syms.predefClass;
137 todo = Todo.instance(context);
138 fileManager = context.get(JavaFileManager.class);
140 Options options = Options.instance(context);
141 pkginfoOpt = PkgInfo.get(options);
142 }
144 /** A hashtable mapping classes and packages to the environments current
145 * at the points of their definitions.
146 */
147 Map<TypeSymbol,Env<AttrContext>> typeEnvs =
148 new HashMap<TypeSymbol,Env<AttrContext>>();
150 /** Accessor for typeEnvs
151 */
152 public Env<AttrContext> getEnv(TypeSymbol sym) {
153 return typeEnvs.get(sym);
154 }
156 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
157 Env<AttrContext> localEnv = getEnv(sym);
158 Env<AttrContext> lintEnv = localEnv;
159 while (lintEnv.info.lint == null)
160 lintEnv = lintEnv.next;
161 localEnv.info.lint = lintEnv.info.lint.augment(sym.attributes_field, sym.flags());
162 return localEnv;
163 }
165 /** The queue of all classes that might still need to be completed;
166 * saved and initialized by main().
167 */
168 ListBuffer<ClassSymbol> uncompleted;
170 /** A dummy class to serve as enclClass for toplevel environments.
171 */
172 private JCClassDecl predefClassDef;
174 /* ************************************************************************
175 * environment construction
176 *************************************************************************/
179 /** Create a fresh environment for class bodies.
180 * This will create a fresh scope for local symbols of a class, referred
181 * to by the environments info.scope field.
182 * This scope will contain
183 * - symbols for this and super
184 * - symbols for any type parameters
185 * In addition, it serves as an anchor for scopes of methods and initializers
186 * which are nested in this scope via Scope.dup().
187 * This scope should not be confused with the members scope of a class.
188 *
189 * @param tree The class definition.
190 * @param env The environment current outside of the class definition.
191 */
192 public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
193 Env<AttrContext> localEnv =
194 env.dup(tree, env.info.dup(new Scope.ClassScope(tree.sym, scopeCounter)));
195 localEnv.enclClass = tree;
196 localEnv.outer = env;
197 localEnv.info.isSelfCall = false;
198 localEnv.info.lint = null; // leave this to be filled in by Attr,
199 // when annotations have been processed
200 return localEnv;
201 }
203 /** Create a fresh environment for toplevels.
204 * @param tree The toplevel tree.
205 */
206 Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
207 Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
208 localEnv.toplevel = tree;
209 localEnv.enclClass = predefClassDef;
210 tree.namedImportScope = new Scope.ImportScope(tree.packge);
211 tree.starImportScope = new Scope.ImportScope(tree.packge);
212 localEnv.info.scope = tree.namedImportScope;
213 localEnv.info.lint = lint;
214 return localEnv;
215 }
217 public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) {
218 Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
219 localEnv.toplevel = tree;
220 localEnv.enclClass = predefClassDef;
221 localEnv.info.scope = tree.namedImportScope;
222 localEnv.info.lint = lint;
223 return localEnv;
224 }
226 /** The scope in which a member definition in environment env is to be entered
227 * This is usually the environment's scope, except for class environments,
228 * where the local scope is for type variables, and the this and super symbol
229 * only, and members go into the class member scope.
230 */
231 Scope enterScope(Env<AttrContext> env) {
232 return (env.tree.getTag() == JCTree.CLASSDEF)
233 ? ((JCClassDecl) env.tree).sym.members_field
234 : env.info.scope;
235 }
237 /* ************************************************************************
238 * Visitor methods for phase 1: class enter
239 *************************************************************************/
241 /** Visitor argument: the current environment.
242 */
243 protected Env<AttrContext> env;
245 /** Visitor result: the computed type.
246 */
247 Type result;
249 /** Visitor method: enter all classes in given tree, catching any
250 * completion failure exceptions. Return the tree's type.
251 *
252 * @param tree The tree to be visited.
253 * @param env The environment visitor argument.
254 */
255 Type classEnter(JCTree tree, Env<AttrContext> env) {
256 Env<AttrContext> prevEnv = this.env;
257 try {
258 this.env = env;
259 tree.accept(this);
260 return result;
261 } catch (CompletionFailure ex) {
262 return chk.completionError(tree.pos(), ex);
263 } finally {
264 this.env = prevEnv;
265 }
266 }
268 /** Visitor method: enter classes of a list of trees, returning a list of types.
269 */
270 <T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) {
271 ListBuffer<Type> ts = new ListBuffer<Type>();
272 for (List<T> l = trees; l.nonEmpty(); l = l.tail) {
273 Type t = classEnter(l.head, env);
274 if (t != null)
275 ts.append(t);
276 }
277 return ts.toList();
278 }
280 @Override
281 public void visitTopLevel(JCCompilationUnit tree) {
282 JavaFileObject prev = log.useSource(tree.sourcefile);
283 boolean addEnv = false;
284 boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info",
285 JavaFileObject.Kind.SOURCE);
286 if (tree.pid != null) {
287 tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid));
288 if (tree.packageAnnotations.nonEmpty() || pkginfoOpt == PkgInfo.ALWAYS) {
289 if (isPkgInfo) {
290 addEnv = true;
291 } else {
292 log.error(tree.packageAnnotations.head.pos(),
293 "pkg.annotations.sb.in.package-info.java");
294 }
295 }
296 } else {
297 tree.packge = syms.unnamedPackage;
298 }
299 tree.packge.complete(); // Find all classes in package.
300 Env<AttrContext> topEnv = topLevelEnv(tree);
302 // Save environment of package-info.java file.
303 if (isPkgInfo) {
304 Env<AttrContext> env0 = typeEnvs.get(tree.packge);
305 if (env0 == null) {
306 typeEnvs.put(tree.packge, topEnv);
307 } else {
308 JCCompilationUnit tree0 = env0.toplevel;
309 if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
310 log.warning(tree.pid != null ? tree.pid.pos()
311 : null,
312 "pkg-info.already.seen",
313 tree.packge);
314 if (addEnv || (tree0.packageAnnotations.isEmpty() &&
315 tree.docComments != null &&
316 tree.docComments.get(tree) != null)) {
317 typeEnvs.put(tree.packge, topEnv);
318 }
319 }
320 }
322 for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner)
323 q.flags_field |= EXISTS;
325 Name name = names.package_info;
326 ClassSymbol c = reader.enterClass(name, tree.packge);
327 c.flatname = names.fromString(tree.packge + "." + name);
328 c.sourcefile = tree.sourcefile;
329 c.completer = null;
330 c.members_field = new Scope.ClassScope(c, scopeCounter);
331 tree.packge.package_info = c;
332 }
333 classEnter(tree.defs, topEnv);
334 if (addEnv) {
335 todo.append(topEnv);
336 }
337 log.useSource(prev);
338 result = null;
339 }
341 @Override
342 public void visitClassDef(JCClassDecl tree) {
343 Symbol owner = env.info.scope.owner;
344 Scope enclScope = enterScope(env);
345 ClassSymbol c;
346 if (owner.kind == PCK) {
347 // We are seeing a toplevel class.
348 PackageSymbol packge = (PackageSymbol)owner;
349 for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner)
350 q.flags_field |= EXISTS;
351 c = reader.enterClass(tree.name, packge);
352 packge.members().enterIfAbsent(c);
353 if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) {
354 log.error(tree.pos(),
355 "class.public.should.be.in.file", tree.name);
356 }
357 } else {
358 if (!tree.name.isEmpty() &&
359 !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) {
360 result = null;
361 return;
362 }
363 if (owner.kind == TYP) {
364 // We are seeing a member class.
365 c = reader.enterClass(tree.name, (TypeSymbol)owner);
366 if ((owner.flags_field & INTERFACE) != 0) {
367 tree.mods.flags |= PUBLIC | STATIC;
368 }
369 } else {
370 // We are seeing a local class.
371 c = reader.defineClass(tree.name, owner);
372 c.flatname = chk.localClassName(c);
373 if (!c.name.isEmpty())
374 chk.checkTransparentClass(tree.pos(), c, env.info.scope);
375 }
376 }
377 tree.sym = c;
379 // Enter class into `compiled' table and enclosing scope.
380 if (chk.compiled.get(c.flatname) != null) {
381 duplicateClass(tree.pos(), c);
382 result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
383 tree.sym = (ClassSymbol)result.tsym;
384 return;
385 }
386 chk.compiled.put(c.flatname, c);
387 enclScope.enter(c);
389 // Set up an environment for class block and store in `typeEnvs'
390 // table, to be retrieved later in memberEnter and attribution.
391 Env<AttrContext> localEnv = classEnv(tree, env);
392 typeEnvs.put(c, localEnv);
394 // Fill out class fields.
395 c.completer = memberEnter;
396 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
397 c.sourcefile = env.toplevel.sourcefile;
398 c.members_field = new Scope.ClassScope(c, scopeCounter);
400 ClassType ct = (ClassType)c.type;
401 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
402 // We are seeing a local or inner class.
403 // Set outer_field of this class to closest enclosing class
404 // which contains this class in a non-static context
405 // (its "enclosing instance class"), provided such a class exists.
406 Symbol owner1 = owner;
407 while ((owner1.kind & (VAR | MTH)) != 0 &&
408 (owner1.flags_field & STATIC) == 0) {
409 owner1 = owner1.owner;
410 }
411 if (owner1.kind == TYP) {
412 ct.setEnclosingType(owner1.type);
413 }
414 }
416 // Enter type parameters.
417 ct.typarams_field = classEnter(tree.typarams, localEnv);
419 // Add non-local class to uncompleted, to make sure it will be
420 // completed later.
421 if (!c.isLocal() && uncompleted != null) uncompleted.append(c);
422 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
424 // Recursively enter all member classes.
425 classEnter(tree.defs, localEnv);
427 result = c.type;
428 }
429 //where
430 /** Does class have the same name as the file it appears in?
431 */
432 private static boolean classNameMatchesFileName(ClassSymbol c,
433 Env<AttrContext> env) {
434 return env.toplevel.sourcefile.isNameCompatible(c.name.toString(),
435 JavaFileObject.Kind.SOURCE);
436 }
438 /** Complain about a duplicate class. */
439 protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) {
440 log.error(pos, "duplicate.class", c.fullname);
441 }
443 /** Class enter visitor method for type parameters.
444 * Enter a symbol for type parameter in local scope, after checking that it
445 * is unique.
446 */
447 @Override
448 public void visitTypeParameter(JCTypeParameter tree) {
449 TypeVar a = (tree.type != null)
450 ? (TypeVar)tree.type
451 : new TypeVar(tree.name, env.info.scope.owner, syms.botType);
452 tree.type = a;
453 if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) {
454 env.info.scope.enter(a.tsym);
455 }
456 result = a;
457 }
459 /** Default class enter visitor method: do nothing.
460 */
461 @Override
462 public void visitTree(JCTree tree) {
463 result = null;
464 }
466 /** Main method: enter all classes in a list of toplevel trees.
467 * @param trees The list of trees to be processed.
468 */
469 public void main(List<JCCompilationUnit> trees) {
470 complete(trees, null);
471 }
473 /** Main method: enter one class from a list of toplevel trees and
474 * place the rest on uncompleted for later processing.
475 * @param trees The list of trees to be processed.
476 * @param c The class symbol to be processed.
477 */
478 public void complete(List<JCCompilationUnit> trees, ClassSymbol c) {
479 annotate.enterStart();
480 ListBuffer<ClassSymbol> prevUncompleted = uncompleted;
481 if (memberEnter.completionEnabled) uncompleted = new ListBuffer<ClassSymbol>();
483 try {
484 // enter all classes, and construct uncompleted list
485 classEnter(trees, null);
487 // complete all uncompleted classes in memberEnter
488 if (memberEnter.completionEnabled) {
489 while (uncompleted.nonEmpty()) {
490 ClassSymbol clazz = uncompleted.next();
491 if (c == null || c == clazz || prevUncompleted == null)
492 clazz.complete();
493 else
494 // defer
495 prevUncompleted.append(clazz);
496 }
498 // if there remain any unimported toplevels (these must have
499 // no classes at all), process their import statements as well.
500 for (JCCompilationUnit tree : trees) {
501 if (tree.starImportScope.elems == null) {
502 JavaFileObject prev = log.useSource(tree.sourcefile);
503 Env<AttrContext> topEnv = topLevelEnv(tree);
504 memberEnter.memberEnter(tree, topEnv);
505 log.useSource(prev);
506 }
507 }
508 }
509 } finally {
510 uncompleted = prevUncompleted;
511 annotate.enterDone();
512 }
513 }
514 }