====== Java Generics ====== * [[https://docs.oracle.com/javase/tutorial/java/generics/index.html|Oracle Java Generics]] * [[https://www.baeldung.com/java-generics|The Basics of Java Generics | Baeldung]] * [[http://www.javacodegeeks.com/2013/07/java-generics-tutorial-example-class-interface-methods-wildcards-and-much-more.html|Java Generics Tutorial – Example Class, Interface, Methods, Wildcards and much more]] * [[http://www.javacodegeeks.com/2012/07/java-generics-interview-questions.html|Java Generics Interview Questions]] * [[http://sayemdb.wordpress.com/2013/04/14/an-introduction-to-generics-in-java-part-1/|An Introduction to Generics in Java – Part 1 | Random Thoughts]] * [[http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html|Java Generics FAQ]] ===== Type Erasure ===== Java Generic은 컴파일 타임에만 타입 체킹용으로 사용되고, 실행시간에는 모두 Genric이 삭제된 상태로 동작한다. 컴파일러는 필요할 경우 타입 캐스팅 코드를 넣어서 컴파일 한다. * [[https://cr.openjdk.java.net/~briangoetz/valhalla/erasure.html|erasure]] ===== 현재 객체의 Generic Type 파라미터 클래스 알아내기 ===== * Type erasure 때문에 원칙적으로는 파라미터로 온 객체의 제너릭 타입을 알아낼 수 는 없다. 하지만 클래스가 제너릭일 경우 클래스 자신의 타입은 알아낼 수 있다. >> 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 들의 배열이므로 원하는 순서의 것을 빼서 사용하면 된다. Type[] classes = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments(); * [[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.baeldung.com/java-generic-constructors|Generic Constructors in Java | Baeldung]]