@Deprecated新外观可能是什么?
JDK增強建議 (JEP)277(“ 增強的棄用 ”)建議“重新定義棄用注釋,并提供工具來增強功能生命周期的尾端?!?當(dāng)前@ java.lang.Deprecated的一些限制使我困擾了一段時間。 我特別希望能夠使用@Deprecated提供文本,而不是被迫在相應(yīng)的Javadoc @deprecated注釋中放置說明性文本。 在這篇文章中,我看了一個自定義批注,它給人一種JEP 277建議包含在新的和改進的@Deprecated批注中的額外元數(shù)據(jù)類型的感覺。
dustin.examples.annotations.Deprecated的代碼清單包含dustin.examples.annotations.Deprecated的定義,該實現(xiàn)主要反映JEP 277提案中描述的內(nèi)容。
不推薦使用dustin.examples.annotations。
package dustin.examples.annotations;import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.TYPE;/*** Conceptual improvement on standard @java.lang.Deprecated annotation* based on preliminary discussion related to JEP 277 and on* desire to include context details with deprecation annotation* rather than relying on presence of Javadoc's @deprecated.** Javadoc comments in this annotation definition are largely* based on descriptions in JEP 277 (http://openjdk.java.net/jeps/277).*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE}) public @interface Deprecated {/*** JEP 277-defined reasons and associated explanations.*/public enum Reason{/*** This API has been deprecated without any reason having been given.* This is the default value; everything that's deprecated today* implicitly has a deprecation reason of UNSPECIFIED.*/UNSPECIFIED,/*** This API is earmarked for removal in a future JDK release. Note,* the use of the word "condemned" here is used in the sense of a* structure that is intended to be torn down. The term is not mean* to imply any moral censure.*/CONDEMNED,/*** Use of this API can lead to data loss, deadlock, security* vulnerability, incorrect results, or loss of JVM integrity.*/DANGEROUS,/*** This API is no longer necessary, and usages should be removed.* No replacement API exists. Note that OBSOLETE APIs might or* might not be marked CONDEMNED.*/OBSOLETE,/*** This API has been replaced by a newer API, and usages should be* migrated away from this API to the newer API. Note that SUPERSEDED* APIs might or might not be marked CONDEMNED.*/SUPERSEDED,/*** Calling this has no effect or will unconditionally throw an exception.*/UNIMPLEMENTED,/*** This API is not a stable part of the specification, and it may* change incompatibly or disappear at any time.*/EXPERIMENTAL;}/*** Provides any combination of one or more of the enum constants,* although not all combinations make sense. It is syntactically possible,* though perverse, to provide an empty array. Such a case should be* treated as if UNSPECIFIED were present.** @return One or more Reasons for deprecation; default value is the enum* constant UNSPECIFIED and absence of values should be treated as such.*/Reason[] value();/*** Provides Strings representing any APIs that replace this API.* This should not specify any replacements if reason is OBSOLETE.** @return Strings returned by this method should be links to replacement* APIs for the API being deprecated. Each string should be in the same* format as the @link Javadoc tag.*/String[] replacement();/*** Provides the release in which the API was deprecated.** @return Release number at which this API became deprecated* in a free-form syntax String with the release numbering* following the same scheme as the @since Javadoc tag.*/String since();/*** Provides the anticipated complete removal of this deprecated API* if any known date or version is anticipated for the API's removal.* This value is most likely to be set for reasons of CONDEMNED,* OBSOLETE, and SUPERSEDED. This value is NOT described in JEP 277.** @return Date or version in which it is anticipated that this* API will be removed altogether.*/String anticipatedRemoval() default "Not Planned"; }下一個代碼清單提供了將上述注釋應(yīng)用于不推薦使用的類的示例。
DeprecatedClass.java:使用改進的@Deprecated的示例
package dustin.examples.annotations.demo;import java.text.DateFormat; import java.text.SimpleDateFormat;import static dustin.examples.annotations.Deprecated.Reason.*;/*** Demonstrates how new and improved @Deprecated might be used.*/ @dustin.examples.annotations.Deprecated(value={SUPERSEDED, CONDEMNED},since="1.5", anticipatedRemoval="1.9",replacement="dustin.examples.annotations.demo.OtherClass") public class DeprecatedClass {final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");@dustin.examples.annotations.Deprecated(value={DANGEROUS}, since="1.0",replacement="java.text.SimpleDateFormat#SimpleDateFormat")public DateFormat getDateFormatter(){return dateFormat;} }最后一個示例演示了改進的@Deprecated注釋的@Deprecated 。 該“ anticipatedRemoval中的”元素@Deprecated注釋不JEP 277中提到,我給它一個默認值,其中,過時的構(gòu)造可能沒有預(yù)期的拆除日期,而是已被棄用,以嚇退只有它的新用途的情況。
上面的代碼清單演示了定義一個新的和經(jīng)過改進的@Deprecated批注(例如JEP 277中闡明的注釋)。但是,JEP 277提出的內(nèi)容遠不止存在一種改進的批注。 該提案還討論了“運行時更改”,以提供“關(guān)于在選擇加入的基礎(chǔ)上使用已棄用API的警告”,“依賴工具增強”,以類似于jdeps工具甚至可能基于jdeps工具的方式分析注釋依賴,以及“ Javadoc增強功能。”
盡管Java的自定義批注支持使實現(xiàn)@Deprecated的版本很容易實現(xiàn),該版本反映了JEP 277中的許多思想,但是經(jīng)過改進的新版@java.lang.Deprecated將具有許多自定義Java批注不具備的優(yōu)點,例如內(nèi)置-JDK中的-in支持并由JDK類使用。 由JDK提供的@Deprecated也將繼續(xù)享受IDE和工具帶來的好處,例如刪除不贊成使用的代碼結(jié)構(gòu)的名稱。
翻譯自: https://www.javacodegeeks.com/2015/11/what-might-a-new-deprecated-look-like.html
總結(jié)
以上是生活随笔為你收集整理的@Deprecated新外观可能是什么?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cc攻击和ddos攻击区别(ddos攻击
- 下一篇: 电脑桌面壁纸汽车红色(红旗车电脑壁纸)