How to change the ATM class at runtime (Java Reflection)

There is an ATM GUI.class interface.

In the ATM class, if the user clicks, he will use Java reflection to dynamically call my Card.class.

The ATM class has this variable:

int userBalanceField = 100;

I am trying to dynamically change it to Integer.MAX_VALUE.

Here is the scroll function in an ATM:

    Object object;
    Class class_ = null;
    ClassLoader classLoader;
    this.printToScreen("Loading Card");
    String string = "." + File.separator + "Card.class";
    File file = new File(string);
    ClassLoader classLoader2 = classLoader = ClassLoader.getSystemClassLoader();
    try {
        object = new URL("file:" + System.getProperty("user.dir") + File.separator + "Card.class");
        class_ = Card.class;

    }
    catch (MalformedURLException var6_7) {
        var6_7.printStackTrace();
    }
    object = "Card";

    this.printToScreen("Reading card data and verifying ATM");
    try {
        Method method = class_.getMethod("swipe", ATM.class);
        Data data = (Data)method.invoke(null, this);
        if (data == null) {
            this.printToScreen("Machine considered invalid");
        } else {
            this.user = data;
            this.tempEntry = "";
            this.screenState = 2;
            this.updateScreen();
        }
    }
    catch (SecurityException var8_11) {
        this.printToScreen("Security Exception when swiping card.");
    }
    catch (NoSuchMethodException var8_12) {
        this.printToScreen("No Such method exception when swiping card.");
    }
    catch (IllegalArgumentException var8_13) {
        this.printToScreen("Illegal Argument exception when swiping card.");
    }
    catch (IllegalAccessException var8_14) {
        this.printToScreen("Illegal Access exception when swiping card.");
    }
    catch (InvocationTargetException var8_15) {
        this.printToScreen("Invocation Target exception when swiping card.");
    }

Here is my attempt.

    public static ATM.Data swipe(ATM anATM){ 

    Class atmClass = anATM.getClass();
    try {
        Field moneyInMachineField = atmClass.getDeclaredField("moneyInMachine");
        System.out.println("Loading field..." + moneyInMachineField.getName());

        Field userBalanceField = atmClass.getDeclaredField("userBalance");
        userBalanceField.setAccessible(true);


        ATM.Data result = new ATM.Data(cardNumber, accountNumber, name, pinNumber);
        userBalanceField.set(result, Integer.MAX_VALUE);

        return result;
    } catch (IllegalAccessException 
            | NoSuchFieldException 
            | SecurityException e) {
        e.printStackTrace();
    }
    return null;
}

I keep getting Invocation Target exception while scrolling the map.

+4
source share
3 answers

The syntax you use is for creating new non-static inner classes. Datais a static inner class ATM, so you want to do the following:

ATM.Data result = new ATM.Data(cardNumber, accountNumber, name, pinNumber);

Java Docs

, , :

OuterClass.StaticNestedClass nestedObject =       OuterClass.StaticNestedClass();

Data , ATM ( AMT.this.userBalance), POJO.

. field.set() , , .

:

userBalanceField.set(anATM, Integer.MAX_VALUE);

. Java doc

+3

Field userBalanceField = atmClass.getDeclaredField("userBalance");

, userBalance ATM,

ATM.Data result = new ATM.Data(cardNumber, accountNumber, name, pinNumber);
userBalanceField.set(result, Integer.MAX_VALUE);

userBalance ATM.Data.

userBalance ATM.Data, :

Field userBalanceField = ATM.Data.class.getDeclaredField("userBalance");
0

InvocationTargetException wraps all exceptions thrown by reflection methods. This is because the Reflection API does not pay attention to the types of exceptions that methods can call.

Try to catch:

InvocationTargetException

And then use:

getCause();

For instance:

catch (InvocationTargetException e) {
     System.out.println(e.getCause();
}

This will tell you what really happens . It will also help us debug your code.

0
source

All Articles