2

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

kingston
  • 11,053
  • 14
  • 62
  • 116
  • This would write you to make multiple builds. If you use `Class.forName()` you can use one build for both and select the difference at runtime. (The application could detect which one to import based on use) – Peter Lawrey Nov 18 '11 at 14:13
  • the problem is that in all the classes where I use that class I have to add the import that is different for different SDKs. This is true unless I wrap the class in a proxy that is what I would like to avoid – kingston Nov 18 '11 at 16:10
  • I tend to avoid multiple builds as this can turn into a nightmare once you start taking that road. – Peter Lawrey Nov 18 '11 at 16:14

1 Answers1

2

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.

Community
  • 1
  • 1
wds
  • 31,873
  • 11
  • 59
  • 84
  • Right but I was trying to avoid writing a proxy because the class has got many methods: it's boring... – kingston Nov 18 '11 at 14:54
  • @user38 I realised, hence the second paragraph. Not an acceptable solution then? – wds Nov 18 '11 at 15:24
  • at the end I wrote some proxies. I think that using Maven would have been reasonable too. Not to get bored writing the proxies I used regular expressions to automatically convert the stub generated by eclipse to the implementation that calls the instance of the "real subject" :-) – kingston Nov 22 '11 at 16:21