X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exo3%2FMain.java;h=e92a5e2ff788d0a7acfc29bb9f320b831461441a;hb=cc162028bdb820adfbb30037091557f057980abd;hp=41749bfccc3a5fb4533c3c98cfe4a3b225c58096;hpb=e8b96f4474afea669923602daf94d34be170634f;p=Project_POO.git diff --git a/exo3/Main.java b/exo3/Main.java index 41749bf..e92a5e2 100644 --- a/exo3/Main.java +++ b/exo3/Main.java @@ -1,3 +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 { @@ -34,11 +40,81 @@ class Main { } + private static void main_A(String[] args) { + if (args.length == 0) { + System.out.println("Please run with java Main "); + System.exit(-1); + } + String className = args[0]; + + Class cl = null; + try { + cl = Class.forName(className); + } + catch (ClassNotFoundException e) { + System.out.println("Entered class name do not exist."); + System.exit(-1); + } + + Field[] fields = cl.getFields(); + Method[] methods = cl.getMethods(); + + for (int i = 0; i < fields.length; i++) { + System.out.println(fields[i]); + } + + 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) { - main_orig(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.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(); + } } }