use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class CtTypeParameterTest method checkTypeParamErasureOfExecutable.
private void checkTypeParamErasureOfExecutable(CtTypeParameter typeParam) throws NoSuchFieldException, SecurityException {
CtExecutable<?> exec = (CtExecutable<?>) typeParam.getParent();
CtParameter<?> param = exec.filterChildren(new NamedElementFilter<>(CtParameter.class, "param" + typeParam.getSimpleName())).first();
assertNotNull("Missing param" + typeParam.getSimpleName() + " in " + exec.getSignature(), param);
int paramIdx = exec.getParameters().indexOf(param);
Class declClass = exec.getParent(CtType.class).getActualClass();
Executable declExec;
if (exec instanceof CtConstructor) {
declExec = declClass.getDeclaredConstructors()[0];
} else {
declExec = getMethodByName(declClass, exec.getSimpleName());
}
Class<?> paramType = declExec.getParameterTypes()[paramIdx];
// contract the type erasure given with Java reflection is the same as the one computed by spoon
assertEquals("TypeErasure of executable param " + getTypeParamIdentification(typeParam), paramType.getName(), typeParam.getTypeErasure().toString());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class CtTypeTest method testIsSubTypeOfonTypeParameters.
@Test
public void testIsSubTypeOfonTypeParameters() throws Exception {
CtType<X> xCtType = buildClass(X.class);
Factory factory = xCtType.getFactory();
CtType<?> oCtType = factory.Type().get("spoon.test.ctType.testclasses.O");
CtType<?> pCtType = factory.Type().get("spoon.test.ctType.testclasses.P");
CtTypeReference<?> objectCtTypeRef = factory.Type().OBJECT;
List<CtTypeParameter> oTypeParameters = oCtType.getFormalCtTypeParameters();
assertTrue(oTypeParameters.size() == 1);
List<CtTypeParameter> pTypeParameters = pCtType.getFormalCtTypeParameters();
assertTrue(pTypeParameters.size() == 2);
CtType<?> O_A_CtType = oTypeParameters.get(0);
CtType<?> P_D_CtType = pTypeParameters.get(0);
CtType<?> P_F_CtType = pTypeParameters.get(1);
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
CtMethod<?> P_FooMethod = pCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
CtType<?> O_B_CtType = O_FooMethod.getType().getDeclaration();
CtType<?> P_E_CtType = P_FooMethod.getType().getDeclaration();
assertTrue(O_B_CtType.isSubtypeOf(xCtType.getReference()));
assertTrue(O_B_CtType.isSubtypeOf(O_A_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(xCtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(P_D_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(O_A_CtType.getReference()));
assertTrue(P_D_CtType.isSubtypeOf(O_A_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(O_B_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(objectCtTypeRef));
assertTrue(P_F_CtType.isSubtypeOf(objectCtTypeRef));
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class FactoryTest method testCtModel.
@Test
public void testCtModel() throws Exception {
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
spoon.buildModel();
CtModel model = spoon.getModel();
// contains Foo and Foo.@Bar
assertEquals(1, model.getAllTypes().size());
// [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
assertEquals(5, model.getAllPackages().size());
// add to itself is fine
model.getRootPackage().addPackage(model.getRootPackage());
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().getPackage("spoon").addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
CtPackage p = model.getElements(new NamedElementFilter<>(CtPackage.class, "spoon")).get(0).clone();
// if we change the implem, merge is impossible
CtField f = spoon.getFactory().Core().createField();
f.setSimpleName("foo");
f.setType(spoon.getFactory().Type().BYTE);
p.getElements(new NamedElementFilter<>(CtPackage.class, "testclasses")).get(0).getType("Foo").addField(f);
try {
model.getRootPackage().addPackage(p);
fail("no exception thrown");
} catch (IllegalStateException success) {
}
model.processWith(new AbstractProcessor<CtType>() {
@Override
public void process(CtType element) {
element.delete();
}
});
assertEquals(0, model.getAllTypes().size());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class ReplaceTest method testReplaceReplace.
@Test
public void testReplaceReplace() throws Exception {
// bug found by Benoit
CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
CtMethod<?> fooMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
assertEquals("foo", fooMethod.getSimpleName());
CtMethod<?> barMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "bar")).get(0);
assertEquals("bar", barMethod.getSimpleName());
CtLocalVariable<?> assignment = (CtLocalVariable<?>) fooMethod.getBody().getStatements().get(0);
CtLocalVariable<?> newAssignment = barMethod.getBody().getStatement(0);
assignment.replace(newAssignment);
assertEquals(fooMethod.getBody(), newAssignment.getParent());
CtLiteral<Integer> lit = (CtLiteral<Integer>) foo.getElements(new TypeFilter<CtLiteral<?>>(CtLiteral.class)).get(0);
final CtElement parent = lit.getParent();
CtLiteral<Integer> newLit = factory.Code().createLiteral(0);
lit.replace(newLit);
assertEquals("int y = 0", fooMethod.getBody().getStatement(0).toString());
assertEquals(parent, newLit.getParent());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class ReplaceTest method testReplaceBlock.
@Test
public void testReplaceBlock() throws Exception {
CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
CtMethod<?> m = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
assertEquals("foo", m.getSimpleName());
final CtStatement parent = m.getBody().getStatements().get(2);
CtAssignment<?, ?> assignment = (CtAssignment<?, ?>) parent;
CtExpression<Integer> s1 = (CtExpression<Integer>) assignment.getAssignment();
CtExpression<Integer> s2 = factory.Code().createLiteral(3);
assertEquals("z = x + 1", assignment.toString());
assertEquals("x + 1", s1.toString());
// do
s1.replace(s2);
assertSame(s2, assignment.getAssignment());
assertEquals("z = 3", assignment.toString());
assertEquals(parent, s2.getParent());
// undo
s2.replace(s1);
assertSame(s1, assignment.getAssignment());
assertEquals("z = x + 1", assignment.toString());
assertEquals(parent, s1.getParent());
}
Aggregations