participate


Generics - Problem with reflection in Generics
<<   Back to Forum  |   Give us Feedback
This topic has 23 replies on 2 pages.    « Previous | 1 | 2 |
gafter
Posts:669
Registered: 6/25/98
Re: Problem with reflection in Generics   
Jun 25, 2003 10:48 AM (reply 21 of 23)  (In reply to original post )

 
We don't have the reflection APIs implemented in the prototpe because that requires VM support, which is a much larger thing that what's included in the 2.0 prototype We will ship a prototype that includes the new VM (and javadoc, and everything else), but unfortunately it won't be for a while. I'm guessing November. We'll be calling it "J2SE 1.5 Beta 1".
 
eadward
Posts:10
Registered: 10/8/04
Re: Problem with reflection in Generics   
Nov 24, 2005 3:15 AM (reply 22 of 23)  (In reply to original post )

 
Hey, mlorton! I guess you want to do something similar to what I want. Look at this code:

class Foo {
    static Map <String, Integer> bar() {
        return null;
    }
 
    public static void main(String[] args) throws Exception {
        Method m = Foo.class.getDeclaredMethod("bar");
        
        ParameterizedType ptype = (ParameterizedType) m.getGenericReturnType();
        for (Type targ: ptype.getActualTypeArguments()) {
            Class tcls = (Class) targ;
            System.out.println(tcls.getName());
        }
    }
}


This snippet will work on any 1.5 JVM and will print: String Integer. I got this information from an IBM site: http://www-128.ibm.com/developerworks/java/library/j-cwt11085.html

It works also if you have an "bar" field instead of a method, but, I couldn't get information about the content of the field (i.e. the instance stored in the field).

Best regards,
Eduardo
 
www.aiteq.com
Posts:1
Registered: 11/25/06
Re: Problem with reflection in Generics   
Nov 25, 2006 4:48 AM (reply 23 of 23)  (In reply to #22 )

 
Hello,

there is yet another workarround:

public class MyGenericClass<T> {
 
  public void getTypeClassName() {
    String genericSuperclass = findGenericSuperclass(getClass()).getGenericSuperclass().toString();
    return genericSuperclass.substring(MyGenericClass.class.getName().length() + 1, genericSuperclass.length() - 1));
  }
  
  private Class findGenericSuperclass(Class clazz) {
    if (clazz.getGenericSuperclass().toString().startsWith(MyGenericClass.class.getName())) {
      return clazz;
    } else {
      return findGenericSuperclass(clazz.getSuperclass());
    }
  }
  
  public static void main(String[] args) throws Exception {
   MyGenericClass test = new Test();
    System.out.println(test.getTypeClassName());
  }
}
 
class Test extends MyGenericClass<Integer> {
}


Regards,

Tomas Klima
www.aiteq.cz
 
This topic has 23 replies on 2 pages.    « Previous | 1 | 2 |
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 129

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums