Changeset 7718


Ignore:
Timestamp:
May 22, 2019, 9:56:58 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2139: Switch to Java 11 (or later)

Fixed the call to ClassLoader.getPackage() in JarClassLoader. It now uses the ClassLoader.getDefinedPackage() instead and iterates up the parent chain of class loaders.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/JarClassLoader.java

    r7642 r7718  
    3434import java.util.jar.JarEntry;
    3535import java.util.jar.Manifest;
     36
    3637import java.util.jar.Attributes;
    3738import java.util.jar.Attributes.Name;
     
    247248    {
    248249      String packageName = name.substring(0, i);
    249       @SuppressWarnings("deprecation")
    250       Package pkg = getPackage(packageName);
    251       if (pkg == null)
     250      if (!isPackageDefined(packageName))
    252251      {
    253252        JarInfo info = jarFiles.get(file);
    254253        Manifest mf = info != null ? info.manifest : null;
    255         pkg = definePackage(packageName, mf);
     254        definePackage(packageName, mf);
    256255      }
    257256    }
    258257    Class<?> clazz = defineClass(name, b, 0, b.length);
    259258    return clazz;
     259  }
     260 
     261  // Safety limit to avoid endless loops in case parent class loaders are incorrectly linked
     262  private static final int MAX_PARENTS = 20;
     263  private boolean isPackageDefined(String name)
     264  {
     265    ClassLoader c = this;
     266    for (int i = 0; c != null && i < MAX_PARENTS; i++)
     267    {
     268      if (c.getDefinedPackage(name) != null) return true;
     269      c = c.getParent();
     270    }
     271    return false;
    260272  }
    261273 
Note: See TracChangeset for help on using the changeset viewer.