-
[Java] @Deprecated, @deprecatedBackEnd/Java 2023. 5. 1. 22:58
강의 : inflearn 백기선
참고 : https://docs.oracle.com/javase/1.5.0/docs/guide/javadoc/deprecation/deprecation.html
자바는 클래스가 진화함에 따라 API가 불가피하게 변경되기 때문에 사용 중지를 표현하는 방법을 제공한다.
메소드의 이름이 변경되고, 새 메소드와 더 나은 메서드가 추가되며 필드가 변경된다. 개발자들이 새로운 API로 전환할 때까지 기존 API를 유지해야 하지만, 개발자들이 계속해서 이전 API로 프로그래밍하는 것을 원하지 않는다.
@Deprecated
- 더 이상 사용되지 않을 수 있음을 의미. (사용 자제 권장)
- Type, Field, Method, ... 등에 붙일 수 있다.
- 컴파일 타임에 경고 메세지를 통해 Client에게 알려준다. (annotation processor)
@deprecated
- javadoc 태그
- 새 API 사용 방법을 설명하는 적절한 주석과 함께 사용한다.
public class Deprecation { /** * @deprecated in favor of * {@link #Deprecation(String)} */ @Deprecated(forRemoval = true, since = "1.2") // 1.2 버전부터 삭제가 될 것임을 의미 public Deprecation() { } private String name; public Deprecation(String name) { this.name = name; } }
java 9부터
forRemoval
,since
property가 추가되었다.조금 더 강력하게 경고.
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE}) public @interface Deprecated { /** * Returns the version in which the annotated element became deprecated. * The version string is in the same format and namespace as the value of * the {@code @since} javadoc tag. The default value is the empty * string. * * @return the version string * @since 9 */ String since() default ""; /** * Indicates whether the annotated element is subject to removal in a * future version. The default value is {@code false}. * * @return whether the element is subject to removal * @since 9 */ boolean forRemoval() default false; }
'BackEnd > Java' 카테고리의 다른 글
Functional Interface 정리 (1) 2025.01.07 [Java] Super Type Token (0) 2024.12.25 [JAVA] ThreadLocal, TreadLocalRandom (0) 2023.10.20 [Java] String Constant Pool (0) 2023.05.02