Java Conditional Compilation: How to Prevent Code Chunks from Being Compiled

Java conditional compilation: how to prevent code chunks from being compiled?

Nope there isn't any support for conditional compilation in Java.

The usual plan is to hide the OS specific bits of your app behind an Interface and then detect the OS type at runtime and load the implementation using Class.forName(String).

In your case there no reason why you can't compile the both OS* (and infact your whole app) using Java 1.6 with -source 1.5 -target 1.5 then in a the factory method for getting hold of OS classes (which would now be an interface) detect that java.awt.Desktop
class is available and load the correct version.

Something like:

 public interface OS {
void openFile(java.io.File file) throws java.io.IOException;
}

public class OSFactory {
public static OS create(){
try{
Class.forName("java.awt.Desktop");
return new OSJ6();
}catch(Exception e){
//fall back
return new OSJ5();
}
}
}

How to mark java code such that it's not compiled

There are no pre-processor directives in Java. Your best choice is commenting out code.

supress warnings: dead code and conditional compilation

This is a Java compilation warning and toggling it on/off really depends on the IDE. Assuming you are using Eclipse, you can navigate to the configurations by Preferences > Java > Compiler > Errors/Warning > Comparing identical values ('x==x') 'ignore' This should turn it off.

Java conditional compilation to support 1.4/1.6 simultaneously

There is a previous SO answer that covers this, essentially using ant's replace task to make a quick and dirty version of IFDEF blocks. That would probably be easier than swapping out whole class files, and would make it easier to go full 1.6 whenever you can (just remove all of the 1.4 IFDEF blocks)

#ifdef for Java to remove finalize function at compile time

According to the javadoc

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

Which means that

a) you're not doing any special finalization unless you're explicitly declaring it in your code, and

b) If you don't include a finalize() method Object.finalize() will be called, which is no-op. So you can't improve on that unless you could somehow hack the jvm to not even call finalize.

A better question might be, do you have a specific reason to worry about finalization time in your application? have you profiled this? It's entirely possible that this is a non-issue in your application.

How to prevent debugging mode code from being compiled into classes in Java?

This is generally frowned upon in Java for many reasons but if you really need to achieve complete removal your best bet is to use a static final boolean.

public static final boolean Debug = false;

public void test() {
if ( Debug ) {
// Debug stuff here - usually will not be included in the .class file if Debug is false.
}
}

This usually (but it is not guaranteed anywhere as far as I know) completely removes the enclosed code.

The alternative is to use some form of late-binding mechanism to dynamically load the debugging classes while leaving an empty stub for your release code.

Android - conditional compiling

The problem has been solved by using android.annotation.TargetApi.
So, I rewrote my code as shown below:

@TargetApi(26)
private void createNotificationChannel(NotificationManager notificationManager){
android.app.NotificationChannel channel = new android.app.NotificationChannel(
NOTIFICATION_CHANNEL_ID, "ID_CHANNEL", NotificationManager.IMPORTANCE_LOW);

channel.setShowBadge(false);
channel.enableLights(true);
channel.setLockscreenVisibility(android.app.Notification.VISIBILITY_PUBLIC);

notificationManager.createNotificationChannel(channel);
}

public void init(Context context){
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

//Build.VERSION_CODES.O is not defined in older version of Android
//So, I have to use a numeric value (26)
//My Build.VERSION.SDK_INT = 21 (Lollipop)
if (Build.VERSION.SDK_INT >= 26) {
createNotificationChannel(notificationManager);
}
}

Now it works without any errors. I hope it helps someone else.

How to prevent debugging mode code from being compiled into classes in Java?

This is generally frowned upon in Java for many reasons but if you really need to achieve complete removal your best bet is to use a static final boolean.

public static final boolean Debug = false;

public void test() {
if ( Debug ) {
// Debug stuff here - usually will not be included in the .class file if Debug is false.
}
}

This usually (but it is not guaranteed anywhere as far as I know) completely removes the enclosed code.

The alternative is to use some form of late-binding mechanism to dynamically load the debugging classes while leaving an empty stub for your release code.



Related Topics



Leave a reply



Submit