File size: 32,631 Bytes
1b558aa | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 | 1
00:00:05,000 --> 00:00:06,000
Hello, Jim.
2
00:00:06,000 --> 00:00:12,000
In this lesson, we're going to learn a really interesting topic that is Skulled Jallow Reflexion API.
3
00:00:12,000 --> 00:00:18,000
We have a lot of things to learn today, but as usual, we're going to start from Seery and Grado.
4
00:00:18,000 --> 00:00:20,000
It will get to the code examples.
5
00:00:20,000 --> 00:00:23,000
So it's the beginning of the lesson we're going to learn.
6
00:00:23,000 --> 00:00:31,000
What a reflection API is and what rules does adapt will review Java like reflect package in order,
7
00:00:31,000 --> 00:00:34,000
you know, where you can look for the official documentation.
8
00:00:35,000 --> 00:00:37,000
And after that, we will start our protocol.
9
00:00:37,000 --> 00:00:43,000
Part of the lesson I'm going to show you how you can grow up with different class entities such as fields,
10
00:00:43,000 --> 00:00:45,000
constructors, mass interfaces.
11
00:00:45,000 --> 00:00:48,000
Superclusters was a help of reflection API.
12
00:00:49,000 --> 00:00:55,000
I will put special focus on a separate group of examples that relates to the work with private entities
13
00:00:55,000 --> 00:00:56,000
inside the class.
14
00:00:57,000 --> 00:01:00,000
As I already said, there will be a lot of interesting things.
15
00:01:01,000 --> 00:01:07,000
Believe me, this will be mind blowing when you understand what you can do with the help of Reflection
16
00:01:07,000 --> 00:01:07,000
API.
17
00:01:08,000 --> 00:01:09,000
So let's start our lesson.
18
00:01:10,000 --> 00:01:16,000
And to start with, let's understand first what's reflexion is one of reflection is used and why do
19
00:01:16,000 --> 00:01:17,000
we need it?
20
00:01:18,000 --> 00:01:25,000
Reflection is a commonly used by programs which requires and devoted to X in line or modifies the runtime
21
00:01:25,000 --> 00:01:28,000
behavior of applications running into Java virtual machine.
22
00:01:29,000 --> 00:01:36,000
In other words, Reflection API allows us to inspect or modify attributes of classis interfaces, fields
23
00:01:36,000 --> 00:01:38,000
and masses, duendes of program execution.
24
00:01:38,000 --> 00:01:42,000
That's a runtime that is basically after the compilation process.
25
00:01:42,000 --> 00:01:43,000
How would you like it?
26
00:01:43,000 --> 00:01:44,000
Tolerate it?
27
00:01:44,000 --> 00:01:52,000
Additionally, we can instantiate new objects, invoke methods and get a set feel to values using reflection.
28
00:01:52,000 --> 00:01:56,000
Even if Zapp Private Fields and Zendō Gatsas or satyrs.
29
00:01:57,000 --> 00:02:04,000
Probably you will not implement codes are related to reflection on daily basis, but 100 percent you
30
00:02:04,000 --> 00:02:09,000
will deal with code that uses reflection inside such libraries as Jeunet.
31
00:02:09,000 --> 00:02:15,000
Marqueta is Imark Power, Mock Sprink framework and many others.
32
00:02:15,000 --> 00:02:18,000
All of them use elements of reflection API.
33
00:02:19,000 --> 00:02:25,000
But as a consumer of the search party library, you just sanjoy and its features without implementing
34
00:02:25,000 --> 00:02:26,000
your own version of G unit.
35
00:02:27,000 --> 00:02:27,000
Correct.
36
00:02:28,000 --> 00:02:32,000
That's why you need to know what's the function is and how to work with it.
37
00:02:32,000 --> 00:02:39,000
This is relatively advanced feature and should be used only by developers who have a strong grasp of
38
00:02:39,000 --> 00:02:43,000
fundamentals of the language with that caveat in mind.
39
00:02:43,000 --> 00:02:50,000
Reflection is a powerful technique and can enable applications to perform operations which would otherwise
40
00:02:50,000 --> 00:02:51,000
be impossible.
41
00:02:52,000 --> 00:02:55,000
And what are the cases when we need the reflection API?
42
00:02:56,000 --> 00:03:02,000
Yeah, various cases when we need to use this API to achieve certain result that we provided was just
43
00:03:02,000 --> 00:03:03,000
a few examples.
44
00:03:04,000 --> 00:03:09,000
The first and probably the most simple case that I assume all of you have worked with, if you are following,
45
00:03:09,000 --> 00:03:11,000
is, of course, is the bargain.
46
00:03:11,000 --> 00:03:18,000
And thus does the bagasse need to be able to ximen private members on glosses.
47
00:03:18,000 --> 00:03:25,000
Desautels also needs to be able to get access, sometimes private masses like you saw in the case with
48
00:03:25,000 --> 00:03:25,000
Spohrer.
49
00:03:26,000 --> 00:03:33,000
Also, when it controls a sequence of mascot's execution to make sure that Nasus Annotated was before
50
00:03:33,000 --> 00:03:39,000
each annotation will be executed, before each test case, for example, or in case we need to initialize
51
00:03:39,000 --> 00:03:45,000
fields of an object, was Moncks and we need to do this on the for objects that that map was inject
52
00:03:45,000 --> 00:03:46,000
more annotations.
53
00:03:47,000 --> 00:03:51,000
A lot of things needed to be controlled by test tools and reflection.
54
00:03:51,000 --> 00:03:52,000
API helps on this.
55
00:03:53,000 --> 00:03:57,000
The next example of using reflection API is extensibility features.
56
00:03:58,000 --> 00:04:05,000
And the application may make use of external user defined classes by creating instances of extensibility
57
00:04:05,000 --> 00:04:08,000
objects using zarf fully qualified names.
58
00:04:09,000 --> 00:04:14,000
This is also relatively specific case and used Durrance advanced programming.
59
00:04:14,000 --> 00:04:19,000
But my goal is to make a pro coder from user and discourse, and I'm not joking.
60
00:04:20,000 --> 00:04:25,000
So we are also going to learn how to do this visual development environments.
61
00:04:25,000 --> 00:04:31,000
Imagine, though, that allows it to generate programming code or tool is used to perform any other
62
00:04:31,000 --> 00:04:32,000
visual development.
63
00:04:33,000 --> 00:04:38,000
And there is some digital information that this needed to be processed according to the specific rules.
64
00:04:38,000 --> 00:04:44,000
And now imagine that you can work in a visual development environment with the selections that you do
65
00:04:44,000 --> 00:04:45,000
inside.
66
00:04:46,000 --> 00:04:51,000
The whole development process might benefit from making use of that information available.
67
00:04:52,000 --> 00:04:57,000
This will help to make proper selections regarding behavior of some components and interactions between
68
00:04:57,000 --> 00:04:59,000
Zao HOVs it.
69
00:04:59,000 --> 00:05:02,000
Now you at least understands what swift action is for.
70
00:05:03,000 --> 00:05:03,000
Let's start.
71
00:05:03,000 --> 00:05:10,000
None of the details, all classes that are related to reflexion mechanism are already present in Jaedicke
72
00:05:10,000 --> 00:05:12,000
in Java, LANC reflect package.
73
00:05:12,000 --> 00:05:18,000
I encourage you to look through the official documentation after the lesson to make sure you can navigate
74
00:05:18,000 --> 00:05:20,000
between different types and just package easily.
75
00:05:21,000 --> 00:05:25,000
There are numerous of interfaces and glosses today during the lesson.
76
00:05:25,000 --> 00:05:27,000
We're going to review majority of them.
77
00:05:28,000 --> 00:05:33,000
And as you already understand, you would need to inkers classes from this package and your Java file.
78
00:05:34,000 --> 00:05:38,000
OK, so let me start screen sharing and proceed with code examples.
79
00:05:38,000 --> 00:05:44,000
Remember that all code examples from the lesson you can always find in attachments to this lesson?
80
00:05:44,000 --> 00:05:48,000
I opened them a class from a reflection package of my learning project.
81
00:05:48,000 --> 00:05:54,000
I will run a program to walk you through the console output and each line of code in this example.
82
00:05:55,000 --> 00:06:01,000
And the first thing to start here is to understand that after completion, class definitions uploaded
83
00:06:01,000 --> 00:06:02,000
into the GBM.
84
00:06:03,000 --> 00:06:07,000
And there is an object of type gloss for each type.
85
00:06:07,000 --> 00:06:12,000
Zet object contains type definition and API to interact with it.
86
00:06:13,000 --> 00:06:20,000
So we can say that the instances of the class class represent classes and interfaces in the running
87
00:06:20,000 --> 00:06:21,000
Java application.
88
00:06:22,000 --> 00:06:28,000
Basically, it will be fair to mention that the objects of class type may represent all the reference
89
00:06:28,000 --> 00:06:29,000
types.
90
00:06:29,000 --> 00:06:35,000
XOs includes, but not limited to arrays in annotations, cetera.
91
00:06:35,000 --> 00:06:41,000
That's why the first things that we need to do is to get the reference to the class object of the specific
92
00:06:41,000 --> 00:06:41,000
type.
93
00:06:41,000 --> 00:06:47,000
If you remember lesson about object class, then you should remember that one of the masses is that
94
00:06:47,000 --> 00:06:50,000
each object has is Geth class method.
95
00:06:51,000 --> 00:06:58,000
So I create an instance of the user class that is our nested closet I created here for the sake of example.
96
00:06:58,000 --> 00:07:02,000
And I call get class method an instance of the user object.
97
00:07:03,000 --> 00:07:09,000
Now, when I have reference to the class object of user time, we can play with the reflection API.
98
00:07:09,000 --> 00:07:12,000
Let me come up with some tasks us to solve.
99
00:07:12,000 --> 00:07:15,000
And we will learn different classes as we go.
100
00:07:15,000 --> 00:07:18,000
For example, let's get the foo class name.
101
00:07:19,000 --> 00:07:20,000
The API is pretty intuitive.
102
00:07:20,000 --> 00:07:23,000
So you just need to go get name method.
103
00:07:23,000 --> 00:07:27,000
As you can see, full class name is printed to consult.
104
00:07:28,000 --> 00:07:31,000
Take into account that the user is in master class.
105
00:07:31,000 --> 00:07:34,000
It has the lower sign after the name of the rupt class.
106
00:07:35,000 --> 00:07:40,000
This is a common convention in Java for the naming of compiler nested classes.
107
00:07:40,000 --> 00:07:42,000
Well, what was this first simple task?
108
00:07:43,000 --> 00:07:44,000
Let's jump to the next one.
109
00:07:45,000 --> 00:07:48,000
Let's find out names of all fields in user class.
110
00:07:49,000 --> 00:07:55,000
Pay attention that I have three non static fields and one static and user class, and they have private
111
00:07:55,000 --> 00:08:01,000
access modifier, no getters setters to declare to interact with these fields.
112
00:08:01,000 --> 00:08:08,000
Using the reference to the class object, we can get array of all fields, fields as a type that contains
113
00:08:08,000 --> 00:08:11,000
information about the field of a single class or interface.
114
00:08:12,000 --> 00:08:15,000
It also provides it was dynamic access to it.
115
00:08:15,000 --> 00:08:23,000
I invoked had declared fields to get the array of fields in which this mass returns all fields from
116
00:08:23,000 --> 00:08:23,000
the class.
117
00:08:24,000 --> 00:08:32,000
This includes public protected default or so-called package access and private fields, but excludes
118
00:08:32,000 --> 00:08:33,000
inherited fields.
119
00:08:33,000 --> 00:08:38,000
The results and other methods of the scope get fields and tensions and get fields.
120
00:08:38,000 --> 00:08:43,000
Massart returns on the public fields, and the one where broken was reflection.
121
00:08:43,000 --> 00:08:46,000
Often we need to get access to private fields, too.
122
00:08:47,000 --> 00:08:54,000
That's why in this particular case, I used another method get declared fields that chance all fields,
123
00:08:54,000 --> 00:08:56,000
including private ones.
124
00:08:57,000 --> 00:09:03,000
And after that, I create a stream out of this array and apply consumer function to each of the field.
125
00:09:03,000 --> 00:09:09,000
If you are not familiar with lambda expressions, functional interface and namely consumer type.
126
00:09:09,000 --> 00:09:13,000
Feel free to take my post functional programming for Jamma Ingenius.
127
00:09:13,000 --> 00:09:16,000
I explained these topics and details in that course.
128
00:09:17,000 --> 00:09:20,000
So as you can see, I it to console names of each field.
129
00:09:21,000 --> 00:09:24,000
And this is only one thing that we can do this field.
130
00:09:24,000 --> 00:09:26,000
We can do much more than this.
131
00:09:27,000 --> 00:09:29,000
Let me also show you other things.
132
00:09:29,000 --> 00:09:32,000
For example, you can explore that type of a field.
133
00:09:33,000 --> 00:09:35,000
Just code must be a generic type.
134
00:09:36,000 --> 00:09:42,000
As you can see, when I printed the return type object to console to string masses has been invoked
135
00:09:42,000 --> 00:09:44,000
and we see full gloss name.
136
00:09:44,000 --> 00:09:46,000
Also, you can check fields modifiers.
137
00:09:47,000 --> 00:09:49,000
Just go get modifiers.
138
00:09:49,000 --> 00:09:51,000
Massive, this method to chance.
139
00:09:51,000 --> 00:09:59,000
And that basically will tell you nothing if you just watch at number to the this end and understand
140
00:09:59,000 --> 00:10:03,000
its meaning when it to use another class from Java language.
141
00:10:03,000 --> 00:10:11,000
Package modifier, this clause has different measures that takes in as much argument and returned,
142
00:10:11,000 --> 00:10:12,000
true or false.
143
00:10:12,000 --> 00:10:20,000
We can check these modify Privert, Estatic, this public is protected, is final and many other things.
144
00:10:20,000 --> 00:10:27,000
And an example you can see that I call is private Massart and deposit into value rections from get modified
145
00:10:27,000 --> 00:10:28,000
SMAS.
146
00:10:29,000 --> 00:10:34,000
Considering that all fields have private modifying my example, I receive two for each field.
147
00:10:35,000 --> 00:10:41,000
Does it make sense, by the way, is a similar way you can check modifiers of classes and methods?
148
00:10:41,000 --> 00:10:48,000
I will not stop on each particular case, but just remember that it is also possible to check modifiers
149
00:10:48,000 --> 00:10:52,000
of all the class elements following this approach, using the class.
150
00:10:52,000 --> 00:10:56,000
I can get the package name, just schools, the package name, and that's it.
151
00:10:57,000 --> 00:10:59,000
Also, there is a specific type package.
152
00:11:00,000 --> 00:11:03,000
You can get the instance of this class by calling massive.
153
00:11:03,000 --> 00:11:07,000
Get the package if you need to do some operations was package object.
154
00:11:07,000 --> 00:11:12,000
If you want to investigate type Iraqui, you can always get the reference to the class.
155
00:11:12,000 --> 00:11:16,000
Object of superclass by column gets super class mass.
156
00:11:16,000 --> 00:11:20,000
The superclass for all reference types in Java is object.
157
00:11:21,000 --> 00:11:25,000
So I get the reference to the instance of object class.
158
00:11:25,000 --> 00:11:31,000
The next things that I'd like to show here is how you can check the interfaces that are implemented
159
00:11:31,000 --> 00:11:31,000
in class.
160
00:11:32,000 --> 00:11:35,000
I print name of each interface to console.
161
00:11:35,000 --> 00:11:37,000
And here you can see poufs.
162
00:11:37,000 --> 00:11:44,000
I have implemented serializable anticolonial interface in user Granit tensions in Zakouma Sync about
163
00:11:44,000 --> 00:11:45,000
all of this.
164
00:11:45,000 --> 00:11:49,000
You can explore a code of your program, you as a program execution.
165
00:11:50,000 --> 00:11:56,000
So you don't need a developer to investigate code of each program to create some useful utility classes,
166
00:11:57,000 --> 00:11:59,000
because code of each program is different.
167
00:12:00,000 --> 00:12:06,000
Instead, you can create a code that investigates the code zero, the execution.
168
00:12:06,000 --> 00:12:09,000
And on top of that, you can build the logic you wish.
169
00:12:10,000 --> 00:12:10,000
Let's move on.
170
00:12:11,000 --> 00:12:15,000
There's also another way to get the reference to the instance of a class.
171
00:12:15,000 --> 00:12:21,000
We can learn Java loss into Jamam by its name and get the reference to the class object.
172
00:12:21,000 --> 00:12:26,000
This is an important example, and we will do something like this later on in our course.
173
00:12:27,000 --> 00:12:32,000
Just use for names static mass and pass the full name of the class I pass.
174
00:12:32,000 --> 00:12:34,000
He has a full name of the method.
175
00:12:34,000 --> 00:12:38,000
Class will use this way of class instantiation.
176
00:12:38,000 --> 00:12:41,000
One, we actually don't need the object of this type.
177
00:12:41,000 --> 00:12:45,000
All we can is created and called get class method.
178
00:12:45,000 --> 00:12:48,000
That's why we can load the class by its name.
179
00:12:49,000 --> 00:12:51,000
Pay attention to this method.
180
00:12:51,000 --> 00:12:54,000
Invocation maestro class not found exception.
181
00:12:54,000 --> 00:12:56,000
This may happen in case you pass.
182
00:12:56,000 --> 00:12:59,000
Last names are just absent in the class boss.
183
00:13:00,000 --> 00:13:02,000
Also, we can get constructors.
184
00:13:02,000 --> 00:13:06,000
Constructor is a separate type from General Lenca effect package.
185
00:13:06,000 --> 00:13:09,000
Heavens, reference is a constructor object.
186
00:13:09,000 --> 00:13:11,000
We can do really a lot of sync's, was it?
187
00:13:12,000 --> 00:13:17,000
But for the sake of this example, only will prints number of constructors and their names.
188
00:13:18,000 --> 00:13:21,000
In this example, I will get declared constructor.
189
00:13:21,000 --> 00:13:30,000
SMAS is the name important is the same as in the masses, yet declared constructor means return of all
190
00:13:30,000 --> 00:13:31,000
constructors.
191
00:13:31,000 --> 00:13:35,000
Is that somebody Covid in the class, including private ones?
192
00:13:35,000 --> 00:13:42,000
There is another massive get constructors that Massata returns on the public constructors.
193
00:13:42,000 --> 00:13:45,000
There are also masses to the specific constructor.
194
00:13:45,000 --> 00:13:51,000
Get declared constructor and git constructor with class variable lengths argument.
195
00:13:51,000 --> 00:13:58,000
You can get the references, specific constructors that you need using the types of constructor parameters.
196
00:13:59,000 --> 00:14:04,000
For example, here I want to get the reference to the private constructor parameter of type string.
197
00:14:04,000 --> 00:14:08,000
I'm just possessory Gloucester's and Massett invocation.
198
00:14:08,000 --> 00:14:14,000
This massive miswrote two exceptions is the first one is no such massive exception.
199
00:14:15,000 --> 00:14:20,000
This exceptionable strong JS constructor was a specified parameter types and wasn't found.
200
00:14:20,000 --> 00:14:24,000
And the second exceptions may be strong is a security exception.
201
00:14:24,000 --> 00:14:29,000
This exception might be sold by the security manager to indicate a security violation.
202
00:14:30,000 --> 00:14:36,000
As you can see in console, we have managed to get the reference to a private constructor, the similar
203
00:14:36,000 --> 00:14:37,000
way we can work with masses.
204
00:14:38,000 --> 00:14:45,000
We get all masses, including privates, once we can get on the public masses and we can get specific
205
00:14:45,000 --> 00:14:45,000
masses.
206
00:14:46,000 --> 00:14:47,000
Let me show you a few examples here.
207
00:14:48,000 --> 00:14:53,000
I cogat declare mass it's massive and print massive names to console.
208
00:14:53,000 --> 00:15:00,000
In the next example here, I'm getting massive by its name and by list of these arguments.
209
00:15:00,000 --> 00:15:03,000
This is how we can separate overload.
210
00:15:03,000 --> 00:15:04,000
That's massive.
211
00:15:05,000 --> 00:15:08,000
The first message argument is a method name, just ring value.
212
00:15:09,000 --> 00:15:12,000
And after that, those verbal length's arguments of that class.
213
00:15:13,000 --> 00:15:16,000
I want to get the message that takes user argument.
214
00:15:16,000 --> 00:15:21,000
That's why I passed pass user class here and the printers and Massett to consult.
215
00:15:22,000 --> 00:15:28,000
As you can see, we have managed to get the object of representation of this massive to perform any
216
00:15:28,000 --> 00:15:29,000
actions we need.
217
00:15:29,000 --> 00:15:31,000
Was it when we tried to get.
218
00:15:32,000 --> 00:15:37,000
Always remember that we can face was no such massive exception and security exception.
219
00:15:38,000 --> 00:15:44,000
These are checked exceptions, and I will recommend you to handle those exceptions for the sake of the
220
00:15:44,000 --> 00:15:45,000
demo.
221
00:15:45,000 --> 00:15:49,000
I put them next Ostrowsky in my main message.
222
00:15:49,000 --> 00:15:50,000
OK.
223
00:15:50,000 --> 00:15:52,000
They learned the API and the high level.
224
00:15:53,000 --> 00:15:58,000
Now you know how you can get the reference to the definition of type elements, such as fields, constructors,
225
00:15:58,000 --> 00:16:03,000
methods, interface, a simple amount of superclass, et cetera.
226
00:16:03,000 --> 00:16:09,000
Now let's understand this topic and understand how to work with object of these types.
227
00:16:09,000 --> 00:16:14,000
Namely, I want you to understand what we can do with them.
228
00:16:14,000 --> 00:16:16,000
Let me start from the constructor.
229
00:16:17,000 --> 00:16:22,000
I am going to show you what important operations we may perform with objects of this type.
230
00:16:22,000 --> 00:16:25,000
I get the reference to the default constructor.
231
00:16:25,000 --> 00:16:32,000
And also I am going to use the reference to a constructor with parameter that we got in previous examples.
232
00:16:32,000 --> 00:16:34,000
Hence the reference to the constructor.
233
00:16:35,000 --> 00:16:41,000
We can create an instance of this class by calling new instance method, and in case we need to pass
234
00:16:41,000 --> 00:16:44,000
some arguments to instantiate the object of this type.
235
00:16:44,000 --> 00:16:46,000
We just passed them and that's it.
236
00:16:47,000 --> 00:16:54,000
I print these user objects to consult that tensions that I have managed even to instantiate object based
237
00:16:54,000 --> 00:16:56,000
on the private constructor.
238
00:16:56,000 --> 00:17:03,000
I have just another email and the email field was initialized in user object was valued even by me.
239
00:17:04,000 --> 00:17:05,000
Isn't that cool?
240
00:17:05,000 --> 00:17:08,000
Do you feel the power of reflection already or not yet?
241
00:17:09,000 --> 00:17:11,000
Let me show you another trick was a few.
242
00:17:11,000 --> 00:17:15,000
All our fields declared in user gloss are private.
243
00:17:15,000 --> 00:17:18,000
And this makes this trick even more interesting.
244
00:17:18,000 --> 00:17:24,000
Different frameworks may insert those in the field, even if there are no public systems.
245
00:17:24,000 --> 00:17:26,000
For example, Mosquita framework.
246
00:17:27,000 --> 00:17:31,000
When they use annotation inject, most fields of compatable was marked.
247
00:17:31,000 --> 00:17:36,000
Darbs are populated with mock objects or in spring framework.
248
00:17:36,000 --> 00:17:42,000
When I configure my spring events, I described object of what types I needed to be injected.
249
00:17:42,000 --> 00:17:45,000
Now I'm going to show you how to do that.
250
00:17:46,000 --> 00:17:51,000
For example, let's give the reference to the first name field and initialize it.
251
00:17:51,000 --> 00:17:55,000
You already know that I need to use get declared fields Massud.
252
00:17:55,000 --> 00:18:01,000
I can extract fields that they need by its name in addition to all previous exceptions.
253
00:18:01,000 --> 00:18:04,000
This massive, nice rule, no such field exception.
254
00:18:05,000 --> 00:18:10,000
I believe that from exception name, you can understand when this exception will be thrown.
255
00:18:11,000 --> 00:18:15,000
The first things that we need to do now is to set accessable mode.
256
00:18:15,000 --> 00:18:18,000
This is needed for private entities inside the class.
257
00:18:19,000 --> 00:18:25,000
Oh, with the exception, Joon's interaction with this entity I call set accessible to.
258
00:18:25,000 --> 00:18:30,000
After this, I can set the value to the field without any fear of exception.
259
00:18:31,000 --> 00:18:35,000
The whole region knows that field is a property of some object.
260
00:18:35,000 --> 00:18:37,000
Xus to initialize the field.
261
00:18:38,000 --> 00:18:42,000
We need to specify the object where we want to initialize this field.
262
00:18:43,000 --> 00:18:43,000
Does it make sense?
263
00:18:44,000 --> 00:18:48,000
Massart set of the field takes two massive arguments.
264
00:18:48,000 --> 00:18:52,000
The first one is the object that is used to initialize field.
265
00:18:52,000 --> 00:18:55,000
And the second one is a values that will be inserted.
266
00:18:55,000 --> 00:18:58,000
Those that fields one that brings the object to console.
267
00:18:58,000 --> 00:19:04,000
You can notice that first name field was updated with the values that we have just sent.
268
00:19:05,000 --> 00:19:11,000
One more thing thread here is that for certain primitives, types exist, separate masses like set in,
269
00:19:12,000 --> 00:19:19,000
said double seven, boolean, etc. For all reference types, you can use set methods that takes object
270
00:19:19,000 --> 00:19:23,000
s first methods, arguments, what to do in case there is a static field.
271
00:19:23,000 --> 00:19:26,000
Everson's is the same beside Wansink.
272
00:19:26,000 --> 00:19:30,000
You don't need to parse the reference to an object in this case.
273
00:19:30,000 --> 00:19:36,000
Just personal instead, like I do in this example, you can see that static.
274
00:19:36,000 --> 00:19:38,000
This has another value.
275
00:19:38,000 --> 00:19:43,000
Now we learned how to set field values, but how to read fields values.
276
00:19:43,000 --> 00:19:44,000
Very simple.
277
00:19:45,000 --> 00:19:46,000
You need to use gets Massart.
278
00:19:47,000 --> 00:19:48,000
The algorithm is the same.
279
00:19:49,000 --> 00:19:51,000
You get the reference to fields first.
280
00:19:51,000 --> 00:19:52,000
We already did that.
281
00:19:53,000 --> 00:20:00,000
So I would just use first name field and I'd field after that to change access mode in case fields are
282
00:20:00,000 --> 00:20:01,000
private.
283
00:20:01,000 --> 00:20:03,000
We have a really cool set.
284
00:20:03,000 --> 00:20:05,000
Sesame masses for both fields.
285
00:20:05,000 --> 00:20:07,000
That's why I don't do it again.
286
00:20:08,000 --> 00:20:10,000
And after that, I just get massive.
287
00:20:11,000 --> 00:20:17,000
I need to pass reference to the object where I want to read field in this field to study.
288
00:20:17,000 --> 00:20:19,000
You shouldn't pass that object.
289
00:20:20,000 --> 00:20:22,000
Is this clear in console?
290
00:20:22,000 --> 00:20:25,000
You can see how I managed to extract the first name.
291
00:20:25,000 --> 00:20:31,000
And I leave this for primitive tribes, use a separate group of masses like get in together, boolean,
292
00:20:31,000 --> 00:20:35,000
etc. to get the value of reference types.
293
00:20:35,000 --> 00:20:38,000
You can go just get massive, right in our example.
294
00:20:39,000 --> 00:20:42,000
Let me show you how we can work with masses.
295
00:20:42,000 --> 00:20:45,000
We can involve them on the object when we need.
296
00:20:46,000 --> 00:20:50,000
Basically, we have two masses that can help us to get the reference to the object.
297
00:20:50,000 --> 00:20:54,000
Masses get declared method and get massive.
298
00:20:55,000 --> 00:21:01,000
If I want to get a reference to the private mass, I have to use get declared mass in all cases.
299
00:21:01,000 --> 00:21:06,000
And it was the strings that contains massive name that I would like to get.
300
00:21:06,000 --> 00:21:10,000
And additionally, we need to pass colossus of parameter such.
301
00:21:11,000 --> 00:21:13,000
This is very blancs argument.
302
00:21:13,000 --> 00:21:18,000
So basically you can posse's and nothing here or colossus of all arguments.
303
00:21:19,000 --> 00:21:26,000
I plus private method name and sue here because this massive base string how I can find this urines
304
00:21:26,000 --> 00:21:33,000
around that again, just those examples of how you can get information about all masses in class after
305
00:21:33,000 --> 00:21:38,000
having the references to all masses, you can expose any names and parameters.
306
00:21:38,000 --> 00:21:42,000
That's how you can build code that explores the program.
307
00:21:42,000 --> 00:21:43,000
Jawanza runtime.
308
00:21:44,000 --> 00:21:49,000
After I have reference to this method, I have to call set accessible to true.
309
00:21:50,000 --> 00:21:56,000
Since this is private mass, I need to perform this massive communication with Iran as a program execution.
310
00:21:57,000 --> 00:21:59,000
After that, I can invoke Massett.
311
00:22:00,000 --> 00:22:02,000
And again, similarities and no static fields.
312
00:22:02,000 --> 00:22:07,000
I need an object doing the rows and massive all because massive.
313
00:22:07,000 --> 00:22:09,000
It is a behavior of an object.
314
00:22:09,000 --> 00:22:13,000
So once I get the mass, I can invoke the Private Massett on an object.
315
00:22:14,000 --> 00:22:18,000
And you can see that I postering argument do in console.
316
00:22:18,000 --> 00:22:20,000
You might notice a private mass has been called.
317
00:22:21,000 --> 00:22:22,000
How do you like it?
318
00:22:23,000 --> 00:22:26,000
As you can see, there are no more orders for reflection.
319
00:22:26,000 --> 00:22:30,000
API and encapsulation means nothing for reflection.
320
00:22:30,000 --> 00:22:37,000
By the way, this is also one of the reasons not to use the reflection API, because it may violate
321
00:22:37,000 --> 00:22:37,000
encapsulation.
322
00:22:38,000 --> 00:22:44,000
Reflection that you may access fields and perform operations that you are not supposed to perform.
323
00:22:44,000 --> 00:22:48,000
But as we discussed Zimride, the case is one reflection.
324
00:22:48,000 --> 00:22:54,000
API is needed to perform some actions that will make the development process easier.
325
00:22:54,000 --> 00:22:56,000
For example, was aesthetic Massart.
326
00:22:56,000 --> 00:22:59,000
Everson is the same beside one sink.
327
00:22:59,000 --> 00:23:03,000
You shouldn't parse the reference to an object to static muscle.
328
00:23:04,000 --> 00:23:06,000
Here's an example of static mess recreation.
329
00:23:07,000 --> 00:23:09,000
As you can see, there is nothing special.
330
00:23:10,000 --> 00:23:12,000
That's all what I wanted to share with you today.
331
00:23:13,000 --> 00:23:15,000
Let's review what we have learned in this lesson.
332
00:23:16,000 --> 00:23:21,000
In this lesson we learned was a reflection API is and why we need it.
333
00:23:21,000 --> 00:23:28,000
We reviewed that reflect package, and after that we started our reflection API on examples.
334
00:23:29,000 --> 00:23:36,000
You saw how we can work with different closs entities such as fields, constructors, methods, interfaces,
335
00:23:36,000 --> 00:23:39,000
super classes with the help of a section API.
336
00:23:40,000 --> 00:23:45,000
Separate group of examples related was private class entities and how to work with them.
337
00:23:45,000 --> 00:23:48,000
Private constructors, private masses, sprout's fields.
338
00:23:49,000 --> 00:23:52,000
That's all what I plan to share with you today.
339
00:23:52,000 --> 00:23:53,000
Thanks a lot for your attention.
340
00:23:54,000 --> 00:23:56,000
Have a great day and see you in the next lesson.
|