Example usage for org.eclipse.jdt.core.dom IMethodBinding overrides

List of usage examples for org.eclipse.jdt.core.dom IMethodBinding overrides

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom IMethodBinding overrides.

Prototype

public boolean overrides(IMethodBinding method);

Source Link

Document

Returns whether this method overrides the given method, as specified in section 8.4.8.1 of The Java Language Specification, Third Edition (JLS3).

Usage

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

/**
 * //from   w  w  w. jav  a2s. co m
 * @param method
 *            The method binding whose superclasses are searched trough for
 *            this bindings super method
 * @return Returns an array with two MethodName. They can be null if they do
 *         not exist.
 */
protected static MethodName[] getSuperMethodNames(IMethodBinding method) {
    MethodName[] methodNames = new CsMethodName[2];
    IMethodBinding firstMethod = null, topLevelMethod = null;

    List<ITypeBinding> types = getTypeHierarchy(method);

    for (int i = 0; i < types.size(); i++) {
        IMethodBinding[] declaredMethods = types.get(i).getDeclaredMethods();
        for (int j = 0; j < declaredMethods.length; j++) {
            if (declaredMethods[j].getName().equals(method.getName()) && method.overrides(declaredMethods[j])) {
                if (firstMethod == null) {
                    firstMethod = declaredMethods[j];
                } else {
                    topLevelMethod = declaredMethods[j];
                }
            }
        }
    }

    methodNames[0] = CsMethodName.newMethodName(methodNameHelper(null, firstMethod, false));

    if (topLevelMethod != null) {
        methodNames[1] = CsMethodName.newMethodName(methodNameHelper(null, topLevelMethod, false));
    }

    return methodNames;
}

From source file:cideplus.ui.astview.TrayContentProvider.java

License:Open Source License

private void addMethodBindingComparions(ArrayList result, Binding trayElement) {
    class OverridesProperty extends DynamicBindingProperty {
        public OverridesProperty(Binding parent) {
            super(parent);
        }/*from w w  w . j a  v  a  2  s  .c o  m*/

        protected String getName() {
            return "*.overrides(this): "; //$NON-NLS-1$
        }

        protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) {
            if (viewerBinding instanceof IMethodBinding) {
                IMethodBinding viewerMB = (IMethodBinding) viewerBinding;
                IMethodBinding trayMB = (IMethodBinding) trayBinding;
                return Boolean.toString(viewerMB.overrides(trayMB));
            } else {
                return "* not an IMethodBinding"; //$NON-NLS-1$
            }
        }
    }
    result.add(new OverridesProperty(trayElement));

    class IsSubsignatureProperty extends DynamicBindingProperty {
        public IsSubsignatureProperty(Binding parent) {
            super(parent);
        }

        protected String getName() {
            return "*.isSubsignature(this): "; //$NON-NLS-1$
        }

        protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) {
            if (viewerBinding instanceof IMethodBinding) {
                IMethodBinding viewerMB = (IMethodBinding) viewerBinding;
                IMethodBinding trayMB = (IMethodBinding) trayBinding;
                return Boolean.toString(viewerMB.isSubsignature(trayMB));
            } else {
                return "* not an IMethodBinding"; //$NON-NLS-1$
            }
        }
    }
    result.add(new IsSubsignatureProperty(trayElement));
}

From source file:com.dnw.depmap.neo.NeoDao.java

License:Open Source License

/**
 * Creates a method with its override hierarchy.
 * /*from   ww w  .  j a v a2 s.  c  om*/
 * @author manbaum
 * @since Oct 10, 2014
 * @param method
 * @return
 */
