fork download
  1. import java.lang.reflect.*;
  2. class RefSamp {
  3. public int times = 0;
  4. public RefSamp(int t) {
  5. this.times = t;
  6. }
  7. public void hello(String msg) {
  8. this.hello(msg, this.times);
  9. }
  10. public void hello(String msg, int t) {
  11. System.out.println("Hello, " + msg + " x " + t);
  12. }
  13. }
  14.  
  15. public class Main {
  16. public static void main(String[] args) throws Exception {
  17. Class clazz = RefSamp.class;
  18. Constructor<?> cons = clazz.getConstructor(int.class);
  19. RefSamp rs = (RefSamp) cons.newInstance(256);
  20. Field f = clazz.getField("times");
  21. f.set(rs, 2);
  22. System.out.println(f.get(rs));
  23. Method m = clazz.getMethod("hello", String.class, int.class);
  24. m.invoke(rs, "refrection!", 128);
  25. boolean pubc = Modifier.isPublic(clazz.getModifiers());
  26. boolean finm = Modifier.isFinal(m.getModifiers());
  27. }
  28. }
Success #stdin #stdout 0.06s 2841600KB
stdin
Standard input is empty
stdout
2
Hello, refrection! x 128