();
227 | Class> type = type();
228 |
229 | do {
230 | for (Field field : type.getDeclaredFields()) {
231 | if (!isClass ^ Modifier.isStatic(field.getModifiers())) {
232 | String name = field.getName();
233 |
234 | if (!result.containsKey(name))
235 | result.put(name, field(name));
236 | }
237 | }
238 |
239 | type = type.getSuperclass();
240 | }
241 | while (type != null);
242 |
243 | return result;
244 | }
245 |
246 | /**
247 | * 调用指定的无参数方法
248 | *
249 | * @param name
250 | * @return
251 | * @throws ReflectException
252 | */
253 | public Reflect call(String name) throws ReflectException {
254 | return call(name, new Object[0]);
255 | }
256 |
257 |
258 | /**
259 | * 指定方法的注解类型 如:方法上有@Callback才会调用该方法
260 | * 调用方法根据传入的参数
261 | * @param name
262 | * @param args
263 | * @return
264 | * @throws ReflectException
265 | */
266 | public void callback(String name, Object... args) throws ReflectException {
267 | Class>[] types = types(args);
268 | try {
269 | Method method = exactMethod(name, types);
270 | callbackOn(method, args);
271 | } catch (NoSuchMethodException e) {
272 | try {
273 | Method method = similarMethod(name, types);
274 | callbackOn(method, args);
275 | } catch (NoSuchMethodException e1) {
276 | throw new ReflectException(e1);
277 | }
278 | }
279 | }
280 |
281 | private void callbackOn(Method method, Object[] args) {
282 | /*Annotation[] methodAnnotations = method.getAnnotations();
283 | for (Annotation annotation : methodAnnotations){
284 | if (annotation instanceof Callback){
285 | on(method, object, args);
286 | break;
287 | }
288 | }*/
289 | if (method.isAnnotationPresent(Callback.class)){
290 | on(method, object, args);
291 | }
292 | }
293 |
294 | /**
295 | * 调用方法根据传入的参数
296 | *
297 | * @param name
298 | * @param args
299 | * @return
300 | * @throws ReflectException
301 | */
302 | public Reflect call(String name, Object... args) throws ReflectException {
303 | Class>[] types = types(args);
304 |
305 | try {
306 | Method method = exactMethod(name, types);
307 | return on(method, object, args);
308 | } catch (NoSuchMethodException e) {
309 | try {
310 | Method method = similarMethod(name, types);
311 | return on(method, object, args);
312 | } catch (NoSuchMethodException e1) {
313 | throw new ReflectException(e1);
314 | }
315 | }
316 | }
317 |
318 |
319 | private Method exactMethod(String name, Class>[] types) throws NoSuchMethodException {
320 | Class> type = type();
321 |
322 | try {
323 | return type.getMethod(name, types);
324 | } catch (NoSuchMethodException e) {
325 | do {
326 | try {
327 | return type.getDeclaredMethod(name, types);
328 | } catch (NoSuchMethodException ignore) {
329 | }
330 |
331 | type = type.getSuperclass();
332 | }
333 | while (type != null);
334 |
335 | throw new NoSuchMethodException();
336 | }
337 | }
338 |
339 | /**
340 | * 根据参数和名称匹配方法,如果找不到方法,
341 | */
342 | private Method similarMethod(String name, Class>[] types) throws NoSuchMethodException {
343 | Class> type = type();
344 |
345 | for (Method method : type.getMethods()) {
346 | if (isSimilarSignature(method, name, types)) {
347 | return method;
348 | }
349 | }
350 |
351 | do {
352 | for (Method method : type.getDeclaredMethods()) {
353 | if (isSimilarSignature(method, name, types)) {
354 | return method;
355 | }
356 | }
357 |
358 | type = type.getSuperclass();
359 | }
360 | while (type != null);
361 |
362 | throw new NoSuchMethodException("No similar method " + name + " with params " + Arrays.toString(types) + " could be found on type " + type() + ".");
363 | }
364 |
365 |
366 | private boolean isSimilarSignature(Method possiblyMatchingMethod, String desiredMethodName, Class>[] desiredParamTypes) {
367 | return possiblyMatchingMethod.getName().equals(desiredMethodName) && match(possiblyMatchingMethod.getParameterTypes(), desiredParamTypes);
368 | }
369 |
370 | /**
371 | * 创建一个实例通过默认构造器
372 | *
373 | * @return
374 | * @throws ReflectException
375 | */
376 | public Reflect create() throws ReflectException {
377 | return create(new Object[0]);
378 | }
379 |
380 |
381 | /**
382 | * 创建一个实例根据传入的参数
383 | *
384 | * @param args
385 | * @return
386 | * @throws ReflectException
387 | */
388 | public Reflect create(Object... args) throws ReflectException {
389 | Class>[] types = types(args);
390 |
391 |
392 | try {
393 | Constructor> constructor = type().getDeclaredConstructor(types);
394 | return on(constructor, args);
395 | } catch (NoSuchMethodException e) {
396 | for (Constructor> constructor : type().getDeclaredConstructors()) {
397 | if (match(constructor.getParameterTypes(), types)) {
398 | return on(constructor, args);
399 | }
400 | }
401 |
402 | throw new ReflectException(e);
403 | }
404 | }
405 |
406 | /**
407 | * 创建一个动态代理根据传入的类型.
408 | * 如果我们正在维护的是一个Map,那么当调用出现异常时我们将从Map中取值.
409 | *
410 | * @param proxyType 需要动态代理的类型
411 | * @return 动态代理生成的对象
412 | */
413 | @SuppressWarnings("unchecked")
414 | public P as(Class
proxyType) {
415 | final boolean isMap = (object instanceof Map);
416 | final InvocationHandler handler = new InvocationHandler() {
417 | @Override
418 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
419 | String name = method.getName();
420 | try {
421 | return on(object).call(name, args).get();
422 | } catch (ReflectException e) {
423 | if (isMap) {
424 | Map map = (Map) object;
425 | int length = (args == null ? 0 : args.length);
426 |
427 | if (length == 0 && name.startsWith("get")) {
428 | return map.get(property(name.substring(3)));
429 | } else if (length == 0 && name.startsWith("is")) {
430 | return map.get(property(name.substring(2)));
431 | } else if (length == 1 && name.startsWith("set")) {
432 | map.put(property(name.substring(3)), args[0]);
433 | return null;
434 | }
435 | }
436 |
437 | throw e;
438 | }
439 | }
440 | };
441 |
442 | return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[]{proxyType}, handler);
443 | }
444 |
445 |
446 | /**
447 | * 将给定字符串的开头改为小写.
448 | *
449 | * @param string
450 | * @return
451 | */
452 | private static String property(String string) {
453 | int length = string.length();
454 |
455 | if (length == 0) {
456 | return "";
457 | } else if (length == 1) {
458 | return string.toLowerCase();
459 | } else {
460 | return string.substring(0, 1).toLowerCase() + string.substring(1);
461 | }
462 | }
463 |
464 |
465 | /**
466 | * 检查两个数组的类型是否匹配,如果数组中包含原始类型,将它们转换为对应的包装类型.
467 | */
468 | private boolean match(Class>[] declaredTypes, Class>[] actualTypes) {
469 | if (declaredTypes.length == actualTypes.length) {
470 | for (int i = 0; i < actualTypes.length; i++) {
471 | if (actualTypes[i] == NULL.class)
472 | continue;
473 |
474 | if (wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i])))
475 | continue;
476 |
477 | return false;
478 | }
479 |
480 | return true;
481 | } else {
482 | return false;
483 | }
484 | }
485 |
486 | /**
487 | * {@inheritDoc}
488 | */
489 | @Override
490 | public int hashCode() {
491 | return object.hashCode();
492 | }
493 |
494 | /**
495 | * {@inheritDoc}
496 | */
497 | @Override
498 | public boolean equals(Object obj) {
499 | if (obj instanceof Reflect) {
500 | return object.equals(((Reflect) obj).get());
501 | }
502 |
503 | return false;
504 | }
505 |
506 | /**
507 | * {@inheritDoc}
508 | */
509 | @Override
510 | public String toString() {
511 | return object.toString();
512 | }
513 |
514 | private static Reflect on(Constructor> constructor, Object... args) throws ReflectException {
515 | try {
516 | return on(accessible(constructor).newInstance(args));
517 | } catch (Exception e) {
518 | throw new ReflectException(e);
519 | }
520 | }
521 |
522 | private static Reflect on(Method method, Object object, Object... args) throws ReflectException {
523 | try {
524 | accessible(method);
525 |
526 | if (method.getReturnType() == void.class) {
527 | method.invoke(object, args);
528 | return on(object);
529 | } else {
530 | return on(method.invoke(object, args));
531 | }
532 | } catch (Exception e) {
533 | throw new ReflectException(e);
534 | }
535 | }
536 |
537 | /**
538 | * 取得内部维护的对象.
539 | */
540 | private static Object unwrap(Object object) {
541 | if (object instanceof Reflect) {
542 | return ((Reflect) object).get();
543 | }
544 |
545 | return object;
546 | }
547 |
548 | /**
549 | * 将Object数组转换为其类型的数组.
550 | * 如果对象中包含null,我们用NULL.class代替.
551 | *
552 | * @see Object#getClass()
553 | */
554 | private static Class>[] types(Object... values) {
555 | if (values == null) {
556 | return new Class[0];
557 | }
558 |
559 | Class>[] result = new Class[values.length];
560 |
561 | for (int i = 0; i < values.length; i++) {
562 | Object value = values[i];
563 | result[i] = value == null ? NULL.class : value.getClass();
564 | }
565 |
566 | return result;
567 | }
568 |
569 | /**
570 | * 取得一个类,此操作会初始化类的static区域.
571 | *
572 | * @see Class#forName(String)
573 | */
574 | private static Class> forName(String name) throws ReflectException {
575 | try {
576 | return Class.forName(name);
577 | } catch (Exception e) {
578 | throw new ReflectException(e);
579 | }
580 | }
581 |
582 | private static Class> forName(String name, ClassLoader classLoader) throws ReflectException {
583 | try {
584 | return Class.forName(name, true, classLoader);
585 | } catch (Exception e) {
586 | throw new ReflectException(e);
587 | }
588 | }
589 |
590 | /**
591 | * 取得我们正在反射的对象的类型.
592 | *
593 | * @see Object#getClass()
594 | */
595 | public Class> type() {
596 | if (isClass) {
597 | return (Class>) object;
598 | } else {
599 | return object.getClass();
600 | }
601 | }
602 |
603 | /**
604 | * 如果给定的Class是原始类型,那么将其包装为对象类型,
605 | * 否则返回本身.
606 | */
607 | public static Class> wrapper(Class> type) {
608 | if (type == null) {
609 | return null;
610 | } else if (type.isPrimitive()) {
611 | if (boolean.class == type) {
612 | return Boolean.class;
613 | } else if (int.class == type) {
614 | return Integer.class;
615 | } else if (long.class == type) {
616 | return Long.class;
617 | } else if (short.class == type) {
618 | return Short.class;
619 | } else if (byte.class == type) {
620 | return Byte.class;
621 | } else if (double.class == type) {
622 | return Double.class;
623 | } else if (float.class == type) {
624 | return Float.class;
625 | } else if (char.class == type) {
626 | return Character.class;
627 | } else if (void.class == type) {
628 | return Void.class;
629 | }
630 | }
631 |
632 | return type;
633 | }
634 | }
635 |
--------------------------------------------------------------------------------