在Java中通过引用获取对象 [英] Getting an Object by reference in Java

查看:50
本文介绍了在Java中通过引用获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个网站的新手,很高兴分享我的第一个问题:)

New to this website, and excited to share my first question :)

好的,所以我将解释我目前的设置,以便可以更好地理解我的问题.

Ok so I'm going to be explaining what I have set-up currently so that my question can be understood better.

我有2个Java应用程序:

I have 2 java applications:

  1. 逻辑应用程序(发生所有繁重任务的地方)
  2. 仪表应用程序(已插入正在运行的游戏中的应用程序)

我对这两个应用程序所做的工作是使用检测应用程序从游戏中提取信息,然后将其发送到逻辑应用程序.信息/数据的提取是通过Java Reflection API完成的.然后,在提取信息之后,将其通过RMI发送到Logic Application(有关此信息,请参见下文).

What I do with these 2 applications is extract information from a game using the Instrumented Application then send it to the Logic Application. The extraction of information/data is done through the Java Reflection API. Then after the information has been extracted, it is sent to the Logic Application through RMI (more on this below).

之所以拥有2个应用程序而不是1个应用程序是因为该游戏在Java 7(旧游戏)上运行,所以Instrumented Application在Java 7上运行,并且我可以在我想要的任何Java版本上运行Logic应用程序.

The reason I have 2 applications instead of 1 is because the game runs on Java 7 (old game), so the Instrumented Application runs on Java 7, and I can run the Logic application on whatever java version I want.

我在两者之间传输数据/信息的方式是通过RMI(远程方法调用).说明:Logic应用程序在Instrumented App上调用getter方法,该方法使用Reflection API从其所检测的游戏中获取字段值并返回.

The way I transfer data/information between the 2 is through RMI (Remote Method Invocation). Explanation: The Logic application invokes a getter method at the Instrumented App, and the method uses the Reflection API to grab the field value from the game it's instrumented into and return it.

现在,就像你们已经了解了我的基本工作一样,现在来挑战了我已经有一段时间了.

Now as you guys have understood what I'm basically doing, now comes the challenge that I have been bamboozled by for quite some time.

问题:

Reflection返回的大多数字段值都是可序列化的,因此它们毫无问题地通过RMI.但是,一些是不可序列化的对象,例如示例"Item".项目"是一个对象,它包含3个值:ID,名称和数量.因此,我的想法是制造一个包装纸并发送该包装纸,这样就可以解决问题了吗?但没有,又出现了另一个挑战.

Most of the Field Values that are returned by Reflection are Serializable, therefore they pass-through RMI with no problem. But, some are Un-serializable objects like for an example "Item". "Item" is an object and it holds 3 values: ID, Name, Quantity. So my thought was to make a wrapper and send the wrapper and it would be problem solved right? but no, another challenge popped up.

弹出的挑战是:如果我需要知道对象"Item"的数量,该怎么办?我又抽出时间了吗?如果我的全部是1个应用程序,那么我将使用Reflection API通过以下方式使用Object获取Field: getClassLoader().loadClass("ItemClass").getDeclaredField("Item").get(ObjectThatIPreviouslyFetched); 但是由于必须将对象转移到逻辑应用程序,所以不能将对象本身的项目"转移到逻辑应用程序.因此,除非再次寻找该对象,否则我将无法返回该对象并获得更新的信息,这会浪费资源,并使我的整个流程变慢.

The challenge that popped up was: What if I needed to know the quantity of the object "Item" that I pulled out at another time? If my whole thing was 1 application, I would've used the Reflection API to get the Field using the Object by: getClassLoader().loadClass("ItemClass").getDeclaredField("Item").get(ObjectThatIPreviouslyFetched); But since I have to transfer the objects over to the Logic Application, I cannot transfer the Object itself "Item" so I have no way to return to that object and get updated information unless I look for the object again which is a waste of resources and makes my whole process slower.

应用程序的工作方式如下:

Applications work like so:

Instrumented App->反射->游戏->字段值(称为项目的对象)->包装器->RMI->逻辑应用程序(已发送对象信息,但由于对象本身无法序列化,因此无法发送对象本身)

Instrumented App -> Reflection -> Game -> Field Value (Object called Item) -> Wrapper -> RMI -> Logic Application (Object information is sent but Object itself can't be sent due to it being unserializable)

问题的摘要(使用上面的指针):如何取回对象(项目),以便可以使用反射获得有关它的更新信息.

Summary of the question (using the pointers just above): How can I get the Object (Item) back so I can get updated information about it using Reflection.

这是我用来获取字段值(对象)的方法:

This is the method I made to get the Field Values (Objects):

   /**
    *
    * @param params owner;fieldName;multiplier
    * @param object null if static
    * @return returns the value of the object provided.
    */
   public Object getFieldValue(String params, Object object) {
       String[] paramsSplit = params.split(";");
       try {
           Class<?> clazz = classLoader.loadClass(paramsSplit[0]);
           Field field = clazz.getDeclaredField(paramsSplit[1]);
           field.setAccessible(true);
           Object o = field.get(object);
           if(!paramsSplit[2].equals("0")) return getMultipliedValue((Number) o,paramsSplit[2]);
           else return o;
       } catch (IllegalAccessException | NoSuchFieldError | ClassNotFoundException | NoSuchFieldException e) {
           e.printStackTrace();
       }
       return null;
   } 

此方法可在逻辑应用程序中找到,并将仪表化应用程序附加到正在运行的游戏中:

This method is found in the Logic Application and it attaches the Instrumented Application to the running game:

    public void inject() {
        VirtualMachine vm = null;
        try {
            vm = VirtualMachine.attach(String.valueOf(pid));
            vm.loadAgent(Info.baseLocation());
        } catch (AttachNotSupportedException | IOException |
                AgentLoadException | AgentInitializationException e) {
            e.printStackTrace();
        } finally {
            try {
                if(vm != null) vm.detach();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

我希望一切都可以理解,我不是英国人,所以如果出现问题,我深表歉意.

I hope everything was understandable, I'm not an English native so I apologize if something seems wrong.

谢谢!

推荐答案

没有权利";这个问题的答案.

There's is no "Right" answer for this question.

我去制作了一个存储对象的地图,并为每个对象分配了唯一的键.然后,我通过RMI发送了对象的唯一密钥,现在我可以使用唯一密钥获取对象,并随时发送对象的内部信息.

I went for making a map that stores the objects, and assigned a unique key for every object. I then sent the object's unique key over RMI, now I can just get the Object using the unique key and send over the object's inner information at any time.

这篇关于在Java中通过引用获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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