Seam 2.2使用反射改变注入的场值

时间:2022-12-14 19:59:18

I use Seam 2.2. I want to get a private field from a Seam component using Reflections. So I have:

我使用Seam 2.2。我想使用Reflection从Seam组件中获取私有字段。所以我有:

    LocalizationEditActionRoot localizationEditActionRoot = (LocalizationEditActionRoot) Component
                        .getInstance(LocalizationEditActionRoot.class, true);
    Class currentClass = localizationEditActionRoot.getClass();
    WebDavMockClient mockClient = new WebDavMockClient();
    Field f = currentClass.getDeclaredField("davClient");
    f.setAccessible(true);
    f.set(localizationEditActionRoot, mockClient);

If I want to get a field called davClient(WebDavClient) I get a NoSuchFieldException. The field is of course injected. If i try something like this:

如果我想获得一个名为davClient(WebDavClient)的字段,我会得到一个NoSuchFieldException。该领域当然是注入的。如果我尝试这样的事情:

    Class currentClass = Class.forName("fr.isiom.bpms.admin.session.ext.action.root.LocalizationEditActionRoot");
    WebDavMockClient mockClient = new WebDavMockClient();
    Field f = currentClass.getDeclaredField("davClient");
    f.setAccessible(true);
    f.set(localizationEditActionRoot, mockClient);

I get the field davClient but the value isn't changed. He is still a instance of WebDavClient instead of WebDavMockClient. How can I change that field value?

我得到字段davClient但值没有改变。他仍然是WebDavClient的实例,而不是WebDavMockClient。如何更改该字段值?

1 个解决方案

#1


0  

I haven't managed to do it with reflection. Instead I found a simpler idea/solution. In my test class, at Create/setUp method I get the sessionContext from Seam Context and change the webDavClient name to be bound with WebDavMockClient instead of WebDavClient. And this is how you solve this whole problem with a one-liner:

我没有设法用反射来做。相反,我发现了一个更简单的想法/解决方案在我的测试类中,在Create / setUp方法中,我从Seam Context获取sessionContext,并将webDavClient名称更改为与WebDavMockClient而不是WebDavClient绑定。这就是你用单线程解决整个问题的方法:

// Setting webDavClient to be injected by seam as WebDavMockClient to mock the actual delete
Contexts.getSessionContext().set("webDavClient", new WebDavMockClient());

#1


0  

I haven't managed to do it with reflection. Instead I found a simpler idea/solution. In my test class, at Create/setUp method I get the sessionContext from Seam Context and change the webDavClient name to be bound with WebDavMockClient instead of WebDavClient. And this is how you solve this whole problem with a one-liner:

我没有设法用反射来做。相反,我发现了一个更简单的想法/解决方案在我的测试类中,在Create / setUp方法中,我从Seam Context获取sessionContext,并将webDavClient名称更改为与WebDavMockClient而不是WebDavClient绑定。这就是你用单线程解决整个问题的方法:

// Setting webDavClient to be injected by seam as WebDavMockClient to mock the actual delete
Contexts.getSessionContext().set("webDavClient", new WebDavMockClient());