Search in sources :

Example 11 with CtImport

use of spoon.reflect.declaration.CtImport in project dspot by STAMP-project.

the class AmplificationHelper method getImport.

@Deprecated
public static Set<CtType> getImport(CtType type) {
    if (!AmplificationHelper.importByClass.containsKey(type)) {
        ImportScanner importScanner = new ImportScannerImpl();
        try {
            importScanner.computeImports(type);
            Set<CtType> set = importScanner.getAllImports().stream().map(CtImport::getReference).filter(Objects::nonNull).filter(ctElement -> ctElement instanceof CtType).map(ctElement -> (CtType) ctElement).collect(Collectors.toSet());
            AmplificationHelper.importByClass.put(type, set);
        } catch (Exception e) {
            AmplificationHelper.importByClass.put(type, new HashSet<>(0));
        }
    }
    return AmplificationHelper.importByClass.get(type);
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) IntStream(java.util.stream.IntStream) CtPackageReference(spoon.reflect.reference.CtPackageReference) java.util(java.util) Logger(org.slf4j.Logger) ImportScanner(spoon.reflect.visitor.ImportScanner) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtInvocation(spoon.reflect.code.CtInvocation) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) CtComment(spoon.reflect.code.CtComment) LoggerFactory(org.slf4j.LoggerFactory) DecimalFormat(java.text.DecimalFormat) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Description(org.junit.runner.Description) CtPackage(spoon.reflect.declaration.CtPackage) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) Minimizer(fr.inria.stamp.minimization.Minimizer) CtImport(spoon.reflect.declaration.CtImport) CtMethod(spoon.reflect.declaration.CtMethod) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) CtType(spoon.reflect.declaration.CtType) ImportScanner(spoon.reflect.visitor.ImportScanner)

Example 12 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method isImported.

private boolean isImported(CtExecutableReference executableReference) {
    CtImport executableImport = executableReference.getFactory().createImport(executableReference);
    if (this.imports.contains(executableImport)) {
        return true;
    } else {
        if (executableReference.getDeclaringType() == null) {
            return false;
        }
        CtTypeReference staticTypeMemberReference = executableReference.getFactory().Type().createWildcardStaticTypeMemberReference(executableReference.getDeclaringType());
        CtImport staticClassImport = executableReference.getFactory().createImport(staticTypeMemberReference);
        return this.imports.contains(staticClassImport);
    }
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 13 with CtImport

use of spoon.reflect.declaration.CtImport 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)

Example 14 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class DefaultCoreFactory method createImport.

public CtImport createImport() {
    CtImport e = new CtImportImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtImportImpl(spoon.support.reflect.declaration.CtImportImpl)

Example 15 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class CtElementImpl method toString.

@Override
public String toString() {
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment());
    String errorMessage = "";
    try {
        // we do not want to compute imports of a CtImport as it may change the print of a reference
        if (!(this instanceof CtImport)) {
            printer.computeImports(this);
        }
        printer.scan(this);
    } catch (ParentNotInitializedException ignore) {
        LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore);
        errorMessage = ERROR_MESSAGE_TO_STRING;
    }
    // removing them from the toString() representation
    return printer.toString().replaceFirst("^\\s+", "") + errorMessage;
}
Also used : CtImport(spoon.reflect.declaration.CtImport) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter)

Aggregations

CtImport (spoon.reflect.declaration.CtImport)22 Test (org.junit.Test)15 Launcher (spoon.Launcher)9 ImportScanner (spoon.reflect.visitor.ImportScanner)8 Factory (spoon.reflect.factory.Factory)7 ImportScannerImpl (spoon.reflect.visitor.ImportScannerImpl)6 CompilationUnit (spoon.reflect.cu.CompilationUnit)5 CtClass (spoon.reflect.declaration.CtClass)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 CtType (spoon.reflect.declaration.CtType)4 MinimalImportScanner (spoon.reflect.visitor.MinimalImportScanner)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)3 CtPackageReference (spoon.reflect.reference.CtPackageReference)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CtMethod (spoon.reflect.declaration.CtMethod)2 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)2 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)1 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)1 Minimizer (fr.inria.stamp.minimization.Minimizer)1