-
Notifications
You must be signed in to change notification settings - Fork 129
/
JWUtils.java
340 lines (317 loc) · 7.8 KB
/
JWUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package cn.wjdiankong.jw.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import android.text.TextUtils;
import android.util.Log;
public class JWUtils {
//过滤规则
public static String logSwitch = null;
public static String filterMethodName = null;
public static String filterMethodReturnType = null;
public static String filterMethodParamType = null;
public static String filterClassName = null;
public static String cmdLineStr = null;
public static long fileModifyTime = System.currentTimeMillis();
public static File file = new File("/data/local/tmp/log.txt");
public static Method[] allMethods;
static{
cmdLineStr = getCmdArgs();
parseCmdLineArgs(cmdLineStr);
fileModifyTime = file.lastModified();
}
/**
* 打印堆栈信息
* @param tag
*/
public static void printErrorStatckTrace(String tag){
if(!isShowLog()){
return;
}
StackTraceElement[] stackElements = new Throwable().getStackTrace();
if(!isFilterInfo(stackElements[1])){
return;
}
try{
throw new NullPointerException();
}catch(Exception e){
Log.d(tag, "++++++++++++++++++Start++++++++++++++++++");
Log.d(tag, ""+e.getLocalizedMessage());
Log.d(tag, "++++++++++++++++++++End++++++++++++++++++\n\n");
}
}
/**
* 打印堆栈信息
* @param tag
*/
public static void printStackTrace(String tag) {
if(!isShowLog()){
return;
}
StackTraceElement[] stackElements = new Throwable().getStackTrace();
if(!isFilterInfo(stackElements[1])){
return;
}
if(stackElements != null){
Log.d(tag, "++++++++++++++++++Start++++++++++++++++++");
for(int i = 0; i < stackElements.length; i++){
Log.d(tag, stackElements[i].toString());
}
Log.d(tag, "++++++++++++++++++++End++++++++++++++++++\n\n");
}
}
public static boolean isFilterInfo(StackTraceElement ment){
boolean isClassName = isFilterClassName(ment);
boolean isMethodName = isFilterMethodName(ment);
boolean isMethodReturnT = isFilterMethodReturnType(ment);
boolean isMethodParamT = isFilterMethodParamType(ment);
return isClassName && isMethodName && isMethodReturnT && isMethodParamT;
}
/**
* 过滤方法
* @param ment
* @return
*/
private static boolean isFilterMethodName(StackTraceElement ment){
if(TextUtils.isEmpty(filterMethodName)){
return true;
}
String methodName = ment.getMethodName();
if(TextUtils.isEmpty(methodName)){
return true;
}
if(filterMethodName.equals(methodName)){
return true;
}
return false;
}
/**
* 过滤类名
* @param ment
* @return
*/
private static boolean isFilterClassName(StackTraceElement ment){
if(TextUtils.isEmpty(filterClassName)){
return true;
}
String className = ment.getClassName();
if(TextUtils.isEmpty(filterClassName)){
return true;
}
if(filterClassName.equals(className)){
return true;
}
return false;
}
/**
* 过滤方法返回类型
* @param ment
* @return
*/
private static boolean isFilterMethodReturnType(StackTraceElement ment){
if(TextUtils.isEmpty(filterMethodReturnType)){
return true;
}
getClassAllMethod(ment);
for(Method method : allMethods){
if(method.getName().equals(ment.getMethodName())){
Class<?> returnType = method.getReturnType();
String returnTypeS = getTypeSign(returnType);
if(returnTypeS.equals(filterMethodReturnType)){
System.out.println("returntype:"+returnTypeS+","+filterMethodReturnType);
return true;
}
}
}
return false;
}
/**
* 过滤方法参数类型
* @param ment
* @return
*/
private static boolean isFilterMethodParamType(StackTraceElement ment){
if(TextUtils.isEmpty(filterMethodParamType)){
return true;
}
getClassAllMethod(ment);
for(Method method : allMethods){
if(method.getName().equals(ment.getMethodName())){
Class<?>[] paramTypes = method.getParameterTypes();
StringBuilder sb = new StringBuilder();
for(Class<?> paramT : paramTypes){
sb.append(getTypeSign(paramT)+";");
}
if(sb.toString().contains(filterMethodParamType)){
System.out.println("paramtype:"+sb.toString()+","+filterMethodParamType);
return true;
}
}
}
return false;
}
/**
* 获取类中所有的方法信息
* @param ment
*/
private static void getClassAllMethod(StackTraceElement ment){
try{
Class<?> objClass = Class.forName(ment.getClassName());
Method[] methods1 = objClass.getMethods();
Method[] methods2 = objClass.getDeclaredMethods();
allMethods = new Method[methods1.length+methods2.length];
for(int i=0;i<methods1.length;i++){
allMethods[i] = methods1[i];
}
for(int j=methods1.length;j<methods1.length+methods2.length;j++){
allMethods[j] = methods2[j-methods1.length];
}
}catch(Throwable e){
}
}
/**
* ͨ获取签名信息
* @param typeClass
* @return
*/
private static String getTypeSign(Class<?> typeClass){
if(boolean.class == typeClass){
return "Z";
}
if(byte.class == typeClass){
return "B";
}
if(short.class == typeClass){
return "S";
}
if(int.class == typeClass){
return "I";
}
if(float.class == typeClass){
return "F";
}
if(double.class == typeClass){
return "D";
}
if(long.class == typeClass){
return "J";
}
if(char.class == typeClass){
return "C";
}
String className = typeClass.getName();
if(className == null){
return "";
}
return className.replace(".", "/");
}
/**
* @return
*/
private static boolean isShowLog(){
if(fileModifyTime != file.lastModified()){
fileModifyTime = file.lastModified();
cmdLineStr = getCmdArgs();
parseCmdLineArgs(cmdLineStr);
}
if("1".equals(logSwitch)){
return true;
}
return false;
}
/**
* 获取命令行参数信息
* @return
*/
public static String getCmdArgs(){
File file = new File("/data/local/tmp/log.txt");
BufferedReader bf= null;
try {
bf= new BufferedReader(new FileReader(file));
String line = null;
while((line = bf.readLine()) != null){
if(line != null){
return line;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
if(bf != null)
bf.close();
}catch(Exception e){
}
}
return null;
}
/**
* 解析命令行参数信息
* -s 1 -m JW -r int -p [int,java.lang.String] -c JWUtils
* @param cmdLine
*/
private static void parseCmdLineArgs(String cmdLine){
try{
String[] strAry = cmdLine.split(" ");
for(int i=0;i<strAry.length;i+=2){
switch(strAry[i]){
case "-s":
logSwitch = strAry[i+1];
break;
case "-m":
filterMethodName = strAry[i+1];
break;
case "-r":
filterMethodReturnType = getTypeSign(strAry[i+1]);
break;
case "-p":
filterMethodParamType = strAry[i+1];
try{
String[] arys = filterMethodParamType.substring(1, filterMethodParamType.length()-1).split(",");
StringBuilder sb = new StringBuilder();
for(String str1 : arys){
sb.append(getTypeSign(str1) + ";");
}
filterMethodParamType = sb.toString();
}catch(Exception e){
}
break;
case "-c":
filterClassName = strAry[i+1];
break;
}
}
}catch(Exception e){
}
}
/**
* @param type
* @return
*/
private static String getTypeSign(String type){
switch(type){
case "boolean":
return getTypeSign(boolean.class);
case "byte":
return getTypeSign(byte.class);
case "short":
return getTypeSign(short.class);
case "int":
return getTypeSign(int.class);
case "char":
return getTypeSign(char.class);
case "float":
return getTypeSign(float.class);
case "double":
return getTypeSign(double.class);
case "long":
return getTypeSign(long.class);
}
try{
return getTypeSign(Class.forName(type));
}catch(Exception e){
return null;
}
}
}