Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
- Class blackBoxClass = BlackBoxInt.class; // We get a hold of the class with all private fields and methods
- 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
- intConstructor.setAccessible(true); // We make it accessible, because it is private
- BlackBoxInt blackBoxInt = (BlackBoxInt) intConstructor.newInstance(0); // We make a new instance of "BlackBoxInt" using ".newInstance()" (The better way)
- Scanner sc = new Scanner(System.in); // Simple "Scanner"
- String line = sc.nextLine();
- while(!line.equals("END")){ // We will recieve the commands in this format: "<commands>_<value>", that's why we split by "_"
- String[] tokens = line.split("_");
- Method method = blackBoxClass.getDeclaredMethod(tokens[0], int.class); // Get the method
- doSomething(Integer.valueOf(tokens[1]), method, blackBoxInt);
- System.out.println(getInnerValue(blackBoxInt)); // Get the "innerValue" field which is private
- line = sc.nextLine();
- }
- }
- static void doSomething(int value, Method method, BlackBoxInt blackBoxInt) throws InvocationTargetException, IllegalAccessException {
- method.setAccessible(true); // Make it accessible, because it's private
- method.invoke(blackBoxInt, value); // Invoke it(we need a reference, that's why the first parameter is a "BlackBoxInt" instance
- }
- static int getInnerValue(BlackBoxInt blackBoxInt) throws IllegalAccessException, NoSuchFieldException { // Here we just return the "innerValue" field, so the user can see it
- Field innerValue = blackBoxInt.getClass().getDeclaredField("innerValue");
- innerValue.setAccessible(true);
- return (int) innerValue.get(blackBoxInt);
- }
- }
- // This down here is the "BlackBoxInt" class
- public class BlackBoxInt {
- private static final int DEFFAULT_VALUE = 0;
- private int innerValue;
- private BlackBoxInt(int innerValue) {
- this.innerValue = innerValue;
- }
- private BlackBoxInt() {
- this.innerValue = DEFFAULT_VALUE;
- }
- private void add(int addend) {
- this.innerValue += addend;
- }
- private void subtract(int subtrahend) {
- this.innerValue -= subtrahend;
- }
- private void multiply(int multiplier) {
- this.innerValue *= multiplier;
- }
- private void divide(int divider) {
- this.innerValue /= divider;
- }
- private void leftShift(int shifter) {
- this.innerValue <<= shifter;
- }
- private void rightShift(int shifter) {
- this.innerValue >>= shifter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement