Search in sources :

Example 31 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class CtRenameLocalVariableRefactoringTest method checkLocalVariableRename.

protected void checkLocalVariableRename(Launcher launcher, CtLocalVariable<?> targetVariable, String newName, boolean renameShouldPass) {
    String originName = targetVariable.getSimpleName();
    CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
    refactor.setTarget(targetVariable);
    refactor.setNewName(newName);
    if (renameShouldPass) {
        try {
            refactor.refactor();
        } catch (SpoonException e) {
            throw new AssertionError(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" should NOT fail when trying rename to \"" + newName + "\"\n" + targetVariable.toString(), e);
        }
        assertEquals(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\" passed, but the name of variable was not changed", newName, targetVariable.getSimpleName());
        assertCorrectModel(launcher, getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\"");
    } else {
        try {
            refactor.refactor();
            fail(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" should fail when trying rename to \"" + newName + "\"");
        } catch (SpoonException e) {
        }
        assertEquals(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" failed when trying rename to \"" + newName + "\" but the name of variable should not be changed", originName, targetVariable.getSimpleName());
    }
    if (renameShouldPass) {
        rollback(targetVariable, originName);
    }
    assertEquals(originName, targetVariable.getSimpleName());
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) SpoonException(spoon.SpoonException)

Example 32 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class CtRenameLocalVariableRefactoringTest method rollback.

private void rollback(CtLocalVariable<?> targetVariable, String originName) {
    String newName = targetVariable.getSimpleName();
    CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
    refactor.setTarget(targetVariable);
    // rollback changes
    refactor.setNewName(originName);
    try {
        refactor.refactor();
    } catch (SpoonException e) {
        throw new AssertionError(getParentMethodName(targetVariable) + " Rename of \"" + originName + "\" to \"" + newName + "\" passed, but rename back to \"" + originName + "\" failed", e);
    }
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) SpoonException(spoon.SpoonException)

Example 33 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class MetaModelTest method singleValueRoleAddSetRemove.

@Test
public void singleValueRoleAddSetRemove() {
    // contract: single value roles supports multivalue interface too
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    CtTypeReference<?> typeRef = factory.Type().createReference("some.test.package.TestType");
    RoleHandler rh = RoleHandlerHelper.getRoleHandler(typeRef.getClass(), CtRole.PACKAGE_REF);
    // contract: single value role provides a List
    List<CtPackageReference> packages = rh.asList(typeRef);
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of existing value fails and changes nothing
    try {
        packages.add(typeRef.getPackage());
        fail();
    } catch (Exception e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of null fails and changes nothing
    try {
        assertFalse(packages.add(null));
        fail();
    } catch (Exception e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of different value fails, and changes nothing
    try {
        packages.add(factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (SpoonException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of different value changes nothing
    assertFalse(packages.remove(factory.Package().createReference("some.test.another_package")));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of null value changes nothing
    assertFalse(packages.remove(null));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of existing value sets value to null and size to 0
    assertTrue(packages.remove(factory.Package().createReference("some.test.package")));
    assertListContracts(packages, typeRef, 0, null);
    // contract add of null into empty collection changes size to 1, but value is still null
    assertTrue(packages.add(null));
    assertListContracts(packages, typeRef, 1, null);
    // contract: adding of new value into collection with single null value fails and changes nothing
    try {
        packages.add(factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (SpoonException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, null);
    // contract: set of new value replaces existing value
    assertEquals(null, packages.set(0, factory.Package().createReference("some.test.package")));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: set of null value keeps size==1 even if value is replaced by null
    assertEquals("some.test.package", packages.set(0, null).getQualifiedName());
    assertListContracts(packages, typeRef, 1, null);
    // contract: remove of null value by index sets size==0 the value is still null
    assertNull(packages.remove(0));
    assertListContracts(packages, typeRef, 0, null);
    // contract: add of null value sets size==1 the value is still null
    assertTrue(packages.add(null));
    assertListContracts(packages, typeRef, 1, null);
    // contract: remove of null value by value sets size==0 the value is still null
    assertTrue(packages.remove(null));
    assertListContracts(packages, typeRef, 0, null);
    // contract: set of new value on empty collection fails with IndexOutOfBounds and changes nothing
    try {
        packages.set(0, factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 0, null);
    // contract: adding of value into empty collection adds value
    assertTrue(packages.add(factory.Package().createReference("some.test.another_package")));
    assertListContracts(packages, typeRef, 1, "some.test.another_package");
    // contract: remove of value by index from collection removes that value
    assertEquals("some.test.another_package", packages.remove(0).getQualifiedName());
    assertListContracts(packages, typeRef, 0, null);
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) SpoonException(spoon.SpoonException) RoleHandler(spoon.reflect.meta.RoleHandler) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) SpoonException(spoon.SpoonException) Test(org.junit.Test)

Example 34 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class QueueProcessingManager method addProcessor.

public void addProcessor(Class<? extends Processor<?>> type) {
    try {
        Processor<?> p = type.newInstance();
        addProcessor(p);
    } catch (Exception e) {
        throw new SpoonException("Unable to instantiate processor \"" + type.getName() + "\" - Your processor should have a constructor with no arguments", e);
    }
}
Also used : SpoonException(spoon.SpoonException) SpoonException(spoon.SpoonException)

Example 35 with SpoonException

use of spoon.SpoonException in project spoon by INRIA.

the class StandardEnvironment method setInputClassLoader.

@Override
public void setInputClassLoader(ClassLoader aClassLoader) {
    if (aClassLoader instanceof URLClassLoader) {
        final URL[] urls = ((URLClassLoader) aClassLoader).getURLs();
        if (urls != null && urls.length > 0) {
            // Check that the URLs are only file URLs
            boolean onlyFileURLs = true;
            for (URL url : urls) {
                if (!url.getProtocol().equals("file")) {
                    onlyFileURLs = false;
                }
            }
            if (onlyFileURLs) {
                List<String> classpath = new ArrayList<>();
                for (URL url : urls) {
                    classpath.add(url.getPath());
                }
                setSourceClasspath(classpath.toArray(new String[0]));
            } else {
                throw new SpoonException("Spoon does not support a URLClassLoader containing other resources than local file.");
            }
        }
        return;
    }
    this.classloader = aClassLoader;
}
Also used : SpoonException(spoon.SpoonException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URL(java.net.URL)

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