Search in sources :

Example 1 with ImportReference

use of org.eclipse.jdt.internal.compiler.ast.ImportReference in project spoon by INRIA.

the class JDTImportBuilder method build.

// package visible method in a package visible class, not in the public API
void build() {
    if (declarationUnit.imports == null || declarationUnit.imports.length == 0) {
        return;
    }
    for (ImportReference importRef : declarationUnit.imports) {
        String importName = importRef.toString();
        if (!importRef.isStatic()) {
            if (importName.endsWith("*")) {
                int lastDot = importName.lastIndexOf(".");
                String packageName = importName.substring(0, lastDot);
                // only get package from the model by traversing from rootPackage the model
                // it does not use reflection to achieve that
                CtPackage ctPackage = this.factory.Package().get(packageName);
                if (ctPackage != null) {
                    this.imports.add(factory.Type().createImport(ctPackage.getReference()));
                }
            } else {
                CtType klass = this.getOrLoadClass(importName);
                if (klass != null) {
                    this.imports.add(factory.Type().createImport(klass.getReference()));
                }
            }
        } else {
            int lastDot = importName.lastIndexOf(".");
            String className = importName.substring(0, lastDot);
            String methodOrFieldName = importName.substring(lastDot + 1);
            CtType klass = this.getOrLoadClass(className);
            if (klass != null) {
                if (methodOrFieldName.equals("*")) {
                    this.imports.add(factory.Type().createImport(factory.Type().createWildcardStaticTypeMemberReference(klass.getReference())));
                } else {
                    List<CtNamedElement> methodOrFields = klass.getElements(new NamedElementFilter<>(CtNamedElement.class, methodOrFieldName));
                    if (methodOrFields.size() > 0) {
                        CtNamedElement methodOrField = methodOrFields.get(0);
                        this.imports.add(factory.Type().createImport(methodOrField.getReference()));
                    }
                }
            }
        }
    }
    spoonUnit.setImports(this.imports);
}
Also used : ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) CtType(spoon.reflect.declaration.CtType) CtPackage(spoon.reflect.declaration.CtPackage) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Example 2 with ImportReference

use of org.eclipse.jdt.internal.compiler.ast.ImportReference in project spoon by INRIA.

the class JDTTreeBuilderQuery method searchPackage.

/**
 * Searches a package used in units declared in a compilation unit.
 *
 * @param packageName
 * 		Package name.
 * @param unitsToProcess
 * 		Search the package expected in units.
 * @return import reference which correspond to the package expected.
 */
static ImportReference searchPackage(char[][] packageName, CompilationUnitDeclaration[] unitsToProcess) {
    for (CompilationUnitDeclaration unit : unitsToProcess) {
        final ImportReference currentPackage = unit.currentPackage;
        if (currentPackage == null) {
            continue;
        }
        final char[][] tokens = currentPackage.tokens;
        if (packageName.length > tokens.length) {
            continue;
        }
        boolean isFound = true;
        for (int i = 0; i < packageName.length; i++) {
            char[] chars = packageName[i];
            if (!CharOperation.equals(chars, tokens[i])) {
                isFound = false;
                break;
            }
        }
        if (isFound) {
            return currentPackage;
        }
    }
    return null;
}
Also used : ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)

Example 3 with ImportReference

use of org.eclipse.jdt.internal.compiler.ast.ImportReference 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

ImportReference (org.eclipse.jdt.internal.compiler.ast.ImportReference)3 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 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