Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

determing the generic type

Thread view: 
Aryeh M. Friedman - 14 Jul 2006 02:22 GMT
First of all let me say I am aware of type erasure.

Let's say I have something like:

ArrayList<Integer> foo=new ArrayList<Integer>();

is there any way at run time to determine the type parameter is
Integer... more specifically if
I have

public interface MyClass<T>
{
...
}

public class MyClassImpl<T> implements MyClass<T>
{
...
}

public class MockMyClass<T> implements MyClass<T>
{
...
}

public
Is there any way of knowing the value of T at run time.

If the above is not possible maybe there is still a way to accomplish
what I have in mind.... basically if some class does the following:

public class MyApp<T>
{
      private ArrayList<T> data;

      public MyApp()
       {
             data=new ArrayList<T>();
       }
....
}

I want to be able to replace T with TImpl (i.e. in the case of MyClass
replace it with an instance of MockMyClass [NOT MyClassImpl]) {this
needs to be done via reflection and not modifing the source code}....
Note I already have something that works for single instances and
arrays but not for collections (thus the need for determing the generic
type)
Chris Smith - 14 Jul 2006 03:10 GMT
> Let's say I have something like:
>
> ArrayList<Integer> foo=new ArrayList<Integer>();
>
> is there any way at run time to determine the type parameter is
> Integer...

How far are you willing to go?  What do you have access to?  If you're
trying to do this with only reflection on class files without modifying
any code, then you're out of luck.  The following may work, though:

1. If you have control of construction of the class, it's fairly common
to have its constructor take a parameter of type Class<T>, where T is
the type parameter.  This can then be used for this kind of thing, and
there's compile-time checking that the right Class object was passed in.

2. If you have access to the source code, you could parse it.

3. If you have access to the bytecode and it's a field or parameter, you
could look at the attributes of the class file, which will contain the
generic type information.  Note this doesn't work for local variables,
and may not work for private variables.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Hendrik Maryns - 14 Jul 2006 10:04 GMT
Aryeh M. Friedman schreef:
> First of all let me say I am aware of type erasure.
>
[quoted text clipped - 23 lines]
> public
> Is there any way of knowing the value of T at run time.

No.  Chris Smith old you some hoops you could jump through to get near.

> If the above is not possible maybe there is still a way to accomplish
> what I have in mind.... basically if some class does the following:
[quoted text clipped - 16 lines]
> arrays but not for collections (thus the need for determing the generic
> type)

So actually your problem is that the interface is not general enough.
Can’t you change the interface?

You can make the constructor generic:

public class MyApp<T>
{
      private ArrayList<? extends T> data;

      public <S extends T> MyApp()
       {
             data=new ArrayList<S>();
       }
....
}

But unfortunately data is declared array list of T.  So you’d have to
know the type even before compiling.  Even if you succeed in getting the
type, all objects you get out of data will only have the T interface.

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.