Search in sources :

Example 11 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class TypeReferenceTest method testGetAllExecutablesForInterfaces.

@Test
public void testGetAllExecutablesForInterfaces() throws Exception {
    /*
		 * This test has been written because getAllExecutables wasn't recursing
		 * into the type hierarchy for interfaces.
		 */
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/reference/Foo.java")).build();
    CtInterface<Foo> foo = factory.Package().get("spoon.test.reference").getType("Foo");
    Collection<CtExecutableReference<?>> execs = foo.getReference().getAllExecutables();
    assertEquals(2, execs.size());
}
Also used : Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) Test(org.junit.Test)

Example 12 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class MethodsRefactoringTest method testExecutableReferenceFilter.

@Test
public void testExecutableReferenceFilter() {
    Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
    List<CtExecutable<?>> executables = factory.getModel().filterChildren((CtExecutable<?> e) -> true).list();
    int nrExecRefsTotal = 0;
    // contract check that ExecutableReferenceFilter found CtExecutableReferences of each executable individually
    for (CtExecutable<?> executable : executables) {
        nrExecRefsTotal += checkExecutableReferenceFilter(factory, Collections.singletonList(executable));
    }
    // contract check that ExecutableReferenceFilter found CtExecutableReferences of all executables together
    int nrExecRefsTotal2 = checkExecutableReferenceFilter(factory, executables);
    assertSame(nrExecRefsTotal, nrExecRefsTotal2);
    // contract check that it found lambdas too
    CtLambda lambda = factory.getModel().filterChildren((CtLambda<?> e) -> true).first();
    assertNotNull(lambda);
    // this test case is quite wild, because there is normally lambda reference in spoon model. So make one lambda reference here:
    CtExecutableReference<?> lambdaRef = lambda.getReference();
    List<CtExecutableReference<?>> refs = lambdaRef.filterChildren(null).select(new ExecutableReferenceFilter(lambda)).list();
    assertEquals(1, refs.size());
    assertSame(lambdaRef, refs.get(0));
}
Also used : CtLambda(spoon.reflect.code.CtLambda) Factory(spoon.reflect.factory.Factory) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter) Test(org.junit.Test)

Example 13 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class ExecutableReferenceTest method testCallMethodOfClassNotPresent.

@Test
public void testCallMethodOfClassNotPresent() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/resources/executable-reference", "--output-type", "nooutput", "--noclasspath" });
    final List<CtExecutableReference<?>> references = Query.getReferences(launcher.getFactory(), new ReferenceTypeFilter<CtExecutableReference<?>>(CtExecutableReference.class) {

        @Override
        public boolean matches(CtExecutableReference<?> reference) {
            return !reference.isConstructor() && super.matches(reference);
        }
    });
    final List<CtInvocation<?>> invocations = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    });
    assertEquals(4, references.size());
    assertEquals(4, invocations.size());
    // Executable reference with 0 parameter.
    final CtExecutableReference<?> executableZeroParameter = references.get(0);
    assertNotNull(executableZeroParameter.getDeclaringType());
    assertNull(executableZeroParameter.getType());
    assertEquals(0, executableZeroParameter.getParameters().size());
    assertEquals("m()", executableZeroParameter.toString());
    assertEquals("new Bar().m()", invocations.get(0).toString());
    // Executable reference with 1 parameter and return type.
    final CtExecutableReference<?> executableOneParameter = references.get(1);
    assertNotNull(executableOneParameter.getDeclaringType());
    assertNotNull(executableOneParameter.getType());
    assertEquals(1, executableOneParameter.getParameters().size());
    assertNotEquals(executableZeroParameter, executableOneParameter);
    assertEquals("m(int)", executableOneParameter.toString());
    assertEquals("bar.m(1)", invocations.get(1).toString());
    // Executable reference with 2 parameters.
    final CtExecutableReference<?> executableTwoParameters = references.get(2);
    assertNotNull(executableTwoParameters.getDeclaringType());
    assertNull(executableTwoParameters.getType());
    assertEquals(2, executableTwoParameters.getParameters().size());
    assertNotEquals(executableTwoParameters, executableZeroParameter);
    assertNotEquals(executableTwoParameters, executableOneParameter);
    assertEquals("m(int,java.lang.String)", executableTwoParameters.toString());
    assertEquals("new Bar().m(1, \"5\")", invocations.get(2).toString());
    // Static Executable reference.
    final CtExecutableReference<?> staticExecutable = references.get(3);
    assertNotNull(staticExecutable.getDeclaringType());
    assertNull(staticExecutable.getType());
    assertEquals(1, staticExecutable.getParameters().size());
    assertNotEquals(staticExecutable, executableZeroParameter);
    assertNotEquals(staticExecutable, executableOneParameter);
    assertEquals("m(java.lang.String)", staticExecutable.toString());
    assertEquals("Bar.m(\"42\")", invocations.get(3).toString());
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) Test(org.junit.Test)

Example 14 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference 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 15 with CtExecutableReference

use of spoon.reflect.reference.CtExecutableReference in project spoon by INRIA.

the class CtParameterRemoveRefactoring method computeAllInvocations.

/**
 * search for all methods and lambdas which has to be refactored together with target method
 */
private void computeAllInvocations() {
    ExecutableReferenceFilter execRefFilter = new ExecutableReferenceFilter();
    for (CtExecutable<?> exec : getTargetExecutables()) {
        execRefFilter.addExecutable(exec);
    }
    // all the invocations, which belongs to same inheritance tree
    final List<CtInvocation<?>> invocations = new ArrayList<>();
    target.getFactory().getModel().filterChildren(execRefFilter).forEach(new CtConsumer<CtExecutableReference<?>>() {

        @Override
        public void accept(CtExecutableReference<?> t) {
            CtElement parent = t.getParent();
            if (parent instanceof CtInvocation<?>) {
                invocations.add((CtInvocation<?>) parent);
            }
        // else ignore other hits, which are not in context of invocation
        }
    });
    targetInvocations = Collections.unmodifiableList(invocations);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ExecutableReferenceFilter(spoon.reflect.visitor.filter.ExecutableReferenceFilter)

Aggregations

CtExecutableReference (spoon.reflect.reference.CtExecutableReference)27 Test (org.junit.Test)13 CtInvocation (spoon.reflect.code.CtInvocation)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)9 Factory (spoon.reflect.factory.Factory)8 Launcher (spoon.Launcher)7 CtMethod (spoon.reflect.declaration.CtMethod)7 ArrayList (java.util.ArrayList)5 CtElement (spoon.reflect.declaration.CtElement)5 CtExecutable (spoon.reflect.declaration.CtExecutable)5 CtExpression (spoon.reflect.code.CtExpression)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 List (java.util.List)3 CtType (spoon.reflect.declaration.CtType)3 CtReference (spoon.reflect.reference.CtReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 HashSet (java.util.HashSet)2 CtFieldAccess (spoon.reflect.code.CtFieldAccess)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtStatement (spoon.reflect.code.CtStatement)2