| 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. |
|
|
|
|