Constructor reflection
In this chapter you will learn:
- How to get constructors from java.lang.Class
- Use java.lang.reflect.Constructor
- How to get the parameters for a constructor
- How to create new instance by using its constructor
Get constructors from java.lang.Class
java.lang.Class
has methods to return constructors for a given class.
The methods are defined as follows:
Constructor<T> getConstructor(Class<?>... parameterTypes)
gets the public constructor of the class represented by this Class object.Constructor<?>[] getConstructors()
returns all public constructors of the class represented by this Class object.
The following code prints out all constructors for java.lang.String
.
import java.lang.reflect.Constructor;
//from ja va2 s . c om
public class Main {
public static void main(String args[]) {
System.out.println("Constructors:");
Constructor constructors[] = new String().getClass().getConstructors();
for (int i = 0; i < constructors.length; i++) {
System.out.println(" " + constructors[i]);
}
}
}
The output:
Constructor Reflection
java.lang.reflect.Constructor
class wraps the information
for a constructor. We can get all kinds of messages from java.lang.reflect.Constructor
.
For example, the parameters of a constructor.
Constructor provides information about a single constructor for a class.
import java.lang.reflect.Constructor;
//from ja v a 2s .c om
public class Main {
public static void main(String[] argv) throws Exception {
Constructor[] constructors = String.class.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor c = constructors[i];
System.out.println(c.toGenericString());
}
}
}
Constructor's parameters and modifiers
Once we get the constructor we can get its modifiers, parameter's type. And by using those information we can restore the signature of a constructor.
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
// ja v a 2 s.c om
public class Main {
public static void main(String[] argv) throws Exception {
Constructor[] constructors = String.class.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor c = constructors[i];
Class[] paramTypes = c.getParameterTypes();
String name = c.getName();
System.out.print(Modifier.toString(c.getModifiers()));
System.out.print(" " + name + "(");
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
System.out.print(", ");
System.out.print(paramTypes[j].getCanonicalName());
}
System.out.println(");");
}
}
}
The output:
Create new instance by calling constructor
We create new instance of a class by using one of its constructors. From reflection we can do the same
thing. In the following code we get the constructor in Point
class by parameters and then
create a new instance of Point class by passing in real value.
import java.awt.Point;
import java.lang.reflect.Constructor;
// j av a2s. c o m
public class Main {
public static void main(String[] argv) throws Exception {
Constructor con = Point.class.getConstructor(new Class[] {
int.class, int.class });
Point obj =
(Point) con.newInstance(new Object[] {
new Integer(1), new Integer(1) });
}
}
Next chapter...
What you will learn in the next chapter: