
Sign up to save your podcasts
Or
If you've ever used the JavaScript language before, you might be familiar with the eval() function.
If you've never used JavaScript before, then no worries, I'll have an in-depth explanation as to what's going on.
For you JavaScript coders out there, the eval() function is used so that you can write any strings you like inside of the function, and the JavaScript engine will interpret it and run it like it was native JS code.
This “execution of code written out as Strings” concept is very similar to what reflection in Java accomplishes.
Reflection in Java is a way to call the methods of objects without needing the actual reference to those objects.
Another way to say it is, you can programmatically execute methods with reflection in Java.
So, one of the keys to working with reflection in Java is to make use of the Method class.
The Method class gives us access to some cool stuff… Namely the ability to “invoke” methods that you want to have invoked (programmatically).
Let's assume you've got a standard Java class that has some getter and setter methods like so:
The User above is a super common Java bean that represents some user.
You've seen what it looks like to “invoke” any of these methods. For example, if we wanted to invoke the getter method for a User‘s email, it would be:
Nothing fancy going on here (assuming you know what a repository is, if you don't I'd recommend reading about it here)
So now let's introduce Java reflection.
Here's that same code, only now we'll invoke the getter method using reflection:
Naturally it takes more code to make reflection do its thing, namely in the number of exceptions that need handling.
But the “business-end” of the code really comes down to two things:
The first step is accomplished by first getting the appropriate class with which you'd like to work: aUser.getClass(). Then once we have the class we'd like to work with, we can locate the method we'd like to invoke via the getMethod() method.
Very meta.
The most important thing to note here is that the process of “getting a method” via the getMethod() method takes a String as an argument.
This means that you can pass in a String variable to this method… This means that the process of “getting” a method and invoking it can be completely dynamic and can change at run-time.
This is the whole reason why Java reflection exists! The ability to programmatically invoke methods.
Okay, so we've seen how to invoke a method that takes no parameters (i.e. a “getter” method) Now let's take a look at how to invoke a method that takes an argument (i.e. a “setter” method)
Note: just in case you're thinking that reflection is only get “getter” and “setter” methods, it is not, you can use reflection to invoke almost any methods you'd like. I say “almost” any methods because I'm not exactly sure if it can be used to invoke ANY method you want, so I'm just covering my own butt :)
Okay, having said that, let's invoke the setEmail() method with Java reflection:
What our code above will do is the equivalent of writing aUser.setEmail("[email protected]").
The difference that you'll notice between the first Java reflection example and the second is that we have to add parameters to our getMethod() and method.invoke() methods.
We need to tell Java what the method signature is for the method we'd like to invoke. The reason we need to do this is because we can have methods with the same names, but different arguments in their method signatures. So just giving the name wouldn't be enough information for Java to locate the specific method you'd like to invoke.
And secondly, you need to actually pass the value(s) of the arguments for the method to use when invoked.
Now, there may come a time when you don't want to pass the exact method name and method signature of the method you'd like to programmatically invoke.
One real-world example of this scenario is when saving values via an AJAX call.
If we have a form that needs to be filled out on a webpage, and the values will be sent to a database for storage as soon as the form fields are filled out, then reflection plays an important role.
Here's a snippet of code that I've used before to save both String and Double values from form fields.
This is some lovely and generic code that allows me to invoke the setter methods of ANY object I wish, and it will dynamically convert the values that I wish to set into the methods (given that it's either a String or a Double). Of course, I can also program it to convert from a String to any other data types, I just need to add them into the if structure.
The post EP08 – Java Reflection – Writing Generic Java Code appeared first on Coders Campus.
If you've ever used the JavaScript language before, you might be familiar with the eval() function.
If you've never used JavaScript before, then no worries, I'll have an in-depth explanation as to what's going on.
For you JavaScript coders out there, the eval() function is used so that you can write any strings you like inside of the function, and the JavaScript engine will interpret it and run it like it was native JS code.
This “execution of code written out as Strings” concept is very similar to what reflection in Java accomplishes.
Reflection in Java is a way to call the methods of objects without needing the actual reference to those objects.
Another way to say it is, you can programmatically execute methods with reflection in Java.
So, one of the keys to working with reflection in Java is to make use of the Method class.
The Method class gives us access to some cool stuff… Namely the ability to “invoke” methods that you want to have invoked (programmatically).
Let's assume you've got a standard Java class that has some getter and setter methods like so:
The User above is a super common Java bean that represents some user.
You've seen what it looks like to “invoke” any of these methods. For example, if we wanted to invoke the getter method for a User‘s email, it would be:
Nothing fancy going on here (assuming you know what a repository is, if you don't I'd recommend reading about it here)
So now let's introduce Java reflection.
Here's that same code, only now we'll invoke the getter method using reflection:
Naturally it takes more code to make reflection do its thing, namely in the number of exceptions that need handling.
But the “business-end” of the code really comes down to two things:
The first step is accomplished by first getting the appropriate class with which you'd like to work: aUser.getClass(). Then once we have the class we'd like to work with, we can locate the method we'd like to invoke via the getMethod() method.
Very meta.
The most important thing to note here is that the process of “getting a method” via the getMethod() method takes a String as an argument.
This means that you can pass in a String variable to this method… This means that the process of “getting” a method and invoking it can be completely dynamic and can change at run-time.
This is the whole reason why Java reflection exists! The ability to programmatically invoke methods.
Okay, so we've seen how to invoke a method that takes no parameters (i.e. a “getter” method) Now let's take a look at how to invoke a method that takes an argument (i.e. a “setter” method)
Note: just in case you're thinking that reflection is only get “getter” and “setter” methods, it is not, you can use reflection to invoke almost any methods you'd like. I say “almost” any methods because I'm not exactly sure if it can be used to invoke ANY method you want, so I'm just covering my own butt :)
Okay, having said that, let's invoke the setEmail() method with Java reflection:
What our code above will do is the equivalent of writing aUser.setEmail("[email protected]").
The difference that you'll notice between the first Java reflection example and the second is that we have to add parameters to our getMethod() and method.invoke() methods.
We need to tell Java what the method signature is for the method we'd like to invoke. The reason we need to do this is because we can have methods with the same names, but different arguments in their method signatures. So just giving the name wouldn't be enough information for Java to locate the specific method you'd like to invoke.
And secondly, you need to actually pass the value(s) of the arguments for the method to use when invoked.
Now, there may come a time when you don't want to pass the exact method name and method signature of the method you'd like to programmatically invoke.
One real-world example of this scenario is when saving values via an AJAX call.
If we have a form that needs to be filled out on a webpage, and the values will be sent to a database for storage as soon as the form fields are filled out, then reflection plays an important role.
Here's a snippet of code that I've used before to save both String and Double values from form fields.
This is some lovely and generic code that allows me to invoke the setter methods of ANY object I wish, and it will dynamically convert the values that I wish to set into the methods (given that it's either a String or a Double). Of course, I can also program it to convert from a String to any other data types, I just need to add them into the if structure.
The post EP08 – Java Reflection – Writing Generic Java Code appeared first on Coders Campus.