检验java配置没有sharing,java-检测对象是否存储状态或可以共享

我正在实现一个对象缓存:

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;

}

这种在特定类型的所有请求之间共享实现接口ProcessingStrategy的对象的方案仅在没有在具体类中存储状态的情况下才有效.

如何编写no_state方法?

我正在考虑使用反射来检查是否所有成员字段都是最终的:

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值