Search in sources :

Example 1 with PlainPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding 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;
    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);
                    // 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) {

                            // PackageBinding was a class instead of abstract class in earlier jdt versions.
                            // To circumvent this change to an abstract class, an anonymous class is used here.
                            @Override
                            public PlainPackageBinding getIncarnation(ModuleBinding arg0) {
                                // https://github.com/eclipse/eclipse.jdt.core/blob/master/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/PlainPackageBinding.java#L43
                                return null;
                            }
                        };
                    }
                    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) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) 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) VoidTypeBinding(org.eclipse.jdt.internal.compiler.lookup.VoidTypeBinding) 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) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) 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) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)

Example 2 with PlainPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding in project bazel-jdt-java-toolchain by salesforce.

the class ModuleElementImpl method getDirectives.

@Override
public List<? extends Directive> getDirectives() {
    if (isUnnamed()) {
        return EMPTY_DIRECTIVES;
    }
    if (this.directives == null)
        this.directives = new ArrayList<>();
    PlainPackageBinding[] packs = this.binding.getExports();
    for (PlainPackageBinding exp : packs) {
        this.directives.add(new ExportsDirectiveImpl(exp));
    }
    Set<ModuleBinding> transitive = new HashSet<>();
    for (ModuleBinding mBinding : this.binding.getRequiresTransitive()) {
        transitive.add(mBinding);
    }
    ModuleBinding[] required = this.binding.getRequires();
    for (ModuleBinding mBinding : required) {
        if (transitive.contains(mBinding)) {
            this.directives.add(new RequiresDirectiveImpl(mBinding, true));
        } else {
            this.directives.add(new RequiresDirectiveImpl(mBinding, false));
        }
    }
    TypeBinding[] tBindings = this.binding.getUses();
    for (TypeBinding tBinding : tBindings) {
        this.directives.add(new UsesDirectiveImpl(tBinding));
    }
    tBindings = this.binding.getServices();
    for (TypeBinding tBinding : tBindings) {
        this.directives.add(new ProvidesDirectiveImpl(tBinding));
    }
    packs = this.binding.getOpens();
    for (PlainPackageBinding exp : packs) {
        this.directives.add(new OpensDirectiveImpl(exp));
    }
    return this.directives;
}
Also used : TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ArrayList(java.util.ArrayList) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) HashSet(java.util.HashSet)

Example 3 with PlainPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding in project bazel-jdt-java-toolchain by salesforce.

the class ModuleDeclaration method resolvePackageDirectives.

/**
 * Resolve those module directives that relate to packages (exports, opens).
 */
public void resolvePackageDirectives(CompilationUnitScope cuScope) {
    if (this.binding == null) {
        this.ignoreFurtherInvestigation = true;
        return;
    }
    if (this.hasResolvedPackageDirectives)
        return;
    this.hasResolvedPackageDirectives = true;
    Set<PlainPackageBinding> exportedPkgs = new HashSet<>();
    for (int i = 0; i < this.exportsCount; i++) {
        ExportsStatement ref = this.exports[i];
        if (ref != null && ref.resolve(cuScope)) {
            if (!exportedPkgs.add(ref.resolvedPackage)) {
                cuScope.problemReporter().invalidPackageReference(IProblem.DuplicateExports, ref);
            }
            char[][] targets = null;
            if (ref.targets != null) {
                targets = new char[ref.targets.length][];
                for (int j = 0; j < targets.length; j++) targets[j] = ref.targets[j].moduleName;
            }
            this.binding.addResolvedExport(ref.resolvedPackage, targets);
        }
    }
    HashtableOfObject openedPkgs = new HashtableOfObject();
    for (int i = 0; i < this.opensCount; i++) {
        OpensStatement ref = this.opens[i];
        if (isOpen()) {
            cuScope.problemReporter().invalidOpensStatement(ref, this);
        } else {
            if (openedPkgs.containsKey(ref.pkgName)) {
                cuScope.problemReporter().invalidPackageReference(IProblem.DuplicateOpens, ref);
            } else {
                openedPkgs.put(ref.pkgName, ref);
                ref.resolve(cuScope);
            }
            char[][] targets = null;
            if (ref.targets != null) {
                targets = new char[ref.targets.length][];
                for (int j = 0; j < targets.length; j++) targets[j] = ref.targets[j].moduleName;
            }
            this.binding.addResolvedOpens(ref.resolvedPackage, targets);
        }
    }
}
Also used : PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject) HashSet(java.util.HashSet)

Example 4 with PlainPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding in project bazel-jdt-java-toolchain by salesforce.

the class ModuleDeclaration method analyseOneDependency.

private void analyseOneDependency(RequiresStatement requiresStat, ModuleBinding requiredModule, CompilationUnitScope skope, Map<String, Set<ModuleBinding>> pack2mods) {
    for (PlainPackageBinding pack : requiredModule.getExports()) {
        Set<ModuleBinding> mods = pack2mods.get(String.valueOf(pack.readableName()));
        if (mods != null && mods.size() > 1) {
            CompilerOptions compilerOptions = skope.compilerOptions();
            boolean inJdtDebugCompileMode = compilerOptions.enableJdtDebugCompileMode;
            if (!inJdtDebugCompileMode) {
                skope.problemReporter().conflictingPackagesFromModules(pack, mods, requiresStat.sourceStart, requiresStat.sourceEnd);
            }
        }
    }
}
Also used : PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) SourceModuleBinding(org.eclipse.jdt.internal.compiler.lookup.SourceModuleBinding)

Example 5 with PlainPackageBinding

use of org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding in project bazel-jdt-java-toolchain by salesforce.

the class ModuleElementImpl method getEnclosedElements.

@Override
public List<? extends Element> getEnclosedElements() {
    ModuleBinding module = this.binding;
    Set<PlainPackageBinding> unique = new HashSet<>();
    for (PlainPackageBinding p : module.declaredPackages.values()) {
        if (!p.hasCompilationUnit(true))
            continue;
        unique.add(p);
    }
    if (module.isUnnamed()) {
        PlainPackageBinding def = module.environment.defaultPackage;
        // FIXME: Does it have any impact for unnamed modules - default package combo?
        if (def != null && def.hasCompilationUnit(true)) {
            unique.add(def);
        }
    } else {
        for (PlainPackageBinding pBinding : this.binding.getExports()) {
            unique.add(pBinding);
        }
        for (PlainPackageBinding pBinding : this.binding.getOpens()) {
            unique.add(pBinding);
        }
    }
    List<Element> enclosed = new ArrayList<>(unique.size());
    for (PlainPackageBinding p : unique) {
        PackageElement pElement = (PackageElement) _env.getFactory().newElement(p);
        enclosed.add(pElement);
    }
    return Collections.unmodifiableList(enclosed);
}
Also used : PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) ModuleElement(javax.lang.model.element.ModuleElement) PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) PackageElement(javax.lang.model.element.PackageElement) HashSet(java.util.HashSet)

Aggregations

PlainPackageBinding (org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding)7 ModuleBinding (org.eclipse.jdt.internal.compiler.lookup.ModuleBinding)6 HashSet (java.util.HashSet)4 SourceModuleBinding (org.eclipse.jdt.internal.compiler.lookup.SourceModuleBinding)3 ArrayList (java.util.ArrayList)2 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Element (javax.lang.model.element.Element)1 ModuleElement (javax.lang.model.element.ModuleElement)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 ImportReference (org.eclipse.jdt.internal.compiler.ast.ImportReference)1 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)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