use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class SpoonMetaModel method getAllInstantiableMetamodelInterfaces.
public List<CtType<? extends CtElement>> getAllInstantiableMetamodelInterfaces() {
SpoonAPI interfaces = new Launcher();
interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/reflect/code");
interfaces.addInputResource("src/main/java/spoon/reflect/reference");
interfaces.buildModel();
SpoonAPI implementations = new Launcher();
implementations.addInputResource("src/main/java/spoon/support/reflect/declaration");
implementations.addInputResource("src/main/java/spoon/support/reflect/code");
implementations.addInputResource("src/main/java/spoon/support/reflect/reference");
implementations.buildModel();
List<CtType<? extends CtElement>> result = new ArrayList<>();
for (CtType<?> itf : interfaces.getModel().getAllTypes()) {
String impl = itf.getQualifiedName().replace("spoon.reflect", "spoon.support.reflect") + "Impl";
CtType implClass = implementations.getFactory().Type().get(impl);
if (implClass != null && !implClass.hasModifier(ModifierKind.ABSTRACT)) {
result.add((CtType<? extends CtElement>) itf);
}
}
return result;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class MainTest method checkModelIsTree.
/*
* contract: each element is used only once
* For example this is always true: field.getType() != field.getDeclaringType()
*/
@Test
public void checkModelIsTree() {
Exception dummyException = new Exception("STACK");
PrinterHelper problems = new PrinterHelper(rootPackage.getFactory().getEnvironment());
Map<CtElement, Exception> allElements = new IdentityHashMap<>();
rootPackage.filterChildren(null).forEach((CtElement ele) -> {
// uncomment this line to get stacktrace of real problem. The dummyException is used to avoid OutOfMemoryException
// Exception secondStack = new Exception("STACK");
Exception secondStack = dummyException;
Exception firstStack = allElements.put(ele, secondStack);
if (firstStack != null) {
if (firstStack == dummyException) {
Assert.fail("The Spoon model is not a tree. The " + ele.getClass().getSimpleName() + ":" + ele.toString() + " is shared");
}
// the element ele was already visited. It means it used on more places
// report the stacktrace of first and second usage, so that place can be found easily
problems.write("The element " + ele.getClass().getSimpleName()).writeln().incTab().write(ele.toString()).writeln().write("Is linked by these stacktraces").writeln().write("1) " + getStackTrace(firstStack)).writeln().write("2) " + getStackTrace(secondStack)).writeln().decTab();
}
});
String report = problems.toString();
if (report.length() > 0) {
Assert.fail(report);
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class MainTest method checkParentConsistency.
public static void checkParentConsistency(CtElement ele) {
final Set<CtElement> inconsistentParents = new HashSet<>();
new CtScanner() {
private Deque<CtElement> previous = new ArrayDeque();
@Override
protected void enter(CtElement e) {
if (e != null) {
if (!previous.isEmpty()) {
try {
if (e.getParent() != previous.getLast()) {
inconsistentParents.add(e);
}
} catch (ParentNotInitializedException ignore) {
inconsistentParents.add(e);
}
}
previous.add(e);
}
super.enter(e);
}
@Override
protected void exit(CtElement e) {
if (e == null) {
return;
}
if (e.equals(previous.getLast())) {
previous.removeLast();
} else {
throw new RuntimeException("Inconsistent stack");
}
super.exit(e);
}
}.scan(ele);
assertEquals("All parents have to be consistent", 0, inconsistentParents.size());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class JavaDocTest method testJavadocNotPresentInAST.
@Test
public void testJavadocNotPresentInAST() throws Exception {
Launcher launcher = new Launcher();
launcher.getEnvironment().setCommentEnabled(false);
launcher.getEnvironment().setNoClasspath(true);
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/javadoc/testclasses/");
launcher.run();
new CtScanner() {
@Override
public void scan(CtElement element) {
if (element != null) {
assertEquals(0, element.getComments().size());
}
super.scan(element);
}
@Override
public void visitCtComment(CtComment comment) {
fail("Shouldn't have comment in the model.");
super.visitCtComment(comment);
}
}.scan(launcher.getModel().getRootPackage());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class ParentContractTest method testContract.
@Test
public void testContract() throws Throwable {
int nSetterCalls = 0;
int nAssertsOnParent = 0;
int nAssertsOnParentInList = 0;
// contract: all setters/adders must set the parent (not necessarily the direct parent, can be upper in the parent tree, for instance when injecting blocks
Object o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
for (CtMethod<?> setter : SpoonTestHelpers.getAllSetters(toTest)) {
Object argument = createCompatibleObject(setter.getParameters().get(0).getType());
try {
// we create a fresh object
CtElement receiver = ((CtElement) o).clone();
// we invoke the setter
Method actualMethod = setter.getReference().getActualMethod();
actualMethod.invoke(receiver, new Object[] { argument });
nSetterCalls++;
nTotalSetterCalls++;
// directly the element
if (CtElement.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
nAssertsOnParent++;
assertTrue(((CtElement) argument).hasParent(receiver));
}
// the element is in a list
if (Collection.class.isInstance(argument) && setter.getAnnotation(UnsettableProperty.class) == null) {
nAssertsOnParentInList++;
assertTrue(((CtElement) ((Collection) argument).iterator().next()).hasParent(receiver));
}
} catch (AssertionError e) {
Assert.fail("call setParent contract failed for " + setter.toString() + " " + e.toString());
} catch (InvocationTargetException e) {
if (e.getCause() instanceof UnsupportedOperationException) {
// this is now documented by @UnsettableProperty
throw e;
} else if (e.getCause() instanceof RuntimeException) {
throw e.getCause();
} else {
throw new SpoonException(e.getCause());
}
}
}
assertTrue(nSetterCalls > 0);
assertTrue(nAssertsOnParent > 0 || nAssertsOnParentInList > 0);
}
Aggregations