X-Git-Url: https://git.piment-noir.org/?p=Project_POO.git;a=blobdiff_plain;f=exo3%2FMain.java;h=2b5ac8c9c8e5007ad0a4bcb8b1258c47b730d063;hp=a0207389935e695e1b825e0d07be76e2636ee5c0;hb=01bad5b37bf95f027f583e1b1da607e074050651;hpb=4845756e424e8a5e3ebc17d089d04dc42a936899 diff --git a/exo3/Main.java b/exo3/Main.java index a020738..2b5ac8c 100644 --- a/exo3/Main.java +++ b/exo3/Main.java @@ -1,5 +1,9 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Scanner; +import java.io.IOException; class Main { @@ -36,11 +40,7 @@ class Main { } - /** - * The main() function - * @param String[] args main() function arguments array - */ - public static void main(String[] args) { + private static void main_A(String[] args) { if (args.length == 0) { System.out.println("Please run with java Main "); System.exit(-1); @@ -66,6 +66,69 @@ class Main { for (int i = 0; i < methods.length; i++) { System.out.println(methods[i]); } + } + + /** + * The main() function + * @param String[] args main() function arguments array + */ + public static void main(String[] args) { + String name = Entiers.class.getName(); + Class cl = null; + Object o = null; + try { + cl = Class.forName(name); + Class[] types = new Class[]{Integer.class}; + Constructor ct = cl.getConstructor(types); + o = ct.newInstance(new Integer(100)); + } + catch (ClassNotFoundException e) { + System.out.println("Class name do not exist."); + System.exit(-1); + } + catch (NoSuchMethodException e) { + e.printStackTrace(); + } + catch (InstantiationException e) { + e.printStackTrace(); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + Scanner uInput = null; + try { + uInput = new Scanner(System.in); + System.out.println("Saisir le nom de la methode à invoquer:"); + String method = uInput.next(); + //TODO: one can build the input list from the method arguments list and types + System.out.println("Saisir l'argument entier paramètre de la méthode:"); + int integer = uInput.nextInt(); + Method m = cl.getMethod(method, new Class[]{Integer.class}); + m.invoke(o, integer); + } + catch (Exception e) { + System.out.println("Erreur:"); + e.printStackTrace(); + } + finally { + try { + Method mDisplay = cl.getMethod("afficher"); + mDisplay.invoke(o); + } + catch (NoSuchMethodException e) { + e.printStackTrace(); + } + catch (IllegalAccessException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + uInput.close(); + } } }