You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by warrior389 <jo...@gmail.com> on 2009/10/08 23:00:28 UTC

NoSuchMethodError exception after casting service to interface

I'm using Fuse 4.1.0.2 and Java 1.6.0_16

I have 3 bundles.  

Bundle1 defines an interface, proj.interfaces.MyInterface.  MyInterface has
a method 'public String getName()'

Bundle2 has a consumer proj.consumers.MyConsumer that, upon activation
registers a service tracker on the class type proj.interfaces.MyInterface. 
The service tracker, on service add, gets the service from the bundle
context for the reference, and calls getName() on it.  Bundle2 imports
proj.interfaces.

Bundle3  defines a class proj.classes.MyClass that implements
proj.interfaces.MyInterface.  It also, upon activation registers a new
instance of proj.classes.MyClass as a service and exposes it as
proj.interfaces.MyInterface.  Bundle3 imports proj.interfaces.

This is a snippet from Bundle2's activator:

ServiceTracker st = new ServiceTracker(bundleContext,
MyInterface.class.getName(), new ServiceTrackerCustomizer() {
  public Object addingService(ServiceReference reference) {
     Object o = bundleContext.getService(reference);
     if (o instanceof MyInterface) {
       System.out.println(((MyInterface)o.getName());
     }
  }  
  ...
});

I start bundle1, then bundle2, then bundle3.  When Bundle3 starts, the
service tracker from Bundle2 throws:

java.lang.NoSuchMethodError:
proj.interfaces.MyInterface.getName()Ljava/lang/String

Whys is this?    If I reflect on the methods in the service object, it shows
getName() as a valid method, and I can invoke it using reflection and get
the result.  It just won't let me cast the MyClass service to MyInterface
and call getName() that way.   I'm assuming its some kind of OSGi bundle
classpath issue, or am I missing something?

Thanks,
John
-- 
View this message in context: http://www.nabble.com/NoSuchMethodError-exception-after-casting-service-to-interface-tp25811457p25811457.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.