|
1 | 1 | package com.blankj.bus; |
2 | 2 |
|
3 | 3 |
|
4 | | -import com.blankj.util.JsonUtils; |
5 | | -import com.blankj.utilcode.util.BusUtils; |
6 | | - |
7 | | -import org.apache.commons.io.FileUtils; |
8 | | -import org.junit.Test; |
9 | | - |
10 | | -import java.io.File; |
11 | | -import java.util.Collection; |
12 | | -import java.util.HashMap; |
13 | | -import java.util.function.BiConsumer; |
14 | | - |
15 | | -import javassist.ClassPool; |
16 | | -import javassist.CtClass; |
17 | | -import javassist.CtField; |
18 | | -import javassist.CtMethod; |
19 | | -import javassist.CtNewMethod; |
20 | | -import javassist.Modifier; |
21 | | -import javassist.NotFoundException; |
22 | | - |
23 | 4 | /** |
24 | 5 | * <pre> |
25 | 6 | * author: Blankj |
|
30 | 11 | */ |
31 | 12 | public class BusTest { |
32 | 13 |
|
33 | | - @Test |
34 | | - public void test() throws Exception { |
35 | | - String rootPath = "/Users/blankj/Repo/AndroidUtilCode/app/build/intermediates/transforms/busTransform/debug/43/"; |
36 | | - File root = new File(rootPath); |
37 | | - ClassPool mPool = new ClassPool(null); |
38 | | - mPool.appendSystemPath(); |
39 | | - mPool.appendClassPath(rootPath); |
40 | | - mPool.appendClassPath("/Users/blankj/Library/Android/sdk/platforms/android-27/android.jar"); |
41 | | - HashMap<String, String> busMap = new HashMap<>(); |
42 | | - if (root.isDirectory()) { |
43 | | - Collection<File> files = FileUtils.listFiles(root, new String[]{"class"}, true); |
44 | | - |
45 | | - for (File file : files) { |
46 | | - String fileName = file.getName(); |
47 | | - |
48 | | - String filePath = file.getCanonicalPath(); |
49 | | - String packagePath = filePath.replace(rootPath, ""); |
50 | | - String className = packagePath.replace(System.getProperty("file.separator"), "."); |
51 | | - // delete .class |
52 | | - className = className.substring(0, className.length() - 6); |
53 | | - |
54 | | - CtClass ctClass = mPool.get(className); |
55 | | - |
56 | | - CtMethod[] methods = ctClass.getDeclaredMethods(); |
57 | | - for (CtMethod method : methods) { |
58 | | - if (method.hasAnnotation(BusUtils.Subscribe.class)) { |
59 | | - String name = ((BusUtils.Subscribe) method.getAnnotation(BusUtils.Subscribe.class)).name(); |
60 | | - if (busMap.containsKey(name)) { |
61 | | - System.out.println("bus of " + name + " has registered." + method.getLongName()); |
62 | | - continue; |
63 | | - } |
64 | | - String longMethodName = method.getLongName(); |
65 | | - if (Modifier.isStatic(method.getModifiers())) { |
66 | | - String sign = method.getReturnType().getName() + ' ' + longMethodName; |
67 | | - busMap.put(name, sign); |
68 | | - } else {// may be is kotlin |
69 | | - processKt(mPool, busMap, method, name, longMethodName); |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - System.out.println(JsonUtils.getFormatJson(busMap)); |
75 | | - } |
76 | | - CtClass ctClass = mPool.makeClass("com.blankj.bus.BusUtils"); |
77 | | - |
78 | | - String src = "" + |
79 | | - "public static Object post(String name, Object[] objects) {\n" + |
80 | | - " if (name == null || name.length() == 0) return null;\n" + |
81 | | - " return null;\n" + |
82 | | - "}"; |
83 | | - |
84 | | - CtMethod make = CtNewMethod.make(src, ctClass); |
85 | | - ctClass.addMethod(make); |
86 | | - make.insertAfter(getInsertContent(busMap)); |
87 | | - ctClass.debugWriteFile(); |
88 | | - |
89 | | - } |
90 | | - |
91 | | - private void processKt(ClassPool mPool, |
92 | | - HashMap<String, String> busMap, |
93 | | - CtMethod method, String name, |
94 | | - String longMethodName) throws NotFoundException { |
95 | | - CtClass innerClass = method.getDeclaringClass(); |
96 | | - try { |
97 | | - CtField instance = innerClass.getField("INSTANCE"); |
98 | | - System.out.println("find INSTANCE: " + name + ": " + longMethodName); |
99 | | - int i = longMethodName.lastIndexOf('('); |
100 | | - String temp = longMethodName.substring(0, i); |
101 | | - int j = temp.lastIndexOf('.'); |
102 | | - String sign = method.getReturnType().getName() + ' ' |
103 | | - + longMethodName.substring(0, j) |
104 | | - + ".INSTANCE" |
105 | | - + longMethodName.substring(j); |
106 | | - System.out.println(sign); |
107 | | - busMap.put(name, sign); |
108 | | - } catch (NotFoundException ignore) { |
109 | | - String innerClassSimpleName = innerClass.getSimpleName(); |
110 | | - if (innerClassSimpleName.contains("$") && !innerClassSimpleName.endsWith("$")) { |
111 | | - String innerClassName = innerClass.getName(); |
112 | | - String outerClassName = innerClassName.substring(0, innerClassName.lastIndexOf('$')); |
113 | | - CtClass outerClass = mPool.get(outerClassName); |
114 | | - try { |
115 | | - CtField ctField = outerClass.getField(innerClassSimpleName.substring(innerClassSimpleName.lastIndexOf('$') + 1)); |
116 | | - String fieldName = ctField.getName(); |
117 | | - String methodName = longMethodName.replace("$" + fieldName, "." + fieldName); |
118 | | - String sign = method.getReturnType().getName() + ' ' + methodName; |
119 | | - busMap.put(name, sign); |
120 | | - } catch (NotFoundException e) { |
121 | | - System.out.println(longMethodName + "is not static"); |
122 | | - } |
123 | | - } else { |
124 | | - System.out.println(longMethodName + "is not static"); |
125 | | - } |
126 | | - } |
127 | | - } |
128 | | - |
129 | | - private static String getInsertContent(HashMap<String, String> bus) { |
130 | | - final StringBuilder sb = new StringBuilder(); |
131 | | - bus.forEach(new BiConsumer<String, String>() { |
132 | | - @Override |
133 | | - public void accept(String name, String sign) { |
134 | | - String[] method = sign.split(" "); |
135 | | - String returnType = method[0]; |
136 | | - String methodName = method[1]; |
137 | | - |
138 | | - sb.append("if (\"").append(name).append("\".equals($1)) {\n"); |
139 | | - |
140 | | - int st = methodName.indexOf('('); |
141 | | - int end = methodName.length(); |
142 | | - String params = methodName.substring(st + 1, end - 1); |
143 | | - if (!params.equals("")) { |
144 | | - String[] paramArr = params.split(","); |
145 | | - |
146 | | - StringBuilder args = new StringBuilder(); |
147 | | - for (int i = 0; i < paramArr.length; i++) { |
148 | | - if (paramArr[i].equals("char")) { |
149 | | - args.append(",$2[").append(i).append("].toString().charAt(0)"); |
150 | | - } else if (paramArr[i].equals("boolean")) { |
151 | | - args.append(",Boolean.parseBoolean($2[").append(i).append("].toString())"); |
152 | | - } else if (paramArr[i].equals("byte")) { |
153 | | - args.append(",Byte.parseByte($2[").append(i).append("].toString())"); |
154 | | - } else if (paramArr[i].equals("short")) { |
155 | | - args.append(",Short.parseShort($2[").append(i).append("].toString())"); |
156 | | - } else if (paramArr[i].equals("int")) { |
157 | | - args.append(",Integer.parseInt($2[").append(i).append("].toString())"); |
158 | | - } else if (paramArr[i].equals("long")) { |
159 | | - args.append(",Long.parseLong($2[").append(i).append("].toString())"); |
160 | | - } else if (paramArr[i].equals("float")) { |
161 | | - args.append(",Float.parseFloat($2[").append(i).append("].toString())"); |
162 | | - } else if (paramArr[i].equals("double")) { |
163 | | - args.append(",Double.parseDouble($2[").append(i).append("].toString())"); |
164 | | - } else { |
165 | | - args.append(",(").append(paramArr[i]).append(")$2[").append(i).append("]"); |
166 | | - } |
167 | | - } |
168 | | - methodName = methodName.substring(0, st + 1) + args.substring(1) + ")"; |
169 | | - } |
170 | | - |
171 | | - if (returnType.equals("void")) { |
172 | | - sb.append(methodName).append(";\n").append("return null;\n"); |
173 | | - } else { |
174 | | - sb.append("return ($w)").append(methodName).append(";\n"); |
175 | | - } |
176 | | - sb.append("}"); |
177 | | - } |
178 | | - }); |
179 | | - return sb.toString(); |
180 | | - } |
| 14 | +// @Test |
| 15 | +// public void test() throws Exception { |
| 16 | +// String rootPath = "/Users/blankj/Repo/AndroidUtilCode/app/build/intermediates/transforms/busTransform/debug/43/"; |
| 17 | +// File root = new File(rootPath); |
| 18 | +// ClassPool mPool = new ClassPool(null); |
| 19 | +// mPool.appendSystemPath(); |
| 20 | +// mPool.appendClassPath(rootPath); |
| 21 | +// mPool.appendClassPath("/Users/blankj/Library/Android/sdk/platforms/android-27/android.jar"); |
| 22 | +// HashMap<String, String> busMap = new HashMap<>(); |
| 23 | +// if (root.isDirectory()) { |
| 24 | +// Collection<File> files = FileUtils.listFiles(root, new String[]{"class"}, true); |
| 25 | +// |
| 26 | +// for (File file : files) { |
| 27 | +// String fileName = file.getName(); |
| 28 | +// |
| 29 | +// String filePath = file.getCanonicalPath(); |
| 30 | +// String packagePath = filePath.replace(rootPath, ""); |
| 31 | +// String className = packagePath.replace(System.getProperty("file.separator"), "."); |
| 32 | +// // delete .class |
| 33 | +// className = className.substring(0, className.length() - 6); |
| 34 | +// |
| 35 | +// CtClass ctClass = mPool.get(className); |
| 36 | +// |
| 37 | +// CtMethod[] methods = ctClass.getDeclaredMethods(); |
| 38 | +// for (CtMethod method : methods) { |
| 39 | +// if (method.hasAnnotation(BusUtils.Subscribe.class)) { |
| 40 | +// String name = ((BusUtils.Subscribe) method.getAnnotation(BusUtils.Subscribe.class)).name(); |
| 41 | +// if (busMap.containsKey(name)) { |
| 42 | +// System.out.println("bus of " + name + " has registered." + method.getLongName()); |
| 43 | +// continue; |
| 44 | +// } |
| 45 | +// String longMethodName = method.getLongName(); |
| 46 | +// if (Modifier.isStatic(method.getModifiers())) { |
| 47 | +// String sign = method.getReturnType().getName() + ' ' + longMethodName; |
| 48 | +// busMap.put(name, sign); |
| 49 | +// } else {// may be is kotlin |
| 50 | +// processKt(mPool, busMap, method, name, longMethodName); |
| 51 | +// } |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +// System.out.println(JsonUtils.getFormatJson(busMap)); |
| 56 | +// } |
| 57 | +// CtClass ctClass = mPool.makeClass("com.blankj.bus.BusUtils"); |
| 58 | +// |
| 59 | +// String src = "" + |
| 60 | +// "public static Object post(String name, Object[] objects) {\n" + |
| 61 | +// " if (name == null || name.length() == 0) return null;\n" + |
| 62 | +// " return null;\n" + |
| 63 | +// "}"; |
| 64 | +// |
| 65 | +// CtMethod make = CtNewMethod.make(src, ctClass); |
| 66 | +// ctClass.addMethod(make); |
| 67 | +// make.insertAfter(getInsertContent(busMap)); |
| 68 | +// ctClass.debugWriteFile(); |
| 69 | +// |
| 70 | +// } |
| 71 | +// |
| 72 | +// private void processKt(ClassPool mPool, |
| 73 | +// HashMap<String, String> busMap, |
| 74 | +// CtMethod method, String name, |
| 75 | +// String longMethodName) throws NotFoundException { |
| 76 | +// CtClass innerClass = method.getDeclaringClass(); |
| 77 | +// try { |
| 78 | +// CtField instance = innerClass.getField("INSTANCE"); |
| 79 | +// System.out.println("find INSTANCE: " + name + ": " + longMethodName); |
| 80 | +// int i = longMethodName.lastIndexOf('('); |
| 81 | +// String temp = longMethodName.substring(0, i); |
| 82 | +// int j = temp.lastIndexOf('.'); |
| 83 | +// String sign = method.getReturnType().getName() + ' ' |
| 84 | +// + longMethodName.substring(0, j) |
| 85 | +// + ".INSTANCE" |
| 86 | +// + longMethodName.substring(j); |
| 87 | +// System.out.println(sign); |
| 88 | +// busMap.put(name, sign); |
| 89 | +// } catch (NotFoundException ignore) { |
| 90 | +// String innerClassSimpleName = innerClass.getSimpleName(); |
| 91 | +// if (innerClassSimpleName.contains("$") && !innerClassSimpleName.endsWith("$")) { |
| 92 | +// String innerClassName = innerClass.getName(); |
| 93 | +// String outerClassName = innerClassName.substring(0, innerClassName.lastIndexOf('$')); |
| 94 | +// CtClass outerClass = mPool.get(outerClassName); |
| 95 | +// try { |
| 96 | +// CtField ctField = outerClass.getField(innerClassSimpleName.substring(innerClassSimpleName.lastIndexOf('$') + 1)); |
| 97 | +// String fieldName = ctField.getName(); |
| 98 | +// String methodName = longMethodName.replace("$" + fieldName, "." + fieldName); |
| 99 | +// String sign = method.getReturnType().getName() + ' ' + methodName; |
| 100 | +// busMap.put(name, sign); |
| 101 | +// } catch (NotFoundException e) { |
| 102 | +// System.out.println(longMethodName + "is not static"); |
| 103 | +// } |
| 104 | +// } else { |
| 105 | +// System.out.println(longMethodName + "is not static"); |
| 106 | +// } |
| 107 | +// } |
| 108 | +// } |
| 109 | +// |
| 110 | +// private static String getInsertContent(HashMap<String, String> bus) { |
| 111 | +// final StringBuilder sb = new StringBuilder(); |
| 112 | +// bus.forEach(new BiConsumer<String, String>() { |
| 113 | +// @Override |
| 114 | +// public void accept(String name, String sign) { |
| 115 | +// String[] method = sign.split(" "); |
| 116 | +// String returnType = method[0]; |
| 117 | +// String methodName = method[1]; |
| 118 | +// |
| 119 | +// sb.append("if (\"").append(name).append("\".equals($1)) {\n"); |
| 120 | +// |
| 121 | +// int st = methodName.indexOf('('); |
| 122 | +// int end = methodName.length(); |
| 123 | +// String params = methodName.substring(st + 1, end - 1); |
| 124 | +// if (!params.equals("")) { |
| 125 | +// String[] paramArr = params.split(","); |
| 126 | +// |
| 127 | +// StringBuilder args = new StringBuilder(); |
| 128 | +// for (int i = 0; i < paramArr.length; i++) { |
| 129 | +// if (paramArr[i].equals("char")) { |
| 130 | +// args.append(",$2[").append(i).append("].toString().charAt(0)"); |
| 131 | +// } else if (paramArr[i].equals("boolean")) { |
| 132 | +// args.append(",Boolean.parseBoolean($2[").append(i).append("].toString())"); |
| 133 | +// } else if (paramArr[i].equals("byte")) { |
| 134 | +// args.append(",Byte.parseByte($2[").append(i).append("].toString())"); |
| 135 | +// } else if (paramArr[i].equals("short")) { |
| 136 | +// args.append(",Short.parseShort($2[").append(i).append("].toString())"); |
| 137 | +// } else if (paramArr[i].equals("int")) { |
| 138 | +// args.append(",Integer.parseInt($2[").append(i).append("].toString())"); |
| 139 | +// } else if (paramArr[i].equals("long")) { |
| 140 | +// args.append(",Long.parseLong($2[").append(i).append("].toString())"); |
| 141 | +// } else if (paramArr[i].equals("float")) { |
| 142 | +// args.append(",Float.parseFloat($2[").append(i).append("].toString())"); |
| 143 | +// } else if (paramArr[i].equals("double")) { |
| 144 | +// args.append(",Double.parseDouble($2[").append(i).append("].toString())"); |
| 145 | +// } else { |
| 146 | +// args.append(",(").append(paramArr[i]).append(")$2[").append(i).append("]"); |
| 147 | +// } |
| 148 | +// } |
| 149 | +// methodName = methodName.substring(0, st + 1) + args.substring(1) + ")"; |
| 150 | +// } |
| 151 | +// |
| 152 | +// if (returnType.equals("void")) { |
| 153 | +// sb.append(methodName).append(";\n").append("return null;\n"); |
| 154 | +// } else { |
| 155 | +// sb.append("return ($w)").append(methodName).append(";\n"); |
| 156 | +// } |
| 157 | +// sb.append("}"); |
| 158 | +// } |
| 159 | +// }); |
| 160 | +// return sb.toString(); |
| 161 | +// } |
181 | 162 | } |
0 commit comments