In this video, we will have a look into hooking and constructor.
I mean, this is not a big deal, but it's nice to know how we can do this.
So when an object is being created, the constructor is being caught and hooking this one is just a
little bit different from what we have learned so far, but only just therefore let's find this out
right now.
So here we are back in Ubuntu VM and now let's have a look into the following application.
So we get back our smartphone screen.
All right.
And then we have already played around with this free to instance application.
This one over here.
So here we can see we've gotten in both instance, and if we hit this boss instance several times,
it disappears just only on the screen and our item appears over here.
So this means this item has been bound to this box object.
And we can also see this in the in the application source code.
So if we go into EPS free to insert and then let's open this with the screen.
Okay then.
But then come picking a free to insert.
We've cut this class over here and there we have it.
This one is the constructor of the sports and we can see this one requires an item.
This item class can be found over here.
So double clicking.
There we have it.
This is now the item.
And the last time we did play around with this application, we did create a new item.
And based on this item, we did create a new boss instance.
Let's have a look into the graphic template.
What's the difference right now between this topic from the last time regarding to this one, hooking
the constructor over here?
All right.
Let's have a look.
Okay.
So here we have back on the graphic tablet.
And the last time we did play around with this application with the default.
So we did create a new boss instance, for example, this one over here, and this was our newly generated
boss instance.
But here we did run into a problem.
This boss instance requires an item as in parameter and this one over here.
So we also had to create a new item first which can be then used as a parameter for this instance over
here.
Therefore we did also create new item, for example, this one over here.
Okay, so this was the newly generated item instance and I guess this has an parameter regarding to
the power level of this item.
So I'm not sure.
But I guess we did put this to 9000.
By having this newly generated instance, we could use it within our constructor over here, within
our newly generated boss object.
Oh, right.
And we did this quite often.
So we did not only create one post instance, we did create an sure, maybe ten or more.
So all over here there were some post instances and this one and this one and this one and this one.
So this was the approach regarding to the last time.
We did also play a little bit around with constructors over here without really knowing this, but this
time we want to make it differently.
What we want to do right now is the following Instead of creating new objects, we want to rewrite the
creation of an object, for example, on this boss instance over here.
And this is exactly what we can do by hooking the constructor.
Let me show what I mean over here.
And so within the on create method of our activity.
We are first generating a new item.
And afterwards we are creating this post instance over here.
And we are using exactly this item we have created over here.
Okay.
Now, we do not want to create a new boss.
We want to interfere or modify the creation of this boss over here.
And therefore, we can hook the constructor of, for example, this boss, or also this new item generation.
So this boss over here appears on our screen.
And if he hit ten times this boss, then we get back our skill set item.
This one over here.
And this means if we are now defeating this boss, then we want to have an item which has not the power
level.
For example, 900.
It has the power level 9000.
Okay.
So this is the difference regarding to the last approach and this approach.
Within the last approach, we did create new instances and now we are interfering the creation of a
new instance over here, and we manipulate it in a way that we want to have it.
Absolutely amazing.
Right.
And it's also not that complex.
So we just get introduced to some new keywords, I guess only one or two.
But we will find this out right now.
So let's see how we can hook and construct that.
The creation of New Object with Frieda.
Let's start right now.
Okay.
Back at Ubuntu VM.
Let's defeat this boss and here we have our skill set.
But this is not the item we want to have.
We want to have a more powerful item.
So therefore, let's find out how we can modify this one over here.
Okay.
So we open up our Visual Studio code editor and now let's have a look.
So, okay, so here we are having this item class and we can see this item has this item power which
is being assigned to this phone.
So when we are hooking our constructor, we can observe which item power will be provided to this one
over here to this object, and we can find this in the main activity.
So let's open this one.
Me.
You have it.
Within our original code, we are creating an item that has 1000 as in power level OC so we can observe
and verify if our constructor hooking has been successful.
So over here we should have some parameter within our constructor with the level 1000 and then we are
going to overwrite it over here.
So this one over here, yes.
We going to override this item power level OC and then we can search through our instances, the existing
ones and we can identify this item.
Yes.
And this should then afterwards have our newly assigned power level.
All right.
This is how we can approve and verify if our hook has been successful.
And now let's dive into this hook.
So we put this on the right in this one on the left.
And now let's see.
As always, the first thing we need to do is we need to close this update window and then we enable
JavaScript.
Okay.
And now we are dealing with an class over here so we can think of this constructor like in class method.
I mean, this might not be correct regarding to the term, but regarding to our free the hooking, we
can think of this in this way.
And this means we need to use Java use to get this class reference and then we need to access the constructor
and define a new implementation for this one.
Alrighty, let's do this.
So here we are connecting to the JDM.
We are providing our anonymous function and here we are having our code.
So we need to first get the class reference.
So our item class reference is now this package.
So we need to use.
Do we still remember this one?
Of course you do.
Because we had a lot of practice regarding to this one.
It's, of course, Java dudes.
All right.
Package name, dot class name.
Let's make this a little bit wider.
Okay.
So here we are having now our class reference.
And that was the question how can we access this constructor?
If you're having a method in here, we would simply use this method name.
But regarding to a constructor, we only have this one over here.
And therefore Frieda has also introduced a new keyword.
The new keyword is now dollar in it.
So with dollar in it, we can now access the constructor, which is this one in line seven.
So all we need to do is we need to use this dollar in it and then define a new implementation.
We will run into a problem here, but we do not know this right now.
So therefore let's encounter this problem together.
And don't worry, we will know how to fix this afterwards.
All right.
Okay, so here we are having our dollar init implementation and now we want to have a new implementation
and this is our parameter or our power level and we can then loop sorry and we can print this out first.
So we need to reset this.
So let's print this out.
Item power of the generated or let's say create that instance is and then let's put an equal sign and
this parameter.
Okay, so first let's have a look if this one over here is working, if this one works, then we can
extend it and modify these values.
But at the beginning, keep it simple.
So verify if if basic scripts work.
If you are encountering some errors over here, we do not have to do a huge investigation because we
only have these three lines of code.
All right.
Okay.
So now let's see.
Let's get back to screen.
Here we have it.
We can leave it open.
I mean, that's absolutely fine.
And then we need to connect to our smartphone and we need to start the free server.
So within data logger ten.
Okay.
And this one is running in the background.
Amazing animation.
And here we are connecting to this process.
So this one is free.
But as you come up hacking free to insert, if I remember correctly.
No, I do not remember correctly.
So let's have a look.
Free the intense.
Yeah.
There we have it.
All right.
So let's see.
Yes.
If you paste this right now into this one, we can see nothing happens.
The reason for this is the method or this constructor has been already called.
This object exists and unless we are not creating a new one, then we also do not see this implementation
over here.
Therefore, we have already encountered an important point.
If we are dealing with constructors, it might be a good idea to already provide your script at the
start up.
So let's save the script over here.
Safe as and then.
It's not a space paying application.
This one is free to insert.
Not having a script in here.
That's ridiculous.
But nevertheless.
So this one is the constructor, Chase.
Okay.
So we closed this one and now we applying the script over here apps for free to insert.
And then we only have this one and we need to start this application and we do not want to have them
pause and make this a little bit larger that you can see this in one line.
Here we have it.
All right.
So now we are starting this application, attending our free script.
It will resume.
And then if the constructor of the item class gets code, we should see this this output over here.
All right, let's try this.
And here we have it.
Yes I took power of the created instance is and so on.
Nice.
So we have we have hooked this constructor.
And to be honest, I would have assumed to have a problem over here.
And so the problem I think we would face right now is the falling.
Most of the time we are having an default constructor that has no parameters in there.
So if you take a look back into our trade output over here, we can see we are having this constructor
within parameter.
And my estimation was that we are also having a constructor without an parameter.
This means we are having two constructors and free to do not know which one to hook.
We have talked about this in the overloading section, so this one is not identical.
Therefore having the constructor within parameter.
So if you haven't watched this part, then please have a look into this one, because that's a very
important section over there.
All right.
But now we can see we are only having one constructor within parameter and therefore we also do not
need to overload this init function over here.
We can also maybe figure this out by clicking on smiley button here by having this smiley code right
now.
And after all our smiley chapter, this is no big deal anymore to read, which is absolutely amazing.
Right?
So here we can see.
Maybe let's let's enlarge this.
So we haven't looked at Smiley quite a while, so let's see.
Okay.
And this one is the class we can see we are not extending in special class over here.
We've got an instance feed item power.
We've got an instance field name from the type string.
And then we've got this one method constructor here, be having this in it and here we can see one parameter
type integer returning void.
So we only having one constructor in there.
Therefore we might not need to overload it in case.
If we need to overload it, we will have a look on the slides how we can do this.
But it's exactly the same like we have done this in the past couple of videos.
All right.
Okay.
Before we go back to the slides and figure out how we can overload this constructor, let's modify this.
So let's put this back on the right.
Okay.
And let's see, we want to have an item power level, which is higher than a thousand.
For example, 9000.
If you want to do this on your own right now, then yet you can pause and rewind this video later on.
I will now move on with the solution on how to modify this value in our constructor.
Therefore, I give you 5 to 10 seconds to make your decision if you want to do this on your own or not.
Okay.
Welcome back.
Now let's have a look into this one.
So here we are overriding the implementation of the constructor.
Let's have a look into this one.
This is our constructor.
Okay.
Here we can see we are taking this parameter and we assign this parameter to this instance field.
This means if you want to hook the constructor, we can simply modify this parameter over here.
So we have already done this approach.
We are taking the original method, overwriting the parameter, and then we are calling this method
once again with our modified version.
Alrighty, let's do this.
So here we keep our debug output from the original output and then we need to call the original constructor.
And this can be done in the following way.
This dot in it and then we need to provide our new value.
Let's do it this way.
All right.
I guess this should do the job.
Let's find this out.
This is our app.
This is our script.
This time I also save it.
Okay, so let's do this.
The application has been restarted and we can see the item power creation instance and so on.
This is our first parameter.
Now let's try to identify this object in our memory over here.
And let's see.
What's this instance?
Field value.
This one.
This item.
Power of the instance.
All right.
Okay, so let's write the script real quick.
Let me write this directly underneath.
So now we are using Char to perform the function, and this time we are using Java to choose.
We need to find all item instances.
So that's the item or that's the class reference.
And then we're having our two callbacks in double click.
Curly brackets enter on match.
Here we are having now our function with this instance.
An instance has been found.
And then that's also access this instance field.
And let's write it in this way.
This one is item power.
So we access in this instance and then this item power dot value.
All right.
And then we are providing our own complete callback.
Then we know we have done finished scanning.
I guess that's I guess that's how it's been called.
So finish scanning.
All right.
And now let's do this.
So we take this one into our into our command line over here, and then I'm hitting this in 3 to 1.
Let's see.
All right, nice.
We can see.
We have found one instance.
This one over here.
This means we have not created a new instance.
And we can also see the power level of this instance has been set to this value.
So now we know we have hooked this constructor over here in this one and we have modified this value
within the constructor generation.
Alrighty.
Amazing, right?
Okay.
Let's have a quick recap on the slides and then we will also find out how we could overload and construct
them.
So in case if we are having multiple ones, let's see how we can overload and hook and construct them.
Alrighty.
Let's have a look.
Okay, so here we are back on the slides on the top.
We've got our item class on the left, we've got this amazing picture and on the button we've got the
hook regarding to the constructor.
And this time we are only having one constructor, therefore no overload, simply just using the dot
in it.
And then we can define a new implementation for this one.
Perfect.
If we are having multiple constructors like in this case, then we have to simply overload it like we
have seen this in the last couple of videos.
So over here we're having now two constructors.
This one does not have in parameter and this one over here has an integer parameter.
If we now want to hook the one with the integer parameter, we need to get our class reference.
We need to apply this in it and for the constructor and then we need to overload it regarding to the
amount of numbers and types of these parameters.
In this case, we are only having one parameter from type integer, therefore free to knows exactly
that we want to hook this one over here.
Then we can access and manipulate it.
Yeah.
Business as usual.
So we can see over here we are having then this parameter and we can print it out or modify it in the
way we like.
All right.
Okay.
In the next video, we will have a look at manipulating the UI thread.
This is the last piece which is missing right now.
And afterwards, I guess we can manipulate over 95% of our Java code in our Android application.
Alrighty.
Okay.
So I'm looking forward to see you there.