Examples of interactions with an object
With the examples above, you see I do not lie to you, the reflection is very simple. It's fine to recover the methods and information, but it'll be even nicer if we could interact with any object! For example, calling any method or construct any instance. Once we have the Method, Constructor, and Field, it is even easier to perform operations on an object.
1. Construct an instance from a Constructor
From Constructor recovered on a type, it is very easy to construct an instance. This is done using the newInstance function. This function takes a list of constructor parameters. By repeating the builder recovered above, this constructor takes a StringBuilder, it is therefore imperative to him to pass a sentence under an exception:
Code:
try
{
Object Instance = constructeur.newInstance (new StringBuilder ());
} Catch (IllegalArgumentException e)
{
// Number of arguments is not good
e.printStackTrace ();
} Catch (InstantiationException e)
{
// Class is abstract (instance of an abstract class can not)
e.printStackTrace ();
} Catch (IllegalAccessException e)
{
// Constructor is not accessible
e.printStackTrace ();
} Catch (InvocationTargetException e)
{
// The constructor threw an exception
e.printStackTrace ();
}
NewInstance function will return the new instance of the type. The exceptions that can be thrown are:
IllegalArgumentException: the number of arguments is not good or are the wrong size
InstantiationException: if one tries to instantiate an abstract class (not in Java)
IllegalAccessException: the constructor is not accessible (private visibility is, for example)
InvocationTargetException: the constructor threw an exception
2. Method Call
From the Method object, we can execute the method on an instance of the object. We use the invoke function parameter that takes an instance of an object on which to execute the method and the list of method parameters. Taking the method recovered in the example above.
Code:
try
{
Object object = "hello");
Concat.invoke Object result = (object, "everybody");
} Catch (IllegalArgumentException e)
{
// Arguments passed to the method are not good
e.printStackTrace ();
} Catch (IllegalAccessException e)
{
// Function not available
e.printStackTrace ();
} Catch (InvocationTargetException e)
{
// Method threw an exception
e.printStackTrace ();
}
If the method executed should return a value, that value is returned back to the call to invoke. The exceptions that can be thrown are:
IllegalArgumentException: the number of arguments is not good or are the wrong size
IllegalAccessException: the method is not accessible (for example, is private)
InvocationTargetException: the method threw an exception
3. Assigning a value to a field
To assign a value to a field in an instance of an object, use the set method Field of the field to edit.
Code:
try
{
champ.set (object, "tutorial");
} Catch (IllegalArgumentException e)
{
// The parameter passed is invalid (not the right type for example)
e.printStackTrace ();
} Catch (IllegalAccessException e)
{
// Function not available
e.printStackTrace ();
}
The exceptions that can be thrown are:
IllegalArgumentException: value field to be defined is not the right type
IllegalAccessException: the field is not accessible (for example, is set to private)
4. Reading a field value
Similarly, one can read the value of a field using the get function of Field.
Code:
try
{
Object = read champ.get (object);
} Catch (IllegalArgumentException e)
{
// The parameter passed is invalid (not the right type for example)
e.printStackTrace ();
} Catch (IllegalAccessException e)
{
// Function not available
e.printStackTrace ();
}
Bookmarks