๐Ÿ“‚ JAVA/์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”

๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ - [5์žฅ. ์ œ๋„ค๋ฆญ(์•„์ดํ…œ27)]

Amenable 2023. 5. 23. 21:14

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. ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š” ์ด์œ  ๐Ÿฆš

  ์ด๋ฒˆ ์•„์ดํ…œ์—์„œ๋Š” '๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ'๋ผ๊ณ  ๋งํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์•ˆ์ „ํ•˜๋‹ค๊ณ  ๊ฒ€์ฆ๋œ ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ˆจ๊ธฐ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๋‘๋ฉด, ์ง„์งœ ๋ฌธ์ œ๋ฅผ ์•Œ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ๊ฒฝ๊ณ ๊ฐ€ ๋‚˜์™€๋„ ๋ˆˆ์น˜์ฑ„์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๋‹ค.
  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;
    }
}

  ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๊ทธ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋ฉฐ, ๋” ์ค‘์š”ํ•˜๊ฒŒ๋Š”, ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๊ทธ ์ฝ”๋“œ๋ฅผ ์ž˜๋ชป ์ˆ˜์ •ํ•˜์—ฌ ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ์žƒ๋Š” ์ƒํ™ฉ์„ ์ค„์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

ํ•ด๋‹น ๊ธ€์€ ๋ฐฑ๊ธฐ์„  ๋‹˜์˜ '์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ์™„๋ฒฝ ๊ณต๋žต'์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๋Œ“๊ธ€์ˆ˜0