Reputation: 97889
I do not mean the compile errors because I made a syntax mistake or whatever. In C++ we can create compile time errors based on conditions as in the following example:
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
#define STATIC_CHECK(expr, msg) { CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
int main(int argc, char* argv[])
{
STATIC_CHECK(false, Compile_Time_Failure);
return 0;
}
In VS 2005 this will output:
------ Build started: Project: Test, Configuration: Debug Win32 ------
Compiling...
Test.cpp
f:\temp\test\test\test.cpp(17) : error C2079: 'ERROR_Compile_Time_Failure' uses undefined struct 'CompileTimeError<__formal>'
with
[
__formal=0
]
Build log was saved at "file://f:\temp\Test\Test\Debug\BuildLog.htm"
Test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Is there any way to achieve this in Java?
Upvotes: 0
Views: 882
Reputation: 8768
Though the question was asked some time ago, I decided to post my answer, because I solved (to a certain degree) a slightly similar problem.
The specific of my task requires two applications with different function set to be built from a single core library (and unused stuff to be not linked in). The selection of function set is made by public static final boolean
flags. The problem is that I want to ensure in each of the applications, that it is built with a proper flag set in the core library. And in the case of improper functions are enabled, the application should not compile giving a compile time error.
The only solution I have found so far is to declare in the library final variables along with the flags: public static final int functionSet1 = 0;
, etc.
In the application package I added a dummy class with the checkup
ConditionalBuild.functionSet1 = 1;
From all the functionSetX variables only one made non-final at a specific build. So only one application can pass the build process without the error. Is there a better way to achieve this? Please let me know in comments.
Upvotes: 0
Reputation: 16851
As Matt Quail answered above, annotations, together with XDoclet, are suited to address your needs. That combinations allows for a quite a bit of preprocessing, code generation, etc.
Upvotes: 0
Reputation: 6229
There is no way to do this in Java, not in the same way it works for you in C++.
You could perhaps use annotations, and run apt before or after compilation to check your annotations.
For example:
@MyStaticCheck(false, "Compile Time Error, kind-of")
public static void main(String[] args) {
return;
}
And then write your own AnnotationProcessorFactory that looked for @MyStaticCheck annotations, and does something with the arguments.
Note: I haven't played too much with apt, but the documentation makes it looks like this is very do-able.
Upvotes: 2
Reputation: 55123
There is no way to produce any actions based on compile-time logic in Java without the use of a separate tool. Technically, it is possible to use the C pre-processor on Java, but you would have to be careful of its built-in assumptions about the underlying language. If I were you, I would find a better way to accomplish whatever it is you're trying to do with this compile-time error. If necessary, you could even write your own pre-processor (possibly using APT) if it is really so unavoidable.
Upvotes: 2