11package apijson .demo .server ;
22
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 ;
912
1013import java .io .File ;
14+ import java .io .IOException ;
1115import java .lang .annotation .Documented ;
1216import java .lang .annotation .Retention ;
1317import java .lang .annotation .Target ;
2024import java .lang .reflect .Type ;
2125import java .util .ArrayList ;
2226import java .util .Collection ;
27+ import java .util .Enumeration ;
2328import java .util .HashMap ;
2429import java .util .HashSet ;
2530import java .util .List ;
2833import java .util .Objects ;
2934import java .util .Set ;
3035
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 ;
3938import zuo .biao .apijson .StringUtil ;
4039
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+
4147public class MethodUtil {
4248
49+
4350 /**非null注解
4451 * javax.validation.constraints.NotNull不在JDK里面,为了减少第三方库引用就在这里实现了一个替代品
4552 * @author Lemon
@@ -63,6 +70,12 @@ public interface Callback {
6370 JSONObject newErrorResult (Exception e );
6471 }
6572
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+
6679 public static String KEY_CODE = "code" ;
6780 public static String KEY_MSG = "msg" ;
6881
@@ -88,31 +101,36 @@ public interface Callback {
88101 public static String KEY_CALL_MAP = "call(){}" ;
89102
90103
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 ;
109106
110107 // Map<class, <constructorArgs, instance>>
111108 public static final Map <Class <?>, Map <Object , Object >> INSTANCE_MAP ;
112109 public static final Map <String , Class <?>> PRIMITIVE_CLASS_MAP ;
113110 public static final Map <String , Class <?>> BASE_CLASS_MAP ;
114111 public static final Map <String , Class <?>> CLASS_MAP ;
115112 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+
116134 INSTANCE_MAP = new HashMap <>();
117135
118136 PRIMITIVE_CLASS_MAP = new HashMap <String , Class <?>>();
@@ -582,12 +600,12 @@ public static JSONArray getMethodListGroupByClass(String pkgName, String clsName
582600
583601 boolean allMethod = isEmpty (methodName , true );
584602
585- List <Class <?>> classlist = findClassList (pkgName , clsName , true );
603+ List <Class <?>> classList = findClassList (pkgName , clsName , true );
586604 JSONArray list = null ;
587- if (classlist != null ) {
588- list = new JSONArray (classlist .size ());
605+ if (classList != null ) {
606+ list = new JSONArray (classList .size ());
589607
590- for (Class <?> cls : classlist ) {
608+ for (Class <?> cls : classList ) {
591609 if (cls == null ) {
592610 continue ;
593611 }
@@ -637,6 +655,10 @@ public static String dot2Separator(String name) {
637655 return name == null ? null : name .replaceAll ("\\ ." , File .separator );
638656 }
639657
658+ public static String separator2dot (String name ) {
659+ return name == null ? null : name .replaceAll (File .separator , "." );
660+ }
661+
640662 // private void initTypesAndValues(JSONArray methodArgs, Class<?>[] types, Object[] args)
641663 // throws IllegalArgumentException, ClassNotFoundException, IOException {
642664 // initTypesAndValues(methodArgs, types, args, false);
@@ -876,7 +898,11 @@ public static Class<?> findClass(String packageOrFileName, String className, boo
876898 * @return
877899 * @throws ClassNotFoundException
878900 */
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+
880906 List <Class <?>> list = new ArrayList <>();
881907
882908 int index = className .indexOf ("<" );
0 commit comments