use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class TypeReferenceTest method testRecursiveTypeReference.
@Test
public void testRecursiveTypeReference() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/Tacos.java");
launcher.setSourceOutputDirectory("./target/spoon-test");
launcher.run();
final CtInvocation<?> inv = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return !element.getExecutable().isConstructor() && super.matches(element);
}
}).get(0);
assertNotNull(inv.getExecutable());
final CtTypeReference<?> returnType = inv.getExecutable().getType();
assertNotNull(returnType);
assertEquals(1, returnType.getActualTypeArguments().size());
final CtTypeParameterReference genericType = (CtTypeParameterReference) returnType.getActualTypeArguments().get(0);
assertNotNull(genericType);
assertNotNull(genericType.getBoundingType());
CtTypeReference<?> extendsGeneric = genericType.getBoundingType();
assertNotNull(extendsGeneric);
assertEquals(1, extendsGeneric.getActualTypeArguments().size());
CtTypeParameterReference genericExtends = (CtTypeParameterReference) extendsGeneric.getActualTypeArguments().get(0);
assertNotNull(genericExtends);
assertNotNull(genericExtends.getBoundingType());
assertTrue(genericExtends.getBoundingType() instanceof CtTypeReference);
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class TypeReferenceTest method testTypeReferenceWithGenerics.
@Test
public void testTypeReferenceWithGenerics() throws Exception {
// contract: in noclasspath, a generic type name should not contain generic information
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();
CtField field = launcher.getModel().getElements(new TypeFilter<CtField>(CtField.class)).get(0);
CtTypeReference fieldTypeRef = field.getType();
assertEquals("spoon.test.imports.testclasses.withgenerics.Target", fieldTypeRef.getQualifiedName());
assertEquals(2, fieldTypeRef.getActualTypeArguments().size());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class VariableAccessTest method testDeclarationArray.
@Test
public void testDeclarationArray() throws Exception {
final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);
final CtMethod<Object> m2 = aPozole.getMethod("m2");
final CtArrayWrite<?> ctArrayWrite = m2.getElements(new TypeFilter<CtArrayWrite<?>>(CtArrayWrite.class)).get(0);
final CtLocalVariable expected = m2.getElements(new TypeFilter<CtLocalVariable>(CtLocalVariable.class)).get(0);
assertEquals(expected, ((CtVariableAccess) ctArrayWrite.getTarget()).getVariable().getDeclaration());
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class VariableAccessTest method testDeclaringTypeOfALambdaReferencedByParameterReference.
@Test
public void testDeclaringTypeOfALambdaReferencedByParameterReference() {
final spoon.Launcher launcher = new spoon.Launcher();
launcher.addInputResource("src/test/resources/noclasspath/Foo3.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setComplianceLevel(8);
launcher.buildModel();
launcher.getModel().getElements(new TypeFilter<CtExecutable<?>>(CtExecutable.class) {
@Override
public boolean matches(CtExecutable<?> exec) {
final List<CtParameterReference<?>> guiParams = exec.getParameters().stream().map(CtParameter::getReference).collect(Collectors.toList());
if (guiParams.size() != 1) {
return false;
}
final CtParameterReference<?> param = guiParams.get(0);
exec.getBody().getElements(new TypeFilter<CtParameterReference<?>>(CtParameterReference.class) {
@Override
public boolean matches(CtParameterReference<?> p) {
assertEquals(p.getSimpleName(), param.getSimpleName());
return super.matches(p);
}
});
return super.matches(exec);
}
});
}
use of spoon.reflect.visitor.filter.TypeFilter in project spoon by INRIA.
the class ReplaceTest method testReplaceBlockTry.
@Test
public void testReplaceBlockTry() throws Exception {
final CtType<Mole> aMole = buildClass(Mole.class);
final CtBlock<?> newBlock = aMole.getFactory().Code().createCtBlock(aMole.getFactory().Code().createCodeSnippetStatement("int j = 0;").compile());
final CtTry ctTry = aMole.getMethod("m").getElements(new TypeFilter<>(CtTry.class)).get(0);
assertNotEquals(newBlock, ctTry.getBody());
ctTry.getBody().replace(newBlock);
assertEquals(newBlock, ctTry.getBody());
}
Aggregations