sten-navie commented on issue #60:
URL: https://github.com/apache/felix-atomos/issues/60#issuecomment-1164154832

   This does not seem to be related the equinox metatype implementation. I was 
able to repeat the same problem, by writing a simple Activator that just tracks 
javax.xml.parsers.SAXParserFactory. When receiving the reference I print out 
some info about the received class vs the wanted class and then try to cast the 
result, which again results in a ClassCastException:
   
   ```
   g! lb
   START LEVEL 1
      ID|State      |Level|Name
       0|Active     |    0|OSGi System Bundle 
(3.17.200.v20220215-2237)|3.17.200.v20220215-2237
       1|Active     |    1|java.base (11.0.14)|11.0.14
       2|Active     |    1|java.compiler (11.0.14)|11.0.14
       3|Active     |    1|java.datatransfer (11.0.14)|11.0.14
       4|Active     |    1|java.desktop (11.0.14)|11.0.14
       5|Active     |    1|java.logging (11.0.14)|11.0.14
       6|Active     |    1|java.management (11.0.14)|11.0.14
       7|Active     |    1|java.management.rmi (11.0.14)|11.0.14
       8|Active     |    1|java.naming (11.0.14)|11.0.14
       9|Active     |    1|java.prefs (11.0.14)|11.0.14
      10|Active     |    1|java.rmi (11.0.14)|11.0.14
      11|Active     |    1|java.security.jgss (11.0.14)|11.0.14
      12|Active     |    1|java.security.sasl (11.0.14)|11.0.14
      13|Active     |    1|java.smartcardio (11.0.14)|11.0.14
      14|Active     |    1|java.xml (11.0.14)|11.0.14
      15|Active     |    1|java.xml.crypto (11.0.14)|11.0.14
      16|Active     |    1|jdk.charsets (11.0.14)|11.0.14
      17|Active     |    1|jdk.compiler (11.0.14)|11.0.14
      18|Active     |    1|jdk.crypto.cryptoki (11.0.14)|11.0.14
      19|Active     |    1|jdk.crypto.ec (11.0.14)|11.0.14
      20|Active     |    1|jdk.internal.opt (11.0.14)|11.0.14
      21|Active     |    1|jdk.jartool (11.0.14)|11.0.14
      22|Active     |    1|jdk.javadoc (11.0.14)|11.0.14
      23|Active     |    1|jdk.jdeps (11.0.14)|11.0.14
      24|Active     |    1|jdk.jfr (11.0.14)|11.0.14
      25|Active     |    1|jdk.jlink (11.0.14)|11.0.14
      26|Active     |    1|jdk.localedata (11.0.14)|11.0.14
      27|Active     |    1|jdk.management (11.0.14)|11.0.14
      28|Active     |    1|jdk.management.jfr (11.0.14)|11.0.14
      29|Active     |    1|jdk.naming.dns (11.0.14)|11.0.14
      30|Active     |    1|jdk.naming.rmi (11.0.14)|11.0.14
      31|Active     |    1|jdk.security.auth (11.0.14)|11.0.14
      32|Active     |    1|jdk.security.jgss (11.0.14)|11.0.14
      33|Active     |    1|jdk.unsupported.desktop (11.0.14)|11.0.14
      34|Active     |    1|jdk.zipfs (11.0.14)|11.0.14
      35|Active     |    1|atomos (1.0.0)|1.0.0
      36|Active     |    1|Apache Felix Gogo Command (1.1.0)|1.1.0
      37|Active     |    1|Apache Felix Gogo Runtime (1.1.2)|1.1.2
      38|Active     |    1|Apache Felix Gogo Shell (1.1.2)|1.1.2
      39|Active     |    1|osgi.core (0.0.0)|0.0.0
   
   g! felix:install 
file:///<REDACTED-PATH>/tmp/atomos-test/atomos-saxparser-test-1.0.0-SNAPSHOT.jar
 
   Bundle ID: 40
   g! felix:start 40
   Starting SAX Test
   Start tracking.
   Wanted class : javax.xml.parsers.SAXParserFactory Module = unnamed module 
@554f5786, Loader = 
org.eclipse.osgi.internal.loader.EquinoxClassLoader@2097740[java.xml:11.0.14(id=14)]
   Received impl class : 
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl Module = module 
java.xml, Loader = null
   Super class : javax.xml.parsers.SAXParserFactory Module = module java.xml, 
Loader = null
   Super class : java.lang.Object Module = module java.base, Loader = null
   Trying to cast
   java.lang.ClassCastException: Cannot cast 
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl to 
javax.xml.parsers.SAXParserFactory
   ```
   
   The relevant code (full code here 
https://gist.github.com/sten-navie/278b2efa077d170fb530083c0648b2dd):
   ```
           System.out.println("Starting SAX Test");
           Class<SAXParserFactory> wantedClass = SAXParserFactory.class;
   
          // use raw types, so that we do not immediately trigger a class-cast 
exception via an implicit cast
           serviceTracker = new ServiceTracker(bundleContext, wantedClass, new 
ServiceTrackerCustomizer() {
               @Override
               public Object addingService(ServiceReference serviceReference) {
                   Object s = bundleContext.getService(serviceReference);
                   if (s == null) {
                       System.out.println("Got null ref!!!");
                       return null;
                   }
                   Class<?> implcl = s.getClass();
                   printClass("Wanted class", wantedClass);
                   printClass("Received impl class", implcl);
                   Class<?> supercl = implcl.getSuperclass();
                   while (supercl != null) {
                       printClass("Super class", supercl);
                       supercl = supercl.getSuperclass();
                   }
   
                   // try to cast
                   System.out.println("Trying to cast");
                   SAXParserFactory f = wantedClass.cast(s);
                   System.out.println("Cast successful. Done!");
                   return f;
               }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@felix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to