Recuperer un type generique par intraspection.

Recuperer un type generique par intraspection. - Java - Programmation

Marsh Posté le 23-11-2005 à 17:16:19    

Salut,
 
   En supposant que j'ai dans ma classe un attribut de type ArrayList<QuelqueChose>, est-il possible de récuperer le .class  de ce QuelqueChose par intraspection ou un autre moyen ?


---------------
Scheme is a programmable programming language ! I heard it through the grapevine !
Reply

Marsh Posté le 23-11-2005 à 17:16:19   

Reply

Marsh Posté le 23-11-2005 à 17:32:04    

non [:pingouino]

Reply

Marsh Posté le 23-11-2005 à 17:57:51    

Si si c'est surement possible :
 

Code :
  1. public class Test {
  2.     java.util.ArrayList<String> a;
  3.     java.util.ArrayList<Double> b = new java.util.ArrayList<Double>();   
  4.    
  5.     /** Creates a new instance of Test */
  6.     public Test() {
  7.     }
  8.    
  9.     public static void main(String [] args){
  10.        
  11.         try{                                   
  12.             Class c = Class.forName("projet_bda.Test" );
  13.             java.lang.reflect.Field f[] = c.getDeclaredFields();
  14.             for(int i = 0; i < f.length;++i){
  15.                 System.out.println("====>" + f[i].getGenericType().getClass());
  16.             }                       
  17.         }catch(Exception ex){
  18.             ex.printStackTrace();
  19.         }
  20.     } 
  21. }


 
Je suis pas loin la.


---------------
Scheme is a programmable programming language ! I heard it through the grapevine !
Reply

Marsh Posté le 23-11-2005 à 17:59:45    

Si tu utilise javap, tu as quoi ?


---------------
JE JE SUIS LIBERTINEEEEEEEEEEE JE SUIS UNE CATINNNNNNNNN §§§§§§§§
Reply

Marsh Posté le 23-11-2005 à 18:01:41    

Rien, je pensais que c'était possible sans desassemblage.


---------------
Scheme is a programmable programming language ! I heard it through the grapevine !
Reply

Marsh Posté le 23-11-2005 à 18:03:51    

non spa possible, les generics ne sont que dans les sources, et utilisés pour faire du type checking à la compilation. une fois compilé, y'a que des Object, rien d'autre [:pingouino]

Reply

Marsh Posté le 23-11-2005 à 18:08:24    

Pas moyen de bidouiller ?  
 

Code :
  1. public class Test {
  2.     java.util.ArrayList<String> a;
  3.     java.util.ArrayList<Double> b = new java.util.ArrayList<Double>();
  4.    
  5.    
  6.     /** Creates a new instance of Test */
  7.     public Test() {
  8.     }
  9.    
  10.     public static void main(String [] args){
  11.        
  12.         try{                                   
  13.             Class c = Class.forName("projet_bda.Test" );
  14.             java.lang.reflect.Field f[] = c.getDeclaredFields();
  15.             for(int i = 0; i < f.length;++i){
  16.                 System.out.println("++>" + f[i].getGenericType());
  17.             }
  18.             String nom = f[1].getGenericType().toString();
  19.            
  20.             System.out.println("------->" + nom);
  21.             Class temp_nom = Class.forName(nom);
  22.             Object o = temp_nom.newInstance();
  23.             System.out.println("====>" + o.getClass().getName());
  24.            
  25.         }catch(Exception ex){
  26.             ex.printStackTrace();
  27.         }
  28.     }   
  29. }


 
run-single:
++>java.util.ArrayList<java.lang.String>
++>java.util.ArrayList<java.lang.Double>
------->java.util.ArrayList<java.lang.Double>
java.lang.ClassNotFoundException: java.util.ArrayList<java.lang.Double>
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:164)
        at projet_bda.Test.main(Test.java:36)
 
Edit : LoL oué c'est mort en fait.  :lol:


Message édité par Chronoklazm le 23-11-2005 à 18:09:50

---------------
Scheme is a programmable programming language ! I heard it through the grapevine !
Reply

Marsh Posté le 23-11-2005 à 20:07:14    

bah oui si on te le dis .... [:kiki]
 
(enfin je dis ca, j'en ai pris conscience avant hier en faisant joujou avec quoi :whistle: )

Reply

Marsh Posté le 24-11-2005 à 15:08:20    

Code :
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.ParameterizedType;
  3. import java.lang.reflect.Type;
  4. import java.util.ArrayList;
  5. import java.util.Map;
  6. public class TestGeneic {
  7. ArrayList<String> a;
  8. ArrayList<Double> b = new java.util.ArrayList<Double>();
  9. Map<String, Integer> m;
  10. public static void main(String[] args) throws Exception {
  11.  Class c = Class.forName("TestGeneic" );
  12.  Field f[] = c.getDeclaredFields();
  13.  for (int i = 0; i < f.length; ++i) {
  14.   System.out.println("++>" + f[i].getGenericType());
  15.   Type type = f[i].getGenericType();
  16.   ParameterizedType pType = (ParameterizedType) type;
  17.   Type[] types = pType.getActualTypeArguments();
  18.   for (int j = 0; j < types.length; j++) {
  19.    Class genC = (Class) types[j];
  20.    System.out.println("++++>parametrized type " + (j+1) + " : " + genC.getName());
  21.   }
  22.  }
  23. }
  24. }


 

Citation :

++>java.util.ArrayList<java.lang.String>
++++>parametrized type 1 : java.lang.String
++>java.util.ArrayList<java.lang.Double>
++++>parametrized type 1 : java.lang.Double
++>java.util.Map<java.lang.String, java.lang.Integer>
++++>parametrized type 1 : java.lang.String
++++>parametrized type 2 : java.lang.Integer


---------------
ma vie, mon oeuvre - HomePlayer
Reply

Marsh Posté le 24-11-2005 à 18:52:28    

Bein oui ... merci.
 
J'étais pas loin ... effectivement.


---------------
Scheme is a programmable programming language ! I heard it through the grapevine !
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed