Search in sources :

Example 21 with CtType

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

the class DefaultJavaPrettyPrinter method visitCtInvocation.

@Override
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
    enterCtStatement(invocation);
    enterCtExpression(invocation);
    if (invocation.getExecutable().isConstructor()) {
        // It's a constructor (super or this)
        elementPrinterHelper.writeActualTypeArguments(invocation.getExecutable());
        CtType<?> parentType;
        try {
            parentType = invocation.getParent(CtType.class);
        } catch (ParentNotInitializedException e) {
            parentType = null;
        }
        if (parentType != null && parentType.getQualifiedName() != null && parentType.getQualifiedName().equals(invocation.getExecutable().getDeclaringType().getQualifiedName())) {
            printer.writeKeyword("this");
        } else {
            if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
                scan(invocation.getTarget());
                printer.writeSeparator(".");
            }
            printer.writeKeyword("super");
        }
    } else {
        // It's a method invocation
        boolean isImported = this.isImported(invocation.getExecutable());
        if (!isImported) {
            try (Writable _context = context.modify()) {
                if (invocation.getTarget() instanceof CtTypeAccess) {
                    _context.ignoreGenerics(true);
                }
                if (invocation.getTarget() != null && !invocation.getTarget().isImplicit()) {
                    scan(invocation.getTarget());
                    printer.writeSeparator(".");
                }
            }
        }
        elementPrinterHelper.writeActualTypeArguments(invocation);
        if (env.isPreserveLineNumbers()) {
            getPrinterHelper().adjustStartPosition(invocation);
        }
        printer.writeIdentifier(invocation.getExecutable().getSimpleName());
    }
    try (ListPrinter lp = elementPrinterHelper.createListPrinter(false, "(", false, false, ",", true, false, ")")) {
        for (CtExpression<?> e : invocation.getArguments()) {
            lp.printSeparatorIfAppropriate();
            scan(e);
        }
    }
    exitCtExpression(invocation);
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtType(spoon.reflect.declaration.CtType) Writable(spoon.reflect.visitor.PrintingContext.Writable) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 22 with CtType

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

the class ImportScannerImpl method computeImports.

@Override
public void computeImports(CtElement element) {
    // look for top declaring type of that simpleType
    if (element instanceof CtType) {
        CtType simpleType = (CtType) element;
        targetType = simpleType.getReference().getTopLevelType();
        addClassImport(simpleType.getReference());
        scan(simpleType);
    } else {
        CtType<?> type = element.getParent(CtType.class);
        targetType = type == null ? null : type.getReference().getTopLevelType();
        scan(element);
    }
}
Also used : CtType(spoon.reflect.declaration.CtType)

Example 23 with CtType

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

the class ImportScannerImpl method visitCtClass.

@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
    addClassImport(ctClass.getReference());
    for (CtTypeMember t : ctClass.getTypeMembers()) {
        if (!(t instanceof CtType)) {
            continue;
        }
        addClassImport(((CtType) t).getReference());
    }
    super.visitCtClass(ctClass);
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType)

Example 24 with CtType

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

the class PotentialVariableDeclarationFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    isTypeOnTheWay = false;
    isInStaticScope = false;
    // Search previous siblings for element which may represents the declaration of this local variable
    CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
    if (variableName != null) {
        // variable name is defined so we have to search only for variables with that name
        siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
    }
    CtElement scopeElement = input;
    // Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
    while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
        CtElement parent = scopeElement.getParent();
        if (parent instanceof CtType<?>) {
            isTypeOnTheWay = true;
            // visit each CtField of `parent` CtType
            CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
            q.forEach((CtField<?> field) -> {
                if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
                    /*
						 * the variable reference is used in static scope,
						 * but the field is not static - ignore it
						 */
                    return;
                }
                // else send field as potential variable declaration
                if (sendToOutput(field, outputConsumer)) {
                    // and terminate the internal query q if outer query is already terminated
                    q.terminate();
                }
            });
            if (query.isTerminated()) {
                return;
            }
        } else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
            // visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
            siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
            if (query.isTerminated()) {
                return;
            }
            // visit parameters of CtCatch and CtExecutable (method, lambda)
            if (parent instanceof CtCatch) {
                CtCatch ctCatch = (CtCatch) parent;
                if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
                    return;
                }
            } else if (parent instanceof CtExecutable) {
                CtExecutable<?> exec = (CtExecutable<?>) parent;
                for (CtParameter<?> param : exec.getParameters()) {
                    if (sendToOutput(param, outputConsumer)) {
                        return;
                    }
                }
            }
        }
        if (parent instanceof CtModifiable) {
            isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
        }
        scopeElement = parent;
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) CtExecutable(spoon.reflect.declaration.CtExecutable) CtBodyHolder(spoon.reflect.code.CtBodyHolder) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtVariable(spoon.reflect.declaration.CtVariable) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) CtCatch(spoon.reflect.code.CtCatch) CtModifiable(spoon.reflect.declaration.CtModifiable)

Example 25 with CtType

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

the class AllTypeMembersFunction method apply.

@Override
public void apply(CtTypeInformation input, final CtConsumer<Object> outputConsumer) {
    final CtQuery q = ((CtQueryable) input).map(new SuperInheritanceHierarchyFunction(distinctSet == null ? new HashSet<String>() : distinctSet).includingSelf(true));
    q.forEach(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> type) {
            for (CtTypeMember typeMember : type.getTypeMembers()) {
                if (memberClass == null || memberClass.isInstance(typeMember)) {
                    outputConsumer.accept(typeMember);
                }
                if (query.isTerminated()) {
                    q.terminate();
                }
            }
        }
    });
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtQueryable(spoon.reflect.visitor.chain.CtQueryable) CtQuery(spoon.reflect.visitor.chain.CtQuery) HashSet(java.util.HashSet)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11