泛型 - 动态创建数据
这是一个复杂的问题,我很难解释,所以我提前道歉。
想象一个调用另一个项目方法的应用程序。我需要一种生成数据以匹配参数列表的方法。显然,如果参数类型属于我无法生成的某个类,那么它应该会失败,但如果它是 int[]
和 int[][]
一个 List
a Map
那么它应该是可能的。
我正在努力寻找解决这个问题的好方法。我可以通过 method.getGenericParameterTypes(); 获取参数的类型,参数 HashMap 的示例为 java.util.HashMap
我认为泛型在这里有一些用处?唯一的问题是我无法控制正在调用的代码。我如何使用其中一种类型,然后为其生成数据?
我对糟糕的解释感到抱歉,感谢任何帮助,
谢谢
这里我们看到了我想要调用的方法的示例,我想记录运行需要多长时间(这是通过反射完成的)但是,我需要生成数据对于参数。我需要一种生成数据的方法来匹配
public void someMethod(Param a, Param b, Param c)
{
//some user code I have no control over
}
This is a complicated question which i will find hard to explain so i appologise in advance.
Imagine an application that invokes another projects methods. I need a way of generating data to match the parameter list. Obviously if the parameter types are of some class that I have no way of generating then it should fail but if its an int[]
and int[][]
a List<String>
a Map<Integer, String>
then it should be possible.
What i am struggling with is a decent approach for solving this. I can get the types of parameters via method.getGenericParameterTypes();
example for the parameter HashMap would be java.util.HashMap<java.lang.String, java.lang.Integer>
but there are quite a lot of different possibilities right!
I assume generics has some use here? The only issue with that being I have no control over the code that is being invoked. How can I use one of these types and then generate data for it?
I am sorry for the poor explanation, any help appreciated
Thanks
Here we see an example of a method i want to invoke, I want to record how long it takes to run (this is being done via reflection) however, i need to generate data for the parameters. I need a way of generating data to match
public void someMethod(Param a, Param b, Param c)
{
//some user code I have no control over
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为也许问题应该是为什么你需要这样做。也许如果您解释了您的使用/业务案例,我们可以提供更清晰(更简单)的解决方案。
从你的最后几行开始,你正在谈论想要分析该方法。一般来说,很少想在处理链的中间只分析一个微小的方法。话虽这么说,我可以想象一些可能发生这种情况的复杂情况。然而,即使如此,您也应该能够自己编写少数情况。
如果这是实际分析的问题,但您不确定如何做到这一点,因此觉得唯一的方法是单独调用每个方法,以便您可以用启动/停止计时器“包装”它,我强烈建议研究AOP。 Spring 和 AspectJ 非常适合 AOP,AspectJ 能够完成 Spring 无法完成的字节编织。
因此,使用 AspectJ,您将能够创建自己的分析计时器类,并在编译时将它们编织到字节码中,然后运行标准测试,但可以在每个方法的基础上可视化您想要/需要的所有分析信息。
最重要的是,它可以避免您需要想出一些复杂的方案来生成与正在测试的方法并不真正相关的随机测试数据。
I think perhaps the question should be why you need to do this. Perhaps if you explained your use/business case, we can provide a cleaner (and easier) solution.
From your very last lines, you are talking about wanting to profile the method. Generally speaking, it is rare to want to profile just one small tiny method in the middle of chain of processing. That being said, I can imagine some convoluted cases where this might occur. However, even at that, it should be a handful of cases that you would be able to code yourself.
If it is a question of actual profiling, but you are not sure how to do it, and consequently feel that the only way is to call each method individually so you can "wrap" it with a start/stop timer, I would strongly recommend looking into AOP. Both Spring and AspectJ are great for AOP, with AspectJ able to do byte-weaving that Spring is unable to accomplish.
Consequently, with AspectJ you would be able to create your own profiling timer classes and weave them into the byte-code at compile time and then run your standard tests, but visualize all the profiling info that you want/need on a per-method basis.
And best of all, it would avoid you needing to come up with some convoluted scheme for producing random test data that isn't really relevant to the method being tested.
听起来是一个可怕的问题。
如果您有代表该方法的参数类型的 class[] ,那么您不能创建每个参数类型的新实例并将其作为参数列表传递给该方法吗?这是未经测试的:
Sounds like a horrible problem.
If you have the class[] that represents the parameter types of the method then can't you create a new instance of each and pass that as the arguments list to the method? This is untested: