Code cleanup : fix C style array declaration.
[Project_POO.git] / exo3 / Main.java
index 4909568f8527f54341b7ea5963925373310971b3..33f6bbc702a87b5cbb6f84224555508368e722df 100644 (file)
@@ -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,16 +40,21 @@ class Main {
 
     }
 
-    /**
-     * The main() function
-     * @param String[] args main() function arguments array
-     */
-    public static void main(String[] args) {
-        if (args.length == 0) {
-            System.out.println("Please run with java Main <Class Name to inspect>");
-            System.exit(-1);
+    private static void main_A(String[] args) {
+        String className = null;
+        Scanner uInput = null;
+        try {
+            uInput = new Scanner(System.in);
+            System.out.println("Saisir le nom de la classe à inspecter:");
+            className = uInput.nextLine();
+        }
+        catch (Exception e) {
+            System.out.println("Erreur:");
+            e.printStackTrace();
+        }
+        finally {
+            uInput.close();
         }
-        String className = args[0];
 
         Class cl = null;
         try {
@@ -66,6 +75,59 @@ class Main {
         for (int i = 0; i < methods.length; i++) {
             System.out.println(methods[i]);
         }
+    }
+
+    private static void main_B(String[] args) {
+        String name = Entiers.class.getName();
+        Class cl = null;
+        Entiers o = null;
+        try {
+            cl = Class.forName(name);
+            o = new Entiers(100);
+        }
+        catch (ClassNotFoundException e) {
+            System.out.println("Class name do not exist.");
+            System.exit(-1);
+        }
+
+        Scanner uInput = null;
+        try {
+            uInput = new Scanner(System.in);
+            System.out.println("Saisir le nom de la methode à invoquer:");
+            String method = uInput.nextLine();
+            //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();
+        }
+    }
 
+    /**
+     * The main() function
+     * @param String[] args main() function arguments array
+     */
+    public static void main(String[] args) {
+        main_A(args);
     }
 }