Search in sources :

Example 6 with CtPackageReference

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;
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtInvocation(spoon.reflect.code.CtInvocation) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtReference(spoon.reflect.reference.CtReference) CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 7 with CtPackageReference

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;
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference)

Example 8 with CtPackageReference

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());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) CtPackageReference(spoon.reflect.reference.CtPackageReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with CtPackageReference

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 10 with CtPackageReference

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();
        }
    }
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtFieldReference(spoon.reflect.reference.CtFieldReference) HashSet(java.util.HashSet)

Aggregations

CtPackageReference (spoon.reflect.reference.CtPackageReference)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 CtClass (spoon.reflect.declaration.CtClass)3 CtField (spoon.reflect.declaration.CtField)3 CtReference (spoon.reflect.reference.CtReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtElement (spoon.reflect.declaration.CtElement)2 CtImport (spoon.reflect.declaration.CtImport)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1