Bonsoir,
je suis entrain de coder une fonction qui permet d'utiliser les setters d'une classe dynamiquement !
Ainsi je suis obligé de passé par la fonction invoke de la classe Method, sauf quand je la lance avec des paramètre null, j'ai une exception .
STOUT !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 static void modifyAttribute(Object form,String param, Object newValue ) { Method[] m = form.getClass().getDeclaredMethods(); String methodName = null; if(param.length()>=1) methodName = "set"+param.toUpperCase().charAt(0)+param.substring(1, param.length()); for (int i = 0; i < m.length; ++i) { if(m[i].getName().equals(methodName)) { Object[] arguments = new Object[] {newValue}; try { m[i].invoke(form,arguments); } catch (IllegalAccessException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at DynamicForm.modifyAttribute(DynamicForm.java:43) at DynamicForm.main(DynamicForm.java:93)
Partager