Example usage for org.eclipse.jdt.internal.compiler.lookup TypeBinding erasure

List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeBinding erasure

Introduction

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

Prototype

public TypeBinding erasure() 

Source Link

Usage

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

License:Open Source License

protected int resolveLevelForType(TypeBinding typeBinding) {
    FieldPattern fieldPattern = (FieldPattern) this.pattern;
    TypeBinding fieldTypeBinding = typeBinding;
    if (fieldTypeBinding != null && fieldTypeBinding.isParameterizedType()) {
        fieldTypeBinding = typeBinding.erasure();
    }//from w ww  .  j  ava2  s.  c om
    return resolveLevelForType(fieldPattern.typeSimpleName, fieldPattern.typeQualification,
            fieldPattern.getTypeArguments(), 0, fieldTypeBinding);
}

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

License:Open Source License

void matchReportReference(Expression expr, int lastIndex, TypeBinding refBinding, MatchLocator locator)
        throws CoreException {

    // Look if there's a need to special report for parameterized type
    if (refBinding.isParameterizedType() || refBinding.isRawType()) {

        // Try to refine accuracy
        ParameterizedTypeBinding parameterizedBinding = (ParameterizedTypeBinding) refBinding;
        updateMatch(parameterizedBinding, this.pattern.getTypeArguments(), this.pattern.hasTypeParameters(), 0,
                locator);/* w w w.  j  a v a 2s  .  c  om*/

        // See whether it is necessary to report or not
        if (this.match.getRule() == 0)
            return; // impossible match
        boolean report = (this.isErasureMatch && this.match.isErasure())
                || (this.isEquivalentMatch && this.match.isEquivalent()) || this.match.isExact();
        if (!report)
            return;

        // Make a special report for parameterized types if necessary
        if (refBinding.isParameterizedType() && this.pattern.hasTypeArguments()) {
            TypeReference typeRef = null;
            TypeReference[] typeArguments = null;
            if (expr instanceof ParameterizedQualifiedTypeReference) {
                typeRef = (ParameterizedQualifiedTypeReference) expr;
                typeArguments = ((ParameterizedQualifiedTypeReference) expr).typeArguments[lastIndex];
            } else if (expr instanceof ParameterizedSingleTypeReference) {
                typeRef = (ParameterizedSingleTypeReference) expr;
                typeArguments = ((ParameterizedSingleTypeReference) expr).typeArguments;
            }
            if (typeRef != null) {
                locator.reportAccurateParameterizedTypeReference(this.match, typeRef, lastIndex, typeArguments);
                return;
            }
        }
    } else if (this.pattern.hasTypeArguments()) { // binding has no type params, compatible erasure if pattern does
        this.match.setRule(SearchPattern.R_ERASURE_MATCH);
    }

    // Report match
    if (expr instanceof ArrayTypeReference) {
        locator.reportAccurateTypeReference(this.match, expr, this.pattern.simpleName);
        return;
    }
    if (refBinding.isLocalType()) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=82673
        LocalTypeBinding local = (LocalTypeBinding) refBinding.erasure();
        IJavaElement focus = this.pattern.focus;
        if (focus != null && local.enclosingMethod != null
                && focus.getParent().getElementType() == IJavaElement.METHOD) {
            IMethod method = (IMethod) focus.getParent();
            if (!CharOperation.equals(local.enclosingMethod.selector, method.getElementName().toCharArray())) {
                return;
            }
        }
    }
    if (this.pattern.simpleName == null) {
        this.match.setOffset(expr.sourceStart);
        this.match.setLength(expr.sourceEnd - expr.sourceStart + 1);
    }
    locator.report(this.match);
}

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

License:Open Source License

