< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page
rev 51675 : 8207690: Parsing API for classpath and similar path strings


  51 import java.util.Objects;
  52 import java.util.Set;
  53 import java.util.Spliterator;
  54 import java.util.Spliterators;
  55 import java.util.Vector;
  56 import java.util.WeakHashMap;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Supplier;
  59 import java.util.stream.Stream;
  60 import java.util.stream.StreamSupport;
  61 
  62 import jdk.internal.loader.BuiltinClassLoader;
  63 import jdk.internal.perf.PerfCounter;
  64 import jdk.internal.loader.BootLoader;
  65 import jdk.internal.loader.ClassLoaders;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.misc.VM;
  68 import jdk.internal.ref.CleanerFactory;
  69 import jdk.internal.reflect.CallerSensitive;
  70 import jdk.internal.reflect.Reflection;

  71 import sun.reflect.misc.ReflectUtil;
  72 import sun.security.util.SecurityConstants;
  73 
  74 /**
  75  * A class loader is an object that is responsible for loading classes. The
  76  * class {@code ClassLoader} is an abstract class.  Given the <a
  77  * href="#binary-name">binary name</a> of a class, a class loader should attempt to
  78  * locate or generate data that constitutes a definition for the class.  A
  79  * typical strategy is to transform the name into a file name and then read a
  80  * "class file" of that name from a file system.
  81  *
  82  * <p> Every {@link java.lang.Class Class} object contains a {@link
  83  * Class#getClassLoader() reference} to the {@code ClassLoader} that defined
  84  * it.
  85  *
  86  * <p> {@code Class} objects for array classes are not created by class
  87  * loaders, but are created automatically as required by the Java runtime.
  88  * The class loader for an array class, as returned by {@link
  89  * Class#getClassLoader()} is the same as the class loader for its element
  90  * type; if the element type is a primitive type, then the array class has no


2538                         unload(name, isBuiltin, handle);
2539                     } finally {
2540                         nativeLibraryContext.pop();
2541                     }
2542 
2543                 }
2544             }
2545         }
2546 
2547         // JNI FindClass expects the caller class if invoked from JNI_OnLoad
2548         // and JNI_OnUnload is NativeLibrary class
2549         static native void unload(String name, boolean isBuiltin, long handle);
2550     }
2551 
2552     // The paths searched for libraries
2553     private static String usr_paths[];
2554     private static String sys_paths[];
2555 
2556     private static String[] initializePath(String propName) {
2557         String ldPath = System.getProperty(propName, "");
2558         int ldLen = ldPath.length();
2559         char ps = File.pathSeparatorChar;
2560         int psCount = 0;
2561 
2562         if (ClassLoaderHelper.allowsQuotedPathElements &&
2563             ldPath.indexOf('\"') >= 0) {
2564             // First, remove quotes put around quoted parts of paths.
2565             // Second, use a quotation mark as a new path separator.
2566             // This will preserve any quoted old path separators.
2567             char[] buf = new char[ldLen];
2568             int bufLen = 0;
2569             for (int i = 0; i < ldLen; ++i) {
2570                 char ch = ldPath.charAt(i);
2571                 if (ch == '\"') {
2572                     while (++i < ldLen &&
2573                         (ch = ldPath.charAt(i)) != '\"') {
2574                         buf[bufLen++] = ch;
2575                     }
2576                 } else {
2577                     if (ch == ps) {
2578                         psCount++;
2579                         ch = '\"';
2580                     }
2581                     buf[bufLen++] = ch;
2582                 }
2583             }
2584             ldPath = new String(buf, 0, bufLen);
2585             ldLen = bufLen;
2586             ps = '\"';
2587         } else {
2588             for (int i = ldPath.indexOf(ps); i >= 0;
2589                  i = ldPath.indexOf(ps, i + 1)) {
2590                 psCount++;
2591             }
2592         }
2593 
2594         String[] paths = new String[psCount + 1];
2595         int pathStart = 0;
2596         for (int j = 0; j < psCount; ++j) {
2597             int pathEnd = ldPath.indexOf(ps, pathStart);
2598             paths[j] = (pathStart < pathEnd) ?
2599                 ldPath.substring(pathStart, pathEnd) : ".";
2600             pathStart = pathEnd + 1;
2601         }
2602         paths[psCount] = (pathStart < ldLen) ?
2603             ldPath.substring(pathStart, ldLen) : ".";
2604         return paths;
2605     }
2606 
2607     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
2608     static void loadLibrary(Class<?> fromClass, String name,
2609                             boolean isAbsolute) {
2610         ClassLoader loader =
2611             (fromClass == null) ? null : fromClass.getClassLoader();
2612         if (sys_paths == null) {
2613             usr_paths = initializePath("java.library.path");
2614             sys_paths = initializePath("sun.boot.library.path");
2615         }
2616         if (isAbsolute) {
2617             if (loadLibrary0(fromClass, new File(name))) {
2618                 return;
2619             }
2620             throw new UnsatisfiedLinkError("Can't load library: " + name);
2621         }
2622         if (loader != null) {
2623             String libfilename = loader.findLibrary(name);
2624             if (libfilename != null) {




  51 import java.util.Objects;
  52 import java.util.Set;
  53 import java.util.Spliterator;
  54 import java.util.Spliterators;
  55 import java.util.Vector;
  56 import java.util.WeakHashMap;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Supplier;
  59 import java.util.stream.Stream;
  60 import java.util.stream.StreamSupport;
  61 
  62 import jdk.internal.loader.BuiltinClassLoader;
  63 import jdk.internal.perf.PerfCounter;
  64 import jdk.internal.loader.BootLoader;
  65 import jdk.internal.loader.ClassLoaders;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.misc.VM;
  68 import jdk.internal.ref.CleanerFactory;
  69 import jdk.internal.reflect.CallerSensitive;
  70 import jdk.internal.reflect.Reflection;
  71 import jdk.internal.util.PathParser;
  72 import sun.reflect.misc.ReflectUtil;
  73 import sun.security.util.SecurityConstants;
  74 
  75 /**
  76  * A class loader is an object that is responsible for loading classes. The
  77  * class {@code ClassLoader} is an abstract class.  Given the <a
  78  * href="#binary-name">binary name</a> of a class, a class loader should attempt to
  79  * locate or generate data that constitutes a definition for the class.  A
  80  * typical strategy is to transform the name into a file name and then read a
  81  * "class file" of that name from a file system.
  82  *
  83  * <p> Every {@link java.lang.Class Class} object contains a {@link
  84  * Class#getClassLoader() reference} to the {@code ClassLoader} that defined
  85  * it.
  86  *
  87  * <p> {@code Class} objects for array classes are not created by class
  88  * loaders, but are created automatically as required by the Java runtime.
  89  * The class loader for an array class, as returned by {@link
  90  * Class#getClassLoader()} is the same as the class loader for its element
  91  * type; if the element type is a primitive type, then the array class has no


2539                         unload(name, isBuiltin, handle);
2540                     } finally {
2541                         nativeLibraryContext.pop();
2542                     }
2543 
2544                 }
2545             }
2546         }
2547 
2548         // JNI FindClass expects the caller class if invoked from JNI_OnLoad
2549         // and JNI_OnUnload is NativeLibrary class
2550         static native void unload(String name, boolean isBuiltin, long handle);
2551     }
2552 
2553     // The paths searched for libraries
2554     private static String usr_paths[];
2555     private static String sys_paths[];
2556 
2557     private static String[] initializePath(String propName) {
2558         String ldPath = System.getProperty(propName, "");



































2559 
2560         return PathParser.parsePath(ldPath, ".");










2561     }
2562 
2563     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
2564     static void loadLibrary(Class<?> fromClass, String name,
2565                             boolean isAbsolute) {
2566         ClassLoader loader =
2567             (fromClass == null) ? null : fromClass.getClassLoader();
2568         if (sys_paths == null) {
2569             usr_paths = initializePath("java.library.path");
2570             sys_paths = initializePath("sun.boot.library.path");
2571         }
2572         if (isAbsolute) {
2573             if (loadLibrary0(fromClass, new File(name))) {
2574                 return;
2575             }
2576             throw new UnsatisfiedLinkError("Can't load library: " + name);
2577         }
2578         if (loader != null) {
2579             String libfilename = loader.findLibrary(name);
2580             if (libfilename != null) {


< prev index next >