Thu, 29 Jul 2010 15:57:43 +0100
6970833: Try-with-resource implementation throws an NPE during Flow analysis
Summary: Updated logic not to rely upon Symbol.implementation (which check in superinterfaces)
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.*;
30 import javax.lang.model.element.ElementKind;
32 import com.sun.tools.javac.code.*;
33 import com.sun.tools.javac.code.Symbol.*;
34 import com.sun.tools.javac.tree.*;
35 import com.sun.tools.javac.tree.JCTree.*;
36 import com.sun.tools.javac.util.*;
37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
38 import com.sun.tools.javac.util.List;
40 import static com.sun.tools.javac.code.Flags.*;
41 import static com.sun.tools.javac.code.Kinds.*;
42 import static com.sun.tools.javac.code.TypeTags.*;
44 /** This pass translates Generic Java to conventional Java.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own risk.
48 * This code and its internal interfaces are subject to change or
49 * deletion without notice.</b>
50 */
51 public class TransTypes extends TreeTranslator {
52 /** The context key for the TransTypes phase. */
53 protected static final Context.Key<TransTypes> transTypesKey =
54 new Context.Key<TransTypes>();
56 /** Get the instance for this context. */
57 public static TransTypes instance(Context context) {
58 TransTypes instance = context.get(transTypesKey);
59 if (instance == null)
60 instance = new TransTypes(context);
61 return instance;
62 }
64 private Names names;
65 private Log log;
66 private Symtab syms;
67 private TreeMaker make;
68 private Enter enter;
69 private boolean allowEnums;
70 private Types types;
71 private final Resolve resolve;
72 private final TypeAnnotations typeAnnotations;
74 /**
75 * Flag to indicate whether or not to generate bridge methods.
76 * For pre-Tiger source there is no need for bridge methods, so it
77 * can be skipped to get better performance for -source 1.4 etc.
78 */
79 private final boolean addBridges;
81 protected TransTypes(Context context) {
82 context.put(transTypesKey, this);
83 names = Names.instance(context);
84 log = Log.instance(context);
85 syms = Symtab.instance(context);
86 enter = Enter.instance(context);
87 overridden = new HashMap<MethodSymbol,MethodSymbol>();
88 Source source = Source.instance(context);
89 allowEnums = source.allowEnums();
90 addBridges = source.addBridges();
91 types = Types.instance(context);
92 make = TreeMaker.instance(context);
93 resolve = Resolve.instance(context);
94 typeAnnotations = TypeAnnotations.instance(context);
95 }
97 /** A hashtable mapping bridge methods to the methods they override after
98 * type erasure.
99 */
100 Map<MethodSymbol,MethodSymbol> overridden;
102 /** Construct an attributed tree for a cast of expression to target type,
103 * unless it already has precisely that type.
104 * @param tree The expression tree.
105 * @param target The target type.
106 */
107 JCExpression cast(JCExpression tree, Type target) {
108 int oldpos = make.pos;
109 make.at(tree.pos);
110 if (!types.isSameType(tree.type, target)) {
111 if (!resolve.isAccessible(env, target.tsym))
112 resolve.logAccessError(env, tree, target);
113 tree = make.TypeCast(make.Type(target), tree).setType(target);
114 }
115 make.pos = oldpos;
116 return tree;
117 }
119 /** Construct an attributed tree to coerce an expression to some erased
120 * target type, unless the expression is already assignable to that type.
121 * If target type is a constant type, use its base type instead.
122 * @param tree The expression tree.
123 * @param target The target type.
124 */
125 JCExpression coerce(JCExpression tree, Type target) {
126 Type btarget = target.baseType();
127 if (tree.type.isPrimitive() == target.isPrimitive()) {
128 return types.isAssignable(tree.type, btarget, Warner.noWarnings)
129 ? tree
130 : cast(tree, btarget);
131 }
132 return tree;
133 }
135 /** Given an erased reference type, assume this type as the tree's type.
136 * Then, coerce to some given target type unless target type is null.
137 * This operation is used in situations like the following:
138 *
139 * class Cell<A> { A value; }
140 * ...
141 * Cell<Integer> cell;
142 * Integer x = cell.value;
143 *
144 * Since the erasure of Cell.value is Object, but the type
145 * of cell.value in the assignment is Integer, we need to
146 * adjust the original type of cell.value to Object, and insert
147 * a cast to Integer. That is, the last assignment becomes:
148 *
149 * Integer x = (Integer)cell.value;
150 *
151 * @param tree The expression tree whose type might need adjustment.
152 * @param erasedType The expression's type after erasure.
153 * @param target The target type, which is usually the erasure of the
154 * expression's original type.
155 */
156 JCExpression retype(JCExpression tree, Type erasedType, Type target) {
157 // System.err.println("retype " + tree + " to " + erasedType);//DEBUG
158 if (erasedType.tag > lastBaseTag) {
159 if (target != null && target.isPrimitive())
160 target = erasure(tree.type);
161 tree.type = erasedType;
162 if (target != null) return coerce(tree, target);
163 }
164 return tree;
165 }
167 /** Translate method argument list, casting each argument
168 * to its corresponding type in a list of target types.
169 * @param _args The method argument list.
170 * @param parameters The list of target types.
171 * @param varargsElement The erasure of the varargs element type,
172 * or null if translating a non-varargs invocation
173 */
174 <T extends JCTree> List<T> translateArgs(List<T> _args,
175 List<Type> parameters,
176 Type varargsElement) {
177 if (parameters.isEmpty()) return _args;
178 List<T> args = _args;
179 while (parameters.tail.nonEmpty()) {
180 args.head = translate(args.head, parameters.head);
181 args = args.tail;
182 parameters = parameters.tail;
183 }
184 Type parameter = parameters.head;
185 assert varargsElement != null || args.length() == 1;
186 if (varargsElement != null) {
187 while (args.nonEmpty()) {
188 args.head = translate(args.head, varargsElement);
189 args = args.tail;
190 }
191 } else {
192 args.head = translate(args.head, parameter);
193 }
194 return _args;
195 }
197 /** Add a bridge definition and enter corresponding method symbol in
198 * local scope of origin.
199 *
200 * @param pos The source code position to be used for the definition.
201 * @param meth The method for which a bridge needs to be added
202 * @param impl That method's implementation (possibly the method itself)
203 * @param origin The class to which the bridge will be added
204 * @param hypothetical
205 * True if the bridge method is not strictly necessary in the
206 * binary, but is represented in the symbol table to detect
207 * erasure clashes.
208 * @param bridges The list buffer to which the bridge will be added
209 */
210 void addBridge(DiagnosticPosition pos,
211 MethodSymbol meth,
212 MethodSymbol impl,
213 ClassSymbol origin,
214 boolean hypothetical,
215 ListBuffer<JCTree> bridges) {
216 make.at(pos);
217 Type origType = types.memberType(origin.type, meth);
218 Type origErasure = erasure(origType);
220 // Create a bridge method symbol and a bridge definition without a body.
221 Type bridgeType = meth.erasure(types);
222 long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE;
223 if (hypothetical) flags |= HYPOTHETICAL;
224 MethodSymbol bridge = new MethodSymbol(flags,
225 meth.name,
226 bridgeType,
227 origin);
228 if (!hypothetical) {
229 JCMethodDecl md = make.MethodDef(bridge, null);
231 // The bridge calls this.impl(..), if we have an implementation
232 // in the current class, super.impl(...) otherwise.
233 JCExpression receiver = (impl.owner == origin)
234 ? make.This(origin.erasure(types))
235 : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
237 // The type returned from the original method.
238 Type calltype = erasure(impl.type.getReturnType());
240 // Construct a call of this.impl(params), or super.impl(params),
241 // casting params and possibly results as needed.
242 JCExpression call =
243 make.Apply(
244 null,
245 make.Select(receiver, impl).setType(calltype),
246 translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null))
247 .setType(calltype);
248 JCStatement stat = (origErasure.getReturnType().tag == VOID)
249 ? make.Exec(call)
250 : make.Return(coerce(call, bridgeType.getReturnType()));
251 md.body = make.Block(0, List.of(stat));
253 // Add bridge to `bridges' buffer
254 bridges.append(md);
255 }
257 // Add bridge to scope of enclosing class and `overridden' table.
258 origin.members().enter(bridge);
259 overridden.put(bridge, meth);
260 }
262 /** Add bridge if given symbol is a non-private, non-static member
263 * of the given class, which is either defined in the class or non-final
264 * inherited, and one of the two following conditions holds:
265 * 1. The method's type changes in the given class, as compared to the
266 * class where the symbol was defined, (in this case
267 * we have extended a parameterized class with non-trivial parameters).
268 * 2. The method has an implementation with a different erased return type.
269 * (in this case we have used co-variant returns).
270 * If a bridge already exists in some other class, no new bridge is added.
271 * Instead, it is checked that the bridge symbol overrides the method symbol.
272 * (Spec ???).
273 * todo: what about bridges for privates???
274 *
275 * @param pos The source code position to be used for the definition.
276 * @param sym The symbol for which a bridge might have to be added.
277 * @param origin The class in which the bridge would go.
278 * @param bridges The list buffer to which the bridge would be added.
279 */
280 void addBridgeIfNeeded(DiagnosticPosition pos,
281 Symbol sym,
282 ClassSymbol origin,
283 ListBuffer<JCTree> bridges) {
284 if (sym.kind == MTH &&
285 sym.name != names.init &&
286 (sym.flags() & (PRIVATE | SYNTHETIC | STATIC)) == 0 &&
287 sym.isMemberOf(origin, types))
288 {
289 MethodSymbol meth = (MethodSymbol)sym;
290 MethodSymbol bridge = meth.binaryImplementation(origin, types);
291 MethodSymbol impl = meth.implementation(origin, types, true);
292 if (bridge == null ||
293 bridge == meth ||
294 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
295 // No bridge was added yet.
296 if (impl != null && isBridgeNeeded(meth, impl, origin.type)) {
297 addBridge(pos, meth, impl, origin, bridge==impl, bridges);
298 } else if (impl == meth
299 && impl.owner != origin
300 && (impl.flags() & FINAL) == 0
301 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
302 && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
303 // this is to work around a horrible but permanent
304 // reflection design error.
305 addBridge(pos, meth, impl, origin, false, bridges);
306 }
307 } else if ((bridge.flags() & SYNTHETIC) != 0) {
308 MethodSymbol other = overridden.get(bridge);
309 if (other != null && other != meth) {
310 if (impl == null || !impl.overrides(other, origin, types, true)) {
311 // Bridge for other symbol pair was added
312 log.error(pos, "name.clash.same.erasure.no.override",
313 other, other.location(origin.type, types),
314 meth, meth.location(origin.type, types));
315 }
316 }
317 } else if (!bridge.overrides(meth, origin, types, true)) {
318 // Accidental binary override without source override.
319 if (bridge.owner == origin ||
320 types.asSuper(bridge.owner.type, meth.owner) == null)
321 // Don't diagnose the problem if it would already
322 // have been reported in the superclass
323 log.error(pos, "name.clash.same.erasure.no.override",
324 bridge, bridge.location(origin.type, types),
325 meth, meth.location(origin.type, types));
326 }
327 }
328 }
329 // where
330 /**
331 * @param method The symbol for which a bridge might have to be added
332 * @param impl The implementation of method
333 * @param dest The type in which the bridge would go
334 */
335 private boolean isBridgeNeeded(MethodSymbol method,
336 MethodSymbol impl,
337 Type dest) {
338 if (impl != method) {
339 // If either method or impl have different erasures as
340 // members of dest, a bridge is needed.
341 Type method_erasure = method.erasure(types);
342 if (!isSameMemberWhenErased(dest, method, method_erasure))
343 return true;
344 Type impl_erasure = impl.erasure(types);
345 if (!isSameMemberWhenErased(dest, impl, impl_erasure))
346 return true;
348 // If the erasure of the return type is different, a
349 // bridge is needed.
350 return !types.isSameType(impl_erasure.getReturnType(),
351 method_erasure.getReturnType());
352 } else {
353 // method and impl are the same...
354 if ((method.flags() & ABSTRACT) != 0) {
355 // ...and abstract so a bridge is not needed.
356 // Concrete subclasses will bridge as needed.
357 return false;
358 }
360 // The erasure of the return type is always the same
361 // for the same symbol. Reducing the three tests in
362 // the other branch to just one:
363 return !isSameMemberWhenErased(dest, method, method.erasure(types));
364 }
365 }
366 /**
367 * Lookup the method as a member of the type. Compare the
368 * erasures.
369 * @param type the class where to look for the method
370 * @param method the method to look for in class
371 * @param erasure the erasure of method
372 */
373 private boolean isSameMemberWhenErased(Type type,
374 MethodSymbol method,
375 Type erasure) {
376 return types.isSameType(erasure(types.memberType(type, method)),
377 erasure);
378 }
380 void addBridges(DiagnosticPosition pos,
381 TypeSymbol i,
382 ClassSymbol origin,
383 ListBuffer<JCTree> bridges) {
384 for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
385 addBridgeIfNeeded(pos, e.sym, origin, bridges);
386 for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
387 addBridges(pos, l.head.tsym, origin, bridges);
388 }
390 /** Add all necessary bridges to some class appending them to list buffer.
391 * @param pos The source code position to be used for the bridges.
392 * @param origin The class in which the bridges go.
393 * @param bridges The list buffer to which the bridges are added.
394 */
395 void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
396 Type st = types.supertype(origin.type);
397 while (st.tag == CLASS) {
398 // if (isSpecialization(st))
399 addBridges(pos, st.tsym, origin, bridges);
400 st = types.supertype(st);
401 }
402 for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
403 // if (isSpecialization(l.head))
404 addBridges(pos, l.head.tsym, origin, bridges);
405 }
407 /* ************************************************************************
408 * Visitor methods
409 *************************************************************************/
411 /** Visitor argument: proto-type.
412 */
413 private Type pt;
415 /** Visitor method: perform a type translation on tree.
416 */
417 public <T extends JCTree> T translate(T tree, Type pt) {
418 Type prevPt = this.pt;
419 try {
420 this.pt = pt;
421 return translate(tree);
422 } finally {
423 this.pt = prevPt;
424 }
425 }
427 /** Visitor method: perform a type translation on list of trees.
428 */
429 public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
430 Type prevPt = this.pt;
431 List<T> res;
432 try {
433 this.pt = pt;
434 res = translate(trees);
435 } finally {
436 this.pt = prevPt;
437 }
438 return res;
439 }
441 public void visitClassDef(JCClassDecl tree) {
442 typeAnnotations.taFillAndLift(tree, true);
443 translateClass(tree.sym);
444 result = tree;
445 }
447 JCMethodDecl currentMethod = null;
448 public void visitMethodDef(JCMethodDecl tree) {
449 tree.sym.typeAnnotations = tree.sym.typeAnnotations;
450 JCMethodDecl previousMethod = currentMethod;
451 try {
452 currentMethod = tree;
453 tree.restype = translate(tree.restype, null);
454 tree.typarams = List.nil();
455 tree.params = translateVarDefs(tree.params);
456 tree.thrown = translate(tree.thrown, null);
457 tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
458 tree.type = erasure(tree.type);
459 result = tree;
460 } finally {
461 currentMethod = previousMethod;
462 }
464 // Check that we do not introduce a name clash by erasing types.
465 for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
466 e.sym != null;
467 e = e.next()) {
468 if (e.sym != tree.sym &&
469 types.isSameType(erasure(e.sym.type), tree.type)) {
470 log.error(tree.pos(),
471 "name.clash.same.erasure", tree.sym,
472 e.sym);
473 return;
474 }
475 }
476 }
478 public void visitVarDef(JCVariableDecl tree) {
479 tree.vartype = translate(tree.vartype, null);
480 tree.init = translate(tree.init, tree.sym.erasure(types));
481 tree.type = erasure(tree.type);
482 result = tree;
483 }
485 public void visitDoLoop(JCDoWhileLoop tree) {
486 tree.body = translate(tree.body);
487 tree.cond = translate(tree.cond, syms.booleanType);
488 result = tree;
489 }
491 public void visitWhileLoop(JCWhileLoop tree) {
492 tree.cond = translate(tree.cond, syms.booleanType);
493 tree.body = translate(tree.body);
494 result = tree;
495 }
497 public void visitForLoop(JCForLoop tree) {
498 tree.init = translate(tree.init, null);
499 if (tree.cond != null)
500 tree.cond = translate(tree.cond, syms.booleanType);
501 tree.step = translate(tree.step, null);
502 tree.body = translate(tree.body);
503 result = tree;
504 }
506 public void visitForeachLoop(JCEnhancedForLoop tree) {
507 tree.var = translate(tree.var, null);
508 Type iterableType = tree.expr.type;
509 tree.expr = translate(tree.expr, erasure(tree.expr.type));
510 if (types.elemtype(tree.expr.type) == null)
511 tree.expr.type = iterableType; // preserve type for Lower
512 tree.body = translate(tree.body);
513 result = tree;
514 }
516 public void visitSwitch(JCSwitch tree) {
517 Type selsuper = types.supertype(tree.selector.type);
518 boolean enumSwitch = selsuper != null &&
519 selsuper.tsym == syms.enumSym;
520 Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
521 tree.selector = translate(tree.selector, target);
522 tree.cases = translateCases(tree.cases);
523 result = tree;
524 }
526 public void visitCase(JCCase tree) {
527 tree.pat = translate(tree.pat, null);
528 tree.stats = translate(tree.stats);
529 result = tree;
530 }
532 public void visitSynchronized(JCSynchronized tree) {
533 tree.lock = translate(tree.lock, erasure(tree.lock.type));
534 tree.body = translate(tree.body);
535 result = tree;
536 }
538 public void visitTry(JCTry tree) {
539 tree.resources = translate(tree.resources, syms.autoCloseableType);
540 tree.body = translate(tree.body);
541 tree.catchers = translateCatchers(tree.catchers);
542 tree.finalizer = translate(tree.finalizer);
543 result = tree;
544 }
546 public void visitConditional(JCConditional tree) {
547 tree.cond = translate(tree.cond, syms.booleanType);
548 tree.truepart = translate(tree.truepart, erasure(tree.type));
549 tree.falsepart = translate(tree.falsepart, erasure(tree.type));
550 tree.type = erasure(tree.type);
551 result = retype(tree, tree.type, pt);
552 }
554 public void visitIf(JCIf tree) {
555 tree.cond = translate(tree.cond, syms.booleanType);
556 tree.thenpart = translate(tree.thenpart);
557 tree.elsepart = translate(tree.elsepart);
558 result = tree;
559 }
561 public void visitExec(JCExpressionStatement tree) {
562 tree.expr = translate(tree.expr, null);
563 result = tree;
564 }
566 public void visitReturn(JCReturn tree) {
567 tree.expr = translate(tree.expr, currentMethod.sym.erasure(types).getReturnType());
568 result = tree;
569 }
571 public void visitThrow(JCThrow tree) {
572 tree.expr = translate(tree.expr, erasure(tree.expr.type));
573 result = tree;
574 }
576 public void visitAssert(JCAssert tree) {
577 tree.cond = translate(tree.cond, syms.booleanType);
578 if (tree.detail != null)
579 tree.detail = translate(tree.detail, erasure(tree.detail.type));
580 result = tree;
581 }
583 public void visitApply(JCMethodInvocation tree) {
584 tree.meth = translate(tree.meth, null);
585 Symbol meth = TreeInfo.symbol(tree.meth);
586 Type mt = meth.erasure(types);
587 List<Type> argtypes = mt.getParameterTypes();
588 if (allowEnums &&
589 meth.name==names.init &&
590 meth.owner == syms.enumSym)
591 argtypes = argtypes.tail.tail;
592 if (tree.varargsElement != null)
593 tree.varargsElement = types.erasure(tree.varargsElement);
594 else
595 assert tree.args.length() == argtypes.length();
596 tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
598 // Insert casts of method invocation results as needed.
599 result = retype(tree, mt.getReturnType(), pt);
600 }
602 public void visitNewClass(JCNewClass tree) {
603 if (tree.encl != null)
604 tree.encl = translate(tree.encl, erasure(tree.encl.type));
605 tree.clazz = translate(tree.clazz, null);
606 if (tree.varargsElement != null)
607 tree.varargsElement = types.erasure(tree.varargsElement);
608 tree.args = translateArgs(
609 tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
610 tree.def = translate(tree.def, null);
611 tree.type = erasure(tree.type);
612 result = tree;
613 }
615 public void visitNewArray(JCNewArray tree) {
616 tree.elemtype = translate(tree.elemtype, null);
617 translate(tree.dims, syms.intType);
618 if (tree.type != null) {
619 tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
620 tree.type = erasure(tree.type);
621 } else {
622 tree.elems = translate(tree.elems, null);
623 }
625 result = tree;
626 }
628 public void visitParens(JCParens tree) {
629 tree.expr = translate(tree.expr, pt);
630 tree.type = erasure(tree.type);
631 result = tree;
632 }
634 public void visitAssign(JCAssign tree) {
635 tree.lhs = translate(tree.lhs, null);
636 tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
637 tree.type = erasure(tree.type);
638 result = tree;
639 }
641 public void visitAssignop(JCAssignOp tree) {
642 tree.lhs = translate(tree.lhs, null);
643 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
644 tree.type = erasure(tree.type);
645 result = tree;
646 }
648 public void visitUnary(JCUnary tree) {
649 tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
650 result = tree;
651 }
653 public void visitBinary(JCBinary tree) {
654 tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
655 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
656 result = tree;
657 }
659 public void visitTypeCast(JCTypeCast tree) {
660 tree.clazz = translate(tree.clazz, null);
661 tree.type = erasure(tree.type);
662 tree.expr = translate(tree.expr, tree.type);
663 result = tree;
664 }
666 public void visitTypeTest(JCInstanceOf tree) {
667 tree.expr = translate(tree.expr, null);
668 tree.clazz = translate(tree.clazz, null);
669 result = tree;
670 }
672 public void visitIndexed(JCArrayAccess tree) {
673 tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
674 tree.index = translate(tree.index, syms.intType);
676 // Insert casts of indexed expressions as needed.
677 result = retype(tree, types.elemtype(tree.indexed.type), pt);
678 }
680 // There ought to be nothing to rewrite here;
681 // we don't generate code.
682 public void visitAnnotation(JCAnnotation tree) {
683 result = tree;
684 }
686 public void visitIdent(JCIdent tree) {
687 Type et = tree.sym.erasure(types);
689 // Map type variables to their bounds.
690 if (tree.sym.kind == TYP && tree.sym.type.tag == TYPEVAR) {
691 result = make.at(tree.pos).Type(et);
692 } else
693 // Map constants expressions to themselves.
694 if (tree.type.constValue() != null) {
695 result = tree;
696 }
697 // Insert casts of variable uses as needed.
698 else if (tree.sym.kind == VAR) {
699 result = retype(tree, et, pt);
700 }
701 else {
702 tree.type = erasure(tree.type);
703 result = tree;
704 }
705 }
707 public void visitSelect(JCFieldAccess tree) {
708 Type t = tree.selected.type;
709 while (t.tag == TYPEVAR)
710 t = t.getUpperBound();
711 if (t.isCompound()) {
712 if ((tree.sym.flags() & IPROXY) != 0) {
713 tree.sym = ((MethodSymbol)tree.sym).
714 implemented((TypeSymbol)tree.sym.owner, types);
715 }
716 tree.selected = cast(
717 translate(tree.selected, erasure(tree.selected.type)),
718 erasure(tree.sym.owner.type));
719 } else
720 tree.selected = translate(tree.selected, erasure(t));
722 // Map constants expressions to themselves.
723 if (tree.type.constValue() != null) {
724 result = tree;
725 }
726 // Insert casts of variable uses as needed.
727 else if (tree.sym.kind == VAR) {
728 result = retype(tree, tree.sym.erasure(types), pt);
729 }
730 else {
731 tree.type = erasure(tree.type);
732 result = tree;
733 }
734 }
736 public void visitTypeArray(JCArrayTypeTree tree) {
737 tree.elemtype = translate(tree.elemtype, null);
738 tree.type = erasure(tree.type);
739 result = tree;
740 }
742 /** Visitor method for parameterized types.
743 */
744 public void visitTypeApply(JCTypeApply tree) {
745 JCTree clazz = translate(tree.clazz, null);
746 result = clazz;
747 }
749 /**************************************************************************
750 * utility methods
751 *************************************************************************/
753 private Type erasure(Type t) {
754 return types.erasure(t);
755 }
757 /**************************************************************************
758 * main method
759 *************************************************************************/
761 private Env<AttrContext> env;
763 void translateClass(ClassSymbol c) {
764 Type st = types.supertype(c.type);
766 // process superclass before derived
767 if (st.tag == CLASS)
768 translateClass((ClassSymbol)st.tsym);
770 Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
771 if (myEnv == null)
772 return;
773 Env<AttrContext> oldEnv = env;
774 try {
775 env = myEnv;
776 // class has not been translated yet
778 TreeMaker savedMake = make;
779 Type savedPt = pt;
780 make = make.forToplevel(env.toplevel);
781 pt = null;
782 try {
783 JCClassDecl tree = (JCClassDecl) env.tree;
784 tree.typarams = List.nil();
785 super.visitClassDef(tree);
786 make.at(tree.pos);
787 if (addBridges) {
788 ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
789 if ((tree.sym.flags() & INTERFACE) == 0)
790 addBridges(tree.pos(), tree.sym, bridges);
791 tree.defs = bridges.toList().prependList(tree.defs);
792 }
793 tree.type = erasure(tree.type);
794 } finally {
795 make = savedMake;
796 pt = savedPt;
797 }
798 } finally {
799 env = oldEnv;
800 }
801 }
803 /** Translate a toplevel class definition.
804 * @param cdef The definition to be translated.
805 */
806 public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
807 // note that this method does NOT support recursion.
808 this.make = make;
809 pt = null;
810 return translate(cdef, null);
811 }
812 }