Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Resolve generic type in subclass?
Resolve generic type in subclass? [message #1775272] Fri, 27 October 2017 06:30 Go to next message
The Hay is currently offline The HayFriend
Messages: 2
Registered: October 2017
Junior Member
Here is a scenario:

public class Super<T> {
protected T id;
}

public class Sub extends Super<Long> {}


Is there a way to evaluate the type of the field "id" within the scope of the class "Sub" using available API? I'm currently using JDT dom API, but other public API is acceptable.


dịch vụ tìm người giúp việc nhà tphcm, Với phương châm "Trao niềm tin - Nhận hạnh phúc", Dịch vụ Sao Mai
Re: Resolve generic type in subclass? [message #1775451 is a reply to message #1775272] Mon, 30 October 2017 10:15 Go to previous messageGo to next message
Eclipse UserFriend

At runtime, it returns Object because of type-erasure which replaces all generic types with Object. There is no way to determine at runtime what generic type was provided at compile-time.
Re: Resolve generic type in subclass? [message #1775453 is a reply to message #1775451] Mon, 30 October 2017 10:43 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

The resolved T is indeed just Object. However if you are prepared to use knowledge of your code you know that T is the first type argument so getClass().getTypeParameters()[0] should get you well on the way,

If you use ASM, there may be helpers that can resolve the original T.

Regards

Ed Willink
Previous Topic:console dimensions on laptop with high density display
Next Topic:Problem with folder/package
Goto Forum:
  


Current Time: Sat Apr 20 00:45:48 GMT 2024

Powered by FUDForum. Page generated in 0.02923 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top