4

I'm just wondering if its possible to prevent my Java app to compile if certain conditions are broken and just throw compile error somehow. ( in my case i want to check if hashcode implementations among certain package returns unique values, for caching purposes ) I know it is possible by writing maven plugin using reflection, but unfortunatelly it's not a solution for me.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
kookee
  • 95
  • 5
  • When you say "compile", do you really mean build? If you can't use Maven, there are other build or unit test or static analysis tools that can help you. See JUnit, Ant, Gradle, http://stackoverflow.com/questions/4080/what-code-analysis-tools-do-you-use-for-your-java-projects http://stackoverflow.com/questions/12388434/java-static-code-analysis – Will Jun 25 '13 at 14:30
  • are you trying to make runtime checks in compilation? – Mehdi Karamosly Jun 25 '13 at 14:30
  • You have tests for that. You can configure your IDE to run always with some sort of tests. Then you can configure your Continuous integration environment to not deploy is some test are not fulfilled. – Damian Leszczyński - Vash Jun 25 '13 at 14:31
  • I guess you are looking for something like template meta-programming in C++? – Raedwald Jun 25 '13 at 14:36

3 Answers3

5

No, you can't do checks like this during compilation (assuming a normal compile with javac).

The usual way to do this is to have unit tests that are executed whenever you make a build (no one really does "manual" compiles in real projects, anyway).

Having the build break with an error when a tests fails is a very common scenario.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
2
  1. The situation you described is solved by a unit test. A unit test can prevent your code from being built or delivered, but of course it can't stop it from being compiled because it needs compiled code to work on. These are very easy to set up and bind to building in Maven, also possible in Ant.

  2. To my knowledge aspect-oriented programming can add compile time constraints. This was briefly covered in this answer here, to a Java question I asked re. a compile-time constraint. Analogizing, if AOP can enforce package dependencies, perhaps it can force class Foo to depend on class Bar, which is your situation - but I don't actually know AOP, so happy researching.

  3. Again, for simpler cases, you could actually just add a precompiling step, even using a C preprocessor and the #error macro. But this is partly what AOP is.

  4. You can add static asserts so that the class fails at load time, which is ahead of runtime (sort of) but later than compile time. An improvement over load time. Again, unit tests are how this problem is actually solved.

  5. As described, you cannot cause a compile-time failure by a runtime computation using pure Java.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • AOP seems to be best way to go in this situation, thank you for helping me out :) – kookee Jun 26 '13 at 07:02
  • @kookee No, it's not. Unit testing and a "non-manual" build are. – djechlin Jun 26 '13 at 11:33
  • The problem here is that i'm going to use it as external lib so it has to be independent of non-manual build / unit testing. Well i'll try aspects if it won't work people will need to write their hashcodes with consideration :) thank you anyways – kookee Jun 26 '13 at 16:12
  • @kookee but... but... the *point* of using a real build and ship is so you *can* use it as an external library. You build and package and test as an internal library then include it on the classpath of your executable as a jar. That's how this works. – djechlin Jun 26 '13 at 16:16
  • yes but the beans with hashcode would be outside library (in project), i was hoping that it's possible to pass beans package to implemenatation of some kind of handler from library which would prevent whole thing to compile... – kookee Jun 26 '13 at 16:55
1

You would need a compiler that supports checking the invariants you want preserved; perhaps an extensible compiler such as JastAddJ.

Most projects rely on either unit tests to check behaviors, or static analysis after compilation using tools like findbugs or Coverity.

It's possible that in a language like Scala you would be able to encode your desired invariants in the type system, in which case the compiler would be able check that a particular sub-class obeys the contract.

Emil Sit
  • 22,894
  • 7
  • 53
  • 75