3
3
import com .midream .sheep .swcj .annotation .Param ;
4
4
import com .midream .sheep .swcj .annotation .RequestType ;
5
5
import com .midream .sheep .swcj .annotation .WebSpider ;
6
- import com .midream .sheep .swcj .Exception .ConfigException ;
7
- import com .midream .sheep .swcj .Exception .EmptyMatchMethodException ;
8
6
import com .midream .sheep .swcj .Exception .InterfaceIllegal ;
9
7
import com .midream .sheep .swcj .cache .CacheCorn ;
10
8
import com .midream .sheep .swcj .data .Constant ;
11
9
import com .midream .sheep .swcj .data .ReptileConfig ;
12
10
import com .midream .sheep .swcj .pojo .buildup .MethodHandler ;
11
+ import com .midream .sheep .swcj .pojo .buildup .MethodMeta ;
13
12
import com .midream .sheep .swcj .pojo .enums .ChooseStrategy ;
14
13
import com .midream .sheep .swcj .pojo .swc .ReptileUrl ;
15
- import com .midream .sheep .swcj .pojo .swc .RootReptile ;
16
14
import com .midream .sheep .swcj .pojo .buildup .SWCJClass ;
17
15
import com .midream .sheep .swcj .pojo .buildup .SWCJMethod ;
18
- import com .midream .sheep .swcj .pojo .swc .passvalue .ReptlileMiddle ;
19
16
import com .midream .sheep .swcj .util .function .StringUtil ;
20
17
21
18
import java .lang .reflect .Method ;
24
21
import java .util .concurrent .atomic .AtomicInteger ;
25
22
import java .util .logging .Logger ;
26
23
27
- import static com .midream .sheep .swcj .util .function .StringUtil .add ;
28
-
29
24
/**
30
25
* 构建工具类
31
26
* @author midreamsheep
32
27
*/
33
28
public class BuildTool {
34
- /**原子性自增数*/
35
- private static final AtomicInteger t = new AtomicInteger (0 );
36
29
/**
37
30
* 获取爬虫的具体类
38
31
* @param className 类名
@@ -41,40 +34,13 @@ public class BuildTool {
41
34
public static Object getObjectFromTool (String className ) {
42
35
return CacheCorn .SPIDER_CACHE .getCacheSpider (className );
43
36
}
44
- /**
45
- * 获取爬虫实体类
46
- * @param middle 传递配置包
47
- * @return 爬虫实体类
48
- * */
49
- public static SWCJClass getSWCJClass (ReptlileMiddle middle ) throws ConfigException , EmptyMatchMethodException {
50
- RootReptile rootReptile = middle .getRootReptile ();
51
- //实例化类
52
- SWCJClass swcjClass = SWCJClass .buildClass ();
53
- //设置接口,类名
54
- swcjClass .setClassName ("swcj" + (t .addAndGet (1 )));
55
- swcjClass .setItIterface (rootReptile .getParentInter ());
56
- //效验接口是否有方法,并注入方法
57
- try {
58
- getFunction (swcjClass , rootReptile , middle .getConfig ());
59
- } catch (ClassNotFoundException e ) {
60
- Logger .getLogger (BuildTool .class .getName ()).severe (e .getMessage ());
61
- throw new ConfigException ("你的接口不存在:" + rootReptile .getParentInter ());
62
- } catch (InterfaceIllegal e ) {
63
- Logger .getLogger (BuildTool .class .getName ()).severe (e .getMessage ());
64
- throw new RuntimeException (e );
65
- }
66
- if (swcjClass .getMethods () == null || swcjClass .getMethods ().size () == 0 ) {
67
- throw new EmptyMatchMethodException ("EmptyMatchMethodException(空匹配方法异常)" );
68
- }
69
- return swcjClass ;
70
- }
71
37
/**
72
38
* 为爬虫实体类注入方法数据
73
39
* @param swcjClass 爬虫实体类
74
- * @param rootReptile 爬虫实体数据
40
+ * @param rus 爬虫配置
75
41
* @param config 爬虫配置数据
76
42
* */
77
- private static void getFunction (SWCJClass swcjClass , RootReptile rootReptile , ReptileConfig config ) throws ClassNotFoundException , InterfaceIllegal {
43
+ public static void getFunction (SWCJClass swcjClass , MethodMeta meta , ReptileConfig config , List < ReptileUrl > rus ) throws ClassNotFoundException , InterfaceIllegal {
78
44
Method [] methods = Class .forName (swcjClass .getItIterface ()).getMethods ();
79
45
for (Method method : methods ) {
80
46
//实例化方法类
@@ -100,29 +66,29 @@ private static void getFunction(SWCJClass swcjClass, RootReptile rootReptile, Re
100
66
swcjMethod .setRequestType (method .getAnnotation (RequestType .class ) == null ? "GET" : method .getAnnotation (RequestType .class ).value ().getValue ());
101
67
swcjMethod .setReturnType (Constant .getClassName (method .getReturnType ().toString ()));
102
68
if (config .getChoice () == ChooseStrategy .ANNOTATION ) {
103
- analysisMethodByAnnotation (swcjMethod , method , rootReptile ,config , swcjClass );
69
+ analysisMethodByAnnotation (swcjMethod , method , rus ,config , swcjClass , meta );
104
70
} else if (config .getChoice () == ChooseStrategy .METHOD_NAME ) {
105
- analysisMethodByMethodName (swcjMethod , method , rootReptile ,config , swcjClass );
71
+ analysisMethodByMethodName (swcjMethod , method , rus ,config , swcjClass , meta );
106
72
}
107
73
}
108
74
}
109
75
/**
110
76
* 通过注解解析方法
111
77
* @param swcjMethod SWCJ方法实体类
112
78
* @param method java反射方法对象
113
- * @param rootReptile 爬虫实体数据
79
+ * @param rus 爬虫配置数据
114
80
* @param swcjClass 爬虫实体类
115
81
* */
116
- private static void analysisMethodByAnnotation (SWCJMethod swcjMethod , Method method , RootReptile rootReptile ,ReptileConfig reptileConfig , SWCJClass swcjClass ) throws InterfaceIllegal {
82
+ private static void analysisMethodByAnnotation (SWCJMethod swcjMethod , Method method , List < ReptileUrl > rus ,ReptileConfig reptileConfig , SWCJClass swcjClass , MethodMeta meta ) throws InterfaceIllegal {
117
83
//获取方法上的注解
118
84
WebSpider spider = method .getAnnotation (WebSpider .class );
119
85
//放入所有有注解的方法
120
86
if (spider == null || spider .value ().equals (Constant .nullString )) {
121
87
throw new InterfaceIllegal ("InterfaceMethodIllegal(接口方法不合法,请定义注解)" );
122
88
}
123
- for (ReptileUrl url : rootReptile . getRu () ) {
89
+ for (ReptileUrl url : rus ) {
124
90
if (url .getName ().equals (spider .value ())){
125
- parsePublicArea (swcjMethod , url , rootReptile ,reptileConfig );
91
+ parsePublicArea (swcjMethod , url , meta ,reptileConfig );
126
92
swcjClass .addMethod (spider .value (), swcjMethod );
127
93
}
128
94
}
@@ -131,34 +97,33 @@ private static void analysisMethodByAnnotation(SWCJMethod swcjMethod, Method met
131
97
* 通过方法名解析方法
132
98
* @param swcjMethod SWCJ方法实体类
133
99
* @param method java反射方法对象
134
- * @param rootReptile 爬虫实体数据
100
+ * @param meta 方法源数据
135
101
* @param swcjClass 爬虫实体类
136
102
* */
137
- private static void analysisMethodByMethodName (SWCJMethod swcjMethod , Method method , RootReptile rootReptile ,ReptileConfig reptileConfig ,SWCJClass swcjClass ) {
138
- for (ReptileUrl url : rootReptile . getRu () ) {
103
+ private static void analysisMethodByMethodName (SWCJMethod swcjMethod , Method method ,List < ReptileUrl > rus ,ReptileConfig reptileConfig ,SWCJClass swcjClass , MethodMeta meta ) {
104
+ for (ReptileUrl url : rus ) {
139
105
if (url .getName ().equals (method .getName ())){
140
- parsePublicArea (swcjMethod , url , rootReptile ,reptileConfig );
106
+ parsePublicArea (swcjMethod , url , meta ,reptileConfig );
141
107
swcjClass .addMethod (method .getName (), swcjMethod );
142
108
return ;
143
109
}
144
110
}
145
111
}
146
112
147
- private static void parsePublicArea (SWCJMethod swcjMethod ,ReptileUrl url , RootReptile rootReptile ,ReptileConfig reptileConfig ) {
113
+ private static void parsePublicArea (SWCJMethod swcjMethod ,ReptileUrl url , MethodMeta meta ,ReptileConfig reptileConfig ) {
148
114
swcjMethod .setName (url .getName ());
149
115
List <String > vars = swcjMethod .getExecuteVars ();
150
- swcjMethod .setParamIn (getMethodParametric (url , swcjMethod ,vars ).replace ("class" ,Constant .nullString ));
151
- swcjMethod .setExecuteStr (StringUtil .getExecuteCharacter (url ,vars ,reptileConfig ,rootReptile ,swcjMethod ));
116
+ swcjMethod .setParamIn (getMethodParametric (swcjMethod ,vars ).replace ("class" ,Constant .nullString ));
117
+ swcjMethod .setExecuteStr (StringUtil .getExecuteCharacter (url ,vars ,reptileConfig ,meta ,swcjMethod ));
152
118
153
119
}
154
120
/**
155
121
* 获取方法参数列表
156
- * @param ru 爬虫方法实体数据
157
122
* @param method 方法实体类
158
123
* @param injection 参数列表
159
124
* @return 参数列表字符串
160
125
* */
161
- public static String getMethodParametric (ReptileUrl ru , SWCJMethod method , List <String > injection ) {
126
+ public static String getMethodParametric (SWCJMethod method , List <String > injection ) {
162
127
//获取拼接对象
163
128
StringBuilder sb = new StringBuilder ();
164
129
//获取方法参数和输入参数
0 commit comments