Determine if a static method is purely functional

435 views Asked by At

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)?

5

There are 5 answers

2
Joachim Sauer On BEST ANSWER

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.

4
everton On

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.

3
Sotirios Delimanolis On

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.)

0
Stroboskop On

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.

0
Sergey Fedorov On

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.