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());
}
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());
}
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);
}
}
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));
}
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());
}
Aggregations