× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I'd second Blair's recommendation of Class.forName() and
reflection/introspection (Class.getMethods(), Class.getMethod(),
and Method.invoke()).
About the only thing I would add is that there's a slightly easier way to
do this if its reasonable to put requirements on the call target in your
environment.
For example, the call target could be required to have a
'public static void main(String args[])' method, or preferable and typesafe
method is to require the call target to implement (be an 'instanceof') an
interface that you share with it.

Here's Blair's example (works great for the 'public static void main(String
args())'
requirement.

     Class fooClz = Class.forName("some.class.name");
     MyClass myFoo = (MyClass) fooClz.newInstance();
     // ...then using the reflection API to determine what methods are
available:
     Method[] myMethods = fooClz.getMethods();
     // ...or if you know the method name and signature:
     Method myMethod = fooClz.getMethod("methodName", new Class[] {
                              parm1_type.class, parm2_type.class...});
     // Once you have the Method object, you can invoke it using:
     myMethod.invoke(Object obj, Object[] parms)



Here's an interface example. I like this better because its typesafe,
you always know you're calling someone who expects to be called for
this task.

     // call
     public interface MyCallableTaskX {
        public void call();
     }


     // This code should call it (uncompiled)
     Class fooClz = Class.forName("some.class.name");
     MyCallableTaskX     myFoo = null;
     try {
         myFoo = (MyCallableTaskX)fooClz.newInstance();
     }
     catch (ClassCastException e) {
         // If you get this, you know that the class wasn't what
         // you expected it to be (implementing MyCallableTaskX)
         // You could also leave the new object as an 'Object'
         // reference, and use instanceof to avoid the exception
         // in the bad case, but this way works fine.
     }
     catch (InstantiationException e) {
         // If you get this, you know that the class wasn't what
         // you expected it to be (Had no default constructor)
     }
     catch (IllegalAccessException e) {
         // If you get this, you know that the class wasn't what
         // you expected it to be (Ctor or class wasn't accessible,
public?)
     }

     // Now, you know exactly what you have. An instantiated
     // version of a MyCallableTaskX object (As a good OO programmer,
     // you don't need or want to know any more details).
     myFoo.call();




In every single ethnic, religious or racial group, there are a
very few truly evil people. For each of those people there
are many, many, many good people.
Assuming anything (evilness or capability for evil) about the
particular group is bigotry and idiocy. Don't do it.  -- Me

Fred A. Kulack  -  IBM eServer iSeries - Java DB2 access, Jdbc, JTA, etc...
IBM in Rochester, MN  (Phone: 507.253.5982   T/L 553-5982)
mailto:kulack@us.ibm.com   Personal: mailto:kulack@magnaspeed.net
AOL Instant Messenger: Home:FKulack  Work:FKulackWrk




As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.