Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-336

MappedPropertyDescriptor#reLoadClass() possible NPE / odd code; also swallows Throwable

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.8.1
    • Bean / Property Utils
    • None

    Description

      MappedPropertyDescriptor#reLoadClass() possible NPE / odd code

      private Class reLoadClass() {
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          // Try the context class loader
          if (classLoader != null) {
              try {
                   return classLoader.loadClass(className);
              } catch (Throwable t) {
                   // ignore
              }
          }
          // Try this class's class loader
          try {
              return classLoader.loadClass(className);
          } catch (Throwable t) {
              return null;
          }
      }
      

      The second call to classLoader.loadClass(className) is likely to fail with NPE - if it is ever called.

      The comments suggest that the second loadClass() invocation should be done using a different class loader, however this is not done.

      Does the code really want to try loading the class twice using the same classloader?
      This is what will happen if the initial loadClass() fails.

      Another problem with the code is that it swallows Throwable.
      It would be better to catch just the expected Exceptions - or even Exception, but not Throwable.

      Attachments

        Activity

          People

            niallp Niall Pemberton
            sebb Sebb
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: