Avoid copy array (#1914)

This commit is contained in:
南宫雪珊 2022-05-05 20:54:48 +08:00 committed by GitHub
parent b0e9c30866
commit f69de757aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 21 deletions

View File

@ -23,17 +23,18 @@ import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import dalvik.system.DelegateLastClassLoader;
import de.robv.android.xposed.XposedBridge;
import hidden.ByteBufferDexClassLoader; import hidden.ByteBufferDexClassLoader;
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
public final class LspModuleClassLoader extends ByteBufferDexClassLoader { public final class LspModuleClassLoader extends ByteBufferDexClassLoader {
private static final String zipSeparator = "!/"; private static final String zipSeparator = "!/";
private static final List<File> systemNativeLibraryDirs =
splitPaths(System.getProperty("java.library.path"));
private final String apk; private final String apk;
private final File[] nativeLibraryDirs; private final List<File> nativeLibraryDirs = new ArrayList<>();
private static List<File> splitPaths(String searchPath) { private static List<File> splitPaths(String searchPath) {
var result = new ArrayList<File>(); var result = new ArrayList<File>();
@ -46,10 +47,8 @@ public final class LspModuleClassLoader extends ByteBufferDexClassLoader {
private LspModuleClassLoader(ByteBuffer[] dexBuffers, private LspModuleClassLoader(ByteBuffer[] dexBuffers,
ClassLoader parent, ClassLoader parent,
String apk, String apk) {
String librarySearchPath) {
super(dexBuffers, parent); super(dexBuffers, parent);
nativeLibraryDirs = initNativeLibraryDirs(librarySearchPath);
this.apk = apk; this.apk = apk;
} }
@ -59,15 +58,13 @@ public final class LspModuleClassLoader extends ByteBufferDexClassLoader {
ClassLoader parent, ClassLoader parent,
String apk) { String apk) {
super(dexBuffers, librarySearchPath, parent); super(dexBuffers, librarySearchPath, parent);
nativeLibraryDirs = initNativeLibraryDirs(librarySearchPath); initNativeLibraryDirs(librarySearchPath);
this.apk = apk; this.apk = apk;
} }
private File[] initNativeLibraryDirs(String librarySearchPath) { private void initNativeLibraryDirs(String librarySearchPath) {
var searchPaths = new ArrayList<File>(); nativeLibraryDirs.addAll(splitPaths(librarySearchPath));
searchPaths.addAll(splitPaths(librarySearchPath)); nativeLibraryDirs.addAll(systemNativeLibraryDirs);
searchPaths.addAll(splitPaths(System.getProperty("java.library.path")));
return searchPaths.toArray(new File[0]);
} }
@Override @Override
@ -174,9 +171,15 @@ public final class LspModuleClassLoader extends ByteBufferDexClassLoader {
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {
if (apk == null) {
return "LspModuleClassLoader[instantiating]";
}
var nativeLibraryDirsString = nativeLibraryDirs.stream()
.map(File::getPath)
.collect(Collectors.joining(", "));
return "LspModuleClassLoader[" + return "LspModuleClassLoader[" +
"module=" + apk + "," + "module=" + apk + ", " +
"nativeLibraryDirs=" + Arrays.toString(nativeLibraryDirs) + "," + "nativeLibraryDirs=" + nativeLibraryDirsString + ", " +
super.toString() + "]"; super.toString() + "]";
} }
@ -192,16 +195,12 @@ public final class LspModuleClassLoader extends ByteBufferDexClassLoader {
return null; return null;
} }
}).filter(Objects::nonNull).toArray(ByteBuffer[]::new); }).filter(Objects::nonNull).toArray(ByteBuffer[]::new);
if (dexBuffers == null) {
XposedBridge.log("Failed to load dex from daemon, falling back to PathDexClassloader");
return new DelegateLastClassLoader(apk, librarySearchPath, parent);
}
LspModuleClassLoader cl; LspModuleClassLoader cl;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
cl = new LspModuleClassLoader(dexBuffers, librarySearchPath, cl = new LspModuleClassLoader(dexBuffers, librarySearchPath, parent, apk);
parent, apk);
} else { } else {
cl = new LspModuleClassLoader(dexBuffers, parent, apk, librarySearchPath); cl = new LspModuleClassLoader(dexBuffers, parent, apk);
cl.initNativeLibraryDirs(librarySearchPath);
} }
Arrays.stream(dexBuffers).parallel().forEach(SharedMemory::unmap); Arrays.stream(dexBuffers).parallel().forEach(SharedMemory::unmap);
dexes.stream().parallel().forEach(SharedMemory::close); dexes.stream().parallel().forEach(SharedMemory::close);