Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: Java  |  size: 1.61 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Reflecter {
  8.         private Class<?> classe;
  9.         private ArrayList<String> listaClassi=new ArrayList<String>();
  10.        
  11.         public void getListaClassi(String lista){
  12.                 Scanner s=new Scanner(lista);
  13.                
  14.                 s.useDelimiter(" ");
  15.                
  16.                 while(s.hasNext()){
  17.                         listaClassi.add(s.next());
  18.                 }
  19.         }
  20.        
  21.         public void creaClasse(){
  22.                 assert (listaClassi.size()>=0): "Errore nella creazione della lista delle classi";
  23.                 int i=1;
  24.                 for(String temp: listaClassi){
  25.                         System.out.println(i + ")" + temp);
  26.                         i++;
  27.                 }
  28.                 Scanner s=new Scanner(System.in);
  29.                
  30.                 int scelta=s.nextInt();
  31.                
  32.                 try {
  33.                         classe=Class.forName(listaClassi.get(scelta-1));
  34.                 } catch (ClassNotFoundException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 }
  38.                
  39.         }
  40.        
  41.         public void invocaMetodo(){
  42.                 assert(classe!=null);
  43.                
  44.                 Method[] metodi=classe.getDeclaredMethods();
  45.                 int i=1;
  46.                 for(Method temp: metodi){
  47.                         System.out.println(i + ")" + temp);
  48.                         i++;
  49.                 }
  50.                
  51.                 Scanner s=new Scanner(System.in);
  52.                
  53.                 int scelta=s.nextInt();
  54.                
  55.                 Method metodoScelto=metodi[scelta-1];
  56.                
  57.                 Class<?>[] parametri=metodoScelto.getParameterTypes();
  58.                
  59.                         try {
  60.                                 if(parametri.length==0)
  61.                                         metodoScelto.invoke(classe);
  62.                                 else
  63.                                         metodoScelto.invoke(classe,parametri);
  64.                         } catch (IllegalArgumentException e) {
  65.                                 e.printStackTrace();
  66.                         } catch (IllegalAccessException e) {
  67.                                 e.printStackTrace();
  68.                         } catch (InvocationTargetException e) {
  69.                                 e.printStackTrace();
  70.                         }
  71.                
  72.         }
  73.        
  74. }