1
1
package apijson .demo .server ;
2
2
3
- import static java .lang .annotation .ElementType .ANNOTATION_TYPE ;
4
- import static java .lang .annotation .ElementType .CONSTRUCTOR ;
5
- import static java .lang .annotation .ElementType .FIELD ;
6
- import static java .lang .annotation .ElementType .METHOD ;
7
- import static java .lang .annotation .ElementType .PARAMETER ;
8
- import static java .lang .annotation .RetentionPolicy .RUNTIME ;
3
+ import android .content .Context ;
4
+
5
+ import com .alibaba .fastjson .JSON ;
6
+ import com .alibaba .fastjson .JSONArray ;
7
+ import com .alibaba .fastjson .JSONObject ;
8
+ import com .alibaba .fastjson .annotation .JSONField ;
9
+ import com .alibaba .fastjson .parser .Feature ;
10
+ import com .alibaba .fastjson .parser .ParserConfig ;
11
+ import com .alibaba .fastjson .util .TypeUtils ;
9
12
10
13
import java .io .File ;
14
+ import java .io .IOException ;
11
15
import java .lang .annotation .Documented ;
12
16
import java .lang .annotation .Retention ;
13
17
import java .lang .annotation .Target ;
20
24
import java .lang .reflect .Type ;
21
25
import java .util .ArrayList ;
22
26
import java .util .Collection ;
27
+ import java .util .Enumeration ;
23
28
import java .util .HashMap ;
24
29
import java .util .HashSet ;
25
30
import java .util .List ;
28
33
import java .util .Objects ;
29
34
import java .util .Set ;
30
35
31
- import com .alibaba .fastjson .JSON ;
32
- import com .alibaba .fastjson .JSONArray ;
33
- import com .alibaba .fastjson .JSONObject ;
34
- import com .alibaba .fastjson .annotation .JSONField ;
35
- import com .alibaba .fastjson .parser .Feature ;
36
- import com .alibaba .fastjson .parser .ParserConfig ;
37
- import com .alibaba .fastjson .util .TypeUtils ;
38
-
36
+ import dalvik .system .DexFile ;
37
+ import dalvik .system .PathClassLoader ;
39
38
import zuo .biao .apijson .StringUtil ;
40
39
40
+ import static java .lang .annotation .ElementType .ANNOTATION_TYPE ;
41
+ import static java .lang .annotation .ElementType .CONSTRUCTOR ;
42
+ import static java .lang .annotation .ElementType .FIELD ;
43
+ import static java .lang .annotation .ElementType .METHOD ;
44
+ import static java .lang .annotation .ElementType .PARAMETER ;
45
+ import static java .lang .annotation .RetentionPolicy .RUNTIME ;
46
+
41
47
public class MethodUtil {
42
48
49
+
43
50
/**非null注解
44
51
* javax.validation.constraints.NotNull不在JDK里面,为了减少第三方库引用就在这里实现了一个替代品
45
52
* @author Lemon
@@ -63,6 +70,12 @@ public interface Callback {
63
70
JSONObject newErrorResult (Exception e );
64
71
}
65
72
73
+
74
+ public interface ClassLoaderCallback {
75
+ List <Class <?>> loadClassList (String packageOrFileName , String className , boolean ignoreError ) throws ClassNotFoundException , IOException ;
76
+ Class <?> loadClass (String className ) throws ClassNotFoundException ;
77
+ }
78
+
66
79
public static String KEY_CODE = "code" ;
67
80
public static String KEY_MSG = "msg" ;
68
81
@@ -88,31 +101,36 @@ public interface Callback {
88
101
public static String KEY_CALL_MAP = "call(){}" ;
89
102
90
103
91
- public static Callback CALLBACK = new Callback () {
92
-
93
- @ Override
94
- public JSONObject newSuccessResult () {
95
- JSONObject result = new JSONObject (true );
96
- result .put (KEY_CODE , CODE_SUCCESS );
97
- result .put (KEY_MSG , MSG_SUCCESS );
98
- return result ;
99
- }
100
-
101
- @ Override
102
- public JSONObject newErrorResult (Exception e ) {
103
- JSONObject result = new JSONObject (true );
104
- result .put (KEY_CODE , CODE_SERVER_ERROR );
105
- result .put (KEY_MSG , e .getMessage ());
106
- return result ;
107
- }
108
- };
104
+ public static ClassLoaderCallback CLASS_LOADER_CALLBACK ;
105
+ public static Callback CALLBACK ;
109
106
110
107
// Map<class, <constructorArgs, instance>>
111
108
public static final Map <Class <?>, Map <Object , Object >> INSTANCE_MAP ;
112
109
public static final Map <String , Class <?>> PRIMITIVE_CLASS_MAP ;
113
110
public static final Map <String , Class <?>> BASE_CLASS_MAP ;
114
111
public static final Map <String , Class <?>> CLASS_MAP ;
115
112
static {
113
+ CLASS_LOADER_CALLBACK = null ;
114
+ CALLBACK = new Callback () {
115
+
116
+ @ Override
117
+ public JSONObject newSuccessResult () {
118
+ JSONObject result = new JSONObject (true );
119
+ result .put (KEY_CODE , CODE_SUCCESS );
120
+ result .put (KEY_MSG , MSG_SUCCESS );
121
+ return result ;
122
+ }
123
+
124
+ @ Override
125
+ public JSONObject newErrorResult (Exception e ) {
126
+ JSONObject result = new JSONObject (true );
127
+ result .put (KEY_CODE , CODE_SERVER_ERROR );
128
+ result .put (KEY_MSG , e .getMessage ());
129
+ return result ;
130
+ }
131
+ };
132
+
133
+
116
134
INSTANCE_MAP = new HashMap <>();
117
135
118
136
PRIMITIVE_CLASS_MAP = new HashMap <String , Class <?>>();
@@ -582,12 +600,12 @@ public static JSONArray getMethodListGroupByClass(String pkgName, String clsName
582
600
583
601
boolean allMethod = isEmpty (methodName , true );
584
602
585
- List <Class <?>> classlist = findClassList (pkgName , clsName , true );
603
+ List <Class <?>> classList = findClassList (pkgName , clsName , true );
586
604
JSONArray list = null ;
587
- if (classlist != null ) {
588
- list = new JSONArray (classlist .size ());
605
+ if (classList != null ) {
606
+ list = new JSONArray (classList .size ());
589
607
590
- for (Class <?> cls : classlist ) {
608
+ for (Class <?> cls : classList ) {
591
609
if (cls == null ) {
592
610
continue ;
593
611
}
@@ -637,6 +655,10 @@ public static String dot2Separator(String name) {
637
655
return name == null ? null : name .replaceAll ("\\ ." , File .separator );
638
656
}
639
657
658
+ public static String separator2dot (String name ) {
659
+ return name == null ? null : name .replaceAll (File .separator , "." );
660
+ }
661
+
640
662
// private void initTypesAndValues(JSONArray methodArgs, Class<?>[] types, Object[] args)
641
663
// throws IllegalArgumentException, ClassNotFoundException, IOException {
642
664
// initTypesAndValues(methodArgs, types, args, false);
@@ -876,7 +898,11 @@ public static Class<?> findClass(String packageOrFileName, String className, boo
876
898
* @return
877
899
* @throws ClassNotFoundException
878
900
*/
879
- public static List <Class <?>> findClassList (String packageOrFileName , String className , boolean ignoreError ) throws ClassNotFoundException {
901
+ public static List <Class <?>> findClassList (String packageOrFileName , String className , boolean ignoreError ) throws ClassNotFoundException , IOException {
902
+ if (CLASS_LOADER_CALLBACK != null ) {
903
+ return CLASS_LOADER_CALLBACK .loadClassList (packageOrFileName , className , ignoreError );
904
+ }
905
+
880
906
List <Class <?>> list = new ArrayList <>();
881
907
882
908
int index = className .indexOf ("<" );
0 commit comments