我可以用Java获取调用对象的实例吗? [英] Can I get the instance of the calling object in Java?

查看:79
本文介绍了我可以用Java获取调用对象的实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个库,它使用一些参数来调用我的方法.我想接收另一个参数,但是该库未将其提供给它调用的方法.

There's a library which calls my method with a few arguments. I'd like to receive another argument, but the library doesn't provide it to the method it calls.

通过反编译该库,我可以看到它具有参数,并且已将其分配给实例变量(既不是私有变量,也不是公共变量).我知道如果有实例,我可以使用反射来获取该变量,但是我也没有实例.

By decompiling the library, I can see that it has the argument, and it's assigned to an instance variable (not private, but not public either.) I know I can get at the variable using reflection if I have the instance, but I don't have the instance, either.

我可以通过实例获取方法吗? SecurityManager具有getClassContext(),但这只为我提供了实例的类-我需要实例本身.

Is there a way I can get at the instance? SecurityManager has getClassContext(), but that just gives me the class of the instance - I want the instance itself.

作为我想要的快速示例:

As a quick example of what I want:

public class A {
    int b;
    public A(int b, int c) {
        this.b = b;
        D(c);
    }
}

public class D {
    public D(int c) {
        // Somehow I want to get at the calling instance of A's b from here,
        // and A belongs to a library which I didn't write.
    }
}

或者...我确实知道 b 被用作调用堆栈上的参数.因此,如果有人知道我如何访问传递到 A 的构造函数中的 b ,那将是可以接受的.

Alternatively... I do know that b was used as an argument on the callstack. So if someone knows how I could access the b which was passed into A's constructor, that would be acceptable.

如果这两个都不可行...我可以反编译 A 并按照自己的方式进行编译,然后要么需要做一些类加载向导,要么必须修改罐子里的东西.这两个听起来都不理想,所以我希望有人知道如何从调用堆栈访问 A 的实例或参数 b .

If neither of these are doable... I can decompile A and compile it the way I want, and then I'd either need to do some classloading wizardry or I'd have to modify the contents of the jar. Neither of those sound desirable, so I'm hopeful someone knows how to either access the instance of A or the argument b from the call stack.

推荐答案

借助您提供的代码,我可以考虑编写一个方面,我的意思是使用aop并尝试使用joinpoint来获取传递给构造函数A的参数.()

With the help of code you provided, i could think of writing an aspect i mean use aop and try using joinpoint to get the arguments that are passed to constructor A()

这篇关于我可以用Java获取调用对象的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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