Conditional imports in JAVA

U

U. George

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.
 
O

Oliver Wong

U. George said:
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.

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
 
A

Andrey Kuznetsov

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

imports are only for compiler there.
You may import everithing you want, it does not change class file.
 
R

Rogan Dawes

U. George said:
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.

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

U. George

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.
 
U

U. George

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.
 
O

Oliver Wong

U. George said:
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.

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
 
R

Roedy Green

I would like to say

#if GNUIO
import gnu.io.*;
#else
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.
 
S

Steve Horsley

U. George said:
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.

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
 
T

Tim Tyler

U. George said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top