Detection if an object holds state or can be split

I implement an object cache:

void cache(String request_type, String request_id, ProcessingStrategy request_strategy) 
{ 
    if (no_state(request_strategy) == true)
       map.put(request_type, request_strategy);
    else
       map.put(request_id, request_strategy); //no chance of keyspace collision
}

ProcessingStrategy lookup(String request_type, String request_id)
{
    ProcessingStrategy request_strategy = map.get(request_type);
    if (request_strategy == null) return map.get(request_id)
    else return request_strategy;
}

This scheme of sharing objects implementing interface ProcessingStrategyfor all requests of a certain type will work only if the state is not saved in a particular class.

How to write a method no_state?

I think that checking for the presence of all member fields finalusing Reflection will be:

for(Field f : request_strategy.getClass().getDeclaredFields())
    if(!Modifier.isFinal(f.getModifiers()))
        return false;
//even if all fields are final;
//how to check that they are not being initialized with differently in the ProcessingStrategy constructor?
+3
source share
2 answers

What you are trying to do (detect the variability of an arbitrary object), I do not think it can be done. Even if it were possible, through some reflexive acrobatics, it still seems like a bad idea.

ProcessingStrategy , , isStateless() StatelessProcessingStrategy, , , .

+2

, , - , Reflection,

.

, . , / ( ) ?

, Java , ... , .

, , "", "" " ", .

+2

All Articles