use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class ImportScannerImpl method addClassImport.
/**
* Adds a type to the classImports.
*/
protected boolean addClassImport(CtTypeReference<?> ref) {
this.exploredReferences.add(ref);
if (ref == null) {
return false;
}
if (targetType != null && targetType.getSimpleName().equals(ref.getSimpleName()) && !targetType.equals(ref)) {
return false;
}
if (classImports.containsKey(ref.getSimpleName())) {
return isImportedInClassImports(ref);
}
// don't import unnamed package elements
if (ref.getPackage() == null || ref.getPackage().isUnnamedPackage()) {
return false;
}
if (targetType != null && targetType.canAccess(ref) == false) {
// ref type is not visible in targetType we must not add import for it, java compiler would fail on that.
return false;
}
if (this.isThereAnotherClassWithSameNameInAnotherPackage(ref)) {
return false;
}
if (targetType != null) {
try {
CtElement parent = ref.getParent();
if (parent != null) {
parent = parent.getParent();
if (parent != null) {
if ((parent instanceof CtFieldAccess) || (parent instanceof CtExecutable) || (parent instanceof CtInvocation)) {
CtTypeReference declaringType;
CtReference reference;
CtPackageReference pack = targetType.getPackage();
if (parent instanceof CtFieldAccess) {
CtFieldAccess field = (CtFieldAccess) parent;
CtFieldReference localReference = field.getVariable();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else if (parent instanceof CtExecutable) {
CtExecutable exec = (CtExecutable) parent;
CtExecutableReference localReference = exec.getReference();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else if (parent instanceof CtInvocation) {
CtInvocation invo = (CtInvocation) parent;
CtExecutableReference localReference = invo.getExecutable();
declaringType = localReference.getDeclaringType();
reference = localReference;
} else {
declaringType = null;
reference = null;
}
if (reference != null && isImported(reference)) {
// if we are in the **same** package we do the import for test with method isImported
if (declaringType != null) {
if (declaringType.getPackage() != null && !declaringType.getPackage().isUnnamedPackage()) {
// ignore java.lang package
if (!declaringType.getPackage().getSimpleName().equals("java.lang")) {
// ignore type in same package
if (declaringType.getPackage().getSimpleName().equals(pack.getSimpleName())) {
classImports.put(ref.getSimpleName(), ref);
return true;
}
}
}
}
}
}
}
}
} catch (ParentNotInitializedException e) {
}
CtPackageReference pack = targetType.getPackage();
if (pack != null && ref.getPackage() != null && !ref.getPackage().isUnnamedPackage()) {
// ignore java.lang package
if (!ref.getPackage().getSimpleName().equals("java.lang")) {
// ignore type in same package
if (ref.getPackage().getSimpleName().equals(pack.getSimpleName())) {
return false;
}
}
}
}
classImports.put(ref.getSimpleName(), ref);
return true;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class CtTypeImpl method getPackageReference.
/**
* Return the package reference for the corresponding type reference. For
* inner type, return the package reference of the top-most enclosing type.
* This helper method is meant to deal with package references that are
* <code>null</code> for inner types.
*
* @param tref the type reference
* @return the corresponding package reference
* @see CtTypeReference#getPackage()
* @since 4.0
*/
private static CtPackageReference getPackageReference(CtTypeReference<?> tref) {
CtPackageReference pref = tref.getPackage();
while (pref == null) {
tref = tref.getDeclaringType();
pref = tref.getPackage();
}
return pref;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class ImportBuilderTest method testWithStaticStarredImportFromInterface.
@Test
public void testWithStaticStarredImportFromInterface() {
// contract: when a starred import is used with a target package, all classes of the package should be imported
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StarredImport.java");
spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/fullpack/");
spoon.getEnvironment().setAutoImports(true);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(StarredImport.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
Collection<CtImport> imports = unitA.getImports();
assertEquals(1, imports.size());
Iterator<CtImport> iterator = imports.iterator();
CtImport ctImport = iterator.next();
assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
assertTrue(ctImport.getReference() instanceof CtPackageReference);
CtPackageReference ref = (CtPackageReference) ctImport.getReference();
assertEquals("spoon.test.jdtimportbuilder.testclasses.fullpack", ref.getQualifiedName());
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class PackageTest method testGetFQNInNoClassPath.
@Test
public void testGetFQNInNoClassPath() {
// contract: CtPackageReference simple name is also the fully qualified name of its referenced package, even in noclasspath
final Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/noclasspath/TorIntegration.java");
spoon.getEnvironment().setNoClasspath(true);
spoon.buildModel();
CtClass torClass = spoon.getFactory().Class().get("com.duckduckgo.mobile.android.util.TorIntegration");
CtField field = torClass.getField("orbotHelper");
CtPackageReference fieldPkg = field.getType().getPackage();
assertEquals("info.guardianproject.onionkit.ui", fieldPkg.getSimpleName());
assertEquals("info.guardianproject.onionkit.ui", fieldPkg.getQualifiedName());
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class ElementPrinterHelper method writeImports.
/**
* writes the imports in a specific order (eg all static imports together
*/
public void writeImports(Collection<CtImport> imports) {
Set<String> setImports = new HashSet<>();
Set<String> setStaticImports = new HashSet<>();
for (CtImport ctImport : imports) {
String importTypeStr;
switch(ctImport.getImportKind()) {
case TYPE:
CtTypeReference typeRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case ALL_TYPES:
CtPackageReference packageRef = (CtPackageReference) ctImport.getReference();
importTypeStr = packageRef.getQualifiedName() + ".*";
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case METHOD:
CtExecutableReference execRef = (CtExecutableReference) ctImport.getReference();
if (execRef.getDeclaringType() != null) {
setStaticImports.add(this.removeInnerTypeSeparator(execRef.getDeclaringType().getQualifiedName()) + "." + execRef.getSimpleName());
}
break;
case FIELD:
CtFieldReference fieldRef = (CtFieldReference) ctImport.getReference();
setStaticImports.add(this.removeInnerTypeSeparator(fieldRef.getDeclaringType().getQualifiedName()) + "." + fieldRef.getSimpleName());
break;
case ALL_STATIC_MEMBERS:
CtTypeReference typeStarRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeStarRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setStaticImports.add(importTypeStr);
}
break;
}
}
List<String> sortedImports = new ArrayList<>(setImports);
Collections.sort(sortedImports);
boolean isFirst = true;
for (String importLine : sortedImports) {
if (isFirst) {
printer.writeln();
printer.writeln();
isFirst = false;
}
printer.writeKeyword("import").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
if (setStaticImports.size() > 0) {
if (isFirst) {
printer.writeln();
}
printer.writeln();
List<String> sortedStaticImports = new ArrayList<>(setStaticImports);
Collections.sort(sortedStaticImports);
for (String importLine : sortedStaticImports) {
printer.writeKeyword("import").writeSpace().writeKeyword("static").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
}
}
Aggregations