3

It might sound a little bit weird, but I am looking for a possibility to test if some statements in the code are rejected by the typechecker (which means that the code should NOT compile).

Be explain my intend: I am running a controlled experiment on type-systems where my subjects have to write some methods in java for me. The functionality of the methods written by the subjects can be easily tested using unit-tests, but I also want the methods to be well-typed (which means that some methodcalls should not be allowed).

One way I could imagine to achieve that would be writing the statements which should break the build into a seperate file, add it to the classpath and run javac to see if any error occurs during the build. Although this might work, it does not feel very sophisticated, so my question is: Is there any better way to (automatically) test if some statements are refected by the typechecker?

Seb
  • 1,721
  • 1
  • 17
  • 30

2 Answers2

5

This is a variation of the Halting Problem, which isn't solvable in the general case. To do this, you have to run (or in this case compile) the code. Therefore, the solution you've already proposed is the best solution.

CDahn
  • 1,795
  • 12
  • 23
  • Compiling the code is not the problem. I can compile the code anytime I want (or even run it). What I am looking for is e.g. some kind of annotation which I can add to a test-method which forces testframework to check if the build will break and after that exclude the method from the compilation (because otherwise the whole project would not compile anymore). – Seb Jul 12 '14 at 14:12
  • You can't test if the build will break without actually building it. This is due to the properties described in the Halting Problem. Therefore, if you must build it anyways, just check if that build failed, parse the failure response and take the appropriate action (e.g., remove the method from the test harness code). – CDahn Jul 12 '14 at 14:28
4

Have you looked at the Checker Framework? It can be used to static code analysis and more. It might be a good fit for what's you are doing. Here is the link on my answer with an example of the annotation type processor.

Also you may find the Java Compiler API quite helpful. It allows to execute javac programmatically in a single java machine. So you could use it as a part of your tests.

Community
  • 1
  • 1
Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48