java reflection System.out.println

匆匆过客 提交于 2019-12-24 05:12:11

问题


I m learning java reflection. I tried

System.exit(3);
Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);

and it works. I successfully also ran

System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null)); 

Now how can i invoke System.out.println reflectively

java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!"); 

is giving error. I know System does not have out function. So suggest a way to call System.out.println reflectively

Here is the complete example

public class ReflectionDemo1 {
public static void main(String[] args) throws Exception {
// java.lang.System.exit(3);
// java.lang.Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);
// java.lang.System.currentTimeMillis()
// System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));
// System.out.println("Hi!");
java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");
}
}

回答1:


out is a static field of the java.lang.System class.

You can use the Field class to refer to it

Class<?> systemClass = java.lang.Class.forName("java.lang.System");
Field outField = systemClass.getDeclaredField("out");

The type of that field is PrintStream. You don't need to know that really, but you do need to retrieve the corresponding Class object for it.

Class<?> printStreamClass = outField.getType();

We know it has a println(String) method, so we can retrieve that too

Method printlnMethod = printStreamClass.getDeclaredMethod("println", String.class);

Now, since println(String) is an instance method, we need to invoke it on an instance. Which instance? The one referenced by the out field. That out field is static so we can get it by passing null to Field#get(object).

Object object = outField.get(null);

Then we invoke the method

printlnMethod.invoke(object, "Hi!");


来源:https://stackoverflow.com/questions/23019482/java-reflection-system-out-println

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!