Example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding isInterface

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding isInterface

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding isInterface.

Prototype

@Override
    public boolean isInterface() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IType lookupType(ReferenceBinding typeBinding) {
    if (typeBinding == null || !typeBinding.isValidBinding())
        return null;

    char[] packageName = typeBinding.qualifiedPackageName();
    IPackageFragment[] pkgs = this.nameLookup.findPackageFragments(
            (packageName == null || packageName.length == 0) ? IPackageFragment.DEFAULT_PACKAGE_NAME
                    : new String(packageName),
            false);//from   w w  w  .j  a  v  a 2s  .c  o  m

    // iterate type lookup in each package fragment
    char[] sourceName = typeBinding.qualifiedSourceName();
    String typeName = new String(sourceName);
    int acceptFlag = 0;
    if (typeBinding.isAnnotationType()) {
        acceptFlag = NameLookup.ACCEPT_ANNOTATIONS;
    } else if (typeBinding.isEnum()) {
        acceptFlag = NameLookup.ACCEPT_ENUMS;
    } else if (typeBinding.isInterface()) {
        acceptFlag = NameLookup.ACCEPT_INTERFACES;
    } else if (typeBinding.isClass()) {
        acceptFlag = NameLookup.ACCEPT_CLASSES;
    }
    if (pkgs != null) {
        for (int i = 0, length = pkgs.length; i < length; i++) {
            IType type = this.nameLookup.findType(typeName, pkgs[i], false, acceptFlag,
                    true/*consider secondary types*/);
            if (type != null)
                return type;
        }
    }

    // search inside enclosing element
    char[][] qualifiedName = CharOperation.splitOn('.', sourceName);
    int length = qualifiedName.length;
    if (length == 0)
        return null;

    IType type = createTypeHandle(new String(qualifiedName[0])); // find the top-level type
    if (type == null)
        return null;

    for (int i = 1; i < length; i++) {
        type = type.getType(new String(qualifiedName[i]));
        if (type == null)
            return null;
    }
    if (type.exists())
        return type;
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java

License:Open Source License

private ReferenceBinding getMatchingSuper(ReferenceBinding binding) {
    if (binding == null)
        return null;
    ReferenceBinding superBinding = binding.superclass();
    int level = resolveLevelForType(this.pattern.declaringSimpleName, this.pattern.declaringQualification,
            superBinding);/*from  w w w .java 2  s  .c o  m*/
    if (level != IMPOSSIBLE_MATCH)
        return superBinding;
    // matches superclass
    if (!binding.isInterface() && !CharOperation.equals(binding.compoundName, TypeConstants.JAVA_LANG_OBJECT)) {
        superBinding = getMatchingSuper(superBinding);
        if (superBinding != null)
            return superBinding;
    }
    // matches interfaces
    ReferenceBinding[] interfaces = binding.superInterfaces();
    if (interfaces == null)
        return null;
    for (int i = 0; i < interfaces.length; i++) {
        level = resolveLevelForType(this.pattern.declaringSimpleName, this.pattern.declaringQualification,
                interfaces[i]);
        if (level != IMPOSSIBLE_MATCH)
            return interfaces[i];
        superBinding = getMatchingSuper(interfaces[i]);
        if (superBinding != null)
            return superBinding;
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java

License:Open Source License

private boolean matchOverriddenMethod(ReferenceBinding type, MethodBinding method, MethodBinding matchMethod) {
    if (type == null || this.pattern.selector == null)
        return false;

    // matches superclass
    if (!type.isInterface() && !CharOperation.equals(type.compoundName, TypeConstants.JAVA_LANG_OBJECT)) {
        ReferenceBinding superClass = type.superclass();
        if (superClass.isParameterizedType()) {
            MethodBinding[] methods = superClass.getMethods(this.pattern.selector);
            int length = methods.length;
            for (int i = 0; i < length; i++) {
                if (methods[i].areParametersEqual(method)) {
                    if (matchMethod == null) {
                        if (methodParametersEqualsPattern(methods[i].original()))
                            return true;
                    } else {
                        if (methods[i].original().areParametersEqual(matchMethod))
                            return true;
                    }/*from  w  ww .j a v a 2 s  .  co m*/
                }
            }
        }
        if (matchOverriddenMethod(superClass, method, matchMethod)) {
            return true;
        }
    }

    // matches interfaces
    ReferenceBinding[] interfaces = type.superInterfaces();
    if (interfaces == null)
        return false;
    int iLength = interfaces.length;
    for (int i = 0; i < iLength; i++) {
        if (interfaces[i].isParameterizedType()) {
            MethodBinding[] methods = interfaces[i].getMethods(this.pattern.selector);
            int length = methods.length;
            for (int j = 0; j < length; j++) {
                if (methods[j].areParametersEqual(method)) {
                    if (matchMethod == null) {
                        if (methodParametersEqualsPattern(methods[j].original()))
                            return true;
                    } else {
                        if (methods[j].original().areParametersEqual(matchMethod))
                            return true;
                    }
                }
            }
        }
        if (matchOverriddenMethod(interfaces[i], method, matchMethod)) {
            return true;
        }
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java

License:Open Source License

/**
 * Returns whether the given reference type binding matches or is a subtype of a type
 * that matches the given qualified pattern.
 * Returns ACCURATE_MATCH if it does.//from  w w  w.  j  a  v  a 2 s  .c  o m
 * Returns INACCURATE_MATCH if resolve fails
 * Returns IMPOSSIBLE_MATCH if it doesn't.
 */
protected int resolveLevelAsSubtype(char[] simplePattern, char[] qualifiedPattern, ReferenceBinding type,
        char[] methodName, TypeBinding[] argumentTypes, char[] packageName, boolean isDefault) {
    if (type == null)
        return INACCURATE_MATCH;

    int level = resolveLevelForType(simplePattern, qualifiedPattern, type);
    if (level != IMPOSSIBLE_MATCH) {
        if (isDefault && !CharOperation.equals(packageName, type.qualifiedPackageName())) {
            return IMPOSSIBLE_MATCH;
        }
        MethodBinding method = argumentTypes == null ? null : getMethodBinding(type, methodName, argumentTypes);
        if (((method != null && !method.isAbstract()) || !type.isAbstract()) && !type.isInterface()) { // if concrete, then method is overridden
            level |= OVERRIDDEN_METHOD_FLAVOR;
        }
        return level;
    }

    // matches superclass
    if (!type.isInterface() && !CharOperation.equals(type.compoundName, TypeConstants.JAVA_LANG_OBJECT)) {
        level = resolveLevelAsSubtype(simplePattern, qualifiedPattern, type.superclass(), methodName,
                argumentTypes, packageName, isDefault);
        if (level != IMPOSSIBLE_MATCH) {
            if (argumentTypes != null) {
                // need to verify if method may be overridden
                MethodBinding method = getMethodBinding(type, methodName, argumentTypes);
                if (method != null) { // one method match in hierarchy
                    if ((level & OVERRIDDEN_METHOD_FLAVOR) != 0) {
                        // this method is already overridden on a super class, current match is impossible
                        return IMPOSSIBLE_MATCH;
                    }
                    if (!method.isAbstract() && !type.isInterface()) {
                        // store the fact that the method is overridden
                        level |= OVERRIDDEN_METHOD_FLAVOR;
                    }
                }
            }
            return level | SUB_INVOCATION_FLAVOR; // add flavor to returned level
        }
    }

    // matches interfaces
    ReferenceBinding[] interfaces = type.superInterfaces();
    if (interfaces == null)
        return INACCURATE_MATCH;
    for (int i = 0; i < interfaces.length; i++) {
        level = resolveLevelAsSubtype(simplePattern, qualifiedPattern, interfaces[i], methodName, null,
                packageName, isDefault);
        if (level != IMPOSSIBLE_MATCH) {
            if (!type.isAbstract() && !type.isInterface()) { // if concrete class, then method is overridden
                level |= OVERRIDDEN_METHOD_FLAVOR;
            }
            return level | SUB_INVOCATION_FLAVOR; // add flavor to returned level
        }
    }
    return IMPOSSIBLE_MATCH;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java

License:Open Source License

private boolean resolveLevelAsSuperInvocation(ReferenceBinding type, TypeBinding[] argumentTypes,
        char[][][] superTypeNames, boolean methodAlreadyVerified) {
    char[][] compoundName = type.compoundName;
    for (int i = 0, max = superTypeNames.length; i < max; i++) {
        if (CharOperation.equals(superTypeNames[i], compoundName)) {
            // need to verify if the type implements the pattern method
            if (methodAlreadyVerified)
                return true; // already verified before enter into this method (see resolveLevel(MessageSend))
            MethodBinding[] methods = type.getMethods(this.pattern.selector);
            for (int j = 0, length = methods.length; j < length; j++) {
                MethodBinding method = methods[j];
                TypeBinding[] parameters = method.parameters;
                if (argumentTypes.length == parameters.length) {
                    boolean found = true;
                    for (int k = 0, l = parameters.length; k < l; k++) {
                        if (parameters[k].erasure() != argumentTypes[k].erasure()) {
                            found = false;
                            break;
                        }//  ww  w .ja v a  2  s  .  com
                    }
                    if (found) {
                        return true;
                    }
                }
            }
            break;
        }
    }

    // If the given type is an interface then a common super interface may be found
    // in a parallel branch of the super hierarchy, so we need to verify all super interfaces.
    // If it's a class then there's only one possible branch for the hierarchy and
    // this branch has been already verified by the test above
    if (type.isInterface()) {
        ReferenceBinding[] interfaces = type.superInterfaces();
        if (interfaces == null)
            return false;
        for (int i = 0; i < interfaces.length; i++) {
            if (resolveLevelAsSuperInvocation(interfaces[i], argumentTypes, superTypeNames, false)) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.SuperTypeReferenceLocator.java

License:Open Source License

protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding,
        int accuracy, MatchLocator locator) throws CoreException {

    if (elementBinding instanceof ReferenceBinding) {
        ReferenceBinding referenceBinding = (ReferenceBinding) elementBinding;
        if (referenceBinding.isClass() && this.pattern.typeSuffix == IIndexConstants.INTERFACE_SUFFIX) {
            // do not report class if expected types are only interfaces
            return;
        }/*from  w ww.j a  v a  2 s  .c o m*/
        if (referenceBinding.isInterface() && this.pattern.typeSuffix == IIndexConstants.CLASS_SUFFIX) {
            // do not report interface if expected types are only classes
            return;
        }
    }
    super.matchReportReference(reference, element, elementBinding, accuracy, locator);
}

From source file:com.google.gwt.dev.jjs.impl.BuildTypeMap.java

License:Apache License

private JDeclaredType createType(String name, SourceInfo info, ReferenceBinding binding) {

    JDeclaredType newType;//from   w w  w. j a  v  a 2 s.  c o m
    if (binding.isClass()) {
        newType = program.createClass(info, name, binding.isAbstract(), binding.isFinal());
    } else if (binding.isInterface() || binding.isAnnotationType()) {
        newType = program.createInterface(info, name);
    } else if (binding.isEnum()) {
        if (binding.isAnonymousType()) {
            // Don't model an enum subclass as a JEnumType.
            newType = program.createClass(info, name, false, true);
        } else {
            newType = program.createEnum(info, name, binding.isAbstract());
        }
    } else {
        throw new InternalCompilerException("ReferenceBinding is not a class, interface, or enum.");
    }

    info.addCorrelation(info.getCorrelator().by(newType));

    /**
     * We emulate static initializers and instance initializers as methods. As
     * in other cases, this gives us: simpler AST, easier to optimize, more like
     * output JavaScript. Clinit is always in slot 0, init (if it exists) is
     * always in slot 1.
     */
    SourceInfo child = info.makeChild();
    JMethod clinit = program.createMethod(child, "$clinit", newType, program.getTypeVoid(), false, true, true,
            AccessModifier.PRIVATE, false);
    clinit.freezeParamTypes();
    clinit.setSynthetic();
    child.addCorrelation(info.getCorrelator().by(clinit));

    if (newType instanceof JClassType) {
        child = info.makeChild();
        JMethod init = program.createMethod(child, "$init", newType, program.getTypeVoid(), false, false, true,
                AccessModifier.PRIVATE, false);
        init.freezeParamTypes();
        init.setSynthetic();
        child.addCorrelation(info.getCorrelator().by(init));
    }

    newType.setExternal(linker.isExternalType(newType.getName()));
    return newType;
}

From source file:com.google.gwt.dev.jjs.impl.BuildTypeMap.java

License:Apache License

private boolean process(ReferenceBinding binding) {
    JDeclaredType type = (JDeclaredType) getType(binding);

    try {//from   ww w. j a va  2  s .  com
        // Create an override for getClass().
        if (type instanceof JClassType && type != program.getTypeJavaLangObject()) {

            SourceInfo info = type.getSourceInfo().makeChild();
            JMethod getClassMethod = program.createMethod(info, "getClass", type,
                    program.getTypeJavaLangClass(), false, false, false, AccessModifier.PUBLIC, false);
            assert (type.getMethods().get(2) == getClassMethod);
            getClassMethod.freezeParamTypes();
            getClassMethod.setSynthetic();
            info.addCorrelation(info.getCorrelator().by(getClassMethod));
        }

        if (binding.isNestedType() && !binding.isStatic() && !(binding instanceof BinaryTypeBinding)) {
            // TODO(tobyr) Do something here for binary types?

            // add synthetic fields for outer this and locals
            assert (type instanceof JClassType);
            NestedTypeBinding nestedBinding = (NestedTypeBinding) binding;
            if (nestedBinding.enclosingInstances != null) {
                for (int i = 0; i < nestedBinding.enclosingInstances.length; ++i) {
                    SyntheticArgumentBinding arg = nestedBinding.enclosingInstances[i];
                    createField(arg, type, Disposition.THIS_REF);
                }
            }

            if (nestedBinding.outerLocalVariables != null) {
                for (int i = 0; i < nestedBinding.outerLocalVariables.length; ++i) {
                    SyntheticArgumentBinding arg = nestedBinding.outerLocalVariables[i];
                    // See InnerClassTest.testOuterThisFromSuperCall().
                    boolean isReallyThisRef = false;
                    if (arg.actualOuterLocalVariable instanceof SyntheticArgumentBinding) {
                        SyntheticArgumentBinding outer = (SyntheticArgumentBinding) arg.actualOuterLocalVariable;
                        if (outer.matchingField != null) {
                            JField field = (JField) typeMap.get(outer.matchingField);
                            if (field.isThisRef()) {
                                isReallyThisRef = true;
                            }
                        }
                    }
                    createField(arg, type, isReallyThisRef ? Disposition.THIS_REF : Disposition.FINAL);
                }
            }
        }

        ReferenceBinding superClassBinding = binding.superclass();
        if (type instanceof JClassType && superClassBinding != null) {
            // TODO: handle separately?
            assert (binding.superclass().isClass() || binding.superclass().isEnum());
            JClassType superClass = (JClassType) getType(superClassBinding);
            ((JClassType) type).setSuperClass(superClass);
        }

        ReferenceBinding[] superInterfaces = binding.superInterfaces();
        for (ReferenceBinding superInterfaceBinding : superInterfaces) {
            assert (superInterfaceBinding.isInterface());
            JInterfaceType superInterface = (JInterfaceType) getType(superInterfaceBinding);
            type.addImplements(superInterface);
        }

        ReferenceBinding enclosingBinding = binding.enclosingType();
        if (enclosingBinding != null) {
            type.setEnclosingType((JDeclaredType) getType(enclosingBinding));
        }

        if (type instanceof JEnumType) {
            processEnumType(binding, (JEnumType) type);
        }

        return true;
    } catch (VirtualMachineError e) {
        // Always rethrow VM errors (an attempt to wrap may fail).
        throw e;
    } catch (InternalCompilerException ice) {
        ice.addNode(type);
        throw ice;
    } catch (Throwable e) {
        throw new InternalCompilerException(type, "Error building type map", e);
    }
}

From source file:com.google.gwt.dev.jjs.impl.GwtAstBuilder.java

License:Apache License

private void resolveTypeRefs(TypeDeclaration x) {
    SourceTypeBinding binding = x.binding;
    JDeclaredType type = (JDeclaredType) typeMap.get(binding);
    try {//from w  ww.j a  v  a  2s .  co m
        ReferenceBinding superClassBinding = binding.superclass();
        if (type instanceof JClassType && superClassBinding != null) {
            assert (binding.superclass().isClass() || binding.superclass().isEnum());
            JClassType superClass = (JClassType) typeMap.get(superClassBinding);
            ((JClassType) type).setSuperClass(superClass);
        }

        ReferenceBinding[] superInterfaces = binding.superInterfaces();
        for (ReferenceBinding superInterfaceBinding : superInterfaces) {
            assert (superInterfaceBinding.isInterface());
            JInterfaceType superInterface = (JInterfaceType) typeMap.get(superInterfaceBinding);
            type.addImplements(superInterface);
        }

        ReferenceBinding enclosingBinding = binding.enclosingType();
        if (enclosingBinding != null) {
            type.setEnclosingType((JDeclaredType) typeMap.get(enclosingBinding));
        }
        if (x.memberTypes != null) {
            for (TypeDeclaration memberType : x.memberTypes) {
                resolveTypeRefs(memberType);
            }
        }
    } catch (Throwable e) {
        InternalCompilerException ice = translateException(null, e);
        StringBuffer sb = new StringBuffer();
        x.printHeader(0, sb);
        ice.addNode(x.getClass().getName(), sb.toString(), type.getSourceInfo());
        throw ice;
    }
}

From source file:com.google.gwt.dev.jjs.impl.ReferenceMapper.java

License:Apache License

private JDeclaredType createType(ReferenceBinding binding) {
    String name = JdtUtil.asDottedString(binding.compoundName);
    SourceInfo info = SourceOrigin.UNKNOWN;
    if (binding.isClass()) {
        return new JClassType(info, name, binding.isAbstract(), binding.isFinal());
    } else if (binding.isInterface() || binding.isAnnotationType()) {
        return new JInterfaceType(info, name);
    } else if (binding.isEnum()) {
        if (binding.isAnonymousType()) {
            // Don't model an enum subclass as a JEnumType.
            return new JClassType(info, name, false, true);
        } else {//w  w  w .j a v a  2s.  co m
            return new JEnumType(info, name, binding.isAbstract());
        }
    } else {
        throw new InternalCompilerException("ReferenceBinding is not a class, interface, or enum.");
    }
}