Search in sources :

Example 1 with ModelBuildingException

use of spoon.compiler.ModelBuildingException in project spoon by INRIA.

the class ExceptionTest method testExceptionIfNotCompilable.

@Test
public void testExceptionIfNotCompilable() throws Exception {
    try {
        Launcher spoon = new Launcher();
        Factory factory = spoon.createFactory();
        spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/spoon/test/exceptions/ClassWithError.java")).build();
        fail();
    } catch (ModelBuildingException e) {
    // perfect
    }
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ModelBuildingException(spoon.compiler.ModelBuildingException) Test(org.junit.Test)

Example 2 with ModelBuildingException

use of spoon.compiler.ModelBuildingException in project spoon by INRIA.

the class ExceptionTest method testExceptionInSnippet.

@Test
public void testExceptionInSnippet() {
    try {
        Factory factory = createFactory();
        factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + // does not compile here
        " int x=Foo;" + "}};").compile();
        fail();
    } catch (ModelBuildingException e) {
    // perfect
    }
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) ModelBuildingException(spoon.compiler.ModelBuildingException) Test(org.junit.Test)

Example 3 with ModelBuildingException

use of spoon.compiler.ModelBuildingException in project spoon by INRIA.

the class JDTBasedSpoonCompiler method report.

protected void report(Environment environment, CategorizedProblem problem) {
    if (problem == null) {
        throw new IllegalArgumentException("problem cannot be null");
    }
    File file = new File(new String(problem.getOriginatingFileName()));
    String filename = file.getAbsolutePath();
    String message = problem.getMessage() + " at " + filename + ":" + problem.getSourceLineNumber();
    if (problem.isError()) {
        if (!environment.getNoClasspath()) {
            // by default, compilation errors are notified as exception
            throw new ModelBuildingException(message);
        } else {
            // in noclasspath mode, errors are only reported
            // but undefined import, type, and name errors are irrelevant
            int problemId = problem.getID();
            if (problemId != IProblem.UndefinedType && problemId != IProblem.UndefinedName && problemId != IProblem.ImportNotFound) {
                environment.report(null, Level.WARN, message);
            }
        }
    }
}
Also used : ModelBuildingException(spoon.compiler.ModelBuildingException) File(java.io.File) SpoonFile(spoon.compiler.SpoonFile)

Aggregations

ModelBuildingException (spoon.compiler.ModelBuildingException)3 Test (org.junit.Test)2 Factory (spoon.reflect.factory.Factory)2 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)2 File (java.io.File)1 Launcher (spoon.Launcher)1 SpoonFile (spoon.compiler.SpoonFile)1