Example usage for org.eclipse.jdt.internal.compiler.lookup ProblemReferenceBinding closestReferenceMatch

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ProblemReferenceBinding closestReferenceMatch

Introduction

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

Prototype

public ReferenceBinding closestReferenceMatch() 

Source Link

Usage

From source file:com.redhat.ceylon.eclipse.core.model.JDTModelLoader.java

License:Open Source License

private static ReferenceBinding toBinding(IType type, LookupEnvironment theLookupEnvironment,
        char[][] compoundName) throws JavaModelException {
    ITypeRoot typeRoot = type.getTypeRoot();

    if (typeRoot instanceof IClassFile) {
        ClassFile classFile = (ClassFile) typeRoot;

        IFile classFileRsrc = (IFile) classFile.getCorrespondingResource();
        if (classFileRsrc != null && !classFileRsrc.exists()) {
            //the .class file has been deleted
            return null;
        }/* ww w .jav  a 2s  . c om*/

        BinaryTypeBinding binaryTypeBinding = null;
        try {
            IBinaryType binaryType = classFile.getBinaryTypeInfo(classFileRsrc, true);
            binaryTypeBinding = theLookupEnvironment.cacheBinaryType(binaryType, null);
        } catch (JavaModelException e) {
            if (!e.isDoesNotExist()) {
                throw e;
            }
        }

        if (binaryTypeBinding == null) {
            ReferenceBinding existingType = theLookupEnvironment.getCachedType(compoundName);
            if (existingType == null || !(existingType instanceof BinaryTypeBinding)) {
                return null;
            }
            binaryTypeBinding = (BinaryTypeBinding) existingType;
        }
        return binaryTypeBinding;
    } else {
        ReferenceBinding referenceBinding = theLookupEnvironment.getType(compoundName);
        if (referenceBinding != null && !(referenceBinding instanceof BinaryTypeBinding)) {

            if (referenceBinding instanceof ProblemReferenceBinding) {
                ProblemReferenceBinding problemReferenceBinding = (ProblemReferenceBinding) referenceBinding;
                if (problemReferenceBinding.problemId() == ProblemReasons.InternalNameProvided) {
                    referenceBinding = problemReferenceBinding.closestReferenceMatch();
                } else {
                    System.out.println(
                            ProblemReferenceBinding.problemReasonString(problemReferenceBinding.problemId()));
                    return null;
                }
            }
            return referenceBinding;
        }
        return null;
    }
}

From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTModelLoader.java

License:Open Source License

