Advertisement
iccaka

Java Reflection example

Nov 5th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. import java.util.Scanner;
  6.  
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
  11.  
  12.         Class blackBoxClass = BlackBoxInt.class;  // We get a hold of the class with  all private fields and methods
  13.         Constructor intConstructor = blackBoxClass.getDeclaredConstructor(int.class);  // There are 2 constructors - without parameters(sets the "innerValue" field to 0 - by default) and one with only one integer
  14.         intConstructor.setAccessible(true);  // We make it accessible, because it is private
  15.         BlackBoxInt blackBoxInt = (BlackBoxInt) intConstructor.newInstance(0);  // We make a new instance of "BlackBoxInt" using ".newInstance()" (The better way)
  16.  
  17.         Scanner sc = new Scanner(System.in);  // Simple "Scanner"
  18.         String line = sc.nextLine();
  19.  
  20.         while(!line.equals("END")){  // We will recieve the commands in this format: "<commands>_<value>", that's why we split by "_"
  21.             String[] tokens = line.split("_");
  22.  
  23.             Method method = blackBoxClass.getDeclaredMethod(tokens[0], int.class);  // Get the method
  24.             doSomething(Integer.valueOf(tokens[1]), method, blackBoxInt);
  25.  
  26.             System.out.println(getInnerValue(blackBoxInt));  // Get the "innerValue" field which is private
  27.  
  28.             line = sc.nextLine();
  29.         }
  30.  
  31.     }
  32.  
  33.     static void doSomething(int value, Method method, BlackBoxInt blackBoxInt) throws InvocationTargetException, IllegalAccessException {
  34.         method.setAccessible(true);  // Make it accessible, because it's private
  35.         method.invoke(blackBoxInt, value);  // Invoke it(we need a reference, that's why the first parameter is a "BlackBoxInt" instance
  36.     }
  37.  
  38.     static int getInnerValue(BlackBoxInt blackBoxInt) throws IllegalAccessException, NoSuchFieldException {  // Here we just return the "innerValue" field, so the user can see it
  39.         Field innerValue = blackBoxInt.getClass().getDeclaredField("innerValue");
  40.         innerValue.setAccessible(true);
  41.         return (int) innerValue.get(blackBoxInt);
  42.     }
  43. }
  44.  
  45. // This down here is the "BlackBoxInt" class
  46.  
  47.  
  48.  
  49. public class BlackBoxInt {
  50.  
  51.     private static final int DEFFAULT_VALUE = 0;
  52.  
  53.     private int innerValue;
  54.  
  55.     private BlackBoxInt(int innerValue) {
  56.         this.innerValue = innerValue;
  57.     }
  58.  
  59.     private BlackBoxInt() {
  60.         this.innerValue = DEFFAULT_VALUE;
  61.     }
  62.  
  63.     private void add(int addend) {
  64.         this.innerValue += addend;
  65.     }
  66.  
  67.     private void subtract(int subtrahend) {
  68.         this.innerValue -= subtrahend;
  69.     }
  70.  
  71.     private void multiply(int multiplier) {
  72.         this.innerValue *= multiplier;
  73.     }
  74.  
  75.     private void divide(int divider) {
  76.         this.innerValue /= divider;
  77.     }
  78.  
  79.     private void leftShift(int shifter) {
  80.         this.innerValue <<= shifter;
  81.     }
  82.  
  83.     private void rightShift(int shifter) {
  84.         this.innerValue >>= shifter;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement