For fun and science, I'm attempting to see how difficult it is to call System.out.println(String) ENTIRELY through reflection.
Here's the code I start myself with
/** Attempt to call System.out.println("You have died of dysentery")
* entirely through reflection. */
public static void main(String[] arguments) throws Exception
{
Method classDOTforName = Class.forName("java.lang.Class").getDeclaredMethod("forName",Class.forName("java.lang.String"));
Method classDOTgetDeclaredMethod = ((Class)classDOTforName.invoke(null,"java.lang.Class")).getDeclaredMethod("getDeclaredMethod",(Class)classDOTforName.invoke(null,"java.lang.String"),(Class)classDOTforName.invoke(null,"[Ljava.lang.Class"));
Method methodYouCreated = ???
//After this point
// 1. You must declare a Method object which to be returned from a classDOTgetDeclaredMethod.invoke(Object referenced, Object...params) call.
// 2. All statements MUST be inline in the parameters of this method call. I don't care how awful it looks, this is the challenge.
// 3. You MAY NOT reference any fields directory (this includes Object.class, String.class, etc.)
// 4. The method you declared should then call invoke. This call to invoke should call System.out.println("You have died of dysentery") ENTIRELY through reflection.
// 5. NO assingments to ANY variables are allowed after this point. Remember, you are supposed to be inlining your method calls.
// 6. NO class casting is allowed after this point (it shouldn't be necessary).
method.invoke(???); //prints "You have died of dysentery\n" to standard output.
}
I've been staring at my computer screen for a couple hours trying to figure this out. It gets tricky really quickly, especially the inlining stipulation. If you're up to it, post your code here.
Aucun commentaire:
Enregistrer un commentaire