使用反射加载类,然后在运行时编辑变量 [英] Load a class using reflection then edit the variables during runtime

查看:84
本文介绍了使用反射加载类,然后在运行时编辑变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个java文件,它正在加载另一个类,我想要的java文件能够编辑和读取的类中运行的变量。

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. And I want the new value which is set to show up on the running java class.

这是我到目前为止已经尝试,但是当我尝试编辑值得到baseX它不会出现在运行类。此外,baseX值应该改变,当我做的东西在运行类,但东西不打印到屏幕上,当我改变它们。就好像反射不能在运行时读取东西。

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. 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. 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);
            }
        }

此外,getter / setter方法如果工作在运行时,我可以让它,当按钮x被按下变量y改变在屏幕上。什么是java bean?还有,如果我想调用一个方法,而不是得到一个值?

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. What is a 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?

推荐答案

请尝试以下操作:

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));
      }
    //}

  }
}

这篇关于使用反射加载类,然后在运行时编辑变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