简体   繁体   English

使用反射加载类,然后在运行时编辑变量

[英]Load a class using reflection then edit the variables during runtime

Okay, so I have a java file which is loading another class and I want the java file to be able to edit and read variables from the class which is running. 好的,所以我有一个Java文件正在加载另一个类,并且我希望该Java文件能够编辑和读取正在运行的类中的变量。

For example: I have a button which when pressed it sets a variable (This is the class file). 例如:我有一个按钮,当按下它时会设置一个变量(这是类文件)。 I want the java file which is loading this class to be able to see the new value of the variable read it, set it and do whatever is needed. 我希望正在加载此类的java文件能够看到变量的新值,并对其进行读取,设置和执行所需的任何操作。 And I want the new value which is set to show up on the running java class. 我想要设置为在正在运行的java类上显示的新值。

This is what I have tried so far but when I try to edit the values like getting baseX it doesn't show up on the running class. 到目前为止,这是我尝试过的方法,但是当我尝试编辑诸如获取baseX的值时,它不会显示在正在运行的类中。 Also, the baseX value should change when I do stuff on the running class but the stuff is not printed to the screen when I change them. 另外,当我在正在运行的类上进行填充时,baseX值应该会更改,但是当我更改它们时,这些填充不会打印到屏幕上。 It's as if reflection can't read stuff on runtime. 好像反射无法在运行时读取内容。 So what does? 那是什么呢?

Class c = Class.forName("Client");
        for (Method m : c.getMethods()) {
            if (m.getName().contentEquals("main")) {
                Object[] passedArgs = { args };
                m.invoke(null, passedArgs);
            }

        }
        Object instance = c.newInstance();

        Field baseX = c.getField("baseX");
        Field loggedIn = c.getField("loggedIn");

        boolean gotValues = false;
        while(!gotValues) {
            boolean loggedin = loggedIn.getBoolean(instance);
            if(loggedin) {
                System.out.println(baseX.get(instance));
            } else {
                System.out.println(loggedin);
                loggedIn.setBoolean(instance, true);
            }
        }

Also yeah getter/setter methods would work if they worked on runtime and I could make it so that when button x is pressed variable y changes on screen. 如果是getter / setter方法,它们也可以在运行时运行,那么我也可以做到,这样,当按下按钮x时,变量y就会在屏幕上改变。 What is a java bean? 什么是Java Bean? Also what if I wanted to just invoke a method and not get a value? 如果我只想调用一个方法而不获取值怎么办? Or what if I wanted to add my own methods/code? 或者,如果我想添加自己的方法/代码怎么办?

Try this: 尝试这个:

public class Client {
  public Object baseX = new Object();
  public boolean loggedIn;
}
-----
public class Main {
  public static void main(String[] args) throws Exception {
    Class c = Class.forName("Client");
    /*for (Method m : c.getMethods()) {
      if (m.getName().contentEquals("main")) {
        Object[] passedArgs = {args};
        m.invoke(null, passedArgs);
      }

    }*/
    Object instance = c.newInstance();

    Field baseX = c.getField("baseX");
    Field loggedIn = c.getField("loggedIn");

    boolean gotValues = false;
    //while (!gotValues) {
      boolean loggedin = loggedIn.getBoolean(instance);
      if (loggedin) {
        System.out.println("Logged in!");
        System.out.println(baseX.get(instance));
      }
      else {
        System.out.println("NOT Logged in!");
        System.out.println(loggedin);
        loggedIn.setBoolean(instance, true);
        System.out.println(loggedIn.getBoolean(instance));
      }
    //}

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM