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);
}
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);
}
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);
}
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());
}
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());
}
Aggregations