English 中文(简体)
如何推翻审校/离队班制方法? (例如,思考,cglib)
原标题:How to override instance/concrete class s method runtime? (e.g. reflection, cglib)

大麻是一种能够使用的方法。

  • generate instance of Class X (a class variable passed in arg) and
  • override some of it s method

更具体地说,第X类我想要凌驾于括号内。

  • Contains no default constructor (e.g. all constructors with args)
  • Constructors calling non-private method within the same class

Originally I thought it s quite simple to use reflection or something similar, Then I found there s limitation on implementing my requirement.

I think this is achievable, since Mockito can do all kinds of method injection runtime.
Please anyone give some advise, Thanks.
The pseudo-code I image is like this:

createAndOverride(Class X) {
    X newObj = X.newInstance(args) {
        @override
        methodOfX(args2) {
            ...
        }
    }
    return newObj;
}
  • Original problem scenario

I was intended to test a Class which has several methods calling X1.get(), X2.get(), X3.get()
In some test case, I need to make Xn.get() to return something I can control for test (e.g. null)
Due to below constraint:

  • But due to mock tool restriction to JMock 1.0 (I have no control :( ), so I cannot just simply mock Xn.get() to returns "someSpecifiedObjects"
  • Xn has no null constructors and constructors calling non-private member

My workaround is self made Xn Class and pass them to test case to let Cn.get() to be expected
code example:

ClassToTest.SomeMethod(new X1() {
    @override
    get() {
        return someSpecifiedObjects;
    }
});

And this kind of thing is spread-ed over the Test Case.
Therefore, In order to reduce duplicate code, I would like to build a method to generate Xn instance with specified overrided method for test. e.g.

X1 x1 = createAndOverride(X1);

然后,该职位的问题就出现了。

最佳回答

因此,我认为你需要的是类似于C# s代码。 Emit:

虽然我没有这样做,但我认为,你应该能够利用思考/接纳和动态的类型创造,以实现你们重新寻找的东西。 然而,我仍然要提到,如果你重新尝试“功能性”而这种“功能性”不是他所操纵的职能,那么你可能根本不做试验。 例如:

SomeObjectInterface get()
{
    if(_someObjectStateIsSet)
    {
        // Return a concrete implementation A
        return new ConcreteImplA();
    }
    else
    {
        // Return a concrete implementation B
        return new ConcreteImplB();
    }
}

在这种情况下,没有可返回<代码>null<>/code>的密码途径,因此,你不必测试<代码>null。 我不敢肯定,如果我正确理解你的问题100%,特别是为什么你重新测试<代码>null,但我想到上述建议,看你的工作是什么。

问题回答

http://www.csg.is.titech.ac.jp/~chiba/javassist/“rel=“nofollow”>javassist? 您可以制定法规,在运行时采用你的方法。 我个人尽量避免操纵法典。 您的法典基础不能凌驾于这些价值观之上,而不能在传单上这样做? 或许像包装一样?





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签