protected void reportDeclaration(ASTNode reference, IJavaElement element, MatchLocator locator,
        SimpleSet knownTypes) throws CoreException {
    int maxType = -1;
    TypeBinding typeBinding = null;
    if (reference instanceof TypeReference) {
        typeBinding = ((TypeReference) reference).resolvedType;
        maxType = Integer.MAX_VALUE;
    } else if (reference instanceof QualifiedNameReference) {
        QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
        Binding binding = qNameRef.binding;
        maxType = qNameRef.tokens.length - 1;
        switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
        case Binding.FIELD: // reading a field
            typeBinding = qNameRef.actualReceiverType;
            maxType -= qNameRef.otherBindings == null ? 1 : qNameRef.otherBindings.length + 1;
            break;
        case Binding.TYPE: //=============only type ==============
            if (binding instanceof TypeBinding)
                typeBinding = (TypeBinding) binding;
            break;
        case Binding.VARIABLE: //============unbound cases===========
        case Binding.TYPE | Binding.VARIABLE:
            if (binding instanceof ProblemFieldBinding) {
                typeBinding = qNameRef.actualReceiverType;
                maxType -= qNameRef.otherBindings == null ? 1 : qNameRef.otherBindings.length + 1;
            } else if (binding instanceof ProblemBinding) {
                ProblemBinding pbBinding = (ProblemBinding) binding;
                typeBinding = pbBinding.searchType; // second chance with recorded type so far
                char[] partialQualifiedName = pbBinding.name;
                maxType = CharOperation.occurencesOf('.', partialQualifiedName) - 1; // index of last bound token is one before the pb token
                if (typeBinding == null || maxType < 0)
                    return;
            }//  ww  w . j a va2s  . c o m
            break;
        }
    } else if (reference instanceof SingleNameReference) {
        typeBinding = (TypeBinding) ((SingleNameReference) reference).binding;
        maxType = 1;
    }

    if (typeBinding instanceof ArrayBinding)
        typeBinding = ((ArrayBinding) typeBinding).leafComponentType;
    if (typeBinding == null || typeBinding instanceof BaseTypeBinding)
        return;
    if (typeBinding instanceof ProblemReferenceBinding) {
        TypeBinding original = typeBinding.closestMatch();
        if (original == null)
            return; // original may not be set (bug 71279)
        typeBinding = original;
    }
    typeBinding = typeBinding.erasure();
    reportDeclaration((ReferenceBinding) typeBinding, maxType, locator, knownTypes);
}

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

License:Apache License

public JType get(TypeBinding binding) {
    binding = binding.erasure();
    String key = JdtUtil.signature(binding);
    JReferenceType sourceType = sourceTypes.get(key);

    if (sourceType != null) {
        assert !sourceType.isExternal();
        return sourceType;
    }/*from w w w .  j a  v a 2  s .c  om*/

    JType type = types.get(key);
    if (type != null) {
        assert type instanceof JPrimitiveType || type == JNullType.INSTANCE || type.isExternal();
        return type;
    }
    assert !(binding instanceof BaseTypeBinding);

    if (binding instanceof ArrayBinding) {
        ArrayBinding arrayBinding = (ArrayBinding) binding;
        JArrayType arrayType = new JArrayType(get(arrayBinding.elementsType()));
        if (arrayType.isExternal()) {
            types.put(key, arrayType);
        } else {
            sourceTypes.put(key, arrayType);
        }
        return arrayType;
    } else {
        ReferenceBinding refBinding = (ReferenceBinding) binding;
        JDeclaredType declType = createType(refBinding);
        try {
            if (declType instanceof JClassType) {
                ReferenceBinding superclass = refBinding.superclass();
                if (superclass != null && superclass.isValidBinding()) {
                    ((JClassType) declType).setSuperClass((JClassType) get(superclass));
                }
            }
            ReferenceBinding[] superInterfaces = refBinding.superInterfaces();
            if (superInterfaces != null) {
                for (ReferenceBinding intf : superInterfaces) {
                    if (intf.isValidBinding()) {
                        declType.addImplements((JInterfaceType) get(intf));
                    }
                }
            }
        } catch (AbortCompilation ignored) {
            /*
             * The currently-compiling unit has no errors; however, we're running
             * into a case where it references something with a bad hierarchy. This
             * doesn't cause an error in the current unit, but it does mean we run
             * into a wall here trying to construct the hierarchy. Catch the error
             * so that compilation can proceed; the error units themselves will
             * eventually cause the full compile to error out.
             */
        }
        // Emulate clinit method for super clinit calls.
        JMethod clinit = new JMethod(SourceOrigin.UNKNOWN, GwtAstBuilder.CLINIT_NAME, declType,
                JPrimitiveType.VOID, false, true, true, AccessModifier.PRIVATE);
        clinit.freezeParamTypes();
        clinit.setSynthetic();
        declType.addMethod(clinit);
        declType.setExternal(true);
        types.put(key, declType);
        return declType;
    }
}

