problem with getConstructor()

Hi,

I have the following code segment originally:

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

import org.omg.CORBA.ORB;

import java.lang.reflect.*;

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);

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

which has the packages defined at compile-time. Now I'd need to

change the code (with Java Reflection) to load the packages at

run-time (the packages won't be known at compile-time).

Here is the code segment I come with to use Java Reflection with

packages unknown at compile-time:

(two packages unkown at compile-time:

com.sun.corba.ee.impl.naming.cosnaming.TransientNameService

com.sun.corba.ee.spi.orb.ORB

)

/***************************/

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() )

Any help would be much apprecaited.

Thanks.

Jen

[2014 byte] By [jenwongusa] at [2007-10-3 3:48:51]
# 1

> 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) );

Your getConstructor call is in error. ORBClass is an instance of java.lang.Class. You don't need to call getClass() on it. The call you have is trying to find a constructor that takes a java.lang.Class object. Just do TransientNameServiceClass.getConstructor(ORBClass);

Updownquarka at 2007-7-14 21:45:54 > top of Java-index,Core,Core APIs...