문제

Given a java.lang.reflect.Method object, is there anyway to determine whether the method is purely functional (i.e., given the same input, it will always produce the same output and it is stateless. In other words, the function does not depend on its environment)?

도움이 되었습니까?

해결책

No, there's no way to do it.

Reflection does not allow you to inspect the actual code behind the method.

And even if that where possible, the actual analysis would probably be ... tricky, to say the least.

다른 팁

No there is no way to do that with reflection or any other mechanism.

The developer knows if the method is functional. For example, Spring has a @Cacheable annotation that gives a hint to the application that the method is functional and can therefore cache the result for a given set of arguments. (Spring will wrap your object in a proxy that provides the caching behavior.)

is there anyway to determine whether the method is purely functional(ie., given the same input, it will always produce the same output

I know it's now what you've asked for, but Unit Tests may help you with this.

No. Reflection can not read the byte code of the method. So you can't really tell what a method does or even what other classes it uses.

Reflection will not help you here. If you really want to define it at run time, you can try to use javap -c classname.class. But it would be better to avoid such a hacks.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top