不变的子类 [英] Immutable subclasses

查看:61
本文介绍了不变的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个多线程框架.为了避免产生副作用,我希望所有由framewok操作的数据必须是不可变的.

I'm currently developping a multithreaded framework. To avoid side effects, I would like to require that all data manipulated by the framewok has to be immutable.

那么Java中是否存在一种方法来指定我希望给定类的所有子类还是实现给定接口的所有类都是不可变的?

So does a way exist in Java to specify I want all subclasses of a given class or all classes implementing a given interface to be immutable?

推荐答案

我建议调查可变性检测器.它执行静态分析以确定给定的类是否不可变.它可以用来添加类似于运行时断言的内容,即,如果您传递了可变类的实例,则可以选择引发异常.请注意,它可以在运行时分析实际的具体类,包括您定义的接口的子类或实现.

I recommend looking into Mutability Detector. It performs static analysis to determine if a given class is immutable. It could be used to add something akin to a runtime assertion, i.e. you could choose to throw an exception if you are passed an instance of a mutable class. Note that it could analyse the actual, concrete class at runtime, including subclasses or implementations of an interface that you have defined.

它仍然是1.0之前的版本,并且在java.lang.String中存在问题,但是可以使用.如果它与您要查找的内容很接近,但并没有完全满足您的要求,我建议您联系邮件列表或提交错误报告,因为我认为项目维护者是一个非常合理的人.免责声明:那个维护者是我;-)

It is still a pre-1.0 release, and has issues with java.lang.String, but it could be usable. If it's close to what you're looking for, but doesn't quite do what you want, I recommend contacting the mailing list or filing a bug report, as I believe the project maintainer is quite a reasonable guy. Disclaimer: that maintainer is me ;-)

这篇关于不变的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