Search in sources :

Example 26 with CtParameter

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

the class ImportTest method testAnotherMissingImport.

@Test
public void testAnotherMissingImport() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setLevel("OFF");
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/AnotherMissingImport.java"));
    compiler.build();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "doSomething"));
    List<CtParameter> parameters = methods.get(0).getParameters();
    CtTypeReference<?> type = parameters.get(0).getType();
    assertEquals("SomeType", type.getSimpleName());
    assertEquals("externallib", type.getPackage().getSimpleName());
    CtMethod<?> mainMethod = factory.Class().getAll().get(0).getMethodsByName("main").get(0);
    List<CtStatement> statements = mainMethod.getBody().getStatements();
    CtStatement invocationStatement = statements.get(1);
    assertTrue(invocationStatement instanceof CtInvocation);
    CtInvocation invocation = (CtInvocation) invocationStatement;
    CtExecutableReference executableReference = invocation.getExecutable();
    assertEquals("doSomething(externallib.SomeType)", executableReference.getSignature());
    assertSame(methods.get(0), executableReference.getDeclaration());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtInvocation(spoon.reflect.code.CtInvocation) CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 27 with CtParameter

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

the class LambdaTest method testLambdaExpressionInIfConditional.

@Test
public void testLambdaExpressionInIfConditional() throws Exception {
    final CtLambda<?> lambda = getLambdaInFooByNumber(7);
    assertTypedBy(Predicate.class, lambda.getType());
    assertParametersSizeIs(1, lambda.getParameters());
    final CtParameter<?> parameter = (CtParameter<?>) lambda.getParameters().get(0);
    assertParameterTypedBy(Foo.Person.class, parameter);
    assertParameterIsNamedBy("p", parameter);
    assertHasExpressionBody(lambda);
    final CtMethod<?> method = foo.getElements(new NamedElementFilter<>(CtMethod.class, "m8")).get(0);
    final CtIf condition = method.getElements(new AbstractFilter<CtIf>(CtIf.class) {

        @Override
        public boolean matches(CtIf element) {
            return true;
        }
    }).get(0);
    final String expected = "if (((java.util.function.Predicate<spoon.test.lambda.testclasses.Foo.Person>) (( p) -> (p.age) > 18)).test(new spoon.test.lambda.testclasses.Foo.Person(10))) {" + System.lineSeparator() + "    java.lang.System.err.println(\"Enjoy, you have more than 18.\");" + System.lineSeparator() + "}";
    assertEquals("Condition must be well printed", expected, condition.toString());
}
Also used : AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Foo(spoon.test.lambda.testclasses.Foo) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtParameter(spoon.reflect.declaration.CtParameter) CtIf(spoon.reflect.code.CtIf) Test(org.junit.Test)

Example 28 with CtParameter

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

the class ParameterTest method testMultiParameterLambdaTypeReference.

@Test
@SuppressWarnings("unchecked")
public void testMultiParameterLambdaTypeReference() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/lambdas/MultiParameterLambda.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    List<CtParameter> parameters;
    // test string parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "stringLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        assertEquals(launcher.getFactory().Type().STRING, refType);
    }
    // test integer parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "integerLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        assertEquals(launcher.getFactory().Type().INTEGER, refType);
    }
    // test unknown parameters
    parameters = launcher.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "unknownLambda")).get(0).getElements(new TypeFilter<>(CtParameter.class));
    assertEquals(2, parameters.size());
    for (final CtParameter param : parameters) {
        CtTypeReference refType = (CtTypeReference) param.getReference().getType();
        // unknown parameters have no type
        assertEquals(null, refType);
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 29 with CtParameter

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

the class MethodReferenceTest method testGetGenericExecutableReference.

@Test
public void testGetGenericExecutableReference() throws Exception {
    CtType<?> classCloud = ModelUtils.buildClass(Cloud.class);
    List<CtMethod<?>> methods = classCloud.getMethodsByName("method");
    assertThat(methods.size(), is(3));
    int n = 0;
    for (CtMethod<?> method1 : classCloud.getMethodsByName("method")) {
        CtExecutableReference<?> execRef = method1.getReference();
        Method method = execRef.getActualMethod();
        assertNotNull(method);
        assertEquals("method", method.getName());
        List<CtParameter<?>> parameters = method1.getParameters();
        assertThat(parameters.size(), is(2));
        // check that we have found the method with correct parameters
        for (int i = 0; i < parameters.size(); i++) {
            CtTypeReference<?> paramTypeRef = parameters.get(i).getType();
            Class<?> paramClass = paramTypeRef.getTypeErasure().getActualClass();
            assertSame(paramClass, method.getParameterTypes()[i]);
            // 
            CtType<?> paramType = paramTypeRef.getDeclaration();
            // contract: declaration of parameter type can be found
            assertNotNull(paramType);
            // contract: reference to found parameter type is equal to origin reference
            CtTypeReference otherParamTypeRef = paramType.getReference();
            assertEquals(paramTypeRef, otherParamTypeRef);
            // contract: reference to type can be still dereferred
            assertSame(paramType, paramType.getReference().getDeclaration());
            n++;
        }
        assertSame(method1, execRef.getDeclaration());
    }
    assertThat(n, is(2 * 3));
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) CtParameter(spoon.reflect.declaration.CtParameter) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 30 with CtParameter

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

the class MethodReferenceTest method testReferenceBuilderWithComplexGenerics.

@Test
public void testReferenceBuilderWithComplexGenerics() throws Exception {
    CtType<?> classCloud = ModelUtils.buildClass(AssertJ.class);
    List<CtMethod<?>> methods = classCloud.getMethodsByName("assertThat");
    assertThat(methods.size(), is(1));
    CtMethod method1 = methods.get(0);
    CtExecutableReference<?> execRef = method1.getReference();
    Method method = execRef.getActualMethod();
    assertNotNull(method);
    assertEquals("assertThat", method.getName());
    List<CtParameter<?>> parameters = method1.getParameters();
    assertThat(parameters.size(), is(1));
    // check that we have found the method with correct parameters
    CtTypeReference<?> paramTypeRef = parameters.get(0).getType();
    Class<?> paramClass = paramTypeRef.getTypeErasure().getActualClass();
    assertSame(paramClass, method.getParameterTypes()[0]);
    assertSame(method1, execRef.getDeclaration());
}
Also used : CtParameter(spoon.reflect.declaration.CtParameter) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtParameter (spoon.reflect.declaration.CtParameter)32 Test (org.junit.Test)18 CtMethod (spoon.reflect.declaration.CtMethod)13 Factory (spoon.reflect.factory.Factory)12 CtTypeReference (spoon.reflect.reference.CtTypeReference)10 ArrayList (java.util.ArrayList)8 Launcher (spoon.Launcher)8 CtElement (spoon.reflect.declaration.CtElement)7 List (java.util.List)6 CtIf (spoon.reflect.code.CtIf)6 CtLocalVariable (spoon.reflect.code.CtLocalVariable)6 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)6 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtField (spoon.reflect.declaration.CtField)5 CtInvocation (spoon.reflect.code.CtInvocation)4 CtClass (spoon.reflect.declaration.CtClass)4 CtType (spoon.reflect.declaration.CtType)4 SpoonException (spoon.SpoonException)3 CtComment (spoon.reflect.code.CtComment)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3