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;
}
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;
}
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);
}
}
}
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);
}
}
}
}
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);
}
Aggregations