-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMethodReflect.java
3260 lines (2794 loc) · 107 KB
/
MethodReflect.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.hy.common;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hy.common.comparate.MethodComparator;
import org.hy.common.comparate.MethodFieldComparator;
/**
* 方法的反射。
*
* 1. 可实现xxx.yyy.www(或getXxx.getYyy.setWww)全路径的解释。不再区分大小写
* 2. 可实现xxx.yyy.www(或getXxx.getYyy.getWww)全路径的解释。不再区分大小写
* 3. 可实现xxx(p1).yyy(p1 ,p2).www 方法带参数的全路径的解释。不再区分大小写
* 4. 无须反射,只许简单的执行方法就成。应用于:外界已明确了待执行的方法(通常是getter方法)。
*
* @author ZhengWei(HY)
* @createDate 2012-09-21
* @version v2.0 2014-07-11 可实现xxx(p1).yyy(p1 ,p2).www 方法带参数的全路径的解释。
* v3.0 2016-03-18 添加新的构造器。无须反射,只许简单的执行方法就成。应用于:外界已明确了待执行的方法(通常是getter方法)。
* 添加:向数据库表插入数据时,通过Java生成主键的功能。与SQL占位符配合使用。
* v4.0 2016-07-30 添加:getMapValue()方法,从Map集合中取值。实现xxx.yyy.www(或getXxx.getYyy.getWww)全路径的解释
* v4.1 2017-01-21 修正:isExtendImplement()方法对接口继承的接口与要作判定。
* v4.2 2017-02-15 添加:将"GET"、"SET"两个分区关键字对外公开。
* v5.0 2017-03-18 添加:Class<?> 类型对识别方法反射的构造器
* v5.1 2017-03-23 添加:xxx.$getYyy.www 全路径中$符号后跟的为完整方法名称的解释功能。
* 即,全路径中一部是简写方法名称的方式,一部又是方法的完整名称。
* v6.0 2017-06-15 添加:MethodReflect()构造器中入参 "i_MethodURL方法全路径" 中的每个方法名称,不再区分大小写
* v7.0 2017-06-23 修正:三个invokeForInstance(...)方法在中间实现对象为Null的处理。
* v8.0 2017-07-12 修改:所有方法名称的判定都不再区分大小。
* 修正:当方法名称正的以$开头时($为本类的关键字符,见v5.1),也要能正确匹配到方法。parser()方法除外。
* v9.0 2017-11-24 添加:invokeSet(...)调用对象的Setter赋值。
* v10.0 2017-12-18 添加:getParameterAnnotations(...)
* v11.0 2017-12-23 修正:MethodReflect实现序列接口,但this.methods的类型Method是非序列,用MethodInfo代替。
* v12.0 2018-01-18 添加:支持BigDecimal类型
* v12.1 2018-01-29 添加:扫描项目所有类时,当发现某一类引用的类不存在时,只错误提示不中断服务。
* v12.2 2018-03-11 添加:解释方法全路径parser(),最后一个Getter方法支持isXXX()方法,支持逻辑方法。
* v12.3 2018-04-26 添加:按方法对应的成员属性名称在类中的编程编写的顺序排序的getGetSetMethods(...)。
* v12.4 2018-05-04 添加:isExtendImplement()方法判定基本类型与包装类型是否一样。
* v12.5 2018-05-08 添加:支持枚举toString()的匹配
* 修改: 当子类实现父类接口时,方法重载时,可能出现方法名称相同的两个getter方法。
* 方法按修饰符排序后取首个方法,不再向外界抛错。
* v12.6 2018-05-15 添加:数据库java.sql.Timestamp时间的转换
* v13.0 2020-01-14 添加:获取排除某些前缀的成员方法 getMethodsExcludeStart()
* v13.1 2021-02-01 修正:解释xx.yy.zz时,当yy为空指针时的解释异常
* v14.0 2021-12-16 添加:判定类是否允许被实例化(默认无参构造器的实例化)allowNew()
* v14.0 2022-05-17 优化:isExtendImplement 方法的判定性能 10万次计算减少用时3秒
*/
public class MethodReflect implements Serializable
{
private static final long serialVersionUID = -4388208505011166797L;
/** Getter、is方法的分区关键字。主用于 getGetSetMethods() 方法的返回值:分区结构 */
public static final String $Partition_GET = "GET";
/** Setter方法的分区关键字。主用于 getGetSetMethods() 方法的返回值:分区结构 */
public static final String $Partition_SET = "SET";
/**
* 类似于Excel单元格中的固定引用单元格的标示符。
*
* 当方法的全路径为:xxx.$getYyy.www 时,$符号表示 getYyy 是一个方法的完整名称,无须再添加 "get"、"is" 或 "set" 前缀。
*
* 只对 isNorm = true 有效
*/
public final static String $FixedMethodName = "$";
/**
* 占位符的标示符
*/
public final static String $Placeholder = ":";
/**
* 占位符+变量名称的正式表达式片段
*/
private final static String $PV = "[\\" + $Placeholder + "\\w]";
/**
* 正则表达式对:方法名称的识别
* 如:add(row)
*/
private final static String $REGEX_METHOD = "[\\" + $FixedMethodName + "\\w]+[\\(]";
/**
* 正则表达式对:方法填写有效性的验证。
* 如:xxx(p1 ,p2 ,... pn)
* 如:xxx(o1.p1 ,o2.p1 ,... on.pn)
*/
private final static String $REGEX_METHOD_VERIFY = "^[\\" + $FixedMethodName + "\\w]+\\( *((" + $PV + "+\\." + $PV + "+ *, *)|(" + $PV + "+ *, *))*((" + $PV + "+\\." + $PV + "+)|(" + $PV + "+)) *\\)$";
/** Setter规范的方法 */
public final static int $NormType_Setter = 1;
/** Getter规范的方法 */
public final static int $NormType_Getter = -1;
/** 继承及实现的判断的高速缓存 */
private final static TablePartitionRID<Class<?> ,Boolean> $IsExtendImplementCache = new TablePartitionRID<Class<?> ,Boolean>();
/**
* i_MethodURL整体是否为规范的setter或getter方法
*
* 即,当 isNorm = true 时,i_MethodURL = xxx.yyy.www
* 相等于 isNorm = false 时,i_MethodURL = getXxx.getYyy.setWww
**/
private boolean isNorm;
/**
* 规范的类型
*
* 1. normType = 1 为 setter 方法,如:getXxx.getYyy.setWww
* 2. normType = -1 为 getter 方法,如:getXxx.getYyy.getWww
**/
private int normType;
/** 方法全路径的Mehod返回对象类型的集合 */
private List<Class<?>> classes;
/** 方法全路径的Mehod返回对象实例的集合。第一元素为构造器的第一个参数值 */
private List<Object> instances;
/**
* 方法全路径的Mehod的集合
* 外层List对应:方法全路径上.第几层次上方法
* 内层List对应:具体层次上的多个相同重载的方法
*/
private List<List<MethodInfo>> methods;
/**
* 方法全路径的Mehod的集合的参数集合
* 外层List对应:方法全路径上.第几层次上方法
* 内层List对应:具体层次上的方法的多个入参参数
*/
private List<List<String>> methodsParams;
/** 方法全路径的分段数组 */
private String [] methodNames;
/** 方法名称的全路径 */
private String methodURL;
/**
* 方法填写有效性的验证
*
* 如:xxx(p1 ,p2 ,... pn)
* 如:xxx(o1.p1 ,o2.p1 ,... on.pn)
*
* @param i_Text
* @return
*/
public static boolean methodVerify(String i_Text)
{
if ( Help.isNull(i_Text) )
{
return false;
}
Pattern v_Pattern = Pattern.compile($REGEX_METHOD_VERIFY);
Matcher v_Matcher = v_Pattern.matcher(i_Text);
return v_Matcher.find();
}
/**
* 解释如 xxx(p1 ,p2 ,... pn) 格式的方法名称
*
* @param i_Text
* @return
*/
public static String parserMethodName(String i_Text)
{
if ( methodVerify(i_Text) )
{
Pattern v_Pattern = Pattern.compile($REGEX_METHOD);
Matcher v_Matcher = v_Pattern.matcher(i_Text);
String v_MethodName = "";
// 识别行级填充方法名称
if ( v_Matcher.find() )
{
v_MethodName = v_Matcher.group();
v_MethodName = v_MethodName.substring(0 ,v_MethodName.length() - 1);
}
else
{
throw new RuntimeException("Method name[" + i_Text + "] is not exist.");
}
return v_MethodName;
}
else
{
return null;
}
}
/**
* 解释如 xxx(p1 ,p2 ,... pn) 格式的方法参数
*
* @param i_Text
* @return
*/
public static String [] parserMethodParams(String i_Text)
{
if ( methodVerify(i_Text) )
{
Pattern v_Pattern = Pattern.compile($REGEX_METHOD);
Matcher v_Matcher = v_Pattern.matcher(i_Text);
String v_MethodName = "";
int v_EndIndex = 0;
String v_Params = null;
// 识别行级填充方法名称
if ( v_Matcher.find() )
{
v_MethodName = v_Matcher.group();
v_MethodName = v_MethodName.substring(0 ,v_MethodName.length() - 1);
v_EndIndex = v_Matcher.end();
}
else
{
throw new RuntimeException("Method name[" + i_Text + "] is not exist.");
}
v_Params = i_Text.substring(v_EndIndex ,i_Text.length() - 1);
return v_Params.split(",");
}
else
{
return new String[0];
}
}
/**
* 继承及实现的判断
*
* 1. 判断某个类是否实现了i_InterfaceClass接口
* 2. 递归判断某个类实现的接口中是否继承了i_InterfaceClass接口
* 3. 递归判断某个类实现的接口的继承的接口是否继承了i_InterfaceClass接口
* 4. 判断某个类是否继承了i_InterfaceClass类
* 5. 递归判断某个类的父类是否实现了i_InterfaceClass接口
* 6. 递归判断某个类的父类是否继承了i_InterfaceClass类
*
* @param i_ObjectClass 类的元类型
* @param i_InterfaceClass 接口的元类型
* @return
*/
public static boolean isExtendImplement(Object i_Object ,Class<?> i_InterfaceClass)
{
return isExtendImplement(i_Object.getClass() ,i_InterfaceClass);
}
/**
* 继承及实现的判断
*
* 1. 判断某个类是否实现了i_InterfaceClass接口
* 2. 递归判断某个类实现的接口中是否继承了i_InterfaceClass接口
* 3. 递归判断某个类实现的接口的继承的接口是否继承了i_InterfaceClass接口
* 4. 判断某个类是否继承了i_InterfaceClass类
* 5. 递归判断某个类的父类是否实现了i_InterfaceClass接口
* 6. 递归判断某个类的父类是否继承了i_InterfaceClass类
*
* @param i_ObjectClass 类的元类型
* @param i_InterfaceClass 接口的元类型
* @return
*/
public static boolean isExtendImplement(Class<?> i_ObjectClass ,Class<?> i_InterfaceClass)
{
if ( i_ObjectClass == i_InterfaceClass )
{
return true;
}
else if ( i_ObjectClass == null || i_InterfaceClass == null )
{
return false;
}
String v_InterfaceClassName = i_InterfaceClass.getName();
Boolean v_Ret = $IsExtendImplementCache.getRow(i_ObjectClass ,v_InterfaceClassName);
if ( v_Ret != null )
{
return v_Ret;
}
// 判定Boolean.class == boolean.class ZhengWei(HY) Add 2018-05-04
// 对于Number类型,不处理
if ( i_ObjectClass.getName().startsWith("java.lang") )
{
String v_LangName1 = i_ObjectClass .getSimpleName().toLowerCase();
String v_LangName2 = i_InterfaceClass.getSimpleName().toLowerCase();
if ( v_LangName1.startsWith(v_LangName2) )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
else
{
// 不能简单的返回 false。如当 String 与 java.io.Serializable 比较时也应返回true的。
}
}
// 判定boolean.class == Boolean.class ZhengWei(HY) Add 2018-05-04
// 对于Number类型,不处理
else if ( v_InterfaceClassName.startsWith("java.lang") )
{
String v_LangName1 = i_InterfaceClass.getSimpleName().toLowerCase();
String v_LangName2 = i_ObjectClass .getSimpleName().toLowerCase();
if ( v_LangName1.startsWith(v_LangName2) )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
else
{
// 不能简单的返回 false。如当 String 与 java.io.Serializable 比较时也应返回true的。
}
}
Class<?> [] v_Interfaces = i_ObjectClass.getInterfaces();
for (int i=0; i<v_Interfaces.length; i++)
{
// 判断某个类是否实现了i_InterfaceClass接口
if ( v_Interfaces[i] == i_InterfaceClass )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
// 递归判断某个类实现的接口中是否继承了i_InterfaceClass接口
if ( isExtendImplement(v_Interfaces[i].getSuperclass() ,i_InterfaceClass) )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
// 判断接口类的继承的接口 ZhengWei(HY) Add 2017-01-21
Class<?> [] v_Interface_Interfaces = v_Interfaces[i].getInterfaces();
for (int x=0; x<v_Interface_Interfaces.length; x++)
{
if ( isExtendImplement(v_Interface_Interfaces[x] ,i_InterfaceClass) )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
}
}
// 判断某个类是否继承了i_InterfaceClass类
if ( i_ObjectClass.getSuperclass() == i_InterfaceClass )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,true);
return true;
}
else if ( i_ObjectClass.getSuperclass() == Object.class )
{
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,false);
return false;
}
else
{
// 递归判断某个类的父类是否实现了i_InterfaceClass接口
// 递归判断某个类的父类是否继承了i_InterfaceClass类
v_Ret = isExtendImplement(i_ObjectClass.getSuperclass() ,i_InterfaceClass);
$IsExtendImplementCache.putRow(i_ObjectClass ,v_InterfaceClassName ,v_Ret);
return v_Ret;
}
}
/**
* 判定类是否允许被实例化(默认无参构造器的实例化)
*
* 即判定执行 i_ObjectClass.newInstance() 是否会报错
*
* @author ZhengWei(HY)
* @createDate 2021-12-16
* @version v1.0
*
* @param i_ObjectClass
* @return
*/
public static boolean allowNew(Class<?> i_ObjectClass)
{
if ( i_ObjectClass == null )
{
return false;
}
Constructor<?>[] v_Constructors = i_ObjectClass.getConstructors();
if ( Help.isNull(v_Constructors) )
{
return false;
}
for (Constructor<?> v_Item : v_Constructors)
{
if ( v_Item.getParameters().length == 0 )
{
return true;
}
}
return false;
}
/**
* 获取方法首个入参参数上的数组元素Class原型(方法入参类型为数组)
*
* @param i_Method
* @return
*/
public static Class<?> getArrayElementType(Method i_Method)
{
return getArrayElementType(i_Method ,0);
}
/**
* 获取方法某个入参参数上的数组元素Class原型(方法入参类型为数组)
*
* @param i_Method
* @param i_ParamIndex 方法的入参参数位置
* @return
*/
public static Class<?> getArrayElementType(Method i_Method ,int i_ParamIndex)
{
try
{
Class<?> v_ArrayClass = i_Method.getParameterTypes()[i_ParamIndex];
return v_ArrayClass.getComponentType();
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取某个Java类的属性上首个位置的范型Class原型
*
* @param i_Class
* @param i_FieldName 属性名称(只能是本类自有的属性)
* @return
*/
public static Class<?> getGenerics(Class<?> i_Class ,String i_FieldName)
{
try
{
return getGenerics(i_Class.getDeclaredField(i_FieldName));
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取某个Java类的属性上首个位置的范型Class原型
*
* @param i_Class
* @param i_FieldName 属性名称(只能是本类自有的属性)
* @param i_GenericsIndex 范型的位置
* @return
*/
public static Class<?> getGenerics(Class<?> i_Class ,String i_FieldName ,int i_GenericsIndex)
{
try
{
return getGenerics(i_Class.getDeclaredField(i_FieldName) ,i_GenericsIndex);
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取某个Java类的方法上某个入参参数的某个位置的范型Class原型
*
* @param i_Class
* @param i_MethodName 方法名称
* @param i_ParamSize 方法入参的个数
* @param i_GenericsIndex 范型的位置
* @return
*/
public static Class<?> getGenerics(Class<?> i_Class ,String i_MethodName ,int i_ParamSize ,int i_GenericsIndex)
{
try
{
return getGenerics(getMethods(i_Class ,i_MethodName ,i_ParamSize).get(0) ,i_GenericsIndex);
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取属性上首个位置的范型Class原型
*
* @param i_Field
* @return
*/
public static Class<?> getGenerics(Field i_Field)
{
return getGenerics(i_Field ,0);
}
/**
* 获取属性上某个位置的范型Class原型
*
* @param i_Field
* @param i_GenericsIndex 范型的位置
* @return
*/
public static Class<?> getGenerics(Field i_Field ,int i_GenericsIndex)
{
try
{
ParameterizedType v_PType = (ParameterizedType) i_Field.getGenericType();
Type v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
return (Class<?>)v_Type;
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取方法首个入参参数上的首个位置的范型Class原型
*
* @param i_Method
* @return
*/
public static Class<?> getGenerics(Method i_Method)
{
return getGenerics(i_Method ,0 ,0);
}
/**
* 获取方法某个入参参数上的首个位置的范型Class原型
*
* @param i_Method
* @param i_ParamIndex
* @return
*/
public static Class<?> getGenerics(Method i_Method ,int i_ParamIndex)
{
return getGenerics(i_Method ,i_ParamIndex ,0);
}
/**
* 获取方法某个入参参数上的某个位置的范型Class原型
*
* @param i_Method
* @param i_ParamIndex 方法的入参参数位置
* @param i_GenericsIndex 入参参数范型的位置
* @return
*/
public static Class<?> getGenerics(Method i_Method ,int i_ParamIndex ,int i_GenericsIndex)
{
try
{
ParameterizedType v_PType = (ParameterizedType) i_Method.getGenericParameterTypes()[i_ParamIndex];
Type v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
return (Class<?>)v_Type;
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取方法返回值上的首个位置的范型Class原型
*
* @param i_Class
* @param i_MethodName 方法名称
* @param i_ParamSize 方法入参的个数
* @return
*/
public static GenericsReturn getGenericsReturn(Class<?> i_Class ,String i_MethodName ,int i_ParamSize)
{
return getGenericsReturn(i_Class ,i_MethodName ,i_ParamSize ,0);
}
/**
* 获取方法返回值上的某个位置的范型Class原型
*
* @param i_Class
* @param i_MethodName 方法名称
* @param i_ParamSize 方法入参的个数
* @param i_GenericsIndex 范型的位置
* @return
*/
public static GenericsReturn getGenericsReturn(Class<?> i_Class ,String i_MethodName ,int i_ParamSize ,int i_GenericsIndex)
{
try
{
return getGenericsReturn(getMethods(i_Class ,i_MethodName ,i_ParamSize).get(0) ,i_GenericsIndex);
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取方法返回值上的首个位置的范型Class原型
*
* @param i_Method
* @return
*/
public static GenericsReturn getGenericsReturn(Method i_Method)
{
return getGenericsReturn(i_Method ,0);
}
/**
* 获取方法返回值上的某个位置的范型Class原型
*
* @param i_Method
* @param i_GenericsIndex 范型的位置
* @return
*/
public static GenericsReturn getGenericsReturn(Method i_Method ,int i_GenericsIndex)
{
GenericsReturn v_GR = new GenericsReturn();
try
{
ParameterizedType v_PType = (ParameterizedType) i_Method.getGenericReturnType();
Type v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
while ( v_Type instanceof ParameterizedType )
{
v_PType = (ParameterizedType) v_Type;
v_GR.addMaster((Class<?>) (v_PType.getRawType()) );
v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
}
v_GR.setGenericType( (Class<?>)v_Type );
}
catch (Exception exce)
{
// Nothing.
}
return v_GR;
}
/**
* 获取Java类的父类上的首个位置的范型Class原型
*
* @param i_Class
* @return
*/
public static Class<?> getGenericsSuper(Class<?> i_Class)
{
return getGenericsSuper(i_Class ,0);
}
/**
* 获取Java类的父类上的某个位置的范型Class原型
*
* @param i_Class
* @param i_GenericsIndex 范型的位置
* @return
*/
public static Class<?> getGenericsSuper(Class<?> i_Class ,int i_GenericsIndex)
{
try
{
ParameterizedType v_PType = (ParameterizedType) i_Class.getGenericSuperclass();
Type v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
return (Class<?>)v_Type;
}
catch (Exception exce)
{
return null;
}
}
public static Class<?> getGenerics(Class<?> i_Class ,int i_GenericsIndex)
{
try
{
ParameterizedType v_PType = (ParameterizedType) i_Class.getGenericInterfaces()[0];
Type v_Type = v_PType.getActualTypeArguments()[i_GenericsIndex];
return (Class<?>)v_Type;
}
catch (Exception exce)
{
return null;
}
}
public static <V> Class<?> getGenerics(List<V> i_List)
{
try
{
return getGenerics(getMethods(i_List.getClass() ,"add" ,1).get(0));
}
catch (Exception exce)
{
return null;
}
}
/**
* 获取默认 Setter 方法(入参个数只有一个的)
*
* @param i_Class
* @param i_SetMethodName
* @param i_IsNorm
* @return
*/
public static Method getSetMethod(Class<?> i_Class ,String i_SetMethodName ,boolean i_IsNorm)
{
String v_SetMethodName = i_SetMethodName.trim();
String v_SetMethodNameFixed = null;
if ( i_IsNorm )
{
if ( v_SetMethodName.startsWith($FixedMethodName) )
{
v_SetMethodNameFixed = "set" + v_SetMethodName;
v_SetMethodName = v_SetMethodName.substring(1);
}
else
{
v_SetMethodName = "set" + StringHelp.toUpperCaseByFirst(v_SetMethodName);
}
}
Method [] v_Methods = i_Class.getMethods();
for (int i=0; i<v_Methods.length; i++)
{
if ( v_Methods[i].getName().equalsIgnoreCase(v_SetMethodName) )
{
if ( v_Methods[i].getParameterTypes().length == 1 )
{
return v_Methods[i];
}
}
}
// 当方法名称真的是get$xxx()、set$xxx()的形式,也是要尝试查询匹配一次的 2017-07-12
if ( v_SetMethodNameFixed != null )
{
for (int i=0; i<v_Methods.length; i++)
{
if ( v_Methods[i].getName().equalsIgnoreCase(v_SetMethodNameFixed) )
{
if ( v_Methods[i].getParameterTypes().length == 1 )
{
return v_Methods[i];
}
}
}
}
return null;
}
/**
* 获取多个重载的 Setter 方法集合(入参个数只有一个的)
*
* @param i_Class
* @param i_SetMethodName
* @param i_IsNorm
* @return
*/
public static List<Method> getSetMethods(Class<?> i_Class ,String i_SetMethodName ,boolean i_IsNorm)
{
List<Method> v_Ret = new ArrayList<Method>();
String v_SetMethodName = i_SetMethodName.trim();
String v_SetMethodNameFixed = null;
if ( i_IsNorm )
{
if ( v_SetMethodName.startsWith($FixedMethodName) )
{
v_SetMethodNameFixed = "set" + v_SetMethodName;
v_SetMethodName = v_SetMethodName.substring(1);
}
else
{
v_SetMethodName = "set" + StringHelp.toUpperCaseByFirst(v_SetMethodName);
}
}
Method [] v_Methods = i_Class.getMethods();
// 当方法名称真的是get$xxx()、set$xxx()的形式,也是要尝试查询匹配一次的 2017-07-12
for (int i=0; i<v_Methods.length; i++)
{
// 不再区分大小写 2017-07-12
if ( v_Methods[i].getName().equalsIgnoreCase(v_SetMethodName)
|| v_Methods[i].getName().equalsIgnoreCase(v_SetMethodNameFixed) )
{
if ( v_Methods[i].getParameterTypes().length == 1 )
{
v_Ret.add(v_Methods[i]);
}
}
}
return v_Ret;
}
/**
* 获取成对出现的 Getter、Setter 方法集合中的Getter方法
*
* 返回结果是按方法名称排序的。
*
* @param i_Class
* @return Map.key 为方法的短名称
* Map.value 为方法对象
*/
public static Map<String ,Method> getGetMethodsMS(Class<?> i_Class)
{
return getGetSetMethods(i_Class).get($Partition_GET);
}
/**
* 获取成对出现的 Getter、Setter 方法集合中的Getter方法
*
* 返回结果是按方法名称排序的。
*
* 方法名称排序规则升级为:按方法对应的成员属性名称在类中的编程编写的顺序排序。
*
* @author ZhengWei(HY)
* @createDate 2018-04-26
* @version v1.0
*
* @param i_Class
* @return Map.key 为方法的短名称
* Map.value 为方法对象
*/
public static Map<String ,Method> getGetMethodsMSByJava(Class<?> i_Class)
{
return getGetSetMethodsByJava(i_Class).get($Partition_GET);
}
/**
* 获取成对出现的 Getter、Setter 方法集合中的Setter方法
*
* 返回结果是按方法名称排序的。
*
* @param i_Class
* @return Map.key 为方法的短名称
* Map.value 为方法对象
*/
public static Map<String ,Method> getSetMethodsMG(Class<?> i_Class)
{
return getGetSetMethods(i_Class).get($Partition_SET);
}
/**
* 获取成对出现的 Getter、Setter 方法集合中的Setter方法
*
* 返回结果是按方法名称排序的。
*
* 方法名称排序规则升级为:按方法对应的成员属性名称在类中的编程编写的顺序排序。
*
* @author ZhengWei(HY)
* @createDate 2018-04-26
* @version v1.0
*
* @param i_Class
* @return Map.key 为方法的短名称
* Map.value 为方法对象
*/
public static Map<String ,Method> getSetMethodsMGByJava(Class<?> i_Class)
{
return getGetSetMethodsByJava(i_Class).get($Partition_SET);
}
/**
* 获取成对出现的 Getter、Setter 方法集合
*
* 返回结果是按方法名称排序的。
*
* @param i_Class
* @return TablePartitionRID.key 只有两种值GET或SET。is开头的方法,也在GET分区中。
* TablePartitionRID.index 为方法的短名称
* TablePartitionRID.value 为方法对象
*/
public static TablePartitionRID<String ,Method> getGetSetMethods(Class<?> i_Class)
{
TablePartitionRID<String ,Method> v_Ret = new TablePartitionRID<String ,Method>(2);
Method [] v_Methods = i_Class.getMethods();
Arrays.sort(v_Methods ,MethodComparator.getInstance());
// 先行过滤出Getter方法(包括is开头的方法)
for (int i=0; i<v_Methods.length; i++)
{
Method v_Method = v_Methods[i];
if ( v_Method.getParameterTypes().length == 0 )