Search in sources :

Example 46 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class TestModule method testMultipleModulesAndParents.

@Ignore
@Test
public void testMultipleModulesAndParents() {
    // contract: Spoon is able to build a model with multiple modules
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setComplianceLevel(9);
    // launcher.addModulePath("./src/test/resources/spoon/test/module/code-multiple-modules/foo");
    // launcher.addModulePath("./src/test/resources/spoon/test/module/code-multiple-modules/bar");
    launcher.addInputResource(MODULE_RESOURCES_PATH + "/code-multiple-modules");
    launcher.run();
    assertEquals(3, launcher.getModel().getAllModules().size());
    CtType barclass = launcher.getFactory().Type().get("packbar.BarClass");
    assertNotNull(barclass);
    assertTrue(barclass.getParent() instanceof CtPackage);
    CtPackage packBar = (CtPackage) barclass.getParent();
    assertTrue(packBar.getParent() instanceof CtModule);
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtModule(spoon.reflect.declaration.CtModule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 47 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class ParentContractTest method createCompatibleObject.

public static Object createCompatibleObject(CtTypeReference<?> parameterType) {
    Class<?> c = parameterType.getActualClass();
    for (CtType t : allInstantiableMetamodelInterfaces) {
        if (c.isAssignableFrom(t.getActualClass())) {
            CtElement argument = factory.Core().create(t.getActualClass());
            // we have to give it a name
            if (argument instanceof CtPackage) {
                ((CtPackage) argument).setSimpleName(argument.getShortRepresentation());
            }
            return argument;
        }
    }
    if (Set.class.isAssignableFrom(c)) {
        // we create one set with one element
        HashSet<Object> objects = new HashSet<>();
        objects.add(createCompatibleObject(parameterType.getActualTypeArguments().get(0)));
        return objects;
    }
    if (Collection.class.isAssignableFrom(c)) {
        // we create one list with one element
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(createCompatibleObject(parameterType.getActualTypeArguments().get(0)));
        return objects;
    }
    throw new IllegalArgumentException("cannot instantiate " + parameterType);
}
Also used : CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) CtPackage(spoon.reflect.declaration.CtPackage) HashSet(java.util.HashSet)

Example 48 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class ParentTest method testGetParentWithFilter.

@Test
public void testGetParentWithFilter() throws Exception {
    // addType should set Parent
    CtClass<Foo> clazz = (CtClass<Foo>) factory.Class().getAll().get(0);
    CtMethod<Object> m = clazz.getMethod("m");
    // get three = "" in one = two = three = "";
    CtExpression statement = ((CtAssignment) ((CtAssignment) m.getBody().getStatement(3)).getAssignment()).getAssignment();
    CtPackage ctPackage = statement.getParent(new TypeFilter<CtPackage>(CtPackage.class));
    assertEquals(Foo.class.getPackage().getName(), ctPackage.getQualifiedName());
    CtStatement ctStatement = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element.getParent() instanceof CtStatementList && super.matches(element);
        }
    });
    // the filter has to return one = two = three = ""
    assertEquals(m.getBody().getStatement(3), ctStatement);
    m = clazz.getMethod("internalClass");
    CtStatement ctStatement1 = m.getElements(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return element instanceof CtLocalVariable && super.matches(element);
        }
    }).get(0);
    // get the top class
    ctStatement1.getParent(CtType.class);
    CtType parent = ctStatement1.getParent(new AbstractFilter<CtType>(CtType.class) {

        @Override
        public boolean matches(CtType element) {
            return !element.isAnonymous() && element.isTopLevel() && super.matches(element);
        }
    });
    assertEquals(clazz, parent);
    assertNotEquals(ctStatement1.getParent(CtType.class), parent);
    // not present element
    CtWhile ctWhile = ctStatement1.getParent(new TypeFilter<CtWhile>(CtWhile.class));
    assertEquals(null, ctWhile);
    CtStatement statementParent = statement.getParent(new AbstractFilter<CtStatement>(CtStatement.class) {

        @Override
        public boolean matches(CtStatement element) {
            return true;
        }
    });
    // getParent must not return the current element
    assertNotEquals(statement, statementParent);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtExpression(spoon.reflect.code.CtExpression) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtWhile(spoon.reflect.code.CtWhile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtPackage(spoon.reflect.declaration.CtPackage) CtStatementList(spoon.reflect.code.CtStatementList) Test(org.junit.Test)

Example 49 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class ParentTest method testParentPackage.

@Test
public void testParentPackage() throws Exception {
    // addType should set Parent
    CtClass<?> clazz = factory.Core().createClass();
    clazz.setSimpleName("Foo");
    CtPackage pack = factory.Core().createPackage();
    pack.setSimpleName("bar");
    pack.addType(clazz);
    assertTrue(pack.getTypes().contains(clazz));
    assertEquals(pack, clazz.getParent());
}
Also used : CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 50 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageTest method testRenameRootPackage.

@Test
public void testRenameRootPackage() throws Exception {
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/noclasspath/app/Test.java");
    spoon.getEnvironment().setNoClasspath(true);
    spoon.buildModel();
    CtPackage rootPackage = spoon.getFactory().Package().getRootPackage();
    String rootPackageName = rootPackage.getSimpleName();
    rootPackage.setSimpleName("test");
    assertEquals(rootPackageName, rootPackage.getSimpleName());
}
Also used : Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2