Search in sources :

Example 6 with CtExecutable

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

the class CtParameterReferenceImpl method lookupDynamically.

private CtParameter<T> lookupDynamically() {
    CtElement element = this;
    CtParameter optional = null;
    String name = getSimpleName();
    try {
        do {
            CtExecutable executable = element.getParent(CtExecutable.class);
            if (executable == null) {
                return null;
            }
            for (CtParameter parameter : (List<CtParameter>) executable.getParameters()) {
                if (name.equals(parameter.getSimpleName())) {
                    optional = parameter;
                }
            }
            element = executable;
        } while (optional == null);
    } catch (ParentNotInitializedException e) {
        return null;
    }
    return optional;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtParameter(spoon.reflect.declaration.CtParameter) List(java.util.List) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 7 with CtExecutable

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

the class InvocationTest method testIssue1753.

@Test
public void testIssue1753() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch1753");
    final CtModel model = launcher.buildModel();
    final List<CtExecutable> executables = model.getElements(new TypeFilter<>(CtExecutable.class)).stream().filter(i -> i.getPosition().getLine() == 190).collect(Collectors.toList());
    assertEquals(1, executables.size());
    final CtExecutable exe = executables.get(0);
    assertNotNull(exe.getReference().getDeclaration());
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ModelUtils.build(spoon.testing.utils.ModelUtils.build) Launcher(spoon.Launcher) CtInvocation(spoon.reflect.code.CtInvocation) Bar(spoon.test.invocations.testclasses.Bar) Assert.assertNotNull(org.junit.Assert.assertNotNull) Foo(spoon.test.invocations.testclasses.Foo) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SpoonAPI(spoon.SpoonAPI) CtTypeAccess(spoon.reflect.code.CtTypeAccess) CtModel(spoon.reflect.CtModel) CtClass(spoon.reflect.declaration.CtClass) Assert.fail(org.junit.Assert.fail) CtExecutable(spoon.reflect.declaration.CtExecutable) Assert.assertEquals(org.junit.Assert.assertEquals) CtMethod(spoon.reflect.declaration.CtMethod) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtModel(spoon.reflect.CtModel) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 8 with CtExecutable

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

the class MethodsRefactoringTest method testAllMethodsSameSignatureFunction.

@Test
public void testAllMethodsSameSignatureFunction() {
    Factory factory = ModelUtils.build(new File("./src/test/java/spoon/test/refactoring/parameter/testclasses"));
    // each executable in test classes is marked with a annotation TestHierarchy,
    // which defines the name of the hierarchy where this executable belongs to.
    // collect all executables which are marked that they belong to hierarchy A_method1
    List<CtExecutable<?>> executablesOfHierarchyA = getExecutablesOfHierarchy(factory, "A_method1");
    // check executables of this hierarchy
    checkMethodHierarchies(executablesOfHierarchyA);
    // collect all executables which are marked that they belong to hierarchy R_method1
    List<CtExecutable<?>> executablesOfHierarchyR = getExecutablesOfHierarchy(factory, "R_method1");
    // check executables of this hierarchy
    checkMethodHierarchies(executablesOfHierarchyR);
    // contract: CtConstructor has no other same signature
    CtConstructor<?> constructorTypeA = factory.Class().get(TypeA.class).getConstructors().iterator().next();
    CtExecutable<?> exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
    assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
    CtConstructor<?> constructorTypeB = factory.Class().get(TypeB.class).getConstructors().iterator().next();
    exec = constructorTypeA.map(new AllMethodsSameSignatureFunction()).first();
    assertNull("Unexpected executable found by Constructor of TypeA " + exec, exec);
    // contract: constructor is returned if includingSelf == true
    assertSame(constructorTypeA, constructorTypeA.map(new AllMethodsSameSignatureFunction().includingSelf(true)).first());
}
Also used : TypeB(spoon.test.refactoring.parameter.testclasses.TypeB) Factory(spoon.reflect.factory.Factory) AllMethodsSameSignatureFunction(spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) TypeA(spoon.test.refactoring.parameter.testclasses.TypeA) Test(org.junit.Test)

Example 9 with CtExecutable

use of spoon.reflect.declaration.CtExecutable 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 10 with CtExecutable

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

the class Refactoring method copyMethod.

/**
 * See doc in {@link CtMethod#copyMethod()}
 */
public static CtMethod<?> copyMethod(final CtMethod<?> method) {
    CtMethod<?> clone = method.clone();
    String tentativeTypeName = method.getSimpleName() + "Copy";
    CtType parent = method.getParent(CtType.class);
    while (parent.getMethodsByName(tentativeTypeName).size() > 0) {
        tentativeTypeName += "X";
    }
    final String cloneMethodName = tentativeTypeName;
    clone.setSimpleName(cloneMethodName);
    parent.addMethod(clone);
    new CtScanner() {

        @Override
        public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) {
            CtExecutable<T> declaration = reference.getDeclaration();
            if (declaration == null) {
                return;
            }
            if (declaration == method) {
                reference.setSimpleName(cloneMethodName);
            }
            if (reference.getDeclaration() != clone) {
                throw new SpoonException("post condition broken " + reference);
            }
            super.visitCtExecutableReference(reference);
        }
    }.scan(clone);
    return clone;
}
Also used : CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) CtScanner(spoon.reflect.visitor.CtScanner) CtExecutable(spoon.reflect.declaration.CtExecutable)

Aggregations

CtExecutable (spoon.reflect.declaration.CtExecutable)26 Test (org.junit.Test)10 CtType (spoon.reflect.declaration.CtType)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)7 Launcher (spoon.Launcher)6 CtElement (spoon.reflect.declaration.CtElement)6 Factory (spoon.reflect.factory.Factory)6 File (java.io.File)5 CtConstructor (spoon.reflect.declaration.CtConstructor)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SpoonException (spoon.SpoonException)4 CtField (spoon.reflect.declaration.CtField)4 CtMethod (spoon.reflect.declaration.CtMethod)4 SpoonModelBuilder (spoon.SpoonModelBuilder)3 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLambda (spoon.reflect.code.CtLambda)3 CtClass (spoon.reflect.declaration.CtClass)3 CtParameter (spoon.reflect.declaration.CtParameter)3