2026년 4월 2일 작성
Java Unchecked Warning
unchecked warning은 generic으로 programming할 때 형 안전성(typesafe)을 보장하기 위한 compile 경고로, runtime에 ClassCastException 발생 가능성을 나타내며, 형 안전성이 확실한 경우에만 @SuppressWarnings("unchecked")로 억제합니다.
Unchecked Warning
- unchecked warning은 generic으로 programming할 때 code의 형 안전성(typesafe)을 보장하기 위한 compile 경고입니다.
- program 실행 도중에
ClassCastException이 발생할 가능성을 나타냅니다. - unchecked warning은 가능하다면 없애야 합니다.
- program 실행 도중에
Unchecked Warning의 종류
- compiler가 발생시키는 unchecked warning은 네 가지 유형이 있습니다.
| 유형 | 설명 |
|---|---|
| unchecked cast warning | 무점검 형변환 경고 |
| unchecked method invocation warning | 무점검 method 호출 경고 |
| unchecked generic array creation warning | 무점검 generic 배열 생성 경고 |
| unchecked conversion warning | 무점검 변환 경고 |
@SuppressWarnings(“unchecked”)
@SuppressWarnings("unchecked")는 형 안전성이 확실하지만 제거할 수 없는 경고를 억제하는 annotation입니다.- 어떤 크기의 단위에도 적용 가능하지만, 가능한 작은 범위에 적용해야 합니다.
- 넓은 범위에 적용하면 중요한 warning을 놓칠 수 있기 때문입니다.
- 반드시 왜 형 안전성을 위반하지 않는지 주석을 통해 정확히 밝혀야 합니다.
public <T> T[] toArray(T[] a) {
if (a.length < size) {
// 아래의 형변환은 배열의 자료형이 인자로 전달된 자료형인 T[]와 같으므로 정확함
@SuppressWarnings("unchecked") T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
System.arraycopy(elements, 0, a, 0, size);
if (a.length > size) {
a[size] = null;
}
return a;
}
@SuppressWarnings를 method 전체가 아닌 변수 선언 단위에 적용하여 경고 억제 범위를 최소화한 예시입니다.