samedi 1 juillet 2017

getAnnotations() empty when invoked in Tomcat Servlet

We are using Tomcat 8.5 with Jersey 2 in a Web Application that compiles Java Code using an InMemoryCompiler that is similar to the one posted here: http://ift.tt/2txviVl

I created a minimal example:

public static void main(String[] a) throws ClassNotFoundException {
    new TemporaryCodexExerciseCreatorWebService().test();
}

@GET
@Path("test")
public String test() throws ClassNotFoundException {
    final String source = "package minimal_example;" +
            "import org.junit.Test;" +
            "import static org.junit.Assert.*;" +
            "public class MinimalExample {" +
            "    @Test" +
            "    public void testTrivial() {" +
            "        assertTrue(true);" +
            "    }" +
            "}";
    List<JavaClass> sourceCodeList = Collections.singletonList(new JavaClass("minimal_example.MinimalExample", source));

    InMemoryCompiler compiler = InMemoryCompiler.compile(sourceCodeList);
    CompilerFeedback compile = compiler.getCompilerFeedback();

    if (!compile.isSuccess()) {
        throw new RuntimeException("Compilation failed: " + compile.getMessages());
    }

    Class<?> compiledClass = compiler.getCompiledClass("minimal_example.MinimalExample");
    Method[] methods = compiledClass.getMethods();
    for (Method m : methods) {
        Annotation[] annotations = m.getAnnotations();

        System.err.println(m.getName() + ": " + Arrays.toString(annotations));
    }

    return null;
}

If I invoke the program using the java interpreter on my command line I correctly receive an output like testTrivial: [@org.junit.Test(timeout=0, expected=class org.junit.Test$None)].

But if I execute the method using the web endpoint ("/test") I only get an empty array.

Why is this happening? I need this to work to be able to execute Unit tests programmatically after compiling them in-memory (I have seen java.lang.Exception: No runnable methods exception in running JUnits, but don't think my issue is related to that since everything works fine if the code is not running in the Servlet).





Aucun commentaire:

Enregistrer un commentaire