简体   繁体   English

为什么Java类库仍然使用String常量来代替枚举

[英]Why does Java class library still use String constants in place of enum

I am using a few Java classes like javax.Mail.Session and MessageDigest for a tool I am building. 我正在使用一些Java类,如javax.Mail.SessionMessageDigest用于我正在构建的工具。

I noticed that it was difficult assigning them properties because they were using String constants for that. 我注意到很难为它们分配属性,因为它们正在使用String常量。

For example, for a Session object, you have to assign String key value pairs in a Property instance which is then used to create a Session . 例如,对于Session对象,您必须在Property实例中分配String键值对,然后用于创建Session So if you want your session to log debugging messages, assign "smtp.mail.debug" , "true" in the Property instance. 因此,如果您希望会话记录调试消息,请在Property实例中指定"smtp.mail.debug""true" Similarly, if you want your MessageDigest to use SHA , create the MessageDigest instance as MessageDigest.getInstance("SHA") 同样,如果您希望MessageDigest使用SHA ,请将MessageDigest实例创建为MessageDigest.getInstance("SHA")

I am yet to figure out what to do and where to get the information if say I wanted to implement MessageDigest using MD5 / RC4 etc, or add another property to my Session object. 如果我想使用MD5 / RC4等实现MessageDigest ,或者在Session对象中添加另一个属性,我还没弄清楚要做什么以及从何处获取信息。

Wouldn't it be really better if public enums were exposed by these respective classes to assign properties ? 如果公共枚举被这些各自的类暴露给分配属性,那不是真的更好吗?

Would save programmers lot of searching time at least. 至少可以为程序员节省大量的搜索时间。

I can see 2 main reasons: 我可以看到两个主要原因:

Backward compatibility 向后兼容性

The two examples you give were already part of the API before enum s got introduced in Java 1.5. 在Java 1.5中引入enum之前,您提供的两个示例已经是API的一部分。 There's many more cases like that out there. 还有更多像这样的案例。

Extensibility 可扩展性

Take a look at for example MessageDigest . 看看例如MessageDigest The javadoc specifies: javadoc指定:

Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: 需要Java平台的每个实现来支持以下标准MessageDigest算法:

• MD5 •MD5
• SHA-1 •SHA-1
• SHA-256 •SHA-256

This lets other java.security.Provider libraries provide MessageDigest implementations for other algorithms than the ones minimally required by the API. 这使得其他java.security.Provider库为其他算法提供MessageDigest实现,而不是API最低要求的算法。 Listing only the default ones in a restrictive enum on the API level would limit extensibility. 仅在API级别的限制性enum列出默认值将限制可扩展性。

The same goes for allowing other javax.mail.Provider implementations to support additional/custom properties in the case of mail session properties. 允许其他javax.mail.Provider实现在邮件会话属性的情况下支持其他/自定义属性也是如此。

This is likely due to the major focus of Java to provide backwards compatiablity to previous versions. 这可能是由于Java的主要焦点在于向以前的版本提供向后兼容性。

enum was introduced in Java 1.5, hence any API which was written against 1.4 or earlier will not provide support for this feature. enum是在Java 1.5中引入的,因此针对1.4或更早版本编写的任何API都不会为此功能提供支持。 This is very common amongst many of the APIs in the JDK 这在JDK中的许多API中非常常见

You also need to remember, enum is not extendable, meaning that if your wanted to introduce a new algorithm in the message digest, for example, you would be stuck...there'd be no way you could do it. 您还需要记住, enum不可扩展,这意味着如果您想在消息摘要中引入新算法,例如,您将被卡住......您无法做到这一点。

The same goes with the mail API, the mail API is provides support for distinctively different concepts, not all of them will have the same series of properties and would be impossible to devise a single enum capable of supporting ALL the various properties between different implementations that exist now or in the future, enum is just simply to inflexible for this job. 邮件API也是如此,邮件API提供了对不同概念的支持,并非所有概念都具有相同的属性系列,并且不可能设计出能够支持不同实现之间的所有各种属性的单个枚举。现在或将来存在, enum只是为了这项工作而不灵活。

It will be to simple to think that reason is only backward compatibility. 简单地认为原因只是向后兼容。

For JavaMail because it has too many different settings related to different connectors and you only need some part of them to setup connection with one of supported protocols. 对于JavaMail,因为它有太多与不同连接器相关的不同设置,您只需要其中一部分与其中一个支持的协议建立连接。 Each connector is implemented in separate class and as I think, there is no reason to let for example, POP3 connector to know settings for IMAP connector. 每个连接器都是在单独的类中实现的,我认为没有理由让POP3连接器知道IMAP连接器的设置。

For MessageDigest the reason is to support non-standard encryption algorithms, which is not packed with JDK but provided by 3rd party JCE adapters. 对于MessageDigest ,原因是支持非标准加密算法,该算法未使用JDK打包,而是由第三方JCE适配器提供。 For example how it will look for GOST algorithm provided by CryptoPro JCE: 例如,它将如何查找CryptoPro JCE提供的GOST算法:

  MessageDigest digest = MessageDigest.getInstance("GOST3411"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM