Search in sources :

Example 21 with CtField

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"));
}
Also used : CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 22 with CtField

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());
}
Also used : CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 23 with CtField

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());
}
Also used : CtField(spoon.reflect.declaration.CtField) Test(org.junit.Test)

Example 24 with CtField

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);
}
Also used : CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableScopeFunction(spoon.reflect.visitor.filter.VariableScopeFunction) ParameterScopeFunction(spoon.reflect.visitor.filter.ParameterScopeFunction) CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) CtField(spoon.reflect.declaration.CtField) CtElement(spoon.reflect.declaration.CtElement) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Example 25 with CtField

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()));
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) RoleHandler(spoon.reflect.meta.RoleHandler) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Aggregations

CtField (spoon.reflect.declaration.CtField)39 Test (org.junit.Test)20 Launcher (spoon.Launcher)16 CtType (spoon.reflect.declaration.CtType)16 CtMethod (spoon.reflect.declaration.CtMethod)13 CtTypeReference (spoon.reflect.reference.CtTypeReference)12 ArrayList (java.util.ArrayList)8 CtElement (spoon.reflect.declaration.CtElement)8 CtClass (spoon.reflect.declaration.CtClass)6 CtTypeMember (spoon.reflect.declaration.CtTypeMember)6 Factory (spoon.reflect.factory.Factory)6 List (java.util.List)5 SpoonException (spoon.SpoonException)5 CtExecutable (spoon.reflect.declaration.CtExecutable)5 CtPackage (spoon.reflect.declaration.CtPackage)5 CtParameter (spoon.reflect.declaration.CtParameter)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 Set (java.util.Set)3