Java Code Examples for com.sun.tools.javac.code.Symbol.MethodSymbol#overrides()

The following examples show how to use com.sun.tools.javac.code.Symbol.MethodSymbol#overrides() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@DefinedBy(Api.LANGUAGE_MODEL)
public boolean overrides(ExecutableElement riderEl,
                         ExecutableElement rideeEl, TypeElement typeEl) {
    MethodSymbol rider = cast(MethodSymbol.class, riderEl);
    MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
    ClassSymbol origin = cast(ClassSymbol.class, typeEl);

    return rider.name == ridee.name &&

           // not reflexive as per JLS
           rider != ridee &&

           // we don't care if ridee is static, though that wouldn't
           // compile
           !rider.isStatic() &&

           // Symbol.overrides assumes the following
           ridee.isMemberOf(origin, types) &&

           // check access and signatures; don't check return types
           rider.overrides(ridee, origin, types, false);
}
 
Example 2
/** Is s a method symbol that overrides a method in a superclass? */
boolean isOverrider(Symbol s) {
    if (s.kind != MTH || s.isStatic())
        return false;
    MethodSymbol m = (MethodSymbol)s;
    TypeSymbol owner = (TypeSymbol)m.owner;
    for (Type sup : types.closure(owner.type)) {
        if (sup == owner.type)
            continue; // skip "this"
        Scope scope = sup.tsym.members();
        for (Symbol sym : scope.getSymbolsByName(m.name)) {
            if (!sym.isStatic() && m.overrides(sym, owner, types, true))
                return true;
        }
    }
    return false;
}
 
Example 3
Source Project: netbeans   File: ElementsService.java    License: Apache License 2.0 6 votes vote down vote up
/**
    * Returns true if this element represents a method which overrides a
    * method in one of its superclasses.
    */
   public boolean overridesMethod(ExecutableElement element) {
       MethodSymbol m = (MethodSymbol)element;
       if ((m.flags() & Flags.STATIC) == 0) {
           ClassSymbol owner = (ClassSymbol) m.owner;
           for (Type sup = jctypes.supertype(m.owner.type);
                   sup.hasTag(TypeTag.CLASS);
                   sup = jctypes.supertype(sup)) {
               for (Symbol sym : sup.tsym.members().getSymbolsByName(m.name)) {
                   if (m.overrides(sym, owner, jctypes, true)) 
                       return true;
               }
           }
       }
return false;
   }
 
Example 4
/**
 * Return the type containing the method that this method overrides.
 * It may be a <code>TypeElement</code> or a <code>TypeParameterElement</code>.
 * @param method target
 * @return a type
 */
public TypeMirror overriddenType(ExecutableElement method) {
    if (utils.isStatic(method)) {
        return null;
    }
    MethodSymbol sym = (MethodSymbol)method;
    ClassSymbol origin = (ClassSymbol) sym.owner;
    for (com.sun.tools.javac.code.Type t = toolEnv.getTypes().supertype(origin.type);
            t.hasTag(com.sun.tools.javac.code.TypeTag.CLASS);
            t = toolEnv.getTypes().supertype(t)) {
        ClassSymbol c = (ClassSymbol) t.tsym;
        for (com.sun.tools.javac.code.Symbol sym2 : c.members().getSymbolsByName(sym.name)) {
            if (sym.overrides(sym2, origin, toolEnv.getTypes(), true)) {
                return t;
            }
        }
    }
    return null;
}
 
Example 5
public boolean overrides(ExecutableElement e1, ExecutableElement e2, TypeElement cls) {
    MethodSymbol rider = (MethodSymbol)e1;
    MethodSymbol ridee = (MethodSymbol)e2;
    ClassSymbol origin = (ClassSymbol)cls;

    return rider.name == ridee.name &&

           // not reflexive as per JLS
           rider != ridee &&

           // we don't care if ridee is static, though that wouldn't
           // compile
           !rider.isStatic() &&

           // Symbol.overrides assumes the following
           ridee.isMemberOf(origin, toolEnv.getTypes()) &&

           // check access, signatures and check return types
           rider.overrides(ridee, origin, toolEnv.getTypes(), true);
}
 
