Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-07-2010
Member
 
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 ();
Reply With Quote
  #2  
Old 29-07-2010
Member
 
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.
Reply With Quote
  #3  
Old 29-07-2010
Member
 
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.
Reply With Quote
  #4  
Old 29-07-2010
Member
 
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.
Reply With Quote
  #5  
Old 29-07-2010
Member
 
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 ();
Reply With Quote
  #6  
Old 29-07-2010
Member
 
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.
Reply With Quote
  #7  
Old 14-09-2010
Member
 
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?
Reply With Quote
  #8  
Old 14-09-2010
Member
 
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 =???
Reply With Quote
  #9  
Old 14-09-2010
Member
 
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 ();
Reply With Quote
  #10  
Old 15-09-2010
Member
 
Join Date: Mar 2008
Posts: 349
Re: Generics vs. Reflection in Java

Quote:
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Generics vs. Reflection in Java"
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


All times are GMT +5.5. The time now is 06:51 AM.