From source file:lombok.eclipse.agent.PatchDelegate.java

License:Open Source License

private static String typeBindingToSignature(TypeBinding binding) {
    binding = binding.erasure();
    if (binding != null && binding.isBaseType()) {
        return new String(binding.sourceName());
    } else if (binding instanceof ReferenceBinding) {
        String pkg = binding.qualifiedPackageName() == null ? "" : new String(binding.qualifiedPackageName());
        String qsn = binding.qualifiedSourceName() == null ? "" : new String(binding.qualifiedSourceName());
        return pkg.isEmpty() ? qsn : (pkg + "." + qsn);
    } else if (binding instanceof ArrayBinding) {
        StringBuilder out = new StringBuilder();
        out.append(typeBindingToSignature(binding.leafComponentType()));
        for (int i = 0; i < binding.dimensions(); i++)
            out.append("[]");
        return out.toString();
    }/*  w  w w.j  a  va  2  s .  c o  m*/

    return "";
}

From source file:lombok.eclipse.agent.PatchExtensionMethod.java

License:Open Source License

private static List<MethodBinding> getApplicableExtensionMethodsDefinedInProvider(EclipseNode typeNode,
        ReferenceBinding extensionMethodProviderBinding, TypeBinding receiverType) {

    List<MethodBinding> extensionMethods = new ArrayList<MethodBinding>();
    CompilationUnitScope cuScope = ((CompilationUnitDeclaration) typeNode.top().get()).scope;
    for (MethodBinding method : extensionMethodProviderBinding.methods()) {
        if (!method.isStatic())
            continue;
        if (!method.isPublic())
            continue;
        if (method.parameters == null || method.parameters.length == 0)
            continue;
        TypeBinding firstArgType = method.parameters[0];
        if (receiverType.isProvablyDistinct(firstArgType)
                && !receiverType.isCompatibleWith(firstArgType.erasure()))
            continue;
        TypeBinding[] argumentTypes = Arrays.copyOfRange(method.parameters, 1, method.parameters.length);
        if ((receiverType instanceof ReferenceBinding) && ((ReferenceBinding) receiverType)
                .getExactMethod(method.selector, argumentTypes, cuScope) != null)
            continue;
        extensionMethods.add(method);//w  w w.ja v a 2s . c  om
    }
    return extensionMethods;
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNodeBuilder.java

License:Open Source License

TypeBinding toRawType(TypeBinding tb) {
    if (tb instanceof ParameterizedTypeBinding) {
        ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) tb;
        // resolver.getScope() can return null (if the resolver hasn't yet been used to resolve something) - using
        // the environment on the ptb seems safe. Other occurrences of getScope in this file could feasibly
        // be changed in the same way if NPEs become problems there too
        return ptb.environment().convertToRawType(ptb.genericType(), false);
        // return resolver.getScope().environment.convertToRawType(ptb.genericType(), false);
    } else if (tb instanceof TypeVariableBinding) {
        TypeBinding fb = ((TypeVariableBinding) tb).firstBound;
        if (fb == null) {
            return tb.erasure(); // Should be JLObject
            // return resolver.getScope().getJavaLangObject();
        }/* w  ww.  j a v a2s .  c o  m*/
        return fb;
    } else if (tb instanceof BinaryTypeBinding) {
        if (tb.isGenericType()) {
            try {
                Field f = BinaryTypeBinding.class.getDeclaredField("environment");
                f.setAccessible(true);
                LookupEnvironment le = (LookupEnvironment) f.get(tb);
                return le.convertToRawType(tb, false);
                // return resolver.getScope().environment.convertToRawType(tb, false);
            } catch (Exception e) {
                throw new RuntimeException("Problem building rawtype ", e);
            }
        } else {
            return tb;
        }
    } else if (tb instanceof ArrayBinding) {
        return tb;
    } else if (tb instanceof BaseTypeBinding) {
        return tb;
    } else if (tb instanceof SourceTypeBinding) {
        return tb;
    }
    throw new IllegalStateException("nyi " + tb.getClass());
}

