• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can you create an object of type unknown until runtime? is it even possible?  RSS feed

 
Steve Buck
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Heres a rundown what I'm doing. Wrote a web service and now writing a swing client for it. Inside the swing client I want the user to be able to specify the port name, service name, wsdl location and namespace. I grab these values with an event handler in the form of Strings.

While these values may change, the actual method signatures behind the endpoint will remain consistent

When creating a proxy in the client, I need to use service.getPort(...) method to retrieve a proxy (or generated stub). This method call requires the java.lang.Class for the service endpoint interface (such as FooIF.class).

I'm not sure how I can pass this. I've heard about class loaders and the using the java.lang.Class methods ... am I headed in the right direction?

Then I'd like to cast it and call a method preferably something like
String results = (class cast somehow)proxy.myMethod();


(This is moreso a Java problem then web services so I figured to put it here)
[ March 25, 2005: Message edited by: Steve Buck ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create objects from classnames like this:

Object o = Class.forName( className ).newInstance();

If you are confident that the class you're getting implmeents some interface, you can cast it, too:

MyInterface o = (MyInterface)(class.forName( className ).newInstance());

Now you can call any method on that interface. This requires that the compiled class for the name you give is in the classpath. If you can't count on that - say you're calling an arbitrary web service from another company you've never heard of before today - you'll have to get into something even more dynamic. A URL ClassLoader might do the job. That's beyond my experience but it can be done. If this is for work look into Glue from WebMethods. It does exactly this and somehow generates the proxies on the fly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!