mirror of
https://github.com/revanced/multidexlib2.git
synced 2025-05-05 08:54:31 +02:00
Make WrappingMultiDexFile generic
This commit is contained in:
parent
9362e6bd47
commit
2a79c6d3e0
@ -18,13 +18,14 @@ import org.jf.dexlib2.iface.DexFile;
|
|||||||
import org.jf.dexlib2.iface.MultiDexContainer;
|
import org.jf.dexlib2.iface.MultiDexContainer;
|
||||||
import org.jf.dexlib2.iface.MultiDexContainer.MultiDexFile;
|
import org.jf.dexlib2.iface.MultiDexContainer.MultiDexFile;
|
||||||
|
|
||||||
public class BasicMultiDexFile<T extends MultiDexContainer<? extends MultiDexFile>> implements WrappingMultiDexFile {
|
public class BasicMultiDexFile<C extends MultiDexContainer<? extends MultiDexFile>, D extends DexFile>
|
||||||
|
implements WrappingMultiDexFile<D> {
|
||||||
|
|
||||||
private final T container;
|
private final C container;
|
||||||
private final String entryName;
|
private final String entryName;
|
||||||
private final DexFile dexFile;
|
private final D dexFile;
|
||||||
|
|
||||||
public BasicMultiDexFile(T container, String entryName, DexFile dexFile) {
|
public BasicMultiDexFile(C container, String entryName, D dexFile) {
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.entryName = entryName;
|
this.entryName = entryName;
|
||||||
this.dexFile = dexFile;
|
this.dexFile = dexFile;
|
||||||
@ -46,12 +47,12 @@ public class BasicMultiDexFile<T extends MultiDexContainer<? extends MultiDexFil
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getContainer() {
|
public C getContainer() {
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DexFile getWrappedDexFile() {
|
public D getWrappedDexFile() {
|
||||||
return dexFile;
|
return dexFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,19 +16,19 @@ import java.util.Map;
|
|||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import org.jf.dexlib2.Opcodes;
|
import org.jf.dexlib2.Opcodes;
|
||||||
import org.jf.dexlib2.iface.DexFile;
|
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
|
||||||
|
|
||||||
public class DirectoryDexContainer extends AbstractMultiDexContainer<WrappingMultiDexFile> {
|
public class DirectoryDexContainer extends AbstractMultiDexContainer<WrappingMultiDexFile<DexBackedDexFile>> {
|
||||||
|
|
||||||
public DirectoryDexContainer(File directory, DexFileNamer namer, Opcodes opcodes) throws IOException {
|
public DirectoryDexContainer(File directory, DexFileNamer namer, Opcodes opcodes) throws IOException {
|
||||||
Map<String, WrappingMultiDexFile> entryMap = new TreeMap<>(new DexFileNameComparator(namer));
|
Map<String, WrappingMultiDexFile<DexBackedDexFile>> entryMap = new TreeMap<>(new DexFileNameComparator(namer));
|
||||||
String[] names = directory.list();
|
String[] names = directory.list();
|
||||||
if (names == null) throw new IOException("Cannot access directory: " + directory);
|
if (names == null) throw new IOException("Cannot access directory: " + directory);
|
||||||
for (String entryName : names) {
|
for (String entryName : names) {
|
||||||
File file = new File(directory, entryName);
|
File file = new File(directory, entryName);
|
||||||
if (file.isFile() && namer.isValidName(entryName)) {
|
if (file.isFile() && namer.isValidName(entryName)) {
|
||||||
DexFile dexFile = RawDexIO.readRawDexFile(file, opcodes);
|
DexBackedDexFile dexFile = RawDexIO.readRawDexFile(file, opcodes);
|
||||||
WrappingMultiDexFile multiDexFile = new BasicMultiDexFile<>(this, entryName, dexFile);
|
WrappingMultiDexFile<DexBackedDexFile> multiDexFile = new BasicMultiDexFile<>(this, entryName, dexFile);
|
||||||
if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName);
|
if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ public class MultiDexIO {
|
|||||||
if (file.isDirectory()) return new DirectoryDexContainer(file, namer, opcodes);
|
if (file.isDirectory()) return new DirectoryDexContainer(file, namer, opcodes);
|
||||||
if (!file.isFile()) throw new FileNotFoundException(file.toString());
|
if (!file.isFile()) throw new FileNotFoundException(file.toString());
|
||||||
if (ZipFileDexContainer.isZipFile(file)) return new ZipFileDexContainer(file, namer, opcodes);
|
if (ZipFileDexContainer.isZipFile(file)) return new ZipFileDexContainer(file, namer, opcodes);
|
||||||
return new SingletonDexContainer(RawDexIO.readRawDexFile(file, opcodes));
|
return new SingletonDexContainer<>(RawDexIO.readRawDexFile(file, opcodes));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
|
@ -16,7 +16,7 @@ import java.util.Map;
|
|||||||
import org.jf.dexlib2.Opcodes;
|
import org.jf.dexlib2.Opcodes;
|
||||||
import org.jf.dexlib2.iface.DexFile;
|
import org.jf.dexlib2.iface.DexFile;
|
||||||
|
|
||||||
public class SingletonDexContainer extends AbstractMultiDexContainer<WrappingMultiDexFile> {
|
public class SingletonDexContainer<D extends DexFile> extends AbstractMultiDexContainer<WrappingMultiDexFile<D>> {
|
||||||
|
|
||||||
// I insist that some dex container entries do not have names
|
// I insist that some dex container entries do not have names
|
||||||
// even though dexlib2 does not allow null entry names.
|
// even though dexlib2 does not allow null entry names.
|
||||||
@ -26,14 +26,14 @@ public class SingletonDexContainer extends AbstractMultiDexContainer<WrappingMul
|
|||||||
// using the '==' operator rather than the 'equals(...)' method.
|
// using the '==' operator rather than the 'equals(...)' method.
|
||||||
public static final String UNDEFINED_ENTRY_NAME = null;
|
public static final String UNDEFINED_ENTRY_NAME = null;
|
||||||
|
|
||||||
public SingletonDexContainer(DexFile dexFile) {
|
public SingletonDexContainer(D dexFile) {
|
||||||
this(UNDEFINED_ENTRY_NAME, dexFile);
|
this(UNDEFINED_ENTRY_NAME, dexFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SingletonDexContainer(String entryName, DexFile dexFile) {
|
public SingletonDexContainer(String entryName, D dexFile) {
|
||||||
Opcodes opcodes = dexFile.getOpcodes();
|
Opcodes opcodes = dexFile.getOpcodes();
|
||||||
WrappingMultiDexFile multiDexFile = new BasicMultiDexFile<>(this, entryName, dexFile);
|
WrappingMultiDexFile<D> multiDexFile = new BasicMultiDexFile<>(this, entryName, dexFile);
|
||||||
Map<String, WrappingMultiDexFile> entryMap = Collections.singletonMap(entryName, multiDexFile);
|
Map<String, WrappingMultiDexFile<D>> entryMap = Collections.singletonMap(entryName, multiDexFile);
|
||||||
initialize(entryMap, opcodes);
|
initialize(entryMap, opcodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ package lanchon.multidexlib2;
|
|||||||
import org.jf.dexlib2.iface.DexFile;
|
import org.jf.dexlib2.iface.DexFile;
|
||||||
import org.jf.dexlib2.iface.MultiDexContainer.MultiDexFile;
|
import org.jf.dexlib2.iface.MultiDexContainer.MultiDexFile;
|
||||||
|
|
||||||
public interface WrappingMultiDexFile extends MultiDexFile {
|
public interface WrappingMultiDexFile<D extends DexFile> extends MultiDexFile {
|
||||||
|
|
||||||
DexFile getWrappedDexFile();
|
D getWrappedDexFile();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,9 @@ import java.util.zip.ZipEntry;
|
|||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
import org.jf.dexlib2.Opcodes;
|
import org.jf.dexlib2.Opcodes;
|
||||||
import org.jf.dexlib2.iface.DexFile;
|
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
|
||||||
|
|
||||||
public class ZipFileDexContainer extends AbstractMultiDexContainer<WrappingMultiDexFile> {
|
public class ZipFileDexContainer extends AbstractMultiDexContainer<WrappingMultiDexFile<DexBackedDexFile>> {
|
||||||
|
|
||||||
public static boolean isZipFile(File zip) {
|
public static boolean isZipFile(File zip) {
|
||||||
if (!zip.isFile()) return false;
|
if (!zip.isFile()) return false;
|
||||||
@ -36,7 +36,7 @@ public class ZipFileDexContainer extends AbstractMultiDexContainer<WrappingMulti
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ZipFileDexContainer(File zip, DexFileNamer namer, Opcodes opcodes) throws IOException {
|
public ZipFileDexContainer(File zip, DexFileNamer namer, Opcodes opcodes) throws IOException {
|
||||||
Map<String, WrappingMultiDexFile> entryMap = new TreeMap<>(new DexFileNameComparator(namer));
|
Map<String, WrappingMultiDexFile<DexBackedDexFile>> entryMap = new TreeMap<>(new DexFileNameComparator(namer));
|
||||||
ZipFile zipFile = new ZipFile(zip);
|
ZipFile zipFile = new ZipFile(zip);
|
||||||
try {
|
try {
|
||||||
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
|
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
|
||||||
@ -44,14 +44,15 @@ public class ZipFileDexContainer extends AbstractMultiDexContainer<WrappingMulti
|
|||||||
ZipEntry zipEntry = zipEntries.nextElement();
|
ZipEntry zipEntry = zipEntries.nextElement();
|
||||||
String entryName = zipEntry.getName();
|
String entryName = zipEntry.getName();
|
||||||
if (namer.isValidName(entryName)) {
|
if (namer.isValidName(entryName)) {
|
||||||
DexFile dexFile;
|
DexBackedDexFile dexFile;
|
||||||
InputStream inputStream = zipFile.getInputStream(zipEntry);
|
InputStream inputStream = zipFile.getInputStream(zipEntry);
|
||||||
try {
|
try {
|
||||||
dexFile = RawDexIO.readRawDexFile(inputStream, zipEntry.getSize(), opcodes);
|
dexFile = RawDexIO.readRawDexFile(inputStream, zipEntry.getSize(), opcodes);
|
||||||
} finally {
|
} finally {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
}
|
}
|
||||||
WrappingMultiDexFile multiDexFile = new BasicMultiDexFile<>(this, entryName, dexFile);
|
WrappingMultiDexFile<DexBackedDexFile> multiDexFile =
|
||||||
|
new BasicMultiDexFile<>(this, entryName, dexFile);
|
||||||
if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName);
|
if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user