Example usage for org.eclipse.jdt.internal.compiler.lookup LookupEnvironment createMissingType

List of usage examples for org.eclipse.jdt.internal.compiler.lookup LookupEnvironment createMissingType

Introduction

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

Prototype

public MissingTypeBinding createMissingType(PackageBinding packageBinding, char[][] compoundName) 

Source Link

Usage

From source file:spoon.support.compiler.jdt.ReferenceBuilder.java

License:Open Source License

/**
 * 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.//from  w  w w . ja  v  a2 s  .  c o m
 *
 * @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;
    if (cuDeclaration == null) {
        return null;
    }
    LookupEnvironment environment = cuDeclaration.scope.environment;

    if (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);
                    // `findImport(chars, false, false);` and `createPackage(chars)` require
                    // an array with a minimum length of 1 and throw an
                    // 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;
}