private ClassMirror buildClassMirror(String name) {
    try {// w ww  .j ava 2 s .c  o m
        IType type = javaProject.findType(name);
        if (type == null) {
            return null;
        }

        LookupEnvironment theLookupEnvironment = getLookupEnvironment();
        if (type.isBinary()) {
            ClassFile classFile = (ClassFile) type.getClassFile();

            if (classFile != null) {
                IPackageFragmentRoot fragmentRoot = classFile.getPackageFragmentRoot();
                if (fragmentRoot != null) {
                    if (isInCeylonClassesOutputFolder(fragmentRoot.getPath())) {
                        return null;
                    }
                }

                IFile classFileRsrc = (IFile) classFile.getCorrespondingResource();
                IBinaryType binaryType = classFile.getBinaryTypeInfo(classFileRsrc, true);
                if (classFileRsrc != null && !classFileRsrc.exists()) {
                    //the .class file has been deleted
                    return null;
                }
                BinaryTypeBinding binaryTypeBinding = theLookupEnvironment.cacheBinaryType(binaryType, null);
                if (binaryTypeBinding == null) {
                    char[][] compoundName = CharOperation.splitOn('/', binaryType.getName());
                    ReferenceBinding existingType = theLookupEnvironment.getCachedType(compoundName);
                    if (existingType == null || !(existingType instanceof BinaryTypeBinding)) {
                        return null;
                    }
                    binaryTypeBinding = (BinaryTypeBinding) existingType;
                }
                return new JDTClass(binaryTypeBinding, theLookupEnvironment);
            }
        } else {
            char[][] compoundName = CharOperation.splitOn('.', type.getFullyQualifiedName().toCharArray());
            ReferenceBinding referenceBinding = theLookupEnvironment.getType(compoundName);
            if (referenceBinding != null) {
                if (referenceBinding instanceof ProblemReferenceBinding) {
                    ProblemReferenceBinding problemReferenceBinding = (ProblemReferenceBinding) referenceBinding;
                    if (problemReferenceBinding.problemId() == ProblemReasons.InternalNameProvided) {
                        referenceBinding = problemReferenceBinding.closestReferenceMatch();
                    } else {
                        System.out.println(ProblemReferenceBinding
                                .problemReasonString(problemReferenceBinding.problemId()));
                        return null;
                    }
                }
                return new JDTClass(referenceBinding, theLookupEnvironment);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Open Source License

public final TypeBinding getType(char[][] compoundName, int typeNameLength) {
    if (typeNameLength == 1) {
        // Would like to remove this test and require senders to specially handle base types
        TypeBinding binding = getBaseType(compoundName[0]);
        if (binding != null)
            return binding;
    }//from   w  w w . j av  a 2 s .com

    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordQualifiedReference(compoundName);
    Binding binding = getTypeOrPackage(compoundName[0],
            typeNameLength == 1 ? Binding.TYPE : Binding.TYPE | Binding.PACKAGE, true);
    if (binding == null) {
        char[][] qName = new char[][] { compoundName[0] };
        return new ProblemReferenceBinding(qName,
                environment().createMissingType(compilationUnitScope().getCurrentPackage(), qName),
                ProblemReasons.NotFound);
    }
    if (!binding.isValidBinding()) {
        if (binding instanceof PackageBinding) {
            char[][] qName = new char[][] { compoundName[0] };
            return new ProblemReferenceBinding(qName, environment().createMissingType(null, qName),
                    ProblemReasons.NotFound);
        }
        return (ReferenceBinding) binding;
    }
    int currentIndex = 1;
    boolean checkVisibility = false;
    if (binding instanceof PackageBinding) {
        PackageBinding packageBinding = (PackageBinding) binding;
        while (currentIndex < typeNameLength) {
            binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++]); // does not check visibility
            if (binding == null) {
                char[][] qName = CharOperation.subarray(compoundName, 0, currentIndex);
                return new ProblemReferenceBinding(qName,
                        environment().createMissingType(packageBinding, qName), ProblemReasons.NotFound);
            }
            if (!binding.isValidBinding())
                return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex),
                        binding instanceof ReferenceBinding
                                ? (ReferenceBinding) ((ReferenceBinding) binding).closestMatch()
                                : null,
                        binding.problemId());
            if (!(binding instanceof PackageBinding))
                break;
            packageBinding = (PackageBinding) binding;
        }
        if (binding instanceof PackageBinding) {
            char[][] qName = CharOperation.subarray(compoundName, 0, currentIndex);
            return new ProblemReferenceBinding(qName, environment().createMissingType(null, qName),
                    ProblemReasons.NotFound);
        }
        checkVisibility = true;
    }

    // binding is now a ReferenceBinding
    ReferenceBinding typeBinding = (ReferenceBinding) binding;
    unitScope.recordTypeReference(typeBinding);
    if (checkVisibility) // handles the fall through case
        if (!typeBinding.canBeSeenBy(this))
            return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex),
                    typeBinding, ProblemReasons.NotVisible);

    while (currentIndex < typeNameLength) {
        typeBinding = getMemberType(compoundName[currentIndex++], typeBinding);
        if (!typeBinding.isValidBinding()) {
            if (typeBinding instanceof ProblemReferenceBinding) {
                ProblemReferenceBinding problemBinding = (ProblemReferenceBinding) typeBinding;
                return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex),
                        problemBinding.closestReferenceMatch(), typeBinding.problemId());
            }
            return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex),
                    (ReferenceBinding) ((ReferenceBinding) binding).closestMatch(), typeBinding.problemId());
        }
    }
    return typeBinding;
}