I am developing with GWT and share a codebase with an Android developer. Some functions we want to share take speciffic arguments like “Drawable” under Android and “Image” under 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 a Generic like this:
interface DrawImgInterf<T extends Object> {
public void DrawImg(T img);
}
However using a preproccesor variable seems better. Is there such a thing in Java?
No, there’s nothing like that in normal Java. You could run a preprocessor of course, but that will make it painful to develop the code. (Anything like an IDE which expects the code to be “normal” Java is going to get confused.)
Have you considered using an interface instead, which abstracts out the common operations, and binds to the appropriate real type at execution time? That won’t always work (as adding a proxy breaks situations where object identity is important) but in some cases it can be helpful.