'Java equivalent of #ifdef that allows non-compilable code
Is it possible in Java to do a sort of #ifdef thing, like in C/C++?
Example:
class Test
{
public static final boolean ANDROID = false;
public Test()
{
if (ANDROID)
{
// do stuff that won't compile if not on android
}
else
{
// do stuff that should be only done on desktop
}
}
}
Note that even if ANDROID
is false
, as in the example, it will still try to compile the code inside of the if
, even though it won't (and shouldn't) compile.
I'm looking for a way to do conditional compilation -- the compiler shouldn't even look at the if
if ANDROID
is false
.
The context of my question is that I have a Processing application in Eclipse. I'm using both normal Processing and Processing for Android in two separate projects, but I want to be able to move the source code of the projects between one another without having compiler errors. For example, I want to be able to have source code files that I can move from the Android project to the desktop project and only have to change a couple of things -- for example, changing ANDROID = true
to ANDROID = false
.
I really need it to be conditional compilation because when I copy the source code from the Android project to the desktop project, the desktop libraries obviously won't include Android libraries, and then the source code won't even compile.
EDIT: So now that I know that there is no preprocessor in Java, my question is: is there any other way to have this functionality in my projects (being able to copy source code from one to the other with only very minor changes) without having to manually [un]comment specific pieces of code and having to remember where those are?
EDIT 2: This is not a duplicate of the other question because my question includes code that may have compiler errors in it, whereas the question that this was closed as a duplicate of does not. (That other question concerns only code that would compile fine even without #ifdef
s.) To explain, the most highly rated (and accepted) answer for the other question talks about code that is compiled, but is simply not emitted in the bytecode. However, my question concerns code that would not even compile originally.
Solution 1:[1]
As others have said, the answer to your actual question is no.
However, you might approach your problem by isolating the Android or desktop code. You could do this by having three separate projects in eclipse:
- Core: This is the "shared" code that exists between both versions.
- Android: This contains only the code that runs on Android.
- Desktop: This contains only the code that runs on desktop.
Both your Android and Desktop projects would contain the Core project on their classpaths. In eclipse, you'd do this by going to your Java Build Path, then clicking the Projects tab, then adding the Core project to the "Required projects" list.
Then you'd set your code up so your Android and Desktop projects are what you actually deploy, and your Core project contains the code shared between them. Here's a simple example. Let's say we have an example class that looks like this:
public class Adder{
public void addAndPrint(int x, int y){
//code that will work on both Android and desktop
int sum = x+y;
if (ANDROID){
//code that will only work on Android
Log.v("example", "Sum:" + sum);
}
else{
//code that will only work on desktop
System.out.println("Sum: " + sum)
}
}
}
You could get around this by refactoring your code to isolate the "core" code that will work on both desktop and Android. Something like this:
//example core class
public class CoreAdder{
Printer printer;
public CoreAdder(Printer printer){
this.printer = printer;
}
public void addAndPrint(int x, int y){
int sum = x+y;
printer.print("Sum: " + sum);
}
}
//example core interface. We might print differently on
//Android and Desktop, so implement this interface in each.
public interface Printer{
public void print(String printMe);
}
Then, you'd isolate the code that will only work on Desktop:
//on desktop, use System.out.println()
public class DesktopPrinter implements Printer{
public void print(String printMe){
System.out.println(printMe);
}
}
//on desktop, entry point is main()
public class DesktopMain{
public static void main(String... args){
DesktopPrinter printer = new DesktopPrinter();
CoreAdder adder = new CoreAdder(printer);
adder.addAndPrint(1, 2);
}
}
And the code that will only work on Android:
//on Android, use a logger
public class AndroidPrinter implements Printer{
public void print(String printMe){
Log.v("example", "index=" + i);
}
}
//on Android, entry point is Activity
public class AndroidActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
AndroidPrinter printer = new AndroidPrinter ();
CoreAdder adder = new CoreAdder(printer);
adder.addAndPrint(1, 2);
}
}
Note that this is just an example, and I know that both System.out.println()
and Log.v()
could work on either platform. But the idea is the same: split your project up into multiple projects, and use interfaces to abstract away the behavior that changes between platforms.
Solution 2:[2]
As Java does not natively include a preprocessor, it would be incumbent upon you to manually execute one before compiling. The c preprocessor is m4, which you can run yourself.
Solution 3:[3]
There are no-preprocessors in java like C,C++ etc. All you can do is comment out the code.
Solution 4:[4]
Use #ifdef
and friends as in C and run the Java sources through the C pre-processor before compiling them.
For gcc the pre-processor is called cpp, for VC it's cl.exe using the option /P
.
Solution 5:[5]
No, there is no such thing as a preprocessor in Java that can hide chunks of code to the JVM.
EDIT: While you could of course run any program against your code base to preprocess it, think about if you really want this. The Android code will diverge more from the other Java code in time and your code will be littered with those #ifdef-like statements. Your IDE will also still see them and give you errors in both areas of code. In this case it's much easier to just make two projects out of it or, and that's my advice, create a platform independent library which you include in both projects and includes the functionality you need.
Solution 6:[6]
By defining productFlavors in build you can use folders that will be compiled when specific flavor is chosen thus you can make code in same codebase available conditionally at compile time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Nathaniel Ford |
Solution 3 | Anup Cowkur |
Solution 4 | alk |
Solution 5 | |
Solution 6 | Renetik |