use of org.eclipse.jdt.internal.compiler.lookup.PackageBinding 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;
}
Aggregations