• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

自分のプロジェクトで利用するための汎用Javaライブラリ


Commit MetaInfo

修订版86a6a5106cd83d07af77b68ac20364b385df873c (tree)
时间2020-10-23 15:52:27
作者Nobuyuki Koseki <nkoseki.lancer@gmai...>
CommiterNobuyuki Koseki

Log Message

test

更改概述

差异

--- a/src/org/dyndns/nuda/tools/util/ReflectUtil.java
+++ b/src/org/dyndns/nuda/tools/util/ReflectUtil.java
@@ -12,10 +12,15 @@ import java.util.List;
1212 import java.util.Set;
1313 import java.util.TreeSet;
1414
15+/**
16+ *
17+ * @author nykos
18+ *
19+ */
1520 public class ReflectUtil {
16-
21+
1722 /**
18- *
23+ *
1924 * @param field
2025 * @return
2126 */
@@ -31,38 +36,38 @@ public class ReflectUtil {
3136 Class<?> result = (Class<?>) types[0];
3237 return result;
3338 }
34-
39+
3540 public static boolean isPresentedByInterface(final Class<?> target,
3641 final Class<?> intf) {
3742 Class<?> temp = target;
38-
43+
3944 boolean isValidClass = false;
4045 while (!temp.getName().equals(Object.class.getName())) {
4146 Class<?>[] interfaces = temp.getInterfaces();
42-
47+
4348 for (Class<?> intf0 : interfaces) {
44-
49+
4550 if (intf0.equals(intf0)) {
4651 isValidClass = true;
4752 break;
4853 }
4954 }
50-
55+
5156 if (isValidClass) {
5257 break;
5358 }
5459 temp = temp.getSuperclass();
5560 }
56-
61+
5762 return isValidClass;
5863 }
59-
64+
6065 public static List<Method> getAnnotationPresentedMethods(
6166 final Class<?> cls, final Class<? extends Annotation> annotation) {
6267 Method[] methods = cls.getMethods();
63-
68+
6469 List<Method> methodAry = new ArrayList<Method>();
65-
70+
6671 for (Method method : methods) {
6772 if (method.isAnnotationPresent(annotation)) {
6873 methodAry.add(method);
@@ -70,13 +75,13 @@ public class ReflectUtil {
7075 }
7176 return methodAry;
7277 }
73-
78+
7479 public static List<Field> getAnnotationPresentedField(final Class<?> cls,
7580 final Class<? extends Annotation> annotation) {
7681 Field[] fields = cls.getDeclaredFields();
77-
82+
7883 List<Field> fieldAry = new ArrayList<Field>();
79-
84+
8085 for (Field field : fields) {
8186 if (field.isAnnotationPresent(annotation)) {
8287 fieldAry.add(field);
@@ -84,23 +89,23 @@ public class ReflectUtil {
8489 }
8590 return fieldAry;
8691 }
87-
92+
8893 public static Class<?>[] getGenericTypeForMethodReturn(final Method method) {
8994 Class<?>[] resultTypes = null;
90-
95+
9196 Type aType = method.getGenericReturnType();
92-
97+
9398 if (aType != null) {
9499 ParameterizedType paramType = null;
95100 try {
96101 paramType = (ParameterizedType) aType;
97102 } catch (Exception e) {
98-
103+
99104 }
100-
105+
101106 if (paramType != null) {
102107 Type[] types = paramType.getActualTypeArguments();
103-
108+
104109 if (types != null && types.length > 0) {
105110 resultTypes = new Class[types.length];
106111 int count = 0;
@@ -111,23 +116,23 @@ public class ReflectUtil {
111116 }
112117 }
113118 }
114-
119+
115120 return resultTypes;
116121 }
117-
122+
118123 public static void main(final String[] args) {
119-
124+
120125 }
121-
126+
122127 /**
123- *
128+ *
124129 * @author nkoseki
125- *
130+ *
126131 */
127132 public static enum PREFIX {
128-
133+
129134 /**
130- *
135+ *
131136 */
132137 GETTER {
133138 @Override
@@ -135,9 +140,9 @@ public class ReflectUtil {
135140 return GETTER_PREFIX;
136141 }
137142 },
138-
143+
139144 /**
140- *
145+ *
141146 */
142147 GETTER_BOOL {
143148 @Override
@@ -146,9 +151,9 @@ public class ReflectUtil {
146151 return GETTER_BOOL_PREFIX;
147152 }
148153 },
149-
154+
150155 /**
151- *
156+ *
152157 */
153158 SETTER {
154159 @Override
@@ -156,9 +161,9 @@ public class ReflectUtil {
156161 return SETTER_PREFIX;
157162 }
158163 },
159-
164+
160165 /**
161- *
166+ *
162167 */
163168 OTHER {
164169 @Override
@@ -169,15 +174,15 @@ public class ReflectUtil {
169174 private static String GETTER_PREFIX = "get";
170175 private static String GETTER_BOOL_PREFIX = "is";
171176 private static String SETTER_PREFIX = "set";
172-
177+
173178 /**
174- *
179+ *
175180 * @return
176181 */
177182 public abstract String prefix();
178-
183+
179184 /**
180- *
185+ *
181186 * @param methodName
182187 * @return
183188 */
@@ -189,10 +194,10 @@ public class ReflectUtil {
189194 return methodName;
190195 }
191196 String suffix = methodName.substring(2, methodName.length());
192-
197+
193198 String firstChar = suffix.substring(0, 1).toLowerCase();
194199 String otherChar = suffix.substring(1, suffix.length());
195-
200+
196201 String result = new StringBuilder().append(firstChar)
197202 .append(otherChar).toString();
198203 return result;
@@ -201,19 +206,19 @@ public class ReflectUtil {
201206 return methodName;
202207 }
203208 String suffix = methodName.substring(3, methodName.length());
204-
209+
205210 String firstChar = suffix.substring(0, 1).toLowerCase();
206211 String otherChar = suffix.substring(1, suffix.length());
207-
212+
208213 String result = new StringBuilder().append(firstChar)
209214 .append(otherChar).toString();
210-
215+
211216 return result;
212217 }
213218 }
214-
219+
215220 /**
216- *
221+ *
217222 * @param propertyName
218223 * @return
219224 */
@@ -224,23 +229,23 @@ public class ReflectUtil {
224229 if (propertyName.isEmpty()) {
225230 return "";
226231 }
227-
232+
228233 if (OTHER.equals(this)) {
229234 return propertyName;
230235 } else {
231236 String pre = propertyName.substring(0, 1).toUpperCase();
232237 String other = propertyName.substring(1, propertyName.length());
233-
238+
234239 String result = new StringBuilder().append(this.prefix())
235240 .append(pre).append(other).toString();
236-
241+
237242 return result;
238243 }
239-
244+
240245 }
241-
246+
242247 /**
243- *
248+ *
244249 * @param methodName
245250 * @return
246251 */
@@ -256,7 +261,7 @@ public class ReflectUtil {
256261 }
257262 }
258263 }
259-
264+
260265 public static class AttributeContext implements
261266 Comparator<AttributeContext>, Comparable<AttributeContext> {
262267 protected String attributeName;
@@ -264,39 +269,39 @@ public class ReflectUtil {
264269 protected boolean isBoolPresend;
265270 protected boolean hasGetter;
266271 protected boolean hasSetter;
267-
272+
268273 public String getAttributeName() {
269274 return this.attributeName;
270275 }
271-
276+
272277 public String getGetterName() {
273278 return PREFIX.GETTER.camelCaseTo(this.attributeName);
274279 }
275-
280+
276281 public String getGetterBoolName() {
277282 return PREFIX.GETTER_BOOL.camelCaseTo(this.attributeName);
278283 }
279-
284+
280285 public String getSetterName() {
281286 return PREFIX.SETTER.camelCaseTo(this.attributeName);
282287 }
283-
288+
284289 public boolean isBoolPresend() {
285290 return this.isBoolPresend;
286291 }
287-
292+
288293 public boolean isHasGetter() {
289294 return this.hasGetter;
290295 }
291-
296+
292297 public boolean isHasSetter() {
293298 return this.hasSetter;
294299 }
295-
300+
296301 public Class<?> getPresentedClass() {
297302 return this.presentedClass;
298303 }
299-
304+
300305 public Object getInitialValue() {
301306 Object result = null;
302307 if (this.presentedClass.isPrimitive()) {
@@ -322,117 +327,117 @@ public class ReflectUtil {
322327 } else {
323328 result = null;
324329 }
325-
330+
326331 return result;
327332 }
328-
333+
329334 /**
330335 * 引数に指定されたクラスオブジェクトを元にセッタメソッドの一覧を返します
331- *
336+ *
332337 * @param cls
333338 * 解析対象クラスオブジェクト
334339 * @return セッタメソッド一覧
335340 */
336341 public static AttributeContext[] getSetterSummary(final Class<?> cls) {
337342 AttributeContext[] contextAry = null;
338-
343+
339344 if (cls != null) {
340-
345+
341346 } else {
342347 contextAry = new AttributeContext[0];
343348 }
344-
349+
345350 return contextAry;
346351 }
347-
352+
348353 /**
349354 * 引数に指定されたクラスオブジェクトをもとにゲッタメソッドの一覧を返します
350- *
355+ *
351356 * @param cls
352357 * 解析対象クラスオブジェクト
353358 * @return ゲッタメソッド一覧
354359 */
355360 public static AttributeContext[] getGetterSummary(final Class<?> cls) {
356361 AttributeContext[] contextAry = null;
357-
362+
358363 if (cls != null) {
359-
364+
360365 } else {
361366 contextAry = new AttributeContext[0];
362367 }
363-
368+
364369 return contextAry;
365370 }
366-
371+
367372 /**
368373 * 引数に指定されたクラスオブジェクトをもとにアクセッサメソッドの一覧を返します
369- *
374+ *
370375 * @param cls
371376 * 解析対象クラスオブジェクト
372377 * @return アクセッサメソッド一覧
373378 */
374379 public static AttributeContext[] getAccessorSummary(final Class<?> cls) {
375380 AttributeContext[] contextAry = null;
376-
381+
377382 if (cls != null) {
378-
383+
379384 } else {
380385 contextAry = new AttributeContext[0];
381386 }
382-
387+
383388 return contextAry;
384389 }
385-
390+
386391 /**
387392 * 引数に指定されたクラスオブジェクトをもとにオペレータ(セッタメソッドでもゲッタメソッドでもないメソッド)の一覧を返します
388- *
393+ *
389394 * @param cls
390395 * 解析対象クラスオブジェクト
391396 * @return オペレータメソッド一覧
392397 */
393398 public static AttributeContext[] getOperatorSummary(final Class<?> cls) {
394399 AttributeContext[] contextAry = null;
395-
400+
396401 if (cls != null) {
397-
402+
398403 } else {
399404 contextAry = new AttributeContext[0];
400405 }
401-
406+
402407 return contextAry;
403408 }
404-
409+
405410 public static Set<AttributeContext> getContexts(final Class<?> cls) {
406411 Set<AttributeContext> result = new TreeSet<AttributeContext>();
407-
412+
408413 if (cls == null) {
409414 return result;
410415 }
411-
416+
412417 for (Method method : cls.getDeclaredMethods()) {
413418 AttributeContext attr = new AttributeContext(method, cls);
414-
419+
415420 result.add(attr);
416421 }
417-
422+
418423 return result;
419424 }
420-
425+
421426 public AttributeContext(final Method method, final Class<?> cls) {
422427 String methodName = method.getName();
423428 PREFIX prefix = ReflectUtil.PREFIX.getPrefix(methodName);
424-
429+
425430 String propertyName = prefix.camelCaseOf(methodName);
426-
431+
427432 this.attributeName = propertyName;
428-
433+
429434 if (method == null || cls == null) {
430435 this.isBoolPresend = false;
431436 this.hasGetter = false;
432437 this.hasSetter = false;
433438 return;
434439 }
435-
440+
436441 // publicメソッドのみを対象とする
437442 if (method.getModifiers() != Modifier.PUBLIC && !cls.isInterface()) {
438443 // LoggerFactory.getLogger(ReflectUtil.class).info(
@@ -443,7 +448,7 @@ public class ReflectUtil {
443448 this.hasSetter = false;
444449 return;
445450 }
446-
451+
447452 // staticメソッドは除外する
448453 if (method.getModifiers() == Modifier.STATIC) {
449454 // LoggerFactory.getLogger(ReflectUtil.class).info(
@@ -454,15 +459,15 @@ public class ReflectUtil {
454459 this.hasSetter = false;
455460 return;
456461 }
457-
462+
458463 String getterMethodName;
459464 String getterBoolMethodName;
460465 String setterMethodName;
461-
466+
462467 Class<?>[] parameterTypes;
463468 Class<?> parameterType;
464469 Class<?> returnType;
465-
470+
466471 switch (prefix) {
467472 case GETTER:
468473
@@ -473,15 +478,15 @@ public class ReflectUtil {
473478 this.hasSetter = false;
474479 return;
475480 }
476-
481+
477482 this.hasGetter = true;
478483 setterMethodName = PREFIX.SETTER.camelCaseTo(propertyName);
479484 returnType = method.getReturnType();
480-
485+
481486 if (returnType.equals(boolean.class)) {
482-
487+
483488 }
484-
489+
485490 this.presentedClass = returnType;
486491 try {
487492 cls.getDeclaredMethod(setterMethodName,
@@ -492,32 +497,32 @@ public class ReflectUtil {
492497 } catch (NoSuchMethodException e) {
493498 this.hasSetter = false;
494499 }
495-
500+
496501 break;
497502 case SETTER:
498503
499504 parameterTypes = method.getParameterTypes();
500-
505+
501506 if (parameterTypes == null || parameterTypes.length != 1) {
502507 this.isBoolPresend = false;
503508 this.hasGetter = false;
504509 this.hasSetter = false;
505510 return;
506511 }
507-
512+
508513 parameterType = parameterTypes[0];
509-
514+
510515 this.presentedClass = parameterType;
511516 if (!parameterType.equals(boolean.class)) {
512517 getterMethodName = PREFIX.GETTER
513518 .camelCaseTo(propertyName);
514-
519+
515520 try {
516521 Method getterM = cls.getDeclaredMethod(
517522 getterMethodName, new Class<?>[] {});
518-
523+
519524 if (getterM.getParameterTypes().length == 0) {
520-
525+
521526 if (getterM.getReturnType().equals(
522527 this.presentedClass)) {
523528 this.isBoolPresend = false;
@@ -528,13 +533,13 @@ public class ReflectUtil {
528533 this.hasGetter = false;
529534 this.hasSetter = true;
530535 }
531-
536+
532537 } else {
533538 this.isBoolPresend = false;
534539 this.hasGetter = false;
535540 this.hasSetter = true;
536541 }
537-
542+
538543 } catch (SecurityException e) {
539544 this.isBoolPresend = false;
540545 this.hasGetter = false;
@@ -543,16 +548,16 @@ public class ReflectUtil {
543548 this.isBoolPresend = false;
544549 this.hasGetter = false;
545550 this.hasSetter = true;
546-
551+
547552 }
548553 } else {
549554 getterBoolMethodName = PREFIX.GETTER_BOOL
550555 .camelCaseTo(propertyName);
551-
556+
552557 try {
553558 Method getterBoolM = cls.getDeclaredMethod(
554559 getterBoolMethodName, new Class<?>[] {});
555-
560+
556561 if (getterBoolM.getReturnType().equals(
557562 boolean.class)) {
558563 this.isBoolPresend = true;
@@ -563,7 +568,7 @@ public class ReflectUtil {
563568 this.hasGetter = false;
564569 this.hasSetter = true;
565570 }
566-
571+
567572 } catch (SecurityException e) {
568573 this.isBoolPresend = false;
569574 this.hasGetter = false;
@@ -584,19 +589,19 @@ public class ReflectUtil {
584589 this.hasSetter = false;
585590 return;
586591 }
587-
592+
588593 returnType = method.getReturnType();
589-
594+
590595 this.presentedClass = returnType;
591596 if (returnType.equals(boolean.class)) {
592597 setterMethodName = PREFIX.SETTER
593598 .camelCaseTo(propertyName);
594-
599+
595600 try {
596601 Method getterBoolM = cls.getDeclaredMethod(
597602 setterMethodName,
598603 new Class<?>[] { returnType });
599-
604+
600605 if (getterBoolM.getReturnType().equals(void.class)) {
601606 this.isBoolPresend = true;
602607 this.hasGetter = false;
@@ -606,7 +611,7 @@ public class ReflectUtil {
606611 this.hasGetter = false;
607612 this.hasSetter = false;
608613 }
609-
614+
610615 } catch (SecurityException e) {
611616 this.isBoolPresend = true;
612617 this.hasGetter = false;
@@ -616,29 +621,29 @@ public class ReflectUtil {
616621 this.hasGetter = false;
617622 this.hasSetter = false;
618623 }
619-
624+
620625 } else {
621626 this.isBoolPresend = false;
622627 this.hasGetter = false;
623628 this.hasSetter = false;
624629 }
625-
630+
626631 break;
627632 case OTHER:
628633 break;
629634 }
630-
635+
631636 }
632-
637+
633638 /**
634639 * 対象のメソッドが正常なプロパティメソッドか検査します
635- *
640+ *
636641 * @return
637642 */
638643 public boolean isValidProperty() {
639644 return (this.hasGetter || this.hasSetter || this.isBoolPresend);
640645 }
641-
646+
642647 @Override
643648 public String toString() {
644649 String result = new StringBuilder().append("attribute name[")
@@ -651,7 +656,7 @@ public class ReflectUtil {
651656 .toString();
652657 return result;
653658 }
654-
659+
655660 @Override
656661 public int hashCode() {
657662 final int prime = 31;
@@ -669,7 +674,7 @@ public class ReflectUtil {
669674 .hashCode());
670675 return result;
671676 }
672-
677+
673678 @Override
674679 public boolean equals(final Object obj) {
675680 if (this == obj) {
@@ -707,7 +712,7 @@ public class ReflectUtil {
707712 }
708713 return true;
709714 }
710-
715+
711716 @Override
712717 public int compare(final AttributeContext o1, final AttributeContext o2) {
713718 if (o1.equals(o2)) {
@@ -715,24 +720,24 @@ public class ReflectUtil {
715720 }
716721 String s1 = o1.getAttributeName();
717722 String s2 = o2.getAttributeName();
718-
723+
719724 return s1.compareTo(s2);
720725 }
721-
726+
722727 @Override
723728 public int compareTo(final AttributeContext o) {
724729 return this.compare(this, o);
725730 }
726731 }
727-
732+
728733 public static class AttributeContextComparator implements
729734 Comparator<AttributeContext> {
730735 private static AttributeContextComparator ME;
731-
736+
732737 private AttributeContextComparator() {
733-
738+
734739 }
735-
740+
736741 @Override
737742 public int compare(final AttributeContext o1, final AttributeContext o2) {
738743 if (o1.equals(o2)) {
@@ -740,10 +745,10 @@ public class ReflectUtil {
740745 }
741746 String s1 = o1.getAttributeName();
742747 String s2 = o2.getAttributeName();
743-
748+
744749 return s1.compareTo(s2);
745750 }
746-
751+
747752 public static AttributeContextComparator getInstance() {
748753 if (ME == null) {
749754 ME = new AttributeContextComparator();
@@ -751,7 +756,7 @@ public class ReflectUtil {
751756 return ME;
752757 }
753758 }
754-
759+
755760 public static final int[] PRIME = { 31, 1231, 1237, 1249, 1259, 1277,
756761 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319 };
757762 }