Java preprocessor variables

I am developing with GWT and sharing the codebase with an Android developer. Some functions that we want to separate take certain arguments, such as "Drawable" for Android and "Image" for GWT.

Is it possible to use a preprocessor variable, as in C ++:

#ifdef ANDROID
public void DrawImg(Drawable img);
#elif GWT
public void DrawImg(Image img);
#endif

The solution we are testing is general:

interface DrawImgInterf<T extends Object> {
    public void DrawImg(T img);
}

However, using a preprocessor variable seems better. Is there such a thing in Java?

+3
source share
5 answers

No, in normal Java there is nothing like this. Of course, you can start the preprocessor, but it will be painful for code development. (Everything that looks like an IDE expecting code to be "normal" Java will be confusing.)

, ? ( , ), .

+4

, Java .

+2

Java + - , :

public static void 
main(String[] args)
{
  System.out.println({{
The answer,
my dearest, 
is {{computeAnswer()}}.
  }});
}
static String computeAnswer()
{
  return {{my computed answer}};
}

+1

, . ,

interface ImageVisitor {
    void visit(GWTImage image);
    void visit(AndroidImage image);
}

interface IImage {
    void accept(ImageVisitor visitor);
}

class GWTImage implements IImage {
    ..
    public void accept(ImageVisitor visitor) {
        visitor.visit(this);
    }
    ..
}

class AndroidImage implements IImage {
    ..
    public void accept(ImageVisitor visitor) {
        visitor.visit(this);
    }
    ..
}

class GWTImageVisitor implements ImageVisitor {    
    public void visit(GWTImage image) {      
        Image img = image.getImage();
        ..
    }
}

class AndroidImageVisitor implements ImageVisitor {    
    public void visit(AndroidImage image) {      
        Drawable drawable = image.getDrawable();
        ..
    }
}
0
source

All Articles