Uploaded image for project: 'XMLBeans'
  1. XMLBeans
  2. XMLBEANS-612

Adjust Class loading to allow using XMLBeans on Android

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • Version 5.0.3
    • Version 5.1.1
    • None
    • None

    Description

      The project https://github.com/centic9/poi-on-android allows to run Apache POI as part of Android Apps and thus also uses XMLBeans.

      When trying to update to Apache POI 5.2.2 and XMLBeans 5.1.0, I saw that there is a problem with the Class-loading.

      XMLBeans tries to do the following

      cl.getResource("xyz.class") 

      However on Android you cannot get class-resources this way.

      It seems this is only done to prevent a ClassNotFoundException in the call to Class.forName which follows.

       

      Therefore I propose to change the code as follows so that we simply expect a ClassNotFoundException instead of doing this separate lookup via getResource()

      // BEGIN CHANGES =========================================================================
      //
      // Simply remove this pre-check and handle ClassNotFoundException below
      // as Android does not find .class-files in getResource()
      //
      // See https://stackoverflow.com/questions/72616517/looking-up-class-files-via-classloader-getresource-on-android
      // for a related question
      //
      /*if (cl.getResource(clName.replace(".", "/") + ".class") == null) {
          // if the first class isn't found in the package, continue with the next package
          break;
      }*/
      
      try {
          @SuppressWarnings("unchecked")
          Class<? extends SchemaTypeLoader> cls = (Class<? extends SchemaTypeLoader>) Class.forName(clName, true, cl);
          list.add((SchemaTypeLoader) cls.getDeclaredField("typeSystem").get(null));
      } catch (ClassNotFoundException e) {
          // if the first class isn't found in the package, continue with the next package
          // this can happen and thus is ignored here
      } catch (Exception e) {
          throw new XmlRuntimeException(e);
      }
      // END-CHANGES =========================================================================

       

      Applying this change locally makes the project work on Android with Apache POI 5.2.2 and XMLBeans 5.1.0.

      Attachments

        Issue Links

          Activity

            People

              dominik.stadler@gmx.at Dominik Stadler
              dominik.stadler@gmx.at Dominik Stadler
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 10m
                  10m