Has sun got 'conditional import' in Java yet?
i got a common source. But there are 2 java 'jar' comm packages that
might be used when program is running.
one comm pkg comes from sun, the other from RXTX. During runtime, if the
pkg is included in classpath, then it is loaded. If not Oh Well.
I would like to say
#if GNUIO
import gnu.io.*;
#else
import javax.comm.*;
#endif
So far, i have to have two separate directories. And slightly modify the
source common to all.
Oliver Wong - 28 Oct 2005 16:12 GMT
> Has sun got 'conditional import' in Java yet?
>
[quoted text clipped - 14 lines]
> So far, i have to have two separate directories. And slightly modify the
> source common to all.
Maybe I'm misunderstanding here, but it sounds like you're initially
asking whether the Java compiler can perform a compile-time decision about
which statements to include, and then later say you want the behaviour to
depend on run-time information.
Is this correct? If so, then you want a decision to be made before the
information is available to make it. Instead, you should write code that
tries to load one class (or set of classes) and if that fails, instead try
to load a second class (or set of classes).
- Oliver
Andrey Kuznetsov - 28 Oct 2005 16:27 GMT
> i got a common source. But there are 2 java 'jar' comm packages that might
> be used when program is running.
[quoted text clipped - 9 lines]
> import javax.comm.*;
> #endif
imports are only for compiler there.
You may import everithing you want, it does not change class file.

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
U. George - 28 Oct 2005 17:28 GMT
Sure it does.
For instance:
catch ( gnu.io.NoSuchPortException e )
or
catch ( javax.comm.NoSuchPortException e )
or
catch ( NoSuchPortException e )
the last catch hangs on to the import that has the definition of
"NoSuchPortException" . So depending on what is imported, that
Nosuchportexception can become either gni.io.nosuchportexception, or
javax.comm.nosuchportexception.
>>i got a common source. But there are 2 java 'jar' comm packages that might
>>be used when program is running.
[quoted text clipped - 12 lines]
> imports are only for compiler there.
> You may import everithing you want, it does not change class file.
Rogan Dawes - 28 Oct 2005 17:25 GMT
> Has sun got 'conditional import' in Java yet?
>
[quoted text clipped - 14 lines]
> So far, i have to have two separate directories. And slightly modify the
> source common to all.
Import both lots of classes, if you like, but wrap the creation of the
object in a try/catch block
However, if the classes have the same names, you can't import them, and
will have to use fully qualified names when creating the objects.
I haven't studied the comm API, so I'm not sure if there is an interface
that you can code to? Otherwise, you may want to wrap the two different
implementations in your own interface to hide the differences.
e.g.
interface MyCommInterface
// the methods that you need
interface GNUCommImpl implements MyCommInterface
interface SunCommImpl implements MyCommInterface
Then, e.g. in GNUCommImpl
try {
whatever = new gnu.io.Whatever();
} catch (ClassNotFoundException cnfe) {
// whatever
}
Or, detect which class is available, and instantiate the corresponding
class of your interface.
Rogan
U. George - 28 Oct 2005 18:02 GMT
unfortunamely the packages javax.comm & gnu.io (RXTX.org) are
implementations of the same communications specs. Just different package
names. which makes it a pain.
the only diff would be the import of the packages. I'd like to keep the
( my ) driver code the same for either comm packages. to minimize
editing, i would have prefered a compiler option/property switch (
-DGNUIO ) to tell what imports are to be done.
>> Has sun got 'conditional import' in Java yet?
>>
[quoted text clipped - 46 lines]
>
> Rogan
Oliver Wong - 28 Oct 2005 18:28 GMT
> unfortunamely the packages javax.comm & gnu.io (RXTX.org) are
> implementations of the same communications specs. Just different package
[quoted text clipped - 4 lines]
> i would have prefered a compiler option/property switch ( -DGNUIO ) to
> tell what imports are to be done.
I posted a solution for you elsewhere in the thread, but perhaps you
didn't see it. Use a factory to try to load each one class, and failing
that, tries to load the other class. That's how you can determine a runtime
which library is present.
Next, create Adapter classes which can be given classes from either
packages and yield the same behaviour with the same interface, delating the
work to the classes given.
Now use the factory to create instances of those Adapter classes,
passing in the appropriate class from the appropriate library.
Your client code, instead of generating instances of the classes
directly, uses the factory to generate instances of the classes.
- Oliver
Roedy Green - 28 Oct 2005 20:19 GMT
On Fri, 28 Oct 2005 14:13:49 GMT, "U. George"
<support@jgpsDOTgatworksDOTcom> wrote, quoted or indirectly quoted
someone who said :
>I would like to say
>
[quoted text clipped - 3 lines]
>import javax.comm.*;
>#endif
I too have run into that. It is one of few instances where you get
nostalgic for C++ macros.
The way Sun handles that in cases like JCE, JMF etc is to provide in
interfacing plug-in class.
At run time you give it the name of the 3rd party library you want,
and it hooks it up. Your application code always deals with the Sun
interface class.
You need a lot of glue, and there is an extra layer of run-time
indirection, and you can't as sure at jar build time you ensured all
was present, but it is quite open ended. You can keep on adding even
more implementations with no change to the applications.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Steve Horsley - 28 Oct 2005 21:28 GMT
> Has sun got 'conditional import' in Java yet?
>
[quoted text clipped - 14 lines]
> So far, i have to have two separate directories. And slightly modify the
> source common to all.
My suggestion:
Write two classes, one that uses package A, and the other that
uses package B. Include both in your distro.
At run-time, try and create your object that uses A, and if that
fails create an instance of the B using class instead, and use
that. It would help if the constructors of your class try to use
the A or B package so that the constructor fails if the package
is missing, rather than further down the road.
Steve
Tim Tyler - 29 Oct 2005 15:50 GMT
U. George <support@jgpsdotgatworksdotcom> wrote or quoted:
> Has sun got 'conditional import' in Java yet?
"Yet"?
I don't see why it should ever have that facility.
Have two different libraries? Use two different interfaces in two
different classes and then choose which class you instantiate.

Signature
__________
|im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.