===== L'introspection===== ===Définition=== TODO Création d'un objet, puis instantiation de celui-ci. Lancement d'une méthode avec ces paramètres =====Exemple de code java===== package fr.test.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.log4j.Logger; public class Introspection { /** * Logger pour les logs */ private static final Logger sLogger = Logger.getLogger(Introspection.class); public Introspection() {} public void testIntrospection(String pParam){ if (sLogger.isDebugEnabled()){ sLogger.debug("testIntrospection => " + pParam); } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String vNomClasse = "fr.test.utils.Introspection"; Object[] vListeParametres = {"Super!!!"}; String vMethodeClasse = "testIntrospection"; Introspection vIntrospection = new Introspection(); vIntrospection.lancerMethode( vIntrospection.creerObjet(vNomClasse), vListeParametres, vMethodeClasse); } /** * creer une instance d'objet en utilisant l'introspection * * @param pNomClasse * @return * @throws Exception */ private Object creerObjet(String pNomClasse) throws Exception { Object instanceObject = null; try { instanceObject = Class.forName(pNomClasse).newInstance(); } catch (ClassNotFoundException e) { sLogger.error("creerObjet => La classe n'existe pas : " + e); throw e; } catch (InstantiationException e) { sLogger.error("creerObjet => " + "La classe est abstract ou est une interface " + "ou n'a pas de constructeur " + "accessible sans paramètre : " + e); throw e; } catch (IllegalAccessException e) { sLogger.error("creerObjet => La classe n'est pas accessible : " + e); throw e; } return instanceObject; } /** * * * @param pObjet * @param pArguments * @param pNomMethode * @param pFormBeanFormulaire * @return * @throws Exception */ private Object lancerMethode(Object pObjet, Object[] pArguments, String pNomMethode) throws Exception { Class[] vParamTypes = null; Method vMethod = null; vParamTypes = new Class [pArguments.length+1]; //Mise en place des autres parametres for (int i = 0; i < pArguments.length; ++i) { vParamTypes[i+1] = pArguments[i].getClass(); } try { vMethod = pObjet.getClass().getMethod(pNomMethode, vParamTypes); } catch (SecurityException e) { sLogger.error("lancerMethode => SecurityException : " + e); throw e; } catch (NoSuchMethodException e) { sLogger.error("lancerMethode => NoSuchMethodException : " + e); throw e; } try { return vMethod.invoke(pObjet, pArguments); } catch (IllegalArgumentException e) { sLogger.error("lancerMethode => IllegalArgumentException : " + e); throw e; } catch (IllegalAccessException e) { sLogger.error("lancerMethode => IllegalAccessException : " + e); throw e; } catch (InvocationTargetException e) { sLogger.error("lancerMethode => InvocationTargetException : " + e); throw e; } } }