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