use of spoon.SpoonException in project spoon by INRIA.
the class Substitution method substitute.
/**
* Substitutes all the template parameters in a random piece of code.
*
* @param targetType
* the target type
* @param template
* the template instance
* @param code
* the code
* @return the code where all the template parameters has been substituted
* by their values
*/
public static <E extends CtElement> E substitute(CtType<?> targetType, Template<?> template, E code) {
if (code == null) {
return null;
}
if (targetType == null) {
throw new RuntimeException("target is null in substitution");
}
E result = (E) code.clone();
List<E> results = new SubstitutionVisitor(targetType.getFactory(), targetType, template).substitute(result);
if (results.size() > 1) {
throw new SpoonException("StatementTemplate cannot return more then one statement");
}
return results.isEmpty() ? null : results.get(0);
}
use of spoon.SpoonException in project spoon by INRIA.
the class TestModifiers method testSetVisibility.
@Test
public void testSetVisibility() {
// contract: setVisibility should only work with public/private/protected modifiers
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/StaticMethod.java");
spoon.buildModel();
CtType<?> myClass = spoon.getFactory().Type().get(StaticMethod.class);
CtMethod methodPublicStatic = myClass.getMethodsByName("maMethod").get(0);
assertEquals(ModifierKind.PUBLIC, methodPublicStatic.getVisibility());
methodPublicStatic.setVisibility(ModifierKind.PROTECTED);
assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
try {
methodPublicStatic.setVisibility(ModifierKind.FINAL);
fail();
} catch (SpoonException e) {
}
assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
}
use of spoon.SpoonException 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);
}
use of spoon.SpoonException in project spoon by INRIA.
the class CtRenameLocalVariableRefactoringTest method testRefactorWrongUsage.
@Test
public void testRefactorWrongUsage() throws Exception {
CtType varRenameClass = ModelUtils.buildClass(CtRenameLocalVariableRefactoringTestSubject.class);
CtLocalVariable<?> local1Var = varRenameClass.filterChildren((CtLocalVariable<?> var) -> var.getSimpleName().equals("local1")).first();
// contract: a target variable is not defined. Throw SpoonException
CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
refactor.setNewName("local1");
try {
refactor.refactor();
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to empty string. Throw SpoonException
refactor.setTarget(local1Var);
try {
refactor.setNewName("");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains space. Throw SpoonException
try {
refactor.setNewName("x ");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains space. Throw SpoonException
try {
refactor.setNewName("x y");
fail();
} catch (SpoonException e) {
// should fail - OK
}
// contract: invalid rename request to variable name which contains character which is not allowed in variable name. Throw SpoonException
try {
refactor.setNewName("x(");
fail();
} catch (SpoonException e) {
// should fail - OK
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class ProcessingTest method testProcessorNotFoundThrowAnException.
@Test
public void testProcessorNotFoundThrowAnException() throws Exception {
try {
new Launcher().run(new String[] { "-p", "fr.inria.gforge.spoon.MakeAnAwesomeTacosProcessor" });
fail("The processor doesn't exist. We must throw an exception.");
} catch (SpoonException ignore) {
}
}
Aggregations