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