Search in sources :

Example 1 with ProblemPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding in project spoon by INRIA.

the class ReferenceBuilder method getDeclaringReferenceFromImports.

/**
 * Try to get the declaring reference (package or type) from imports of the current
 * compilation unit declaration (current class). This method returns a CtReference
 * which can be a CtTypeReference if it retrieves the information in an static import,
 * a CtPackageReference if it retrieves the information in an standard import, otherwise
 * it returns null.
 *
 * @param expectedName Name expected in imports.
 * @return CtReference which can be a CtTypeReference, a CtPackageReference or null.
 */
CtReference getDeclaringReferenceFromImports(char[] expectedName) {
    CompilationUnitDeclaration cuDeclaration = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration;
    LookupEnvironment environment = cuDeclaration.scope.environment;
    if (cuDeclaration != null && cuDeclaration.imports != null) {
        for (ImportReference anImport : cuDeclaration.imports) {
            if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], expectedName)) {
                if (anImport.isStatic()) {
                    int indexDeclaring = 2;
                    if ((anImport.bits & ASTNode.OnDemand) != 0) {
                        // With .*
                        indexDeclaring = 1;
                    }
                    char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - indexDeclaring);
                    char[][] className = CharOperation.subarray(anImport.getImportName(), anImport.getImportName().length - indexDeclaring, anImport.getImportName().length - (indexDeclaring - 1));
                    PackageBinding aPackage;
                    try {
                        if (packageName.length != 0) {
                            aPackage = environment.createPackage(packageName);
                        } else {
                            aPackage = null;
                        }
                        final MissingTypeBinding declaringType = environment.createMissingType(aPackage, className);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = true;
                        final CtTypeReference<Object> typeReference = getTypeReference(declaringType);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = false;
                        return typeReference;
                    } catch (NullPointerException e) {
                        return null;
                    }
                } else {
                    PackageBinding packageBinding = null;
                    char[][] chars = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1);
                    // ArrayIndexOutOfBoundsException if `chars.length == 0`. Fixes #759.
                    if (chars.length > 0) {
                        Binding someBinding = cuDeclaration.scope.findImport(chars, false, false);
                        if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) {
                            packageBinding = (PackageBinding) someBinding;
                        } else {
                            try {
                                packageBinding = environment.createPackage(chars);
                            } catch (NullPointerException e) {
                                packageBinding = null;
                            }
                        }
                    }
                    if (packageBinding == null || packageBinding instanceof ProblemPackageBinding) {
                        // Big crisis here. We are already in noclasspath mode but JDT doesn't support always
                        // creation of a package in this mode. So, if we are in this brace, we make the job of JDT...
                        packageBinding = new PackageBinding(chars, null, environment, environment.module);
                    }
                    return getPackageReference(packageBinding);
                }
            }
        }
    }
    return null;
}
Also used : BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ProblemBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) CatchParameterBinding(org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding)

Aggregations

CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 ImportReference (org.eclipse.jdt.internal.compiler.ast.ImportReference)1 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1 BaseTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding)1 BinaryTypeBinding (org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding)1 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)1 CaptureBinding (org.eclipse.jdt.internal.compiler.lookup.CaptureBinding)1 CatchParameterBinding (org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding)1 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)1 LocalTypeBinding (org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding)1 LocalVariableBinding (org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding)1 LookupEnvironment (org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1 MissingTypeBinding (org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding)1 PackageBinding (org.eclipse.jdt.internal.compiler.lookup.PackageBinding)1 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)1 PolyTypeBinding (org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding)1 ProblemBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemBinding)1 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)1 ProblemPackageBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding)1