Example 6
void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
    TypeSymbol c = site.tsym;
    for (Symbol sym : c.members().getSymbolsByName(m.name)) {
        if (m.overrides(sym, origin, types, false)) {
            if ((sym.flags() & ABSTRACT) == 0) {
                checkOverride(tree, m, (MethodSymbol)sym, origin);
            }
        }
    }
}
 
Example 7
Source Project: netbeans   File: ElementsService.java    License: Apache License 2.0 5 votes vote down vote up
/**
    * Returns true if this element represents a method which 
    * implements a method in an interface the parent class implements.
    */
   public boolean implementsMethod(ExecutableElement element) {
       MethodSymbol m = (MethodSymbol)element;
TypeSymbol owner = (TypeSymbol) m.owner;
for (Type type : jctypes.interfaces(m.owner.type)) {
           for (Symbol sym : type.tsym.members().getSymbolsByName(m.name)) {
	if (m.overrides(sym, owner, jctypes, true)) 
	    return true;
    }
}
return false;
   }
 
Example 8
Source Project: netbeans   File: ElementsService.java    License: Apache License 2.0 5 votes vote down vote up
public Element getImplementationOf(ExecutableElement method, TypeElement origin) {
    MethodSymbol msym = (MethodSymbol)method;
    MethodSymbol implmethod = (msym).implementation((TypeSymbol)origin, jctypes, true);
    if ((msym.flags() & Flags.STATIC) != 0) {
        // return null if outside of hierarchy, or the method itself if origin extends method's class
        if (jctypes.isSubtype(((TypeSymbol)origin).type,  ((TypeSymbol)((MethodSymbol)method).owner).type)) {
            return method;
        } else {
            return null;
        }
    }
    if (implmethod == null || implmethod == method) {
        //look for default implementations
        if (allowDefaultMethods) {
            com.sun.tools.javac.util.List<MethodSymbol> candidates = jctypes.interfaceCandidates(((TypeSymbol) origin).type, (MethodSymbol) method);
            X: for (com.sun.tools.javac.util.List<MethodSymbol> ptr = candidates; ptr.head != null; ptr = ptr.tail) {
                MethodSymbol prov = ptr.head;
                if (prov != null && prov.overrides((MethodSymbol) method, (TypeSymbol) origin, jctypes, true) &&
                    hasImplementation(prov)) {
                    // PENDING: even if `prov' overrides the method, there may be a different method, in different interface, that overrides `method'
                    // 'prov' must override all such compatible methods in order to present a valid implementation of `method'.
                    for (com.sun.tools.javac.util.List<MethodSymbol> sibling = candidates; sibling.head != null; sibling = sibling.tail) {
                        MethodSymbol redeclare = sibling.head;

                        // if the default method does not override the alternative candidate from an interface, then the default will be rejected
                        // as specified in JLS #8, par. 8.4.8
                        if (!prov.overrides(redeclare, (TypeSymbol)origin, jctypes, allowDefaultMethods)) {
                            break X;
                        }
                    }
                    implmethod = prov;
                    break;
                }
            }
        }
    }
    return implmethod;
}
 
Example 9
/** Add bridge if given symbol is a non-private, non-static member
 *  of the given class, which is either defined in the class or non-final
 *  inherited, and one of the two following conditions holds:
 *  1. The method's type changes in the given class, as compared to the
 *     class where the symbol was defined, (in this case
 *     we have extended a parameterized class with non-trivial parameters).
 *  2. The method has an implementation with a different erased return type.
 *     (in this case we have used co-variant returns).
 *  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 | STATIC)) == 0 &&
        (sym.flags() & SYNTHETIC) != SYNTHETIC &&
        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) == SYNTHETIC) {
            final Pair<MethodSymbol, MethodSymbol> bridgeSpan = bridgeSpans.get(bridge);
            MethodSymbol other = bridgeSpan == null ? null : bridgeSpan.fst;
            if (other != null && other != meth) {
                if (impl == null || !impl.overrides(other, origin, types, true)) {
                    // Is bridge effectively also the bridge for `meth', if so no clash.
                    MethodSymbol target = bridgeSpan == null ? null : bridgeSpan.snd;
                    if (target == null || !target.overrides(meth, origin, types, true, false)) {
                        // Bridge for other symbol pair was added
                        log.error(pos, Errors.NameClashSameErasureNoOverride(
                                other.name, types.memberType(origin.type, other).asMethodType().getParameterTypes(),
                                other.location(origin.type, types),
                                meth.name, types.memberType(origin.type, meth).asMethodType().getParameterTypes(),
                                meth.location(origin.type, types)));
                    }
                }
            }
        } else if (!bridge.overrides(meth, origin, types, true)) {
            // Accidental binary override without source override.
            // Don't diagnose the problem if it would already
            // have been reported in the superclass
            if (bridge.owner == origin ||
                types.asSuper(bridge.owner.type, meth.owner) == null) {
                log.error(pos, Errors.NameClashSameErasureNoOverride(
                        bridge.name, types.memberType(origin.type, bridge).asMethodType().getParameterTypes(),
                        bridge.location(origin.type, types),
                        meth.name, types.memberType(origin.type, meth).asMethodType().getParameterTypes(),
                        meth.location(origin.type, types)));
            }
        }
    }
}
 
Example 10
/** Check that all non-override equivalent methods accessible from 'site'
 *  are mutually compatible (JLS 8.4.8/9.4.1).
 *
 *  @param pos  Position to be used for error reporting.
 *  @param site The class whose methods are checked.
 *  @param sym  The method symbol to be checked.
 */
void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
     ClashFilter cf = new ClashFilter(site);
    //for each method m1 that is overridden (directly or indirectly)
    //by method 'sym' in 'site'...

    List<MethodSymbol> potentiallyAmbiguousList = List.nil();
    boolean overridesAny = false;
    for (Symbol m1 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) {
        if (!sym.overrides(m1, site.tsym, types, false)) {
            if (m1 == sym) {
                continue;
            }

            if (!overridesAny) {
                potentiallyAmbiguousList = potentiallyAmbiguousList.prepend((MethodSymbol)m1);
            }
            continue;
        }

        if (m1 != sym) {
            overridesAny = true;
            potentiallyAmbiguousList = List.nil();
        }

        //...check each method m2 that is a member of 'site'
        for (Symbol m2 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) {
            if (m2 == m1) continue;
            //if (i) the signature of 'sym' is not a subsignature of m1 (seen as
            //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error
            if (!types.isSubSignature(sym.type, types.memberType(site, m2), allowStrictMethodClashCheck) &&
                    types.hasSameArgs(m2.erasure(types), m1.erasure(types))) {
                sym.flags_field |= CLASH;
                if (m1 == sym) {
                    log.error(pos, Errors.NameClashSameErasureNoOverride(
                        m1.name, types.memberType(site, m1).asMethodType().getParameterTypes(), m1.location(),
                        m2.name, types.memberType(site, m2).asMethodType().getParameterTypes(), m2.location()));
                } else {
                    ClassType ct = (ClassType)site;
                    String kind = ct.isInterface() ? "interface" : "class";
                    log.error(pos, Errors.NameClashSameErasureNoOverride1(
                        kind,
                        ct.tsym.name,
                        m1.name,
                        types.memberType(site, m1).asMethodType().getParameterTypes(),
                        m1.location(),
                        m2.name,
                        types.memberType(site, m2).asMethodType().getParameterTypes(),
                        m2.location()));
                }
                return;
            }
        }
    }

    if (!overridesAny) {
        for (MethodSymbol m: potentiallyAmbiguousList) {
            checkPotentiallyAmbiguousOverloads(pos, site, sym, m);
        }
    }
}