From source file:org.eclipse.jdt.internal.compiler.lookup.MethodVerifier15.java

License:Open Source License

boolean areTypesEqual(TypeBinding one, TypeBinding two) {
    if (one == two)
        return true;
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329584
    switch (one.kind()) {
    case Binding.TYPE:
        switch (two.kind()) {
        case Binding.PARAMETERIZED_TYPE:
        case Binding.RAW_TYPE:
            if (one == two.erasure())
                return true;
        }//from   ww w. j av  a2 s . c o  m
        break;
    case Binding.RAW_TYPE:
    case Binding.PARAMETERIZED_TYPE:
        switch (two.kind()) {
        case Binding.TYPE:
            if (one.erasure() == two)
                return true;
        }
    }

    // need to consider X<?> and X<? extends Object> as the same 'type'
    if (one.isParameterizedType() && two.isParameterizedType())
        return one.isEquivalentTo(two) && two.isEquivalentTo(one);

    // Can skip this since we resolved each method before comparing it, see computeSubstituteMethod()
    //   if (one instanceof UnresolvedReferenceBinding)
    //      return ((UnresolvedReferenceBinding) one).resolvedType == two;
    //   if (two instanceof UnresolvedReferenceBinding)
    //      return ((UnresolvedReferenceBinding) two).resolvedType == one;
    return false; // all other type bindings are identical
}

From source file:org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.java

License:Open Source License

/**
 * Answer true if the receiver is visible to the receiverType and the invocationType.
 */// w w  w.java  2  s .c  om
