Results 1 to 10 of 10

Thread: Generics vs. Reflection in Java

  1. #1
    Join Date
    Jan 2010
    Posts
    70

    Generics vs. Reflection in Java

    I have to crack a tough nut. Given the following interfaces:
    interface Entity (... )
    BaseInterface <e MyEntity> interface extends ... ( )
    interface interface Special extends <e MyEntity> extends BaseInterface <E> {...}
    Special interface and a suitable implementation this:
    MyClazz <e MyEntity> class extends implements interface <E> (Special ... )
    Now let I want to create the instances of MyClazz not yourself, but do these over a factory method:
    public static <B extends BaseInterface <E>, E extends Entity> B getInstance (Class clazz <B>) (... )
    The call currently looks like this:
    Code:
    Class cls = <SpecialInterface> SpecialInterface.class; 
    Special Interface <E> si = getInstance (cls); 
    si.doSth ();

  2. #2
    Join Date
    Mar 2008
    Posts
    227

    Re: Generics vs. Reflection in Java

    I think you should look initially make clear how the Java Generics work, the types of information is only available at compile time and not to maturity. Each generic type for example.
    Code:
    class Foo extends <A Bar> 
    ( 
    A GetIt (){...} 
    )
    has indeed at compile-time generic capabilities explained. However, there is no corresponding code in the bytecode depict these properties could.

  3. #3
    Join Date
    Jan 2010
    Posts
    70

    Re: Generics vs. Reflection in Java

    I would like to thank you for trying to help me sooner The code that I mentioned works only once, but leads firstly to a compiler warning (due to 'unchecked conversation'). And secondly, I can in MyClazz (via reflection) is no longer figure out what specific type of (<E>) my body is now. So actually I need the following:
    Class <Special Interface <E>> cls = ?????
    So here:
    Class <Special Interface <E>> cls = (Class <Special Interface <E>>) SpecialInterface.class;
    bleats the compiler ('Can not cast from class to class <SpecialInterface> <Special Interface <E>>'). Any ideas would be appreciable.

  4. #4
    Join Date
    Mar 2008
    Posts
    232

    Re: Generics vs. Reflection in Java

    The compiler is now trying a workaround (Type Erasure) to come around this restriction. It replaces each generic type against the type of object and adds the appropriate casts. The following is an example that explains the same :
    To compile:
    Foo = new Foo <BarImpl> aFoo <BarImpl> ();
    Bar = abar aFoo.getit ()

    At runtime:
    AFoo foo = new Foo ();
    Abar bar = (Bar) aFoo.getit ()
    That is what is in the figurative sense. Via reflection so you can not get the information. The compiler warning that you get with your type conversion:
    Class cls = <SpecialInterface> SpecialInterface.class;
    Special Interface <E> si = getInstance (cls);
    has a similar basis. The Special Interface Type information is not included in <E> SpecialInterface.class. (Has something to do with the implementation of Generics) means you walk without a special interface type information into an instance of type information. This generates the compiler warning.

  5. #5
    Join Date
    Jan 2010
    Posts
    70

    Re: Generics vs. Reflection in Java

    Thank you for your answer!!! However, I believe I have not formulated my problem clearly enough My problem is that I need a Class-typed object of the special interface ... Suppose I have a XXX-Entity:
    XXX class implements Entity (... )
    Then I could build as just another interface:
    Special XXXSpecialInterface interface extends interface XXX (... )
    and then later in your code
    Code:
    Class cls = <XXXSpecialInterface> XXXSpecialInterface.class; 
    XXXSpecialInterface si = getInstance (cls); 
    si.doSth ();
    The nice thing is that I might as desired via reflection to find out 'si', that is typed with XXX (note link at the bottom). But I will find no extra interface for each and group of possible types, I want 'simple' only:
    Code:
    Class <Special interface XXX> cls = XXX Special interface. Class; 
    Special Interface <XXX> si = getInstance (cls); 
    si.doSth ();

  6. #6
    Join Date
    Mar 2008
    Posts
    227

    Re: Generics vs. Reflection in Java

    It is true that information on Generics can be found in the bytecode, but if I have not left my memory, only for the generic definitions. But not the specific types of instances of generic classes. Moreover, it is already doubtful that the generic type information is effectively accessible, in the above scenario by you. Either you know the production already, come what specific classes are used (then you have the info already on the programming time and create these can without generic Factory), or you know it there yet, then you have in and by the Generics another program run anything.

  7. #7
    Join Date
    Aug 2006
    Posts
    227

    Re: Generics vs. Reflection in Java

    Maybe it helps yes, if you describe your solution instead of the idea of the problem, so that what you want to accomplish. use among other things, how do you use the generated instances / what information you want and to programming level need. For example, you write about an "E" as a parameter, but also so must be somewhere concretely proven or provable. May help a factory registry approach depend on the problem?

  8. #8
    Join Date
    Jan 2010
    Posts
    70

    Re: Generics vs. Reflection in Java

    I mean that you want to help me with my architecture, but I would really reluctant to discuss their meaning and nonsense. It stands for me that am not in doubt. It should be clear that I have here a minimal sample which actually has little relation to reality. Perhaps only this: I have built an OR-Mapper is configured using annotations and generics. The OR mapper must at some point know for which specific entity types because he works at all (and it works reliably well) ... You need not to be the question and yes, I know, and Hibernate JPA and Toplink and JDO and whatever you want the proposal from me now. That is not all my problem - everything works perfectly some time ago, I would like to make only a tiny optimization, and for that I need 'simple' only:
    Class <Special interface XXX> cls =???

  9. #9
    Join Date
    Jul 2006
    Posts
    286

    Re: Generics vs. Reflection in Java

    There are no parameterized class objects. Told you that the compiler already. So I said yes and that possibly is a Factory-registry approach in question, where you fetch your first parameter a factory for your class from a registry through which you will create with the specific entity type your instance. For example, like (via inference):
    Code:
     Special Interface Factory sf = getFactory (SpecialInterface.class); 
    Special Interface <YourEntity> sf.create si = (); 
    <e Extends Entity> Special <E> interface create ();

  10. #10
    Join Date
    Mar 2008
    Posts
    349

    Re: Generics vs. Reflection in Java

    It is true that information on Generics can be found in the bytecode, but if I have not left my memory, only for the generic definitions. But not the specific types of instances of generic classes.
    That is not correct, the generic definition is deleted by the compiler and also to suppress any information about specific types. Java uses a code-sharing for generics, not like C + + code-Specialization. There is only one class file, it is used for all instantiations of the class whether or Foo <String> <bar>.
    Code:
     template <class T> 
    class Foo 
    ( 
    )
    The compiler generates for every generic instance. In the fall of Foo Foo <ns:Bar> <std::string> or a class for string and bar is created. In Java, it looks different, there is only one -> Foo <Object>. In C # this has been implemented differently, C # uses a per-instance type VTable can provide the Information and runtime. But that is regulated within the VM.

Similar Threads

  1. How to use Reflection in Java
    By The$Hulk in forum Guides & Tutorials
    Replies: 4
    Last Post: 12-01-2011, 01:18 PM
  2. Generics problem in java
    By Gillian Anderson in forum Software Development
    Replies: 4
    Last Post: 31-03-2010, 01:08 PM
  3. Getting warning in java generics
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 22-03-2010, 01:45 PM
  4. Remoting and Reflection in C#
    By BansiJ in forum Software Development
    Replies: 5
    Last Post: 04-12-2009, 07:13 PM
  5. Java Programming Language Basics: Reflection Basics and Class Class
    By mayuri_gunjan in forum Guides & Tutorials
    Replies: 6
    Last Post: 29-08-2005, 12:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,501,497.21368 seconds with 17 queries