public boolean createMethod(IMethodBinding method) {
    if (method == null)
        return false;
    if (isBlocked(method))
        return false;
    ITypeBinding type = method.getDeclaringClass();
    if (!createType(type))
        return false;
    IMethodBinding declaration = method.getMethodDeclaration();
    if (isCached(declaration))
        return true;
    BindingCache.put(declaration, AstUtil.nameOf(declaration));
    w.createMethod(declaration);
    w.createDeclare(declaration);
    if (type.isInterface()) {
        for (ITypeBinding t : type.getInterfaces()) {
            for (IMethodBinding m : t.getDeclaredMethods()) {
                if (method.overrides(m)) {
                    IMethodBinding d = m.getMethodDeclaration();
                    if (createMethod(m))
                        w.createOverride(declaration, d);
                }
            }
        }
    } else {
        for (ITypeBinding t : type.getInterfaces()) {
            for (IMethodBinding m : t.getDeclaredMethods()) {
                if (method.overrides(m)) {
                    IMethodBinding d = m.getMethodDeclaration();
                    if (createMethod(m))
                        w.createOverride(declaration, d);
                }
            }
        }
        ITypeBinding t = type.getSuperclass();
        if (t != null)
            for (IMethodBinding m : t.getDeclaredMethods()) {
                if (method.overrides(m)) {
                    IMethodBinding d = m.getMethodDeclaration();
                    if (createMethod(m))
                        w.createOverride(declaration, d);
                }
            }
    }
    return true;
}

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

/**
 * Determines whether a method is overridden by any of a list of other methods.
 *//*from   w w  w  . j  a  va 2  s  .  c  om*/
private boolean hasOverride(List<IMethodBinding> methods, IMethodBinding otherMethod) {
    for (IMethodBinding method : methods) {
        if (method.overrides(otherMethod)) {
            return true;
        }
    }
    return false;
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSMethodTranslator.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    if (node.getBody() != null) {
        visit(node.getBody());//w w w .j  av a  2  s  . c  o  m
    }
    IMethodBinding binding = Types.getMethodBinding(node);
    for (IMethodBinding overridable : overridableMethods) {
        if (!binding.isConstructor() && (binding.isEqualTo(overridable) || binding.overrides(overridable))) {
            JavaMethod md = getDescription(overridable);
            String key = md.getKey();
            String value = methodMappings.get(key);
            if (value != null) {
                IOSMethod iosMethod = new IOSMethod(value, binding, ast);
                node.setName(ast.newSimpleName(iosMethod.getName()));
                Types.addBinding(node.getName(), iosMethod.resolveBinding());
                Types.addMappedIOSMethod(binding, iosMethod);

                // Map parameters, if any.
                @SuppressWarnings("unchecked")
                List<SingleVariableDeclaration> parameters = node.parameters();
                int n = parameters.size();
                if (n > 0) {
                    List<IOSParameter> iosArgs = iosMethod.getParameters();
                    assert n == iosArgs.size() || iosMethod.isVarArgs();

                    // Pull parameters out of list, so they can be reordered.
                    SingleVariableDeclaration[] params = parameters.toArray(new SingleVariableDeclaration[n]);

                    for (int i = 0; i < n; i++) {
                        SingleVariableDeclaration var = params[i];
                        IVariableBinding varBinding = Types.getVariableBinding(var);
                        IOSParameter iosArg = iosArgs.get(i);
                        SimpleType paramType = ast
                                .newSimpleType(NameTable.unsafeSimpleName(iosArg.getType(), ast));
                        Types.addBinding(paramType, varBinding);
                        Types.addBinding(paramType.getName(), varBinding);
                        var.setType(paramType);
                        Types.addBinding(var.getName(), varBinding);
                        parameters.set(iosArg.getIndex(), var);
                    }
                }

                Types.addMappedIOSMethod(binding, iosMethod);
            }
            return false;
        }
    }
    return false;
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSMethodTranslator.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    // translate any embedded method invocations
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
    }/*from ww  w. j a  va  2 s  .  c om*/
    @SuppressWarnings("unchecked")
    List<Expression> args = node.arguments(); // safe by definition
    for (Expression e : args) {
        e.accept(this);
    }

    IMethodBinding binding = Types.getMethodBinding(node);
    JavaMethod md = descriptions.get(binding);
    if (md == null && !binding.getName().equals("clone")) { // never map clone()
        IVariableBinding receiver = node.getExpression() != null
                ? Types.getVariableBinding(node.getExpression())
                : null;
        ITypeBinding clazz = receiver != null ? receiver.getType() : binding.getDeclaringClass();
        if (clazz != null && !clazz.isArray()) {
            for (IMethodBinding method : descriptions.keySet()) {
                if (binding.isSubsignature(method)
                        && clazz.isAssignmentCompatible(method.getDeclaringClass())) {
                    md = descriptions.get(method);
                    break;
                }
            }
        }
    }
    if (md != null) {
        String key = md.getKey();
        String value = methodMappings.get(key);
        if (value == null) {
            J2ObjC.error(node, createMissingMethodMessage(binding));
            return true;
        }
        IOSMethod iosMethod = new IOSMethod(value, binding, ast);
        NameTable.rename(binding, iosMethod.getName());
        if (node.getExpression() instanceof SimpleName) {
            SimpleName expr = (SimpleName) node.getExpression();
            if (expr.getIdentifier().equals(binding.getDeclaringClass().getName())
                    || expr.getIdentifier().equals(binding.getDeclaringClass().getQualifiedName())) {
                NameTable.rename(binding.getDeclaringClass(), iosMethod.getDeclaringClass());
            }
        }
        Types.addMappedIOSMethod(binding, iosMethod);
        Types.addMappedInvocation(node, iosMethod.resolveBinding());
    } else {
        // Not mapped, check if it overrides a mapped method.
        for (IMethodBinding methodBinding : mappedMethods) {
            if (binding.overrides(methodBinding)) {
                JavaMethod desc = getDescription(methodBinding);
                String value = methodMappings.get(desc.getKey());
                if (value != null) {
                    IOSMethod iosMethod = new IOSMethod(value, binding, ast);
                    NameTable.rename(methodBinding, iosMethod.getName());
                    Types.addMappedIOSMethod(binding, iosMethod);
                    Types.addMappedInvocation(node, iosMethod.resolveBinding());
                    break;
                }
            }
        }
    }
    return false;
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSMethodTranslator.java

License:Open Source License

@Override
public boolean visit(SuperMethodInvocation node) {
    // translate any embedded method invocations
    @SuppressWarnings("unchecked")
    List<Expression> args = node.arguments(); // safe by definition
    for (Expression e : args) {
        e.accept(this);
    }//from ww  w  .  j  av  a2  s .  co  m

    IMethodBinding binding = Types.getMethodBinding(node);
    JavaMethod md = descriptions.get(binding);
    if (md != null) {
        String key = md.getKey();
        String value = methodMappings.get(key);
        if (value == null) {
            // Method has same name as a mapped method's, but it's ignored since
            // it doesn't override it.
            return super.visit(node);
        }
        IOSMethod iosMethod = new IOSMethod(value, binding, ast);
        node.setName(NameTable.unsafeSimpleName(iosMethod.getName(), ast));
        SimpleName name = node.getName();
        if (name.getIdentifier().equals(binding.getDeclaringClass().getName())
                || name.getIdentifier().equals(binding.getDeclaringClass().getQualifiedName())) {
            node.setName(NameTable.unsafeSimpleName(iosMethod.getDeclaringClass(), ast));
        }
        Types.addMappedIOSMethod(binding, iosMethod);
        IMethodBinding newBinding = iosMethod.resolveBinding();
        Types.addMappedInvocation(node, newBinding);
        Types.addBinding(node, newBinding);
        Types.addBinding(name, newBinding);
    } else {
        // Not mapped, check if it overrides a mapped method.
        for (IMethodBinding methodBinding : mappedMethods) {
            if (binding.overrides(methodBinding)) {
                JavaMethod desc = getDescription(methodBinding);
                String value = methodMappings.get(desc.getKey());
                if (value != null) {
                    IOSMethod iosMethod = new IOSMethod(value, binding, ast);
                    node.setName(NameTable.unsafeSimpleName(iosMethod.getName(), ast));
                    Types.addMappedIOSMethod(binding, iosMethod);
                    IMethodBinding newBinding = iosMethod.resolveBinding();
                    Types.addBinding(node, newBinding);
                    Types.addBinding(node.getName(), newBinding);
                }
            }
        }
    }
    return false;
}

