1. ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๊ฐ๋ ๐ฆ
'๋น๊ฒ์ฌ(unchecked) ๊ฒฝ๊ณ '๋ ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์์ ์ฑ์ ํ์ธํ๋๋ฐ ํ์ํ ์ ๋ณด๊ฐ ์ถฉ๋ถํ์ง ์์ ๋ ๋ฐ์์ํค๋ ๊ฒฝ๊ณ ์ด๋ค.
๋น๊ฒ์ฌ ๊ฒฝ๊ณ ์ ์์ ์ค์ ํ๋๋ก ๋ค์๊ณผ ๊ฐ์ด ๋ก ํ์ ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ฅผ ๋ค ์ ์๋ค.
public static void main(String[] args) {
// ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฐ์
// Raw use of parameterized class 'Set'
Set names = new HashSet();
// ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฐ์
// Raw use of parameterized class 'HashSet'
Set<String> strings = new HashSet();
}
2. ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฅผ ์ ๊ฑฐํด์ผ ํ๋ ์ด์ ๐ฆ
์ด๋ฒ ์์ดํ ์์๋ '๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฅผ ์ ๊ฑฐํ๋ผ'๋ผ๊ณ ๋งํ๊ณ ์๋ค. ๊ทธ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
- ์์ ํ๋ค๊ณ ๊ฒ์ฆ๋ ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฅผ ์จ๊ธฐ์ง ์๊ณ ๊ทธ๋๋ก ๋๋ฉด, ์ง์ง ๋ฌธ์ ๋ฅผ ์๋ฆฌ๋ ์๋ก์ด ๊ฒฝ๊ณ ๊ฐ ๋์๋ ๋์น์ฑ์ง ๋ชปํ ์ ์๋ค.
- ์ ๊ฑฐํ์ง ์์ ์๋ง์ ๊ฑฐ์ง ๊ฒฝ๊ณ ์์ ์๋ก์ด ๊ฒฝ๊ณ ๊ฐ ํ๋ฌปํ ์ ์๋ค.
๊ทธ๋์ ํ ์ ์๋ ํ ๋ชจ๋ ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฅผ ์ ๊ฑฐํด์ผ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฒฝ๊ณ ๋ฅผ ์ ๊ฑฐํ ์๋ ์์ง๋ง ํ์ ์ด ์์ ํ๋ค๊ณ ํ์ ํ๋ฉด @SuppressWarnings("unchecked") ์ ๋ํ ์ด์ ์ ๋ฌ์์ ๊ฒฝ๊ณ ๋ฅผ ์จ๊ธฐ๋๋ก ํด์ผ ํ๋ค.
3. @SuppressWarnings("unchecked") ์ฌ์ฉ ๋ฐฉ๋ฒ ๐ฆข
@SuppressWarnings("unchecked") ์ ๋ํ ์ด์ ์ ๊ฐ๋ณ ์ง์ญ๋ณ์ ์ ์ธ๋ถํฐ ํด๋์ค ์ ์ฒด๊น์ง ์ด๋ค ์ ์ธ์๋ ๋ชจ๋ ๋ฌ ์ ์๋ค.
// ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
// ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๋ฐ์
// Unchecked cast: 'java.lang.Object[]' to 'T[]'
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
// 1. ํด๋์ค์ @SuppressWarnings๋ฅผ ์ ์ฉํ๋ ๊ฒฝ์ฐ
@SuppressWarnings("unchecked")
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
// 2. ๋ฉ์๋์ @SuppressWarnings๋ฅผ ์ ์ฉํ๋ ๊ฒฝ์ฐ
public class ListExample {
private int size;
Object[] elements;
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if(a.length < size) {
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
// 3. ๋ณ์ ์ ์ธ์ @SuppressWarnings๋ฅผ ์ ์ฉํ๋ ๊ฒฝ์ฐ
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
@SuppressWarnings("unchecked")
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
ํ์ง๋ง @SuppressWarnings ์ ๋ํ ์ด์ ์ ํญ์ ๊ฐ๋ฅํ ํ ์ข์ ๋ฒ์์ ์ ์ฉํด์ผ ํ๋ค. ๊ฐ๋ฅํ ํ ์ข์ ๋ฒ์์ ์ ์ฉํด์ผ ํ๋ ์ด์ ๋ ๋์ ๋ฒ์์ @SuppressWarnings๋ฅผ ์ ์ฉํ๊ฒ ๋๋ค๋ฉด ํด๋น ๋ฒ์์์ ์ฐ๋ฆฌ๊ฐ ์ธ์งํ์ง ๋ชปํ ๋น๊ฒ์ฌ ๊ฒฝ๊ณ ๊ฐ ์จ๊ฒจ์ง ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ํ, ์ ๋ํ ์ด์ ์ ์ ์ธ์๋ง ๋ฌ ์ ์๊ธฐ ๋๋ฌธ์ return ๋ฌธ์๋ @SuppressWarnings๋ฅผ ๋ค๋ ๊ฒ ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐํ๊ฐ์ ๋ด์ ์ง์ญ๋ณ์๋ฅผ ํ๋ ์ ์ธํ๊ณ ๊ทธ ๋ณ์์ ์ ๋ํ ์ด์ ์ ๋ฌ์์ฃผ์.
// return๋ฌธ์ @SuppressWarnings๋ฅผ ๋ถ์ฐฉํ๋ ๊ฒฝ์ฐ
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
// 'Annotations are not allowed here' ๋ผ๊ณ ์๋ ค์ค
@SuppressWarnings("unchecked")
return (T[]) Arrays.copyOf(elements, size, a.getClass());
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
// ๋ฐํ๊ฐ์ ๋ด์ ์ง์ญ๋ณ์ ์ด์ฉ
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
@SuppressWarnings("unchecked")
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
๋ง์ง๋ง์ผ๋ก @SuppressWarnings("unchecked") ์ ๋ํ ์ด์ ์ ์ฌ์ฉํ ๋๋ฉด ๊ทธ ๊ฒฝ๊ณ ๋ฅผ ๋ฌด์ํด๋ ์์ ํ ์ด์ ๋ฅผ ํญ์ ์ฃผ์์ผ๋ก ๋จ๊ฒจ์ผ ํ๋ค.
public class ListExample {
private int size;
Object[] elements;
public <T> T[] toArray(T[] a) {
if(a.length < size) {
/**
* ๊ฒฝ๊ณ ๋ฅผ ๋ฌด์ํด๋ ์์ ํ ์ด์ ๋ ~~~ ์ด๋ค.
*/
@SuppressWarnings("unchecked")
T[] result = (T[]) Arrays.copyOf(elements, size, a.getClass());
return result;
}
if(a.length > size) {
a[size] = null;
}
return a;
}
}
์ด๋ ๊ฒ ํ๋ฉด ๋ค๋ฅธ ์ฌ๋์ด ๊ทธ ์ฝ๋๋ฅผ ์ดํดํ๋ ๋ฐ ๋์์ด ๋๋ฉฐ, ๋ ์ค์ํ๊ฒ๋, ๋ค๋ฅธ ์ฌ๋์ด ๊ทธ ์ฝ๋๋ฅผ ์๋ชป ์์ ํ์ฌ ํ์ ์์ ์ฑ์ ์๋ ์ํฉ์ ์ค์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค.
ํด๋น ๊ธ์ ๋ฐฑ๊ธฐ์ ๋์ '์ดํํฐ๋ธ ์๋ฐ ์๋ฒฝ ๊ณต๋ต'์ ์ฐธ๊ณ ํ์์ต๋๋ค.