Help with use of Java reflection/Class.forName()


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Help with use of Java reflection/Class.forName()

  1. #1
    Join Date
    Feb 2006
    Posts
    5

    Help with use of Java reflection/Class.forName()

    Hi,

    I have some segment of the code as below that I'd like to use
    Java reflection/Class.forName() etc, so that some of
    the package names are not known at compile-time and
    can be decided at run-time.


    /////////////////////////////////////

    import org.omg.CORBA.ORB;
    import java.lang.reflect.*;

    import com.sun.enterprise.util.ORBManager;
    import com.sun.corba.ee.impl.naming.cosnaming.TransientNameService;

    private static ORB orb = null;

    Properties p = new Properties();
    p.put("org.omg.CORBA.ORBInitialHost","localhost");
    p.put("org.omg.CORBA.ORBInitialPort","3700");
    orb = ORBManager.getORB(p);
    TransientNameService nameService = new TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);

    ////////////////////////////////////////

    The three packages:

    com.sun.enterprise.util.ORBManager
    com.sun.corba.ee.impl.naming.cosnaming.TransientNameService
    com.sun.corba.ee.spi.orb.ORB

    are not known until run-time, so I have to use Java reflection/Class.forName()
    to handle :

    /***********************/
    orb = ORBManager.getORB(p);
    TransientNameService nameService = new TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);
    /***********************/

    I think I have some idea on how to hanlde the first statement
    orb = ORBManager.getORB(p);

    /***************************/
    String StrORBManager = "com.sun.enterprise.util.ORBManager";

    Class ORBManagerClass = Class.forName( StrORBManager );
    Method getORBMethod = ORBManagerClass.getMethod("getORB", p.getClass());
    orb = (ORB) getORBMethod.invoke(null, p) ;
    /***************************/

    But I am not quite sure how to deal with the second statement:
    ------------------
    TransientNameService nameService = new TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);
    ------------------

    while the packages :
    ===========
    com.sun.corba.ee.impl.naming.cosnaming.TransientNameService
    com.sun.corba.ee.spi.orb.ORB
    =============
    are both unknown at compile-time.

    Could someone help me with this and let me know how to convert
    the code into java reflection so that the pkacakges can be unknown
    until run-time (and the code would compile)?

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    In order to pass the type checker, you're going to need to know something about these definitions. Usually you extract an interface that is in a known package and then declare all the objects that you instantiate as types of that class. That way the function will pass type checking.

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Mar 2004
    Posts
    78
    HI,

    Fisrst you need to know how to create the ORB object.

    Then as you know the class name Class.forName(:String) will force the class loader to load the class whose name you hava as a String.

    Call the newInstance() mthod in order to instanciate TransientNameService.

    Then call the method you need.

    Code in peace )

  4. #4
    Join Date
    Feb 2006
    Posts
    5

    getConstructor() problem: RE Help with use of Java reflection/Class.forName()

    Thansk for the responses from evlich and PLS.

    I think I have a better idea now how to implement it.
    I still have one issue, though, with getConstructor() call.
    Could someone help?

    the original code (with compile-time known packages):
    /////////////////////////////////////

    import org.omg.CORBA.ORB;
    import java.lang.reflect.*;

    import com.sun.enterprise.util.ORBManager;
    import com.sun.corba.ee.impl.naming.cosnaming.TransientNameService;

    private static ORB orb = null;

    TransientNameService nameService = new TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);

    ////////////////////////////////////////

    Here is the code segment to use Java Reflection with
    packages unknown at compile-time:

    /***************************/
    Class TransientNameServiceClass = Class.forName("com.sun.corba.ee.impl.naming.cosnaming.TransientNameService");

    Class ORBClass = Class.forName( "com.sun.corba.ee.spi.orb.ORB" );

    Constructor TransientNameServiceConstructor = TransientNameServiceClass.getConstructor( ORBClass.getClass() ) ;

    Object nameService = TransientNameServiceConstructors[0].newInstance( ORBClass.cast(orb) );
    /***************************/


    somehow the next to last statement call:
    TransientNameServiceClass.getConstructor( ORBClass.getClass() )
    throws "java.lang.NoSuchMethodException".

    But if I call:
    Constructor[] TransientNameServiceConstructors = TransientNameServiceClass.getConstructors();

    and then go through the list of Constructors found and compare the
    params, I could find the matching Constructor that has
    ORBClass as the only param.

    IS there anything wrong with my call to:
    TransientNameServiceClass.getConstructor( ORBClass.getClass() )

    Thanks.

  5. #5
    Join Date
    Aug 2003
    Posts
    313
    It looks like you are passing in the Class which represents the Class which represents com.sun.corba.ee.spi.orb.ORB, rather than just the class. Maybe a little clearer to say it with the generic type, it looks like you are passing in Class<Class<com.sun.corba.ee.spi.orb.ORB>> rather than Class<com.sun.corba.ee.spi.orb.ORB>. Try removing the getClass() from the parameter passed to getConstructor().

    Hope this helps.
    ~evlich

  6. #6
    Join Date
    Mar 2004
    Posts
    78
    I am getting nervous.... )

    You get the Exception as there is no method like the one you try to call.

    java.lang.Class has only this:
    Constructor getConstructor(Class[] parameterTypes)

    And it requires and ARRAY with parameters - the values you want to pass to the constructor.

    // the original code (with compile-time known packages)
    // we want to do this by reflection, wright ?

    import org.omg.CORBA.ORB;
    import java.lang.reflect.*;

    import com.sun.enterprise.util.ORBManager;
    import com.sun.corba.ee.impl.naming.cosnaming.TransientNameService;

    private static ORB orb = null;

    Properties p = new Properties();
    p.put("org.omg.CORBA.ORBInitialHost","localhost");
    p.put("org.omg.CORBA.ORBInitialPort","3700");
    orb = ORBManager.getORB(p);
    TransientNameService nameService = new TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);

    /*
    The three packages:

    com.sun.enterprise.util.ORBManager
    com.sun.corba.ee.impl.naming.cosnaming.TransientNameService
    com.sun.corba.ee.spi.orb.ORB

    are not known until run-time
    */
    // here we wll do the stuff with reflection only:

    public static void main(String[] args) throws Exception {
    Properties p = new Properties();
    p.put("org.omg.CORBA.ORBInitialHost","localhost");
    p.put("org.omg.CORBA.ORBInitialPort","3700");

    Class orbManagerClass = Class.forName("com.sun.enterprise.util.ORBManager");

    Class[] classParams = new Class[]{Properties.class};
    Method methodGetOrb = orbManagerClass.getDeclaredMethod("getORB", classParams );

    Object[] objParams = new Object[]{p};
    // get the ORB instance
    Object orbInstance = methodGetOrb.invoke(orbManagerClass, objParams);


    Class tnsClass = Class.forName("sun.corba.ee.impl.naming.cosnaming.TransientNameService");

    Class orbClass = Class.forName("com.sun.corba.ee.spi.orb.ORB");

    classParams = new Class[]{orbClass};
    Constructor tnsCtor = tnsClass.getDeclaredConstructor(classParams);

    objParams = new Object[]{orbInstance};
    // create instance
    Object tnsInstance = tnsCtor.newInstance(objParams);
    }


    So, the reflection works like this:

    1. You load the class by name
    2. you get the desired Method or constructor By Name.
    The problem is that we have different methods with one and the same name but different parameters(the so called overloading) and we have to provide an array with java.lang.Class objects to precise the method we need
    3. we invoke the method on the object we have by providing the Object itself and the array with arguments required

    I hope that this is clear enough.

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 02:57 PM
  4. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center