Skip to content

Commit 414a343

Browse files
Added:
- loadFields - loadMethods (bug) - autoload
1 parent b024f36 commit 414a343

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.util.Log;
55

66
import java.io.File;
7+
import java.lang.reflect.Field;
78
import java.lang.reflect.InvocationTargetException;
89
import java.lang.reflect.Method;
910
import java.util.HashMap;
@@ -38,19 +39,23 @@ of this software and associated documentation files (the "Software"), to deal
3839
*/
3940

4041
public class Hotpatch {
41-
Context context;
42-
String jarpath;
43-
DexClassLoader dexClassLoader;
44-
HashMap<String, Class<Object> > classLoaded;
45-
HashMap<String, Object > classInstance;
46-
HashMap<String, Method> method;
42+
public static final String TAG = "AndroidHotpatch";
43+
44+
private Context context;
45+
private String jarpath;
46+
private DexClassLoader dexClassLoader;
47+
private HashMap<String, Class<Object> > classLoaded;
48+
private HashMap<String, Object> classInstance;
49+
private HashMap<String, Field> fields;
50+
private HashMap<String, Method> method;
4751

4852
public void Hotpatch() {
4953
jarpath = null;
5054
dexClassLoader = null;
51-
classLoaded = new HashMap<>();
52-
classInstance = new HashMap<>();
53-
method = new HashMap<>();
55+
classLoaded = null;
56+
classInstance = null;
57+
method = null;
58+
fields = null;
5459

5560
}
5661

@@ -83,34 +88,70 @@ public void loadClass(String className) throws ClassNotFoundException,
8388
classLoaded = new HashMap<>();
8489

8590
if (classLoaded.containsKey(className)) {
86-
Log.d("AndroidHotpatch", "Class " + className + " is already loaded");
91+
Log.d(TAG, "Class " + className + " is already loaded");
8792
return;
8893
}
8994

9095
Class<Object> tmpClass = (Class<Object>)dexClassLoader.loadClass(className);
9196
classLoaded.put(className, tmpClass);
9297

93-
Object tmpClassInstance = tmpClass.newInstance();
9498
if(classInstance == null)
9599
classInstance = new HashMap<>();
96100

101+
Object tmpClassInstance = tmpClass.newInstance();
97102
classInstance.put(className, tmpClassInstance);
98103
}
99104

105+
public void loadClasses() {
106+
/* TODO */
107+
}
108+
109+
public void loadFields(String className) {
110+
Field fields[] = classLoaded.get(className).getFields();
111+
112+
if (this.fields == null)
113+
this.fields = new HashMap<>();
114+
115+
for (Field field : fields) {
116+
Log.d(TAG, "Field " + field.getName() + ": " + field.toGenericString());
117+
this.fields.put(className + ":" + field.getName(), field);
118+
}
119+
120+
}
121+
100122
public void loadMethods(String className, String methods[]) throws NoSuchMethodException {
101123
if (!classLoaded.containsKey(className))
102124
throw new IllegalArgumentException("Class " + className + " is not loaded");
103125

104-
if(this.method == null)
126+
if (this.method == null)
105127
this.method = new HashMap<>();
106128

107129
for (String methodName: methods) {
130+
Log.d(TAG, "Loading method " + className + "." + methodName);
108131
Method tmpMethod = classLoaded.get(className).getMethod(methodName);
109132

110133
method.put(className + ":" + methodName, tmpMethod);
111134
}
112135
}
113136

137+
public void loadMethods(String className) {
138+
/* FIX: java.lang.NoSuchMethodException: equals [] */
139+
140+
if (!classLoaded.containsKey(className))
141+
throw new IllegalArgumentException("Class " + className + " is not loaded");
142+
143+
if (this.method == null)
144+
this.method = new HashMap<>();
145+
146+
Method methods[] = classLoaded.get(className).getMethods();
147+
148+
for (Method method : methods) {
149+
String name = method.getName();
150+
151+
this.method.put(className + ":" + name, method);
152+
}
153+
}
154+
114155
public Object call(String className, String methodName, Object... args) throws IllegalAccessException,
115156
InvocationTargetException {
116157

@@ -144,4 +185,17 @@ public void reload() throws InstantiationException, IllegalAccessException, Clas
144185
}
145186

146187
}
188+
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+
}
199+
200+
}
147201
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,5 @@ public void onClick(View v) {
7777
}
7878
}
7979
});
80-
8180
}
8281
}

0 commit comments

Comments
 (0)