Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a static method is purely functional

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

like image 521
One Two Three Avatar asked Dec 12 '13 15:12

One Two Three


People also ask

Are static methods pure functions?

No. Just being static does not make a function pure. In purely functional programming the outcome of the function should depend only on their arguments, regardless of global state. Static functions can easily access and modify global state.

Is static method a function?

A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.

How do you know if a method should be static?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.

Can static methods have parameters?

Like a mathematical function, a Java static method can take on more than one argument, and therefore can have more than one parameter variable.


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

like image 98
Joachim Sauer Avatar answered Sep 17 '22 20:09

Joachim Sauer


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

like image 21
Sotirios Delimanolis Avatar answered Sep 20 '22 20:09

Sotirios Delimanolis