From source file:com.google.devtools.j2cpp.types.Types.java

License:Open Source License

/**
 * If this method overrides another method, return the binding for the
 * original declaration.// w  ww.  j a v a 2s  .  co m
 */
public static IMethodBinding getOriginalMethodBinding(IMethodBinding method) {
    if (method != null) {
        ITypeBinding clazz = method.getDeclaringClass();
        ITypeBinding superclass = clazz.getSuperclass();
        if (superclass != null) {
            for (IMethodBinding interfaceMethod : superclass.getDeclaredMethods()) {
                if (!(interfaceMethod instanceof IOSMethodBinding) && method.overrides(interfaceMethod)) {
                    IMethodBinding decl = interfaceMethod.getMethodDeclaration();
                    return decl != null ? decl : interfaceMethod.getMethodDeclaration();
                }
            }
        }

        // Collect all interfaces implemented by this class.
        Set<ITypeBinding> allInterfaces = Sets.newHashSet();
        while (clazz != null) {
            allInterfaces.addAll(getAllInterfaces(clazz));
            clazz = clazz.getSuperclass();
        }

        for (ITypeBinding interfaceBinding : allInterfaces) {
            for (IMethodBinding interfaceMethod : interfaceBinding.getDeclaredMethods()) {
                if (method.overrides(interfaceMethod)) {
                    IMethodBinding decl = interfaceMethod.getMethodDeclaration();
                    return decl != null ? decl : interfaceMethod.getMethodDeclaration();
                }
            }
        }

    }
    return method;
}

From source file:com.google.devtools.j2objc.translate.DefaultMethodShimGenerator.java

License:Open Source License

/**
 * Implement the shims that call the default methods defined in the interfaces.
 *
 * To match the semantics of Java 8, we need to observe the following constraints:
 *
 * 1. If an interface I has a default method M, and if class C or any of C's super classes
 *    implements I, only one shim is generated to call M in the class that implements I.
 * 2. If an interface I1 has a default method M and I2 redeclares M, the class C that implements
 *    I2 should have a shim that calls I2's M, not I1's.
 * 3. If a class C inherits a concrete method M that's also declared in some interface I as
 *    a default method, the concrete method takes precedence and no shim should be generated.
 *
 * We let JDT handle the improbable cases -- for example, if interface I2 re-declares method M
 * in I1 and turns it into abstract (that is I2 no longer provides a body for M), than a class C
 * that implements I2 can only be an abstract class. If C is not abstract, it results in a
 * compiler error. Of course, if C is an abstract class because of the abstract M, we should never
 * generate a shim for M.//from  ww w .  jav  a 2s. co m
 *
 * Note that the node parameter here can be a class or an interface. If node is an interface,
 * the shim methods added here will go into its companion class. We need the shims so that lambdas
 * based on the interface will also carry the default methods.
 */
