|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Generics vs. Reflection in Java I have to crack a tough nut. Given the following interfaces: interface Entity (... )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
| |||
| |||
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 (){...} ) |
#3
| |||
| |||
Re: Generics vs. Reflection in Java I would like to thank you for trying to help me sooner ![]() 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
| |||
| |||
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: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;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
| |||
| |||
Re: Generics vs. Reflection in Java Thank you for your answer!!! However, I believe I have not formulated my problem clearly enough ![]() 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 (); Code: Class <Special interface XXX> cls = XXX Special interface. Class; Special Interface <XXX> si = getInstance (cls); si.doSth (); |
#6
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
Re: Generics vs. Reflection in Java Quote:
Code: template <class T> class Foo ( ) |
![]() |
|
Tags: entity, generic class, generics, interface, java, reflection |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to use Reflection in Java | The$Hulk | Guides & Tutorials | 4 | 12-01-2011 01:18 PM |
Generics problem in java | Gillian Anderson | Software Development | 4 | 31-03-2010 01:08 PM |
Getting warning in java generics | Angelica Maria | Software Development | 5 | 22-03-2010 01:45 PM |
Remoting and Reflection in C# | BansiJ | Software Development | 5 | 04-12-2009 07:13 PM |
Java Programming Language Basics: Reflection Basics and Class Class | mayuri_gunjan | Guides & Tutorials | 6 | 29-08-2005 12:04 AM |