사용자 도구

사이트 도구


java:generics

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
마지막 판 양쪽 다음 판
java:generics [2018/10/19 15:22]
kwon37xi
java:generics [2019/04/22 14:19]
kwon37xi
줄 13: 줄 13:
  
 ===== 현재 객체의 Generic Type 파라미터 클래스 알아내기 ===== ===== 현재 객체의 Generic Type 파라미터 클래스 알아내기 =====
-  * 원칙적으로는 파라미터로 온 객체의 제너릭 타입을 알아낼 수 는 없다. 하지만 클래스가 제너릭일 경우 클래스 자신의 타입은 알아낼 수 있다.+  * Type erasure 때문에 원칙적으로는 파라미터로 온 객체의 제너릭 타입을 알아낼 수 는 없다. 하지만 클래스가 제너릭일 경우 클래스 자신의 타입은 알아낼 수 있다.
  
->> Nope, that is not possible. Due to downwards compatibility issues, Java's generics are based on type erasure, i.a. at runtime, all you have is a non-generic List object. There is some information about type parameters at runtime, but it resides in class definitions (i.e. you can ask "what generic type does this field's definition use?"), not in object instances.+>> Nope, that is not possible. Due to downwards compatibility issues, Java's generics are based on [[https://docs.oracle.com/javase/tutorial/java/generics/erasure.html|type erasure]], i.a. at runtime, all you have is a non-generic List object. There is some information about type parameters at runtime, but it resides in class definitions (i.e. you can ask [[https://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html#getGenericType()|"what generic type does this field's definition use?"]]), not in object instances.
  
   * 아래의 결과는 Class 들의 배열이므로 원하는 순서의 것을 빼서 사용하면 된다.   * 아래의 결과는 Class 들의 배열이므로 원하는 순서의 것을 빼서 사용하면 된다.
줄 24: 줄 24:
   * [[https://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection|Get type of a generic parameter in Java with reflection - Stack Overflow]]   * [[https://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection|Get type of a generic parameter in Java with reflection - Stack Overflow]]
   * [[https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html|Advanced Java generics: retreiving generic type arguments]]   * [[https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html|Advanced Java generics: retreiving generic type arguments]]
 +
 +===== 참조 =====
 +  * [[https://www.baeldung.com/java-generic-constructors|Generic Constructors in Java | Baeldung]]
  
java/generics.txt · 마지막으로 수정됨: 2020/09/18 21:41 저자 kwon37xi