public final boolean canBeSeenBy(ReferenceBinding receiverType, ReferenceBinding invocationType) {
    if (isPublic())
        return true;

    if (invocationType == this && invocationType == receiverType)
        return true;

    if (isProtected()) {
        // answer true if the invocationType is the declaringClass or they are in the same package
        // OR the invocationType is a subclass of the declaringClass
        //    AND the invocationType is the invocationType or its subclass
        //    OR the type is a static method accessed directly through a type
        //    OR previous assertions are true for one of the enclosing type
        if (invocationType == this)
            return true;
        if (invocationType.fPackage == this.fPackage)
            return true;

        TypeBinding currentType = invocationType.erasure();
        TypeBinding declaringClass = enclosingType().erasure(); // protected types always have an enclosing one
        if (declaringClass == invocationType)
            return true;
        if (declaringClass == null)
            return false; // could be null if incorrect top-level protected type
        //int depth = 0;
        do {
            if (currentType.findSuperTypeOriginatingFrom(declaringClass) != null)
                return true;
            //depth++;
            currentType = currentType.enclosingType();
        } while (currentType != null);
        return false;
    }

    if (isPrivate()) {
        // answer true if the receiverType is the receiver or its enclosingType
        // AND the invocationType and the receiver have a common enclosingType
        receiverCheck: {
            if (!(receiverType == this || receiverType == enclosingType())) {
                // special tolerance for type variable direct bounds, but only if compliance <= 1.6, see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=334622
                if (receiverType.isTypeVariable()) {
                    TypeVariableBinding typeVariable = (TypeVariableBinding) receiverType;
                    if (typeVariable.environment.globalOptions.complianceLevel <= ClassFileConstants.JDK1_6
                            && (typeVariable.isErasureBoundTo(erasure())
                                    || typeVariable.isErasureBoundTo(enclosingType().erasure())))
                        break receiverCheck;
                }
                return false;
            }
        }

        if (invocationType != this) {
            ReferenceBinding outerInvocationType = invocationType;
            ReferenceBinding temp = outerInvocationType.enclosingType();
            while (temp != null) {
                outerInvocationType = temp;
                temp = temp.enclosingType();
            }

            ReferenceBinding outerDeclaringClass = (ReferenceBinding) erasure();
            temp = outerDeclaringClass.enclosingType();
            while (temp != null) {
                outerDeclaringClass = temp;
                temp = temp.enclosingType();
            }
            if (outerInvocationType != outerDeclaringClass)
                return false;
        }
        return true;
    }

    // isDefault()
    if (invocationType.fPackage != this.fPackage)
        return false;

    ReferenceBinding currentType = receiverType;
    TypeBinding originalDeclaringClass = (enclosingType() == null ? this : enclosingType()).original();
    do {
        if (currentType.isCapture()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=285002
            if (originalDeclaringClass == currentType.erasure().original())
                return true;
        } else {
            if (originalDeclaringClass == currentType.original())
                return true;
        }
        PackageBinding currentPackage = currentType.fPackage;
        // package could be null for wildcards/intersection types, ignore and recurse in superclass
        if (currentPackage != null && currentPackage != this.fPackage)
            return false;
    } while ((currentType = currentType.superclass()) != null);
    return false;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.java

License:Open Source License

/**
 * Answer true if the receiver is visible to the type provided by the scope.
 */// ww  w  . ja  v a  2 s  .co m
public final boolean canBeSeenBy(Scope scope) {
    // GROOVY start
    if (scope.compilationUnitScope() != null && scope.compilationUnitScope().canSeeEverything()) {
        return true;
    }
    // GROOVY end
    if (isPublic())
        return true;

    SourceTypeBinding invocationType = scope.enclosingSourceType();
    if (invocationType == this)
        return true;

    if (invocationType == null) // static import call
        return !isPrivate() && scope.getCurrentPackage() == this.fPackage;

    if (isProtected()) {
        // answer true if the invocationType is the declaringClass or they are in the same package
        // OR the invocationType is a subclass of the declaringClass
        //    AND the invocationType is the invocationType or its subclass
        //    OR the type is a static method accessed directly through a type
        //    OR previous assertions are true for one of the enclosing type
        if (invocationType.fPackage == this.fPackage)
            return true;

        TypeBinding declaringClass = enclosingType(); // protected types always have an enclosing one
        if (declaringClass == null)
            return false; // could be null if incorrect top-level protected type
        declaringClass = declaringClass.erasure();// erasure cannot be null
        TypeBinding currentType = invocationType.erasure();
        // int depth = 0;
        do {
            if (declaringClass == invocationType)
                return true;
            if (currentType.findSuperTypeOriginatingFrom(declaringClass) != null)
                return true;
            // depth++;
            currentType = currentType.enclosingType();
        } while (currentType != null);
        return false;
    }
    if (isPrivate()) {
        // answer true if the receiver and the invocationType have a common enclosingType
        // already know they are not the identical type
        ReferenceBinding outerInvocationType = invocationType;
        ReferenceBinding temp = outerInvocationType.enclosingType();
        while (temp != null) {
            outerInvocationType = temp;
            temp = temp.enclosingType();
        }

        ReferenceBinding outerDeclaringClass = (ReferenceBinding) erasure();
        temp = outerDeclaringClass.enclosingType();
        while (temp != null) {
            outerDeclaringClass = temp;
            temp = temp.enclosingType();
        }
        return outerInvocationType == outerDeclaringClass;
    }

    // isDefault()
    return invocationType.fPackage == this.fPackage;
}