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

- 6,160
- 5
- 32
- 46

- 22,327
- 24
- 73
- 114
-
Why don't you test it with a unit test? Fill it with random data and see what happens. How do you want to retrieve this result? – Jeroen Vannevel Dec 12 '13 at 15:30
-
You are hard pressed to find a 100% effective solution – everton Dec 12 '13 at 15:46
-
Please clarify: Why are you doing this? Do you own all the code that you intent to apply this testing? Do you want to test any external (api) code? – everton Dec 12 '13 at 15:56
-
Imagine you are writing an API fucntion that takes a Method object and returns a boolean indicating whether the method is purely functional. – One Two Three Dec 12 '13 at 15:59
-
Do you own all the code that you intent to apply this testing?¹ – everton Dec 12 '13 at 16:01
-
Yes, I do. Hmm.. why? – One Two Three Dec 12 '13 at 16:15
-
I don't know if this is possible, but asking to see if someone with more expertise can help. Would it be possible to take just the code from the function and put it into a VM where it had no security permissions to affect application state? Again I don't know enough about things like SecurityManager, so this may be completely undoable. But the idea is generally to put the code into an isolated container (VM, etc.), that enforces the rules needed, and see if any violations occur. – CLo Dec 12 '13 at 16:25
-
5wouldn't it be equivalent to the halting problem? – ratchet freak Dec 12 '13 at 16:25
-
So if you own all the code you're testing, why don't you know whether it's functional or not? – OrangeDog Dec 12 '13 at 17:55
-
If you do own all the code you intent to test, go with @SotiriosDelimanolis approach. Just add an annotation (or configure somehow somewhere) if the methods are functional or not. – everton Dec 13 '13 at 11:56
5 Answers
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.

- 302,674
- 57
- 556
- 614
-
Do you happen to know of any paper that discussed the computability of this kind of problem? And thanks for the answer.:) – One Two Three Dec 13 '13 at 14:55
-
@OneTwoThree: nope, don't know of that. Intuitively I'd say you're running close to halting-problem area here, but practically you can probably get pretty far by allowing "don't know" and treating it as no (in other words: a few simple heuristics will probably work for most positive cases). – Joachim Sauer Dec 13 '13 at 15:04
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.)

- 274,122
- 60
- 696
- 724
-
+1 I was about to comment something like "make the dev add an annotation". – Joachim Sauer Dec 12 '13 at 15:31
-
I think that this applies to a very short set of scenarios, given that he would need to control all the code he intends to check. But if the OP does have that control, yes it's effective. Also, this relies on what it's being said about the method (the annotation), not what it really does. – everton Dec 12 '13 at 15:48
-
@EvertonAgner You can apply caching without the annotation, but with AOP config. The document linked explains the configuration required. – Sotirios Delimanolis Dec 12 '13 at 15:50
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.

- 7,579
- 2
- 29
- 42
-
But only if you can properly define what "the environment" is so that you can create all possible scenarios in which you can change something in it to see if that change influences the method / static function. Otherwise even by throwing unit tests at it, you still can't know with reasonable certainty. – Gimby Dec 12 '13 at 15:36
-
Yeah, I'm aware of that. But if you can indeed load an entire "test" environment for the unit tests, probably this will be very effective. – everton Dec 12 '13 at 15:38
-
What if I am writing an API that takes a method as its input and return a boolean? – One Two Three Dec 12 '13 at 15:44
-
You would probably just test N times the method with it's parameter and then check if the results are the same. – everton Dec 12 '13 at 15:55
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.

- 4,327
- 5
- 35
- 52
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.

- 2,169
- 1
- 15
- 26