What happens when jdk compiles the generic type <T>

2022-08-16

ClassCastException does not occur when cast to a different type from the type passed to generic <T>

๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ์ฝ”๋“œ

1public static <T> T getData(MyObject myObject, Class<T> resultType) throws CustomException{
2  JSONParser parser = new JSONParser();
3  JSONObject result;
4  T data;
5  
6  try{
7    result = (JSONObject) parser.parse(myObject.getPayload());
8    data = (T) result.get("data");
9  } catch(ParseException e) {
10    e.printStackTrace();
11    throw new CustomException(404, e.getMessage());
12  } catch(ClassCastException e){
13    throw new CustomException(500, e.getMessage());
14  }
15  
16  return data;
17}
  • data = (T) result.get("data"); ์ด ๋ถ€๋ถ„์—์„œ resultType์œผ๋กœ ๋„˜๊ธด ๊ฒƒ๊ณผ ์‹ค์ œ "data"์˜ ํƒ€์ž…์ด ๋‹ค๋ฅด๋ฉด,
    catch ๊ตฌ๋ฌธ์—์„œ ClassCastException ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ๋’ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์—์„œ CustomException์ด ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ์Œ.
    ํ•˜์ง€๋งŒ ์‹คํ–‰ํ•ด๋ณด๋‹ˆ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์—์„œ ClassCastException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ.

Generic <T>๊ฐ€ ์ปดํŒŒ์ผ ๋  ๋•Œ

  • Generic Type Erasure
    1Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming.  
    2To implement generics, the Java compiler applies type erasure to:
    3
    4- Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded.
    5  The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
    6- Insert type casts if necessary to preserve type safety.
    7- Generate bridge methods to preserve polymorphism in extended generic types.
    8
    9Type erasure ensures that no new classes are created for parameterized types;  
    10consequently, generics incur no runtime overhead.
    • ์ฒซ๋ฒˆ์งธ ๊ทœ์น™ : java code๊ฐ€ ์ปดํŒŒ์ผ ๋  ๋•Œ, unbound ์ƒํƒœ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋ฉด Object๋กœ bound๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฉด bound class๋กœ ๋ณ€๊ฒฝ๋จ.
      1//before compile
      2public class Node<T> {
      3  private T data;
      4  private Node<T> next;
      5
      6  public Node(T data, Node<T> next){
      7    this.data = data;
      8    this.next = next;
      9  }
      10
      11  public T getData(){
      12    return data;
      13  }
      14}
      15//after compile
      16public class Node {
      17  private Object data;
      18  private Node next;
      19
      20  public Node(Object data, Node next){
      21    this.data = data;
      22    this.next = next;
      23  }
      24
      25  public Object getData(){
      26    return data;
      27  }
      28}
      • ์ด ๊ทœ์น™์— ์˜ํ•ด ๋‚ด๊ฐ€ ์ฒ˜์Œ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๊ฐ€ ์ปดํŒŒ์ผ ์ดํ›„์—๋Š” (Object) ์ด๋Ÿฐ์‹์œผ๋กœ ์บ์ŠคํŒ…ํ•˜๋„๋ก ๋ณ€๊ฒฝ๋จ์„ ์•Œ์•˜์Œ ์ด๋•Œ, Object๋Š” java ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ƒ์œ„ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์›๋ž˜ ์˜๋„ํ•˜๋˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ String์ด ๋ฆฌํ„ด๋œ๋‹ค๊ณ  ํ•ด๋„ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ๋ฆฌ๊ฐ€ ์—†์—ˆ๋‹ค.
    • ๋‘๋ฒˆ์งธ ๊ทœ์น™ : type ์•ˆ์ •์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด์„œ type cast๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค ์ด ๊ฒฝ์šฐ data = (T) result.get("data"); ๋Œ€์‹  data = (String) result.get("data"); ๊ณผ ๊ฐ™์ด ๋ช…์‹œ์  ํ˜•๋ณ€ํ™˜ ํ•  ๋•Œ ํƒ€์ž…์„ ๋ช…์‹œํ•˜๋ผ๋Š” ์˜๋„์ด์ง€๋งŒ
      ๋‚ด๊ฐ€ ์ž‘์„ฑ๋˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ด ๊ณณ์— ๋ช…์‹œํ•  ์ˆ˜๋Š” ์—†์–ด์„œ ์ตœ์ข…์ ์œผ๋กœ ๋‹ค์Œ ์ฑ•ํ„ฐ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ˆ˜์ •ํ–ˆ์Œ

์ˆ˜์ •ํ•œ ์ฝ”๋“œ

1public static <T> T getData(MyObject myObject, Class<T> resultType) throws CustomException{
2  JSONParser parser = new JSONParser();
3  JSONObject result;
4  T data;
5
6  try{
7    result = (JSONObject) parser.parse(myObject.getPayload());
8    data = resultType.cast(result.get("data"));
9  } catch(ParseException e) {
10    e.printStackTrace();
11    throw new CustomException(404, e.getMessage());
12  } catch(ClassCastException e){
13    throw new CustomException(500, e.getMessage());
14  }
15
16  return data;
17}

Generic์— ๋Œ€ํ•ด์„œ ๋ถ„๋ช…ํžˆ ์ทจ์—…์ „์— ๊ณต๋ถ€ํ–ˆ๋Š”๋ฐ, ๋ง‰์ƒ ์‹ค์ œ ์—…๋ฌด ์ฝ”๋“œ์— ์ ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ทธ๋ ‡๊ณ  ์ปดํŒŒ์ผ ๋’ค์— ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€๋„ ๊ทธ๋ ‡๊ณ 
์•„์ง ์•Œ์•„์•ผํ•  ๊ฒƒ์ด ๋งŽ์Œ์„ ๋А๊ผˆ๋‹ค.

/end of What happens when jdk compiles the generic type <T>
CONTENT LISTMERRI๏ผ‡s DEVELOG
Ubuntu ๋ช…๋ น์–ด ์ •๋ฆฌ(2)
2022-11-09