Path: blob/master/src/java.base/macosx/classes/jdk/internal/loader/ClassLoaderHelper.java
67848 views
/*1* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.loader;2627import java.io.File;28import java.util.ArrayList;29import sun.security.action.GetPropertyAction;3031class ClassLoaderHelper {32private static final boolean hasDynamicLoaderCache;33static {34String osVersion = GetPropertyAction.privilegedGetProperty("os.version");35// dynamic linker cache support on os.version >= 11.x36int major = 11;37int i = osVersion.indexOf('.');38try {39major = Integer.parseInt(i < 0 ? osVersion : osVersion.substring(0, i));40} catch (NumberFormatException e) {}41// SDK 10.15 and earlier always reports 10.16 instead of 11.x.x42hasDynamicLoaderCache = major >= 11 || osVersion.equals("10.16");43}4445private ClassLoaderHelper() {}4647/**48* Returns true if loading a native library only if49* it's present on the file system.50*51* @implNote52* On macOS 11.x or later which supports dynamic linker cache,53* the dynamic library is not present on the filesystem. The54* library cannot determine if a dynamic library exists on a55* given path or not and so this method returns false.56*/57static boolean loadLibraryOnlyIfPresent() {58return !hasDynamicLoaderCache;59}6061/**62* Returns an alternate path name for the given file63* such that if the original pathname did not exist, then the64* file may be located at the alternate location.65* For mac, this replaces the final .dylib suffix with .jnilib66*/67static File mapAlternativeName(File lib) {68String name = lib.toString();69int index = name.lastIndexOf('.');70if (index < 0) {71return null;72}73return new File(name.substring(0, index) + ".jnilib");74}7576/**77* Parse a PATH env variable.78*79* Empty elements will be replaced by dot.80*/81static String[] parsePath(String ldPath) {82char ps = File.pathSeparatorChar;83ArrayList<String> paths = new ArrayList<>();84int pathStart = 0;85int pathEnd;86while ((pathEnd = ldPath.indexOf(ps, pathStart)) >= 0) {87paths.add((pathStart < pathEnd) ?88ldPath.substring(pathStart, pathEnd) : ".");89pathStart = pathEnd + 1;90}91int ldLen = ldPath.length();92paths.add((pathStart < ldLen) ?93ldPath.substring(pathStart, ldLen) : ".");94return paths.toArray(new String[paths.size()]);95}96}979899