use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class CtClassTest method testSpoonShouldInferImplicitPackageInNoClasspath.
@Test
public void testSpoonShouldInferImplicitPackageInNoClasspath() throws Exception {
// contract: in noClasspath, when a type is used and no import is specified, then Spoon
// should infer that this type is in the same package as the current class.
final Launcher launcher2 = new Launcher();
launcher2.addInputResource("./src/test/resources/noclasspath/issue1293/com/cristal/ircica/applicationcolis/userinterface/fragments/TransporteurFragment.java");
launcher2.getEnvironment().setNoClasspath(true);
launcher2.buildModel();
final CtClass<Object> aClass2 = launcher2.getFactory().Class().get("com.cristal.ircica.applicationcolis.userinterface.fragments.TransporteurFragment");
final String type2 = aClass2.getSuperclass().getQualifiedName();
CtField field = aClass2.getField("transporteurRadioGroup");
assertThat(field.getType().getQualifiedName(), is("android.widget.RadioGroup"));
assertThat(type2, is("com.cristal.ircica.applicationcolis.userinterface.fragments.CompletableFragment"));
}
use of spoon.reflect.declaration.CtField 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.declaration.CtField in project spoon by INRIA.
the class ReplaceTest method testReplaceField.
@Test
public void testReplaceField() {
CtClass<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
Assert.assertEquals(factory.Type().createReference(int.class), sample.getField("i").getType());
// replace with another type
CtField replacement = factory.Core().createField();
replacement.setSimpleName("i");
replacement.setType(factory.Type().createReference(double.class));
sample.getField("i").replace(replacement);
Assert.assertEquals(factory.Type().createReference(double.class), sample.getField("i").getType());
// replace with another name
replacement = factory.Core().createField();
replacement.setSimpleName("j");
replacement.setType(factory.Type().createReference(double.class));
sample.getField("i").replace(replacement);
Assert.assertNull(sample.getField("i"));
Assert.assertNotNull(sample.getField("j"));
Assert.assertEquals(factory.Type().createReference(double.class), sample.getField("j").getType());
}
use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class VariableReferencesTest method testVariableScopeFunction.
@Test
public void testVariableScopeFunction() throws Exception {
// visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
// Comparing with the result found by basic functions
List list = modelClass.filterChildren((CtVariable<?> var) -> {
if (var.getSimpleName().equals("field")) {
if (var instanceof CtField) {
// field scope is not supported
return false;
}
CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
if (var instanceof CtLocalVariable) {
assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtField) {
// assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtParameter) {
assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtCatchVariable) {
assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else {
fail("Unexpected variable of type " + var.getClass().getName());
}
return true;
}
return false;
}).list();
assertTrue(list.size() > 0);
}
use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class MetaModelTest method listValueRoleSetOn.
@Test
public void listValueRoleSetOn() {
// contract: multi-value role supports set(int, Object)
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtClass<?> ctClass = factory.Class().create("some.test.TestClass");
RoleHandler rh = RoleHandlerHelper.getRoleHandler(ctClass.getClass(), CtRole.TYPE_MEMBER);
List<CtTypeMember> typeMembers = rh.asList(ctClass);
assertEquals(0, typeMembers.size());
CtField<?> field1 = createField(factory, "field1");
CtField<?> field2 = createField(factory, "field2");
CtField<?> field3 = createField(factory, "field3");
// check that field was not added in type yet
assertEquals(0, typeMembers.size());
// contract: call of add on RoleHandler collection adds the item into real collection too
typeMembers.add(field1);
assertEquals(1, typeMembers.size());
assertEquals(1, ctClass.getTypeMembers().size());
assertSame(ctClass, field1.getDeclaringType());
assertThat(asList("field1"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
// contract: call of add on RoleHandler collection adds the item into real collection too
typeMembers.add(field2);
assertSame(ctClass, field2.getDeclaringType());
assertThat(asList("field1", "field2"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
// contract: call of set on RoleHandler collection replaces the item in real collection
typeMembers.set(0, field3);
assertSame(ctClass, field3.getDeclaringType());
assertThat(asList("field3", "field2"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
typeMembers.set(1, field1);
assertThat(asList("field3", "field1"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
// contract: call of remove(int) on RoleHandler collection removes the item in real collection
assertSame(field3, typeMembers.remove(0));
assertThat(asList("field1"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
// contract: call of remove(Object) which does not exist does nothing
assertFalse(typeMembers.remove(field2));
assertThat(asList("field1"), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
// contract: call of remove(Object) on RoleHandler collection removes the item in real collection
assertTrue(typeMembers.remove(field1));
assertThat(asList(), is(ctClass.filterChildren(new TypeFilter(CtField.class)).map((CtField e) -> e.getSimpleName()).list()));
}
Aggregations