kingston kingston - 4 months ago 7
Java Question

pre-compiler switches in java: how to change the path for an imported class

I need to use the same class on two diffwrent platform-SDKs.

Although the class and its methods are the same the path to access the class is different.
On one device I need to use:

import a1.a2.classname

while for the second device I need to use:

import b1.b2.b3.classname

Please don't ask me why: I did not write the SDKs and I can't change them.

So the question is: how would you do it? Is there a way to import different versions of the same class on different sdk versions?

In C++ I would use a precompiler switch like:

#ifdef SDK1
#include "path1/class.h"
#else
#include "path2/class.h"
#endif


How would you do this in java?

Please consider that I can't derive from the class because I have access to its interface only and the number of methods is too big to think about using a proxy

wds wds
Answer

You could write a light proxy layer and check which classes are available with Class.forName(). This is the way to go if your two classes will diverge in signatures etc. in the future.

If, however, you are entirely sure they will always retain identical functionality, then just search and replace all instances of the import in question in your build tool. In maven you could use resource filtering, this question has an example.