Hello, Jim.
In this lesson, we're going to learn a really interesting topic that is Skulled Jallow Reflexion API.
We have a lot of things to learn today, but as usual, we're going to start from Seery and Grado.
It will get to the code examples.
So it's the beginning of the lesson we're going to learn.
What a reflection API is and what rules does adapt will review Java like reflect package in order,
you know, where you can look for the official documentation.
And after that, we will start our protocol.
Part of the lesson I'm going to show you how you can grow up with different class entities such as fields,
constructors, mass interfaces.
Superclusters was a help of reflection API.
I will put special focus on a separate group of examples that relates to the work with private entities
inside the class.
As I already said, there will be a lot of interesting things.
Believe me, this will be mind blowing when you understand what you can do with the help of Reflection
API.
So let's start our lesson.
And to start with, let's understand first what's reflexion is one of reflection is used and why do
we need it?
Reflection is a commonly used by programs which requires and devoted to X in line or modifies the runtime
behavior of applications running into Java virtual machine.
In other words, Reflection API allows us to inspect or modify attributes of classis interfaces, fields
and masses, duendes of program execution.
That's a runtime that is basically after the compilation process.
How would you like it?
Tolerate it?
Additionally, we can instantiate new objects, invoke methods and get a set feel to values using reflection.
Even if Zapp Private Fields and Zendō Gatsas or satyrs.
Probably you will not implement codes are related to reflection on daily basis, but 100 percent you
will deal with code that uses reflection inside such libraries as Jeunet.
Marqueta is Imark Power, Mock Sprink framework and many others.
All of them use elements of reflection API.
But as a consumer of the search party library, you just sanjoy and its features without implementing
your own version of G unit.
Correct.
That's why you need to know what's the function is and how to work with it.
This is relatively advanced feature and should be used only by developers who have a strong grasp of
fundamentals of the language with that caveat in mind.
Reflection is a powerful technique and can enable applications to perform operations which would otherwise
be impossible.
And what are the cases when we need the reflection API?
Yeah, various cases when we need to use this API to achieve certain result that we provided was just
a few examples.
The first and probably the most simple case that I assume all of you have worked with, if you are following,
is, of course, is the bargain.
And thus does the bagasse need to be able to ximen private members on glosses.
Desautels also needs to be able to get access, sometimes private masses like you saw in the case with
Spohrer.
Also, when it controls a sequence of mascot's execution to make sure that Nasus Annotated was before
each annotation will be executed, before each test case, for example, or in case we need to initialize
fields of an object, was Moncks and we need to do this on the for objects that that map was inject
more annotations.
A lot of things needed to be controlled by test tools and reflection.
API helps on this.
The next example of using reflection API is extensibility features.
And the application may make use of external user defined classes by creating instances of extensibility
objects using zarf fully qualified names.
This is also relatively specific case and used Durrance advanced programming.
But my goal is to make a pro coder from user and discourse, and I'm not joking.
So we are also going to learn how to do this visual development environments.
Imagine, though, that allows it to generate programming code or tool is used to perform any other
visual development.
And there is some digital information that this needed to be processed according to the specific rules.
And now imagine that you can work in a visual development environment with the selections that you do
inside.
The whole development process might benefit from making use of that information available.
This will help to make proper selections regarding behavior of some components and interactions between
Zao HOVs it.
Now you at least understands what swift action is for.
Let's start.
None of the details, all classes that are related to reflexion mechanism are already present in Jaedicke
in Java, LANC reflect package.
I encourage you to look through the official documentation after the lesson to make sure you can navigate
between different types and just package easily.
There are numerous of interfaces and glosses today during the lesson.
We're going to review majority of them.
And as you already understand, you would need to inkers classes from this package and your Java file.
OK, so let me start screen sharing and proceed with code examples.
Remember that all code examples from the lesson you can always find in attachments to this lesson?
I opened them a class from a reflection package of my learning project.
I will run a program to walk you through the console output and each line of code in this example.
And the first thing to start here is to understand that after completion, class definitions uploaded
into the GBM.
And there is an object of type gloss for each type.
Zet object contains type definition and API to interact with it.
So we can say that the instances of the class class represent classes and interfaces in the running
Java application.
Basically, it will be fair to mention that the objects of class type may represent all the reference
types.
XOs includes, but not limited to arrays in annotations, cetera.
That's why the first things that we need to do is to get the reference to the class object of the specific
type.
If you remember lesson about object class, then you should remember that one of the masses is that
each object has is Geth class method.
So I create an instance of the user class that is our nested closet I created here for the sake of example.
And I call get class method an instance of the user object.
Now, when I have reference to the class object of user time, we can play with the reflection API.
Let me come up with some tasks us to solve.
And we will learn different classes as we go.
For example, let's get the foo class name.
The API is pretty intuitive.
So you just need to go get name method.
As you can see, full class name is printed to consult.
Take into account that the user is in master class.
It has the lower sign after the name of the rupt class.
This is a common convention in Java for the naming of compiler nested classes.
Well, what was this first simple task?
Let's jump to the next one.
Let's find out names of all fields in user class.
Pay attention that I have three non static fields and one static and user class, and they have private
access modifier, no getters setters to declare to interact with these fields.
Using the reference to the class object, we can get array of all fields, fields as a type that contains
information about the field of a single class or interface.
It also provides it was dynamic access to it.
I invoked had declared fields to get the array of fields in which this mass returns all fields from
the class.
This includes public protected default or so-called package access and private fields, but excludes
inherited fields.
The results and other methods of the scope get fields and tensions and get fields.
Massart returns on the public fields, and the one where broken was reflection.
Often we need to get access to private fields, too.
That's why in this particular case, I used another method get declared fields that chance all fields,
including private ones.
And after that, I create a stream out of this array and apply consumer function to each of the field.
If you are not familiar with lambda expressions, functional interface and namely consumer type.
Feel free to take my post functional programming for Jamma Ingenius.
I explained these topics and details in that course.
So as you can see, I it to console names of each field.
And this is only one thing that we can do this field.
We can do much more than this.
Let me also show you other things.
For example, you can explore that type of a field.
Just code must be a generic type.
As you can see, when I printed the return type object to console to string masses has been invoked
and we see full gloss name.
Also, you can check fields modifiers.
Just go get modifiers.
Massive, this method to chance.
And that basically will tell you nothing if you just watch at number to the this end and understand
its meaning when it to use another class from Java language.
Package modifier, this clause has different measures that takes in as much argument and returned,
true or false.
We can check these modify Privert, Estatic, this public is protected, is final and many other things.
And an example you can see that I call is private Massart and deposit into value rections from get modified
SMAS.
Considering that all fields have private modifying my example, I receive two for each field.
Does it make sense, by the way, is a similar way you can check modifiers of classes and methods?
I will not stop on each particular case, but just remember that it is also possible to check modifiers
of all the class elements following this approach, using the class.
I can get the package name, just schools, the package name, and that's it.
Also, there is a specific type package.
You can get the instance of this class by calling massive.
Get the package if you need to do some operations was package object.
If you want to investigate type Iraqui, you can always get the reference to the class.
Object of superclass by column gets super class mass.
The superclass for all reference types in Java is object.
So I get the reference to the instance of object class.
The next things that I'd like to show here is how you can check the interfaces that are implemented
in class.
I print name of each interface to console.
And here you can see poufs.
I have implemented serializable anticolonial interface in user Granit tensions in Zakouma Sync about
all of this.
You can explore a code of your program, you as a program execution.
So you don't need a developer to investigate code of each program to create some useful utility classes,
because code of each program is different.
Instead, you can create a code that investigates the code zero, the execution.
And on top of that, you can build the logic you wish.
Let's move on.
There's also another way to get the reference to the instance of a class.
We can learn Java loss into Jamam by its name and get the reference to the class object.
This is an important example, and we will do something like this later on in our course.
Just use for names static mass and pass the full name of the class I pass.
He has a full name of the method.
Class will use this way of class instantiation.
One, we actually don't need the object of this type.
All we can is created and called get class method.
That's why we can load the class by its name.
Pay attention to this method.
Invocation maestro class not found exception.
This may happen in case you pass.
Last names are just absent in the class boss.
Also, we can get constructors.
Constructor is a separate type from General Lenca effect package.
Heavens, reference is a constructor object.
We can do really a lot of sync's, was it?
But for the sake of this example, only will prints number of constructors and their names.
In this example, I will get declared constructor.
SMAS is the name important is the same as in the masses, yet declared constructor means return of all
constructors.
Is that somebody Covid in the class, including private ones?
There is another massive get constructors that Massata returns on the public constructors.
There are also masses to the specific constructor.
Get declared constructor and git constructor with class variable lengths argument.
You can get the references, specific constructors that you need using the types of constructor parameters.
For example, here I want to get the reference to the private constructor parameter of type string.
I'm just possessory Gloucester's and Massett invocation.
This massive miswrote two exceptions is the first one is no such massive exception.
This exceptionable strong JS constructor was a specified parameter types and wasn't found.
And the second exceptions may be strong is a security exception.
This exception might be sold by the security manager to indicate a security violation.
As you can see in console, we have managed to get the reference to a private constructor, the similar
way we can work with masses.
We get all masses, including privates, once we can get on the public masses and we can get specific
masses.
Let me show you a few examples here.
I cogat declare mass it's massive and print massive names to console.
In the next example here, I'm getting massive by its name and by list of these arguments.
This is how we can separate overload.
That's massive.
The first message argument is a method name, just ring value.
And after that, those verbal length's arguments of that class.
I want to get the message that takes user argument.
That's why I passed pass user class here and the printers and Massett to consult.
As you can see, we have managed to get the object of representation of this massive to perform any
actions we need.
Was it when we tried to get.
Always remember that we can face was no such massive exception and security exception.
These are checked exceptions, and I will recommend you to handle those exceptions for the sake of the
demo.
I put them next Ostrowsky in my main message.
OK.
They learned the API and the high level.
Now you know how you can get the reference to the definition of type elements, such as fields, constructors,
methods, interface, a simple amount of superclass, et cetera.
Now let's understand this topic and understand how to work with object of these types.
Namely, I want you to understand what we can do with them.
Let me start from the constructor.
I am going to show you what important operations we may perform with objects of this type.
I get the reference to the default constructor.
And also I am going to use the reference to a constructor with parameter that we got in previous examples.
Hence the reference to the constructor.
We can create an instance of this class by calling new instance method, and in case we need to pass
some arguments to instantiate the object of this type.
We just passed them and that's it.
I print these user objects to consult that tensions that I have managed even to instantiate object based
on the private constructor.
I have just another email and the email field was initialized in user object was valued even by me.
Isn't that cool?
Do you feel the power of reflection already or not yet?
Let me show you another trick was a few.
All our fields declared in user gloss are private.
And this makes this trick even more interesting.
Different frameworks may insert those in the field, even if there are no public systems.
For example, Mosquita framework.
When they use annotation inject, most fields of compatable was marked.
Darbs are populated with mock objects or in spring framework.
When I configure my spring events, I described object of what types I needed to be injected.
Now I'm going to show you how to do that.
For example, let's give the reference to the first name field and initialize it.
You already know that I need to use get declared fields Massud.
I can extract fields that they need by its name in addition to all previous exceptions.
This massive, nice rule, no such field exception.
I believe that from exception name, you can understand when this exception will be thrown.
The first things that we need to do now is to set accessable mode.
This is needed for private entities inside the class.
Oh, with the exception, Joon's interaction with this entity I call set accessible to.
After this, I can set the value to the field without any fear of exception.
The whole region knows that field is a property of some object.
Xus to initialize the field.
We need to specify the object where we want to initialize this field.
Does it make sense?
Massart set of the field takes two massive arguments.
The first one is the object that is used to initialize field.
And the second one is a values that will be inserted.
Those that fields one that brings the object to console.
You can notice that first name field was updated with the values that we have just sent.
One more thing thread here is that for certain primitives, types exist, separate masses like set in,
said double seven, boolean, etc. For all reference types, you can use set methods that takes object
s first methods, arguments, what to do in case there is a static field.
Everson's is the same beside Wansink.
You don't need to parse the reference to an object in this case.
Just personal instead, like I do in this example, you can see that static.
This has another value.
Now we learned how to set field values, but how to read fields values.
Very simple.
You need to use gets Massart.
The algorithm is the same.
You get the reference to fields first.
We already did that.
So I would just use first name field and I'd field after that to change access mode in case fields are
private.
We have a really cool set.
Sesame masses for both fields.
That's why I don't do it again.
And after that, I just get massive.
I need to pass reference to the object where I want to read field in this field to study.
You shouldn't pass that object.
Is this clear in console?
You can see how I managed to extract the first name.
And I leave this for primitive tribes, use a separate group of masses like get in together, boolean,
etc. to get the value of reference types.
You can go just get massive, right in our example.
Let me show you how we can work with masses.
We can involve them on the object when we need.
Basically, we have two masses that can help us to get the reference to the object.
Masses get declared method and get massive.
If I want to get a reference to the private mass, I have to use get declared mass in all cases.
And it was the strings that contains massive name that I would like to get.
And additionally, we need to pass colossus of parameter such.
This is very blancs argument.
So basically you can posse's and nothing here or colossus of all arguments.
I plus private method name and sue here because this massive base string how I can find this urines
around that again, just those examples of how you can get information about all masses in class after
having the references to all masses, you can expose any names and parameters.
That's how you can build code that explores the program.
Jawanza runtime.
After I have reference to this method, I have to call set accessible to true.
Since this is private mass, I need to perform this massive communication with Iran as a program execution.
After that, I can invoke Massett.
And again, similarities and no static fields.
I need an object doing the rows and massive all because massive.
It is a behavior of an object.
So once I get the mass, I can invoke the Private Massett on an object.
And you can see that I postering argument do in console.
You might notice a private mass has been called.
How do you like it?
As you can see, there are no more orders for reflection.
API and encapsulation means nothing for reflection.
By the way, this is also one of the reasons not to use the reflection API, because it may violate
encapsulation.
Reflection that you may access fields and perform operations that you are not supposed to perform.
But as we discussed Zimride, the case is one reflection.
API is needed to perform some actions that will make the development process easier.
For example, was aesthetic Massart.
Everson is the same beside one sink.
You shouldn't parse the reference to an object to static muscle.
Here's an example of static mess recreation.
As you can see, there is nothing special.
That's all what I wanted to share with you today.
Let's review what we have learned in this lesson.
In this lesson we learned was a reflection API is and why we need it.
We reviewed that reflect package, and after that we started our reflection API on examples.
You saw how we can work with different closs entities such as fields, constructors, methods, interfaces,
super classes with the help of a section API.
Separate group of examples related was private class entities and how to work with them.
Private constructors, private masses, sprout's fields.
That's all what I plan to share with you today.
Thanks a lot for your attention.
Have a great day and see you in the next lesson.