를 구현에서 형식 매개 변수의 클래스를 얻기? [나가서 직접 구현하기 전에 알아 두어야 할 사항입니다. Foo
클래스의 메타 데이터는 일반 슈퍼 클래스와 인터페이스와 ((ParameterizedType)Foo.class.getGenericSuperClass()).getActualTypeArguments()[0]
를 사용하는 것을 new LinkedList<String>()
는 어떻게 (이 예입니다 <code>String</code>에 임) <code>List</code>의 유형 변수를 얻을 수있다, 나는 <code>List<T></code>이 <code>String</code>로를 확장하는 <code>Foo</code>라는 구현 자바에 몇 가지 클래스가 있다고 가정하자 클래스
주의 내부의 유형을 알고 싶은와 동일하지가 포함되어 있기 때문에 나는 스프링 프레임 워크가이 수행 될 수 있다는
주의 사항이 작업을 수행 할 수 있다는 것을 확신 내가 원하는 것을 보여줍니다
코드 : (Foo
및 Bar
아래의 코드를 참조) 내 요구에 충분하지 않습니다
// java.util: interface List<T> { }
interface TypeList<A, T> extends List<T> { }
interface Foo extends List<String> { }
interface Bar extends TypeList<Integer, Long> { }
Class<?> getTypeParameter(Class<?> fromClass, Class<?> superTypeWithTypeVariables, int indexOfTypeVariable) {
// ...
}
// Foo -> List<String> -> List<E> -> E is #0
assertEquals(String.class, getTypeParameter(Foo.class, List.class, 0))
// Bar -> TypeList<..., Long> -> TypeList<..., T> -> List<T> -> List<E> -> E is #0
assertEquals(Long.class, getTypeParameter(Bar.class, List.class, 0))
,
구현시 모든 클래스 계층 구조를 검토하고 ParameterizedType 및 TypeVariable 인터페이스 및 일부 다른 스태프를 사용해야합니다. 내가 이것을 구현 어떤 라이브러리를 요구하기 때문에
는하지만이 문제는 (https://stackoverflow.com/help/on-topic 참조) 당신은 그것을 here를 참조 내 유틸리티 GenericUtil
을 사용할 수 있습니다
도서관 요청 오프 주제입니다. –
@JornVernee : 이것은 라이브러리 요청이 아닙니다. – SLaks
@SLaks This is a –