📄 transtypes.java
字号:
* If a bridge already exists in some other class, no new bridge is added. * Instead, it is checked that the bridge symbol overrides the method symbol. * (Spec ???). * todo: what about bridges for privates??? * * @param pos The source code position to be used for the definition. * @param sym The symbol for which a bridge might have to be added. * @param origin The class in which the bridge would go. * @param bridges The list buffer to which the bridge would be added. */ void addBridgeIfNeeded(DiagnosticPosition pos, Symbol sym, ClassSymbol origin, ListBuffer<JCTree> bridges) { if (sym.kind == MTH && sym.name != names.init && (sym.flags() & (PRIVATE | SYNTHETIC | STATIC)) == 0 && sym.isMemberOf(origin, types)) { MethodSymbol meth = (MethodSymbol)sym; MethodSymbol bridge = meth.binaryImplementation(origin, types); MethodSymbol impl = meth.implementation(origin, types, true); if (bridge == null || bridge == meth || (impl != null && !bridge.owner.isSubClass(impl.owner, types))) { // No bridge was added yet. if (impl != null && isBridgeNeeded(meth, impl, origin.type)) { addBridge(pos, meth, impl, origin, bridge==impl, bridges); } else if (impl == meth && impl.owner != origin && (impl.flags() & FINAL) == 0 && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) { // this is to work around a horrible but permanent // reflection design error. addBridge(pos, meth, impl, origin, false, bridges); } } else if ((bridge.flags() & SYNTHETIC) != 0) { MethodSymbol other = overridden.get(bridge); if (other != null && other != meth) { if (impl == null || !impl.overrides(other, origin, types, true)) { // Bridge for other symbol pair was added log.error(pos, "name.clash.same.erasure.no.override", other, other.location(origin.type, types), meth, meth.location(origin.type, types)); } } } else if (!bridge.overrides(meth, origin, types, true)) { // Accidental binary override without source override. if (bridge.owner == origin || types.asSuper(bridge.owner.type, meth.owner) == null) // Don't diagnose the problem if it would already // have been reported in the superclass log.error(pos, "name.clash.same.erasure.no.override", bridge, bridge.location(origin.type, types), meth, meth.location(origin.type, types)); } } } // where /** * @param method The symbol for which a bridge might have to be added * @param impl The implementation of method * @param dest The type in which the bridge would go */ private boolean isBridgeNeeded(MethodSymbol method, MethodSymbol impl, Type dest) { if (impl != method) { // If either method or impl have different erasures as // members of dest, a bridge is needed. Type method_erasure = method.erasure(types); if (!isSameMemberWhenErased(dest, method, method_erasure)) return true; Type impl_erasure = impl.erasure(types); if (!isSameMemberWhenErased(dest, impl, impl_erasure)) return true; // If the erasure of the return type is different, a // bridge is needed. return !types.isSameType(impl_erasure.getReturnType(), method_erasure.getReturnType()); } else { // method and impl are the same... if ((method.flags() & ABSTRACT) != 0) { // ...and abstract so a bridge is not needed. // Concrete subclasses will bridge as needed. return false; } // The erasure of the return type is always the same // for the same symbol. Reducing the three tests in // the other branch to just one: return !isSameMemberWhenErased(dest, method, method.erasure(types)); } } /** * Lookup the method as a member of the type. Compare the * erasures. * @param type the class where to look for the method * @param method the method to look for in class * @param erasure the erasure of method */ private boolean isSameMemberWhenErased(Type type, MethodSymbol method, Type erasure) { return types.isSameType(erasure(types.memberType(type, method)), erasure); } void addBridges(DiagnosticPosition pos, TypeSymbol i, ClassSymbol origin, ListBuffer<JCTree> bridges) { for (Scope.Entry e = i.members().elems; e != null; e = e.sibling) addBridgeIfNeeded(pos, e.sym, origin, bridges); for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail) addBridges(pos, l.head.tsym, origin, bridges); } /** Add all necessary bridges to some class appending them to list buffer. * @param pos The source code position to be used for the bridges. * @param origin The class in which the bridges go. * @param bridges The list buffer to which the bridges are added. */ void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) { Type st = types.supertype(origin.type); while (st.tag == CLASS) {// if (isSpecialization(st)) addBridges(pos, st.tsym, origin, bridges); st = types.supertype(st); } for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)// if (isSpecialization(l.head)) addBridges(pos, l.head.tsym, origin, bridges); }/* ************************************************************************ * Visitor methods *************************************************************************/ /** Visitor argument: proto-type. */ private Type pt; /** Visitor method: perform a type translation on tree. */ public <T extends JCTree> T translate(T tree, Type pt) { Type prevPt = this.pt; try { this.pt = pt; return translate(tree); } finally { this.pt = prevPt; } } /** Visitor method: perform a type translation on list of trees. */ public <T extends JCTree> List<T> translate(List<T> trees, Type pt) { Type prevPt = this.pt; List<T> res; try { this.pt = pt; res = translate(trees); } finally { this.pt = prevPt; } return res; } public void visitClassDef(JCClassDecl tree) { translateClass(tree.sym); result = tree; } JCMethodDecl currentMethod = null; public void visitMethodDef(JCMethodDecl tree) { JCMethodDecl previousMethod = currentMethod; try { currentMethod = tree; tree.restype = translate(tree.restype, null); tree.typarams = List.nil(); tree.params = translateVarDefs(tree.params); tree.thrown = translate(tree.thrown, null); tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType()); tree.type = erasure(tree.type); result = tree; } finally { currentMethod = previousMethod; } // Check that we do not introduce a name clash by erasing types. for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name); e.sym != null; e = e.next()) { if (e.sym != tree.sym && types.isSameType(erasure(e.sym.type), tree.type)) { log.error(tree.pos(), "name.clash.same.erasure", tree.sym, e.sym); return; } } } public void visitVarDef(JCVariableDecl tree) { tree.vartype = translate(tree.vartype, null); tree.init = translate(tree.init, tree.sym.erasure(types)); tree.type = erasure(tree.type); result = tree; } public void visitDoLoop(JCDoWhileLoop tree) { tree.body = translate(tree.body); tree.cond = translate(tree.cond, syms.booleanType); result = tree; } public void visitWhileLoop(JCWhileLoop tree) { tree.cond = translate(tree.cond, syms.booleanType); tree.body = translate(tree.body); result = tree; } public void visitForLoop(JCForLoop tree) { tree.init = translate(tree.init, null); if (tree.cond != null) tree.cond = translate(tree.cond, syms.booleanType); tree.step = translate(tree.step, null); tree.body = translate(tree.body); result = tree; } public void visitForeachLoop(JCEnhancedForLoop tree) { tree.var = translate(tree.var, null); Type iterableType = tree.expr.type; tree.expr = translate(tree.expr, erasure(tree.expr.type)); if (types.elemtype(tree.expr.type) == null) tree.expr.type = iterableType; // preserve type for Lower tree.body = translate(tree.body); result = tree; } public void visitSwitch(JCSwitch tree) { Type selsuper = types.supertype(tree.selector.type); boolean enumSwitch = selsuper != null && selsuper.tsym == syms.enumSym; Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType; tree.selector = translate(tree.selector, target); tree.cases = translateCases(tree.cases); result = tree; } public void visitCase(JCCase tree) { tree.pat = translate(tree.pat, null); tree.stats = translate(tree.stats); result = tree; } public void visitSynchronized(JCSynchronized tree) { tree.lock = translate(tree.lock, erasure(tree.lock.type)); tree.body = translate(tree.body); result = tree; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -