As far as I understood, constructors, instance initializers and static initializers are just methods with special names. I know that <
and >
are illegal characters for ordinary identifiers.
How can I retrieve these special methods as Method
objects and call them? You might take a look at this code snippet I wrote:
public class Program {
/**
* static <init>() {
* }
*/
static {
}
/**
* <init>() {
* }
*/
{
}
/**
* <cinit>() {
* }
*/
Program(){
}
public static void main(String[] args) throws ReflectiveOperationException {
//get native getDeclaredMethods method
Method Class$getDeclaredMethods0 = Class.class.getDeclaredMethod("getDeclaredMethods0", boolean.class);
Class$getDeclaredMethods0.setAccessible(true);
//list methods of this class
Method[] methods = (Method[])Class$getDeclaredMethods0.invoke(Program.class, false);
for (Method m : methods) {
System.out.println(m);
}
//Console output:
/*
public static void dirty.Program.main(java.lang.String[]) throws java.lang.ReflectiveOperationException
*/
}
}
Aucun commentaire:
Enregistrer un commentaire