Skip to content

Commit 76c0d30

Browse files
- Fixed bug in loadMethods()
- Implemented loadClasses() (bug) - Implemented autoload()
1 parent 71f2ae9 commit 76c0d30

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

app/src/main/java/com/chars/android_hotpatch/Hotpatch.java

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import android.util.Log;
55

66
import java.io.File;
7+
import java.io.IOException;
78
import java.lang.reflect.Field;
89
import java.lang.reflect.InvocationTargetException;
910
import java.lang.reflect.Method;
11+
import java.util.ArrayList;
12+
import java.util.Enumeration;
1013
import java.util.HashMap;
1114
import java.util.Set;
1215

1316
import dalvik.system.DexClassLoader;
17+
import dalvik.system.DexFile;
1418

1519
/**
1620
Created by chars on 04/04/17.
@@ -76,14 +80,14 @@ public void loadLibrary(String jarpath, Context context) {
7680
File optimizedLibrarypath = context.getDir("dex", 0);
7781

7882
dexClassLoader = new DexClassLoader(jarpath, optimizedLibrarypath.getAbsolutePath(),
79-
null, context.getClassLoader());
83+
null, context.getClassLoader());
8084

8185
}
8286

8387
@SuppressWarnings("unchecked")
8488
public void loadClass(String className) throws ClassNotFoundException,
85-
IllegalAccessException,
86-
InstantiationException {
89+
IllegalAccessException,
90+
InstantiationException {
8791
if (classLoaded == null)
8892
classLoaded = new HashMap<>();
8993

@@ -92,6 +96,8 @@ public void loadClass(String className) throws ClassNotFoundException,
9296
return;
9397
}
9498

99+
Log.d(TAG, "Loading class " + className);
100+
95101
Class<Object> tmpClass = (Class<Object>)dexClassLoader.loadClass(className);
96102
classLoaded.put(className, tmpClass);
97103

@@ -100,10 +106,31 @@ public void loadClass(String className) throws ClassNotFoundException,
100106

101107
Object tmpClassInstance = tmpClass.newInstance();
102108
classInstance.put(className, tmpClassInstance);
109+
110+
103111
}
104112

105-
public void loadClasses() {
106-
/* TODO */
113+
public void loadClasses() throws IOException,
114+
ClassNotFoundException,
115+
IllegalAccessException,
116+
InstantiationException {
117+
DexFile dexFile = DexFile.loadDex(this.jarpath, File.createTempFile("opt", "dex", this.context.getCacheDir()).getPath(),
118+
0);
119+
120+
121+
String className = "";
122+
for(Enumeration<String> classNames = dexFile.entries(); classNames.hasMoreElements();
123+
className = classNames.nextElement()) {
124+
125+
if(!className.isEmpty()) {
126+
127+
//Remove partial classes <class>$1..
128+
if(className.contains("$"))
129+
className = className.substring(0, className.indexOf("$"));
130+
131+
this.loadClass(className);
132+
}
133+
}
107134
}
108135

109136
public void loadFields(String className) {
@@ -119,7 +146,7 @@ public void loadFields(String className) {
119146

120147
}
121148

122-
public void loadMethods(String className, String methods[]) throws NoSuchMethodException {
149+
public void loadMethods(String className, String methods[], Class<?>... parameterTypes) throws NoSuchMethodException {
123150
if (!classLoaded.containsKey(className))
124151
throw new IllegalArgumentException("Class " + className + " is not loaded");
125152

@@ -128,14 +155,14 @@ public void loadMethods(String className, String methods[]) throws NoSuchMethodE
128155

129156
for (String methodName: methods) {
130157
Log.d(TAG, "Loading method " + className + "." + methodName);
131-
Method tmpMethod = classLoaded.get(className).getMethod(methodName);
158+
Method tmpMethod = classLoaded.get(className).getMethod(methodName, parameterTypes);
132159

133160
method.put(className + ":" + methodName, tmpMethod);
134161
}
135162
}
136163

137164
public void loadMethods(String className) {
138-
/* FIX: java.lang.NoSuchMethodException: equals [] */
165+
/* TOOD: fix java.lang.NoSuchMethodException: equals [] */
139166

140167
if (!classLoaded.containsKey(className))
141168
throw new IllegalArgumentException("Class " + className + " is not loaded");
@@ -153,7 +180,7 @@ public void loadMethods(String className) {
153180
}
154181

155182
public Object call(String className, String methodName, Object... args) throws IllegalAccessException,
156-
InvocationTargetException {
183+
InvocationTargetException {
157184

158185
if (!this.method.containsKey(className + ":" + methodName))
159186
throw new IllegalArgumentException("No such method " + methodName + " for class " + className);
@@ -169,7 +196,7 @@ public Object call(String className, String methodName, Object... args) throws I
169196

170197

171198
public void reload() throws InstantiationException, IllegalAccessException, ClassNotFoundException,
172-
NoSuchMethodException {
199+
NoSuchMethodException {
173200
this.loadLibrary(jarpath, context);
174201

175202
Set<String> methodNames = method.keySet();
@@ -186,16 +213,13 @@ public void reload() throws InstantiationException, IllegalAccessException, Clas
186213

187214
}
188215

189-
public void autoload() {
190-
if (this.classLoaded == null)
191-
throw new RuntimeException("You must load classes first!");
192-
193-
Set<String> classNames = this.classLoaded.keySet();
194-
195-
for (String className : classNames) {
196-
this.loadMethods(className);
197-
this.loadFields(className);
198-
}
216+
public void autoload() throws IOException,
217+
ClassNotFoundException,
218+
IllegalAccessException,
219+
InstantiationException {
199220

221+
this.loadClasses();
222+
//load methods
223+
//load fields
200224
}
201225
}

0 commit comments

Comments
 (0)