Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 #ifdefs.) 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.

like image 706
Jashaszun Avatar asked Oct 30 '13 13:10

Jashaszun


3 Answers

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.

like image 137
Kevin Workman Avatar answered Oct 19 '22 19:10

Kevin Workman


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.

like image 3
nobody Avatar answered Oct 19 '22 19:10

nobody


There are no-preprocessors in java like C,C++ etc. All you can do is comment out the code.

like image 1
Anup Cowkur Avatar answered Oct 19 '22 18:10

Anup Cowkur