use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class ParentTest method testParentOfCtPackageReference.
@Test
public void testParentOfCtPackageReference() throws Exception {
// contract: a parent at a top level must be the root package and in the code, the element which call getParent().
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/reference-package");
launcher.run();
final CtType<Object> panini = launcher.getFactory().Type().get("Panini");
CtElement topLevelParent = panini.getPackage().getParent();
assertNotNull(topLevelParent);
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, panini.getPackage().getSimpleName());
CtPackage pack1 = factory.Package().getRootPackage();
// the factory are not the same
assertNotEquals(factory, launcher.getFactory());
// so the root packages are not deeply equals
assertNotEquals(pack1, topLevelParent);
final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference<?> reference) {
return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
}
}).get(0);
assertNotNull(burritos.getPackage().getParent());
assertEquals("com.awesome", burritos.getPackage().getSimpleName());
assertEquals(burritos, burritos.getPackage().getParent());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class SetParentTest method testContract.
@Test
public void testContract() throws Throwable {
Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
CtMethod<?> setter = factory.Type().get(CtElement.class).getMethodsByName("setParent").get(0);
Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
if (!(argument instanceof CtElement)) {
// is a primitive type or a list
throw new AssertionError("impossible, setParent always takes an element");
}
// we create a fresh object
CtElement receiver = ((CtElement) o).clone();
if ("CtClass".equals(toTest.getSimpleName()) || "CtInterface".equals(toTest.getSimpleName()) || "CtEnum".equals(toTest.getSimpleName()) || "CtAnnotationType".equals(toTest.getSimpleName()) || "CtPackage".equals(toTest.getSimpleName())) {
// contract: root package is the parent for those classes
assertTrue(receiver.getParent() instanceof CtModelImpl.CtRootPackage);
} else if ("CtModule".equals(toTest.getSimpleName())) {
// contract: module parent is necessarily the unnamedmodule
assertTrue(receiver.getParent() instanceof ModuleFactory.CtUnnamedModule);
} else {
// contract: there is no parent before
try {
receiver.getParent().hashCode();
fail(receiver.getParent().getClass().getSimpleName());
} catch (ParentNotInitializedException normal) {
}
}
Method actualMethod = setter.getReference().getActualMethod();
CtElement argumentClone = ((CtElement) argument).clone();
actualMethod.invoke(receiver, new Object[] { argument });
// contract: the parent has not been changed by a call to setParent on an elemnt
assertTrue(argument.equals(argumentClone));
assertFalse(argument == argumentClone);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class PathTest method testMultiPathFromString.
@Test
public void testMultiPathFromString() throws Exception {
// When role match a list but no index is provided, all of them must be returned
Collection<CtElement> results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo#body#statement").evaluateOn(factory.getModel().getRootPackage());
assertEquals(results.size(), 3);
// When role match a set but no name is provided, all of them must be returned
results = new CtPathStringBuilder().fromString("#subPackage").evaluateOn(factory.getModel().getRootPackage());
assertEquals(results.size(), 1);
// When role match a map but no key is provided, all of them must be returned
results = new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar##annotation[index=0]#value").evaluateOn(factory.getModel().getRootPackage());
assertEquals(results.size(), 1);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class LinesTest method testIdenticalPrettyPrinter.
@Test
public void testIdenticalPrettyPrinter() throws Exception {
// contract: the end line should also be preserved
// setup
String[] options = { "--output-type", "compilationunits", "--output", "target/testIdenticalPrettyPrinter", // those three options together are the closest to what the developer wrote
"--enable-comments", "--lines", "--with-imports" };
List<String> paths = new ArrayList<>();
paths.add("spoon/test/prettyprinter/testclasses/A.java");
paths.add("spoon/test/prettyprinter/testclasses/AClass.java");
// paths.add("spoon/test/prettyprinter/testclasses/QualifiedThisRef.java");
// paths.add("spoon/test/prettyprinter/testclasses/ImportStatic.java");
// paths.add("spoon/test/prettyprinter/testclasses/QualifiedThisRef.java");
// paths.add("spoon/test/prettyprinter/testclasses/Rule.java");
// paths.add("spoon/test/prettyprinter/testclasses/TypeIdentifierCollision.java");
final Launcher launcher = new Launcher();
launcher.setArgs(options);
for (String path : paths) {
launcher.addInputResource("./src/test/java/" + path);
}
launcher.run();
final Launcher launcher2 = new Launcher();
launcher2.setArgs(options);
for (String path : paths) {
launcher2.addInputResource("./target/testIdenticalPrettyPrinter/" + path);
}
launcher2.run();
int n = 0;
List<CtElement> elements = launcher.getModel().getElements(new TypeFilter<>(CtElement.class));
for (int i = 0; i < elements.size(); i++) {
n++;
CtElement e = elements.get(i);
CtElement el2 = launcher2.getModel().getElements(new TypeFilter<>(CtElement.class)).get(i);
assertNotSame(e, el2);
assertEquals(e.toString() + " not handled", e.getPosition().getLine(), el2.getPosition().getLine());
assertEquals(e.toString() + " not handled", e.getPosition().getEndLine(), el2.getPosition().getEndLine());
}
assertTrue(n > 20);
}
use of spoon.reflect.declaration.CtElement in project dspot by STAMP-project.
the class AssertionRemover method removeAssertion.
/**
* Replaces an invocation with its arguments.
*
* @param invocation Invocation
*/
public void removeAssertion(CtInvocation<?> invocation) {
final Factory factory = invocation.getFactory();
invocation.getArguments().forEach(argument -> {
CtExpression clone = ((CtExpression) argument).clone();
if (clone instanceof CtUnaryOperator) {
clone = ((CtUnaryOperator) clone).getOperand();
}
if (clone instanceof CtStatement) {
clone.getTypeCasts().clear();
invocation.insertBefore((CtStatement) clone);
} else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) {
CtTypeReference<?> typeOfParameter = clone.getType();
if (clone.getType().equals(factory.Type().NULL_TYPE)) {
typeOfParameter = factory.Type().createReference(Object.class);
}
final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, typeOfParameter.getSimpleName() + "_" + counter[0]++, clone);
invocation.insertBefore(localVariable);
}
});
CtElement currentParent = invocation;
while (!(currentParent.getParent() instanceof CtStatementList)) {
currentParent = currentParent.getParent();
}
((CtStatementList) currentParent.getParent()).removeStatement((CtStatement) currentParent);
}
Aggregations