23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.lang.reflect; |
26 package java.lang.reflect; |
27 |
27 |
|
28 import sun.reflect.CallerSensitive; |
28 import sun.reflect.FieldAccessor; |
29 import sun.reflect.FieldAccessor; |
29 import sun.reflect.Reflection; |
30 import sun.reflect.Reflection; |
30 import sun.reflect.generics.repository.FieldRepository; |
31 import sun.reflect.generics.repository.FieldRepository; |
31 import sun.reflect.generics.factory.CoreReflectionFactory; |
32 import sun.reflect.generics.factory.CoreReflectionFactory; |
32 import sun.reflect.generics.factory.GenericsFactory; |
33 import sun.reflect.generics.factory.GenericsFactory; |
374 * @exception NullPointerException if the specified object is null |
375 * @exception NullPointerException if the specified object is null |
375 * and the field is an instance field. |
376 * and the field is an instance field. |
376 * @exception ExceptionInInitializerError if the initialization provoked |
377 * @exception ExceptionInInitializerError if the initialization provoked |
377 * by this method fails. |
378 * by this method fails. |
378 */ |
379 */ |
|
380 @CallerSensitive |
379 public Object get(Object obj) |
381 public Object get(Object obj) |
380 throws IllegalArgumentException, IllegalAccessException |
382 throws IllegalArgumentException, IllegalAccessException |
381 { |
383 { |
|
384 if (!override) { |
|
385 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
386 Class<?> caller = Reflection.getCallerClass(); |
|
387 checkAccess(caller, clazz, obj, modifiers); |
|
388 } |
|
389 } |
382 return getFieldAccessor(obj).get(obj); |
390 return getFieldAccessor(obj).get(obj); |
383 } |
391 } |
384 |
392 |
385 /** |
393 /** |
386 * Gets the value of a static or instance {@code boolean} field. |
394 * Gets the value of a static or instance {@code boolean} field. |
402 * and the field is an instance field. |
410 * and the field is an instance field. |
403 * @exception ExceptionInInitializerError if the initialization provoked |
411 * @exception ExceptionInInitializerError if the initialization provoked |
404 * by this method fails. |
412 * by this method fails. |
405 * @see Field#get |
413 * @see Field#get |
406 */ |
414 */ |
|
415 @CallerSensitive |
407 public boolean getBoolean(Object obj) |
416 public boolean getBoolean(Object obj) |
408 throws IllegalArgumentException, IllegalAccessException |
417 throws IllegalArgumentException, IllegalAccessException |
409 { |
418 { |
|
419 if (!override) { |
|
420 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
421 Class<?> caller = Reflection.getCallerClass(); |
|
422 checkAccess(caller, clazz, obj, modifiers); |
|
423 } |
|
424 } |
410 return getFieldAccessor(obj).getBoolean(obj); |
425 return getFieldAccessor(obj).getBoolean(obj); |
411 } |
426 } |
412 |
427 |
413 /** |
428 /** |
414 * Gets the value of a static or instance {@code byte} field. |
429 * Gets the value of a static or instance {@code byte} field. |
430 * and the field is an instance field. |
445 * and the field is an instance field. |
431 * @exception ExceptionInInitializerError if the initialization provoked |
446 * @exception ExceptionInInitializerError if the initialization provoked |
432 * by this method fails. |
447 * by this method fails. |
433 * @see Field#get |
448 * @see Field#get |
434 */ |
449 */ |
|
450 @CallerSensitive |
435 public byte getByte(Object obj) |
451 public byte getByte(Object obj) |
436 throws IllegalArgumentException, IllegalAccessException |
452 throws IllegalArgumentException, IllegalAccessException |
437 { |
453 { |
|
454 if (!override) { |
|
455 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
456 Class<?> caller = Reflection.getCallerClass(); |
|
457 checkAccess(caller, clazz, obj, modifiers); |
|
458 } |
|
459 } |
438 return getFieldAccessor(obj).getByte(obj); |
460 return getFieldAccessor(obj).getByte(obj); |
439 } |
461 } |
440 |
462 |
441 /** |
463 /** |
442 * Gets the value of a static or instance field of type |
464 * Gets the value of a static or instance field of type |
460 * and the field is an instance field. |
482 * and the field is an instance field. |
461 * @exception ExceptionInInitializerError if the initialization provoked |
483 * @exception ExceptionInInitializerError if the initialization provoked |
462 * by this method fails. |
484 * by this method fails. |
463 * @see Field#get |
485 * @see Field#get |
464 */ |
486 */ |
|
487 @CallerSensitive |
465 public char getChar(Object obj) |
488 public char getChar(Object obj) |
466 throws IllegalArgumentException, IllegalAccessException |
489 throws IllegalArgumentException, IllegalAccessException |
467 { |
490 { |
|
491 if (!override) { |
|
492 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
493 Class<?> caller = Reflection.getCallerClass(); |
|
494 checkAccess(caller, clazz, obj, modifiers); |
|
495 } |
|
496 } |
468 return getFieldAccessor(obj).getChar(obj); |
497 return getFieldAccessor(obj).getChar(obj); |
469 } |
498 } |
470 |
499 |
471 /** |
500 /** |
472 * Gets the value of a static or instance field of type |
501 * Gets the value of a static or instance field of type |
490 * and the field is an instance field. |
519 * and the field is an instance field. |
491 * @exception ExceptionInInitializerError if the initialization provoked |
520 * @exception ExceptionInInitializerError if the initialization provoked |
492 * by this method fails. |
521 * by this method fails. |
493 * @see Field#get |
522 * @see Field#get |
494 */ |
523 */ |
|
524 @CallerSensitive |
495 public short getShort(Object obj) |
525 public short getShort(Object obj) |
496 throws IllegalArgumentException, IllegalAccessException |
526 throws IllegalArgumentException, IllegalAccessException |
497 { |
527 { |
|
528 if (!override) { |
|
529 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
530 Class<?> caller = Reflection.getCallerClass(); |
|
531 checkAccess(caller, clazz, obj, modifiers); |
|
532 } |
|
533 } |
498 return getFieldAccessor(obj).getShort(obj); |
534 return getFieldAccessor(obj).getShort(obj); |
499 } |
535 } |
500 |
536 |
501 /** |
537 /** |
502 * Gets the value of a static or instance field of type |
538 * Gets the value of a static or instance field of type |
520 * and the field is an instance field. |
556 * and the field is an instance field. |
521 * @exception ExceptionInInitializerError if the initialization provoked |
557 * @exception ExceptionInInitializerError if the initialization provoked |
522 * by this method fails. |
558 * by this method fails. |
523 * @see Field#get |
559 * @see Field#get |
524 */ |
560 */ |
|
561 @CallerSensitive |
525 public int getInt(Object obj) |
562 public int getInt(Object obj) |
526 throws IllegalArgumentException, IllegalAccessException |
563 throws IllegalArgumentException, IllegalAccessException |
527 { |
564 { |
|
565 if (!override) { |
|
566 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
567 Class<?> caller = Reflection.getCallerClass(); |
|
568 checkAccess(caller, clazz, obj, modifiers); |
|
569 } |
|
570 } |
528 return getFieldAccessor(obj).getInt(obj); |
571 return getFieldAccessor(obj).getInt(obj); |
529 } |
572 } |
530 |
573 |
531 /** |
574 /** |
532 * Gets the value of a static or instance field of type |
575 * Gets the value of a static or instance field of type |
550 * and the field is an instance field. |
593 * and the field is an instance field. |
551 * @exception ExceptionInInitializerError if the initialization provoked |
594 * @exception ExceptionInInitializerError if the initialization provoked |
552 * by this method fails. |
595 * by this method fails. |
553 * @see Field#get |
596 * @see Field#get |
554 */ |
597 */ |
|
598 @CallerSensitive |
555 public long getLong(Object obj) |
599 public long getLong(Object obj) |
556 throws IllegalArgumentException, IllegalAccessException |
600 throws IllegalArgumentException, IllegalAccessException |
557 { |
601 { |
|
602 if (!override) { |
|
603 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
604 Class<?> caller = Reflection.getCallerClass(); |
|
605 checkAccess(caller, clazz, obj, modifiers); |
|
606 } |
|
607 } |
558 return getFieldAccessor(obj).getLong(obj); |
608 return getFieldAccessor(obj).getLong(obj); |
559 } |
609 } |
560 |
610 |
561 /** |
611 /** |
562 * Gets the value of a static or instance field of type |
612 * Gets the value of a static or instance field of type |
580 * and the field is an instance field. |
630 * and the field is an instance field. |
581 * @exception ExceptionInInitializerError if the initialization provoked |
631 * @exception ExceptionInInitializerError if the initialization provoked |
582 * by this method fails. |
632 * by this method fails. |
583 * @see Field#get |
633 * @see Field#get |
584 */ |
634 */ |
|
635 @CallerSensitive |
585 public float getFloat(Object obj) |
636 public float getFloat(Object obj) |
586 throws IllegalArgumentException, IllegalAccessException |
637 throws IllegalArgumentException, IllegalAccessException |
587 { |
638 { |
|
639 if (!override) { |
|
640 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
641 Class<?> caller = Reflection.getCallerClass(); |
|
642 checkAccess(caller, clazz, obj, modifiers); |
|
643 } |
|
644 } |
588 return getFieldAccessor(obj).getFloat(obj); |
645 return getFieldAccessor(obj).getFloat(obj); |
589 } |
646 } |
590 |
647 |
591 /** |
648 /** |
592 * Gets the value of a static or instance field of type |
649 * Gets the value of a static or instance field of type |
610 * and the field is an instance field. |
667 * and the field is an instance field. |
611 * @exception ExceptionInInitializerError if the initialization provoked |
668 * @exception ExceptionInInitializerError if the initialization provoked |
612 * by this method fails. |
669 * by this method fails. |
613 * @see Field#get |
670 * @see Field#get |
614 */ |
671 */ |
|
672 @CallerSensitive |
615 public double getDouble(Object obj) |
673 public double getDouble(Object obj) |
616 throws IllegalArgumentException, IllegalAccessException |
674 throws IllegalArgumentException, IllegalAccessException |
617 { |
675 { |
|
676 if (!override) { |
|
677 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
678 Class<?> caller = Reflection.getCallerClass(); |
|
679 checkAccess(caller, clazz, obj, modifiers); |
|
680 } |
|
681 } |
618 return getFieldAccessor(obj).getDouble(obj); |
682 return getFieldAccessor(obj).getDouble(obj); |
619 } |
683 } |
620 |
684 |
621 /** |
685 /** |
622 * Sets the field represented by this {@code Field} object on the |
686 * Sets the field represented by this {@code Field} object on the |
682 * @exception NullPointerException if the specified object is null |
746 * @exception NullPointerException if the specified object is null |
683 * and the field is an instance field. |
747 * and the field is an instance field. |
684 * @exception ExceptionInInitializerError if the initialization provoked |
748 * @exception ExceptionInInitializerError if the initialization provoked |
685 * by this method fails. |
749 * by this method fails. |
686 */ |
750 */ |
|
751 @CallerSensitive |
687 public void set(Object obj, Object value) |
752 public void set(Object obj, Object value) |
688 throws IllegalArgumentException, IllegalAccessException |
753 throws IllegalArgumentException, IllegalAccessException |
689 { |
754 { |
|
755 if (!override) { |
|
756 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
757 Class<?> caller = Reflection.getCallerClass(); |
|
758 checkAccess(caller, clazz, obj, modifiers); |
|
759 } |
|
760 } |
690 getFieldAccessor(obj).set(obj, value); |
761 getFieldAccessor(obj).set(obj, value); |
691 } |
762 } |
692 |
763 |
693 /** |
764 /** |
694 * Sets the value of a field as a {@code boolean} on the specified object. |
765 * Sets the value of a field as a {@code boolean} on the specified object. |
712 * and the field is an instance field. |
783 * and the field is an instance field. |
713 * @exception ExceptionInInitializerError if the initialization provoked |
784 * @exception ExceptionInInitializerError if the initialization provoked |
714 * by this method fails. |
785 * by this method fails. |
715 * @see Field#set |
786 * @see Field#set |
716 */ |
787 */ |
|
788 @CallerSensitive |
717 public void setBoolean(Object obj, boolean z) |
789 public void setBoolean(Object obj, boolean z) |
718 throws IllegalArgumentException, IllegalAccessException |
790 throws IllegalArgumentException, IllegalAccessException |
719 { |
791 { |
|
792 if (!override) { |
|
793 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
794 Class<?> caller = Reflection.getCallerClass(); |
|
795 checkAccess(caller, clazz, obj, modifiers); |
|
796 } |
|
797 } |
720 getFieldAccessor(obj).setBoolean(obj, z); |
798 getFieldAccessor(obj).setBoolean(obj, z); |
721 } |
799 } |
722 |
800 |
723 /** |
801 /** |
724 * Sets the value of a field as a {@code byte} on the specified object. |
802 * Sets the value of a field as a {@code byte} on the specified object. |
742 * and the field is an instance field. |
820 * and the field is an instance field. |
743 * @exception ExceptionInInitializerError if the initialization provoked |
821 * @exception ExceptionInInitializerError if the initialization provoked |
744 * by this method fails. |
822 * by this method fails. |
745 * @see Field#set |
823 * @see Field#set |
746 */ |
824 */ |
|
825 @CallerSensitive |
747 public void setByte(Object obj, byte b) |
826 public void setByte(Object obj, byte b) |
748 throws IllegalArgumentException, IllegalAccessException |
827 throws IllegalArgumentException, IllegalAccessException |
749 { |
828 { |
|
829 if (!override) { |
|
830 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
831 Class<?> caller = Reflection.getCallerClass(); |
|
832 checkAccess(caller, clazz, obj, modifiers); |
|
833 } |
|
834 } |
750 getFieldAccessor(obj).setByte(obj, b); |
835 getFieldAccessor(obj).setByte(obj, b); |
751 } |
836 } |
752 |
837 |
753 /** |
838 /** |
754 * Sets the value of a field as a {@code char} on the specified object. |
839 * Sets the value of a field as a {@code char} on the specified object. |
772 * and the field is an instance field. |
857 * and the field is an instance field. |
773 * @exception ExceptionInInitializerError if the initialization provoked |
858 * @exception ExceptionInInitializerError if the initialization provoked |
774 * by this method fails. |
859 * by this method fails. |
775 * @see Field#set |
860 * @see Field#set |
776 */ |
861 */ |
|
862 @CallerSensitive |
777 public void setChar(Object obj, char c) |
863 public void setChar(Object obj, char c) |
778 throws IllegalArgumentException, IllegalAccessException |
864 throws IllegalArgumentException, IllegalAccessException |
779 { |
865 { |
|
866 if (!override) { |
|
867 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
868 Class<?> caller = Reflection.getCallerClass(); |
|
869 checkAccess(caller, clazz, obj, modifiers); |
|
870 } |
|
871 } |
780 getFieldAccessor(obj).setChar(obj, c); |
872 getFieldAccessor(obj).setChar(obj, c); |
781 } |
873 } |
782 |
874 |
783 /** |
875 /** |
784 * Sets the value of a field as a {@code short} on the specified object. |
876 * Sets the value of a field as a {@code short} on the specified object. |
802 * and the field is an instance field. |
894 * and the field is an instance field. |
803 * @exception ExceptionInInitializerError if the initialization provoked |
895 * @exception ExceptionInInitializerError if the initialization provoked |
804 * by this method fails. |
896 * by this method fails. |
805 * @see Field#set |
897 * @see Field#set |
806 */ |
898 */ |
|
899 @CallerSensitive |
807 public void setShort(Object obj, short s) |
900 public void setShort(Object obj, short s) |
808 throws IllegalArgumentException, IllegalAccessException |
901 throws IllegalArgumentException, IllegalAccessException |
809 { |
902 { |
|
903 if (!override) { |
|
904 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
905 Class<?> caller = Reflection.getCallerClass(); |
|
906 checkAccess(caller, clazz, obj, modifiers); |
|
907 } |
|
908 } |
810 getFieldAccessor(obj).setShort(obj, s); |
909 getFieldAccessor(obj).setShort(obj, s); |
811 } |
910 } |
812 |
911 |
813 /** |
912 /** |
814 * Sets the value of a field as an {@code int} on the specified object. |
913 * Sets the value of a field as an {@code int} on the specified object. |
832 * and the field is an instance field. |
931 * and the field is an instance field. |
833 * @exception ExceptionInInitializerError if the initialization provoked |
932 * @exception ExceptionInInitializerError if the initialization provoked |
834 * by this method fails. |
933 * by this method fails. |
835 * @see Field#set |
934 * @see Field#set |
836 */ |
935 */ |
|
936 @CallerSensitive |
837 public void setInt(Object obj, int i) |
937 public void setInt(Object obj, int i) |
838 throws IllegalArgumentException, IllegalAccessException |
938 throws IllegalArgumentException, IllegalAccessException |
839 { |
939 { |
|
940 if (!override) { |
|
941 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
942 Class<?> caller = Reflection.getCallerClass(); |
|
943 checkAccess(caller, clazz, obj, modifiers); |
|
944 } |
|
945 } |
840 getFieldAccessor(obj).setInt(obj, i); |
946 getFieldAccessor(obj).setInt(obj, i); |
841 } |
947 } |
842 |
948 |
843 /** |
949 /** |
844 * Sets the value of a field as a {@code long} on the specified object. |
950 * Sets the value of a field as a {@code long} on the specified object. |
862 * and the field is an instance field. |
968 * and the field is an instance field. |
863 * @exception ExceptionInInitializerError if the initialization provoked |
969 * @exception ExceptionInInitializerError if the initialization provoked |
864 * by this method fails. |
970 * by this method fails. |
865 * @see Field#set |
971 * @see Field#set |
866 */ |
972 */ |
|
973 @CallerSensitive |
867 public void setLong(Object obj, long l) |
974 public void setLong(Object obj, long l) |
868 throws IllegalArgumentException, IllegalAccessException |
975 throws IllegalArgumentException, IllegalAccessException |
869 { |
976 { |
|
977 if (!override) { |
|
978 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
979 Class<?> caller = Reflection.getCallerClass(); |
|
980 checkAccess(caller, clazz, obj, modifiers); |
|
981 } |
|
982 } |
870 getFieldAccessor(obj).setLong(obj, l); |
983 getFieldAccessor(obj).setLong(obj, l); |
871 } |
984 } |
872 |
985 |
873 /** |
986 /** |
874 * Sets the value of a field as a {@code float} on the specified object. |
987 * Sets the value of a field as a {@code float} on the specified object. |
892 * and the field is an instance field. |
1005 * and the field is an instance field. |
893 * @exception ExceptionInInitializerError if the initialization provoked |
1006 * @exception ExceptionInInitializerError if the initialization provoked |
894 * by this method fails. |
1007 * by this method fails. |
895 * @see Field#set |
1008 * @see Field#set |
896 */ |
1009 */ |
|
1010 @CallerSensitive |
897 public void setFloat(Object obj, float f) |
1011 public void setFloat(Object obj, float f) |
898 throws IllegalArgumentException, IllegalAccessException |
1012 throws IllegalArgumentException, IllegalAccessException |
899 { |
1013 { |
|
1014 if (!override) { |
|
1015 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
1016 Class<?> caller = Reflection.getCallerClass(); |
|
1017 checkAccess(caller, clazz, obj, modifiers); |
|
1018 } |
|
1019 } |
900 getFieldAccessor(obj).setFloat(obj, f); |
1020 getFieldAccessor(obj).setFloat(obj, f); |
901 } |
1021 } |
902 |
1022 |
903 /** |
1023 /** |
904 * Sets the value of a field as a {@code double} on the specified object. |
1024 * Sets the value of a field as a {@code double} on the specified object. |
922 * and the field is an instance field. |
1042 * and the field is an instance field. |
923 * @exception ExceptionInInitializerError if the initialization provoked |
1043 * @exception ExceptionInInitializerError if the initialization provoked |
924 * by this method fails. |
1044 * by this method fails. |
925 * @see Field#set |
1045 * @see Field#set |
926 */ |
1046 */ |
|
1047 @CallerSensitive |
927 public void setDouble(Object obj, double d) |
1048 public void setDouble(Object obj, double d) |
928 throws IllegalArgumentException, IllegalAccessException |
1049 throws IllegalArgumentException, IllegalAccessException |
929 { |
1050 { |
|
1051 if (!override) { |
|
1052 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
1053 Class<?> caller = Reflection.getCallerClass(); |
|
1054 checkAccess(caller, clazz, obj, modifiers); |
|
1055 } |
|
1056 } |
930 getFieldAccessor(obj).setDouble(obj, d); |
1057 getFieldAccessor(obj).setDouble(obj, d); |
931 } |
1058 } |
932 |
1059 |
933 // Convenience routine which performs security checks |
1060 // security check is done before calling this method |
934 private FieldAccessor getFieldAccessor(Object obj) |
1061 private FieldAccessor getFieldAccessor(Object obj) |
935 throws IllegalAccessException |
1062 throws IllegalAccessException |
936 { |
1063 { |
937 doSecurityCheck(obj); |
|
938 boolean ov = override; |
1064 boolean ov = override; |
939 FieldAccessor a = (ov)? overrideFieldAccessor : fieldAccessor; |
1065 FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor; |
940 return (a != null)? a : acquireFieldAccessor(ov); |
1066 return (a != null) ? a : acquireFieldAccessor(ov); |
941 } |
1067 } |
942 |
1068 |
943 // NOTE that there is no synchronization used here. It is correct |
1069 // NOTE that there is no synchronization used here. It is correct |
944 // (though not efficient) to generate more than one FieldAccessor |
1070 // (though not efficient) to generate more than one FieldAccessor |
945 // for a given Field. However, avoiding synchronization will |
1071 // for a given Field. However, avoiding synchronization will |
980 if (root != null) { |
1106 if (root != null) { |
981 root.setFieldAccessor(accessor, overrideFinalCheck); |
1107 root.setFieldAccessor(accessor, overrideFinalCheck); |
982 } |
1108 } |
983 } |
1109 } |
984 |
1110 |
985 // NOTE: be very careful if you change the stack depth of this |
|
986 // routine. The depth of the "getCallerClass" call is hardwired so |
|
987 // that the compiler can have an easier time if this gets inlined. |
|
988 private void doSecurityCheck(Object obj) throws IllegalAccessException { |
|
989 if (!override) { |
|
990 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { |
|
991 Class<?> caller = Reflection.getCallerClass(4); |
|
992 |
|
993 checkAccess(caller, clazz, obj, modifiers); |
|
994 } |
|
995 } |
|
996 } |
|
997 |
|
998 /** |
1111 /** |
999 * @throws NullPointerException {@inheritDoc} |
1112 * @throws NullPointerException {@inheritDoc} |
1000 * @since 1.5 |
1113 * @since 1.5 |
1001 */ |
1114 */ |
1002 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
1115 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |