Search in sources :

Example 21 with SpoonException

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);
}
Also used : SubstitutionVisitor(spoon.support.template.SubstitutionVisitor) SpoonException(spoon.SpoonException)

Example 22 with SpoonException

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());
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with SpoonException

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);
}
Also used : SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) Collection(java.util.Collection) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 24 with SpoonException

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
    }
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) Test(org.junit.Test)

Example 25 with SpoonException

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) {
    }
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

SpoonException (spoon.SpoonException)57 Test (org.junit.Test)15 Launcher (spoon.Launcher)12 CtMethod (spoon.reflect.declaration.CtMethod)9 CtType (spoon.reflect.declaration.CtType)9 CtElement (spoon.reflect.declaration.CtElement)8 Factory (spoon.reflect.factory.Factory)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CtField (spoon.reflect.declaration.CtField)6 URL (java.net.URL)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 Collection (java.util.Collection)3 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtExecutable (spoon.reflect.declaration.CtExecutable)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtScanner (spoon.reflect.visitor.CtScanner)3 Filter (spoon.reflect.visitor.Filter)3 FileNotFoundException (java.io.FileNotFoundException)2