如果当然...如果您对应用程序中使用的集合类具有全部控制,或者可能会遇到库...然后测试类 May 更可行。
But I think we agreed that try catch is not a valid control flow tool.
It is debatable. But lets not debate it.
Is there something I'm missing?
No.
[Is there] any method that returns a boolean (modifiable or not) or an interface that signals modifiablity / unmodifiablity?
No and No.
There is no standard API method or marker interface. (I guess you could implement your own ... but you would run into difficulties when using the standard collection implementation classes.)
The best you could do is to do some messy tests for specific implementation classes that are known to be modifiable or non-modifiable. But that approach has problems too:
You can't just test for the (private) UnmodifiableCollection etc classes. Other collection classes can be unmodifiable.
Even within the Java SE class library, the classes you would need to test for actually differs between different Java versions. (See @Holger's comment!)
You can't use instanceof because someone could subclass a modifiable class to be unmodifiable ... or vice versa.
Modifiability could depend on a collection object's runtime state. For example, the object could have a "frozen" flag that can be set after populating the collection using add methods.
In general, catching the exception is the most practical solution, irrespective of your feelings on how "proper" it is to use exceptions for this.
If course ... if you have total control over the collection classes that are used in your application, or may be encountered by your library ... then testing the classes may be more viable.
if(yourcolletion.getClass().getSimpleName().equals("UnmodifiableCollection")) {
//can not modified
} else {
//given list is modifiable
}
you can also add for more check for UnmodifiableList etc.
Note: I would have recommended to use instanceof instead of checking for class name but you can not import the UnmodifiableList or UnmodifiableCollection classes into your code
发布评论
评论(2)
这是有争议的。但不要辩论。
不。
否。
没有标准的API方法或标记接口。 (我想您可以实施自己的...但是,在使用标准集合实施类时,您会遇到困难。)
您能做的最好的方法是对特定的实现类进行一些凌乱的测试,这些测试已知是可修改或非 -可修改。但是该方法也存在问题:
您不能仅测试(私有)
undifiableCollection
等类。其他收集课程可能无法解码。即使在Java SE类库中,您还需要测试的类在不同的Java版本之间确实有所不同。 (请参阅 @Holger的评论!)
您不能使用
instanceof
,因为有人可以将可修改类的类子类别用于无法解码...或反之亦然。。
可修改性可以取决于集合对象的运行时状态。例如,该对象可以使用
add
methods。通常,最实际的解决方案是最实用的解决方案,无论您对使用例外情况如何正确的感觉如何。
如果当然...如果您对应用程序中使用的集合类具有全部控制,或者可能会遇到库...然后测试类 May 更可行。
It is debatable. But lets not debate it.
No.
No and No.
There is no standard API method or marker interface. (I guess you could implement your own ... but you would run into difficulties when using the standard collection implementation classes.)
The best you could do is to do some messy tests for specific implementation classes that are known to be modifiable or non-modifiable. But that approach has problems too:
You can't just test for the (private)
UnmodifiableCollection
etc classes. Other collection classes can be unmodifiable.Even within the Java SE class library, the classes you would need to test for actually differs between different Java versions. (See @Holger's comment!)
You can't use
instanceof
because someone could subclass a modifiable class to be unmodifiable ... or vice versa.Modifiability could depend on a collection object's runtime state. For example, the object could have a "frozen" flag that can be set after populating the collection using
add
methods.In general, catching the exception is the most practical solution, irrespective of your feelings on how "proper" it is to use exceptions for this.
If course ... if you have total control over the collection classes that are used in your application, or may be encountered by your library ... then testing the classes may be more viable.
尝试以下尝试:
您还可以添加更多检查
unodifiablelist
等。注意:我建议使用
实例
而不是检查类名称但是您无法将undifiablelist
或undifiableCollection
类导入代码try below:
you can also add for more check for
UnmodifiableList
etc.Note: I would have recommended to use
instanceof
instead of checking for class name but you can not import theUnmodifiableList
orUnmodifiableCollection
classes into your code