You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Roland <wg...@ids.de> on 2013/07/11 11:12:00 UTC

Experiences with ClassCastException

Hi,
I would like to tell you about my experiences that I made yesterday. I had a
error that occurred during integration testing and which is difficult to
understand. But I was able to fix the error. Maybe I can prevent someone
making the same mistake.

I have a Utility-Class which is used by the Felix-Launcher (systembundle) as
well as by some other bundles. This Utility-Class provides some basic
functionality like catching services with interface-casting and error
handling. Since the systembundle can not import packages the Utility-package
has to be embedded to the systembundle. So far so good. At any point of time
I register a number of services by a Bundle A. And at a later point of time
I catch the services by calling ServiceTracker::getServices() in a Bundle B.

  public <T> T[] getServices(BundleContext bundlecontext, Class<T> clazz)
  {
    ServiceTracker st = new ServiceTracker(bundlecontext, clazz.getName(),
null);
    st.open();
    Object[] objs = st.getServices();
    st.close();
    try{
        return (T[]) objs;
      }catch(LinkageError e){
        ...
      }catch(ClassCastException e){
        ...
      }

The service-objects are instanciated by the bundle-ClassLoader of Bundle A.
The array "Object[] objs" (see above) holding the service-instances is
instanciated by the bootstrap-ClassLoader but not by the ClassLoader of
Bundle A. This causes a ClassCastException because of two different
ClassLoaders. Very nasty!

The solution is to use Collection<T> instead of T[].

Regards
Roland




--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5004270.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Experiences with ClassCastException

Posted by Roland <wg...@ids.de>.
Hello Jan,
I introduce the framework into an existing project. Certain requirements
make it necessary to use the low-level APIs. In principle, you're right.



--
View this message in context: http://apache-felix.18485.x6.nabble.com/ClassCastException-after-isAssignableTo-tp5003252p5004275.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Experiences with ClassCastException

Posted by Jan Willem Janssen <ja...@luminis.eu>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 7/11/13 11:12 AM, Roland wrote:
> Hi, I would like to tell you about my experiences that I made
> yesterday. I had a error that occurred during integration testing
> and which is difficult to understand. But I was able to fix the
> error. Maybe I can prevent someone making the same mistake.
> 
> I have a Utility-Class which is used by the Felix-Launcher
> (systembundle) as well as by some other bundles. This Utility-Class
> provides some basic functionality like catching services with
> interface-casting and error handling. Since the systembundle can
> not import packages the Utility-package has to be embedded to the
> systembundle. So far so good. At any point of time I register a
> number of services by a Bundle A. And at a later point of time I
> catch the services by calling ServiceTracker::getServices() in a
> Bundle B.
> 
> public <T> T[] getServices(BundleContext bundlecontext, Class<T>
> clazz) { ServiceTracker st = new ServiceTracker(bundlecontext,
> clazz.getName(), null); st.open(); Object[] objs =
> st.getServices(); st.close(); try{ return (T[]) objs; 
> }catch(LinkageError e){ ... }catch(ClassCastException e){ ... }
> 
> The service-objects are instanciated by the bundle-ClassLoader of
> Bundle A. The array "Object[] objs" (see above) holding the
> service-instances is instanciated by the bootstrap-ClassLoader but
> not by the ClassLoader of Bundle A. This causes a
> ClassCastException because of two different ClassLoaders. Very
> nasty!
> 
> The solution is to use Collection<T> instead of T[].

tl;drc> Why don't you use frameworks that can manage the dependencies
for you, like DS or Felix DependencyManager, instead of fiddling with
the low-level APIs?

- -- 
Met vriendelijke groeten | Kind regards

Jan Willem Janssen | Software Architect
+31 631 765 814

/My world is revolving around PulseOn and Amdatu/

Luminis Technologies B.V.
J.C. Wilslaan 29
7313 HK   Apeldoorn
+31 88 586 46 30

http://www.luminis-technologies.com
http://www.luminis.eu

KvK (CoC) 09 16 28 93
BTW (VAT) NL8169.78.566.B.01
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJR3nv2AAoJEKF/mP2eHDc4cXUP/30bgJdUSbU4PKaV9iAJcdTs
J5sZvSI7NCkwwU7lhZFmURqyqxarKRwv/FQObvI87nRu4S/Q/8x9sU4cSIuXXsGj
PKIV+yYVH1XD01x/LDyXgyuFsMqfqHT8MObV8pSyFuNs2gnnJDd11Z492ZYn56H6
nbIL2RCMUCs5clJLpwfrAGS6y2835ZtuDn0TDHub6UOZ5sexfHKKsWH0UhYused8
vsmdLGlIw3V9W1XfQQhOosAbbUFzRHGIa6OflcBMiMZiixZZzR1ysmIr0DCAVXzU
2QL1q9or+94eVbPhZuOUsJgZLeHxOtjJpzguTP2ofM5801/1GKlxOuAYI78Wqi5d
qYZ8DExwf1Y/9s9ESQa6QcAq6uOFpDAGV8OO8KAFj4usKTP3uaK0wrGH1sg+AmBC
vw0jXNFTuTDW8JmC4MfDjqAx9WnS2+1yIZLS4zaYjJ3iVanJAJSkAUCGHeFhgeZ1
K1KwJtnySxhP01K4li/uOA9U8jEr/dbMtq0+Xgp4fQLJvDbMiy4reddhUn/H6Y4w
+xv4sW6h48OkeA8kxSK60zA0cp3EpPgYK1rUicpl03k9zt435aB1teMdo3RKdQ+i
QC6GlIOvjZppv3gOIN+QAtltL3Iog64kw6WqvrfSvTP7Gdo1HaK/+OR6wM7XMZIm
J28WFlt0M2xm8u8xHzpT
=Vdw/
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org