private void addDefaultMethodShims(AbstractTypeDeclaration node) {
    ITypeBinding type = node.getTypeBinding();
    if (type.isAnnotation()) {
        return;
    }

    // First, collect all interfaces that are implemented by this type.
    Set<ITypeBinding> interfaces = BindingUtil.getAllInterfaces(type);

    // Now, collect those implemented by the super. This gets an empty set if type is an interface.
    Set<ITypeBinding> implementedBySuper = BindingUtil.getAllInterfaces(type.getSuperclass());

    // Remove those already implemented by super. These are the interfaces we care about. This
    // guarantees that only one shim is ever generated for one default method (provided the
    // default method is not re-declared later) in the inheritance chain.
    interfaces.removeAll(implementedBySuper);

    // Collect the methods declared in the interfaces. If there is already an existing method in
    // sigMethods, we test if the iterating method overrides it. If so, we replace the entry.
    // This guaranteed that the collected methods are from the leaf interfaces implemented by type.
    Map<String, IMethodBinding> sigMethods = new TreeMap<>();
    for (ITypeBinding t : interfaces) {
        for (IMethodBinding method : t.getDeclaredMethods()) {
            String signature = BindingUtil.getDefaultMethodSignature(method);
            IMethodBinding existingMethod = sigMethods.get(signature);
            if (existingMethod == null || method.overrides(existingMethod)) {
                sigMethods.put(signature, method);
            }
        }
    }

    // Remove the methods declared in this type.
    for (IMethodBinding method : type.getDeclaredMethods()) {
        sigMethods.remove(BindingUtil.getDefaultMethodSignature(method));
    }

    // The concrete methods that the type inherits take precedence.
    ITypeBinding superType = type;
    while ((superType = superType.getSuperclass()) != null) {
        for (IMethodBinding method : superType.getDeclaredMethods()) {
            if (BindingUtil.isAbstract(method)) {
                continue;
            }
            sigMethods.remove(BindingUtil.getDefaultMethodSignature(method));
        }
    }

    // The remaining default methods are what we need to create shims for.
    for (IMethodBinding method : sigMethods.values()) {
        if (!BindingUtil.isDefault(method)) {
            continue;
        }

        for (String selector : nameTable.getAllSelectors(method)) {
            node.addBodyDeclaration(createDefaultMethodShim(selector, method, type));
        }
    }
}

From source file:com.google.devtools.j2objc.util.NameTable.java

License:Open Source License

/**
 * Finds all the original overridden method bindings. If the the method is
 * overridden multiple times in the hierarchy, only the original is included.
 * Multiple results are still possible if the the given method overrides
 * methods from multiple interfaces or classes that do not share the same
 * hierarchy./* w w w  .  ja va 2  s .  c  o  m*/
 */
private List<IMethodBinding> getOriginalMethodBindings(IMethodBinding method) {
    method = method.getMethodDeclaration();
    if (method.isConstructor() || BindingUtil.isStatic(method)) {
        return Lists.newArrayList(method);
    }
    ITypeBinding declaringClass = method.getDeclaringClass();
    List<IMethodBinding> originalBindings = Lists.newArrayList();
    originalBindings.add(method);

    // Collect all the inherited types.
    // Predictable ordering is important, so we use a LinkedHashSet.
    Set<ITypeBinding> inheritedTypes = Sets.newLinkedHashSet();
    BindingUtil.collectAllInheritedTypes(declaringClass, inheritedTypes);
    if (declaringClass.isInterface()) {
        inheritedTypes.add(typeEnv.resolveJavaType("java.lang.Object"));
    }

    // Find all overridden methods.
    for (ITypeBinding inheritedType : inheritedTypes) {
        for (IMethodBinding interfaceMethod : inheritedType.getDeclaredMethods()) {
            if (method.overrides(interfaceMethod)) {
                originalBindings.add(interfaceMethod);
            }
        }
    }

    // Remove any overridden method that overrides another overriden method,
    // leaving only the original overridden methods. Usually there is just one
    // but not always.
    Iterator<IMethodBinding> iter = originalBindings.iterator();
    while (iter.hasNext()) {
        IMethodBinding inheritedMethod = iter.next();
        for (IMethodBinding otherInheritedMethod : originalBindings) {
            if (inheritedMethod != otherInheritedMethod && inheritedMethod.overrides(otherInheritedMethod)) {
                iter.remove();
                break;
            }
        }
    }

    return originalBindings;
}