Hello,
J'ai un petit souci... J'ai un programme qui fonctionne bien! Tant mieux... Mais il est vraiment pas joli!
En gros, je fais un programme de configuration, qui prend des valeurs dans un XML (qui retourne des String) et qui doit les stocker dans une classe (grace au paquetage reflect)
Mon souci est le suivant: Quand je stock mes valeurs, je dois faire un traitement différent pour chaque type possible, et il y en a une floppée:
Cela car je n'ai pas réussi (et pourtant j'ai essayé un bon moment) à envoyer un objet dans la méthode set de reflect... Attention à ne pas oublier les tableaux
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 Class[] classes = { boolean.class, Boolean.class, byte.class, Byte.class, short.class, Short.class, int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class, String.class, boolean[].class, Boolean[].class, byte[].class, Byte[].class, short[].class, Short[].class, int[].class, Integer[].class, long[].class, Long[].class, float[].class, Float[].class, double[].class, Double[].class, String[].class};
Sinon il me met une exception du genre (pour u boolean):
java.lang.IllegalArgumentException: Can not set boolean field Configuration.Config01.booleanP to java.lang.String
Voici donc mon programme (une partie), si quelqun a une idée, je suis vraiment preneur!!
Ah, et BOOLEAN_PRIMITIVE, .... sont des constantes entières qui référencent le tableau dans le code en dessus... (des indices de tableaux)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 c=this.o.getClass(); f=c.getFields(); ... public void endElement... // Type of current field int intType = searchArray(classes, type); switch(intType){ case BOOLEAN_PRIMITIVE: case BOOLEAN: val = Boolean.parseBoolean(this.value.get(0)); break; case BYTE_PRIMITIVE: case BYTE: val = Byte.parseByte(this.value.get(0)); break; case SHORT_PRIMITIVE: case SHORT: val = Short.parseShort(this.value.get(0)); break; case INTEGER_PRIMITIVE: case INTEGER: val = Integer.parseInt(this.value.get(0)); break; case LONG_PRIMITIVE: case LONG: val = Long.parseLong(this.value.get(0)); break; case FLOAT_PRIMITIVE: case FLOAT: val = Float.parseFloat(this.value.get(0)); break; case DOUBLE_PRIMITIVE: case DOUBLE: val = Double.parseDouble(this.value.get(0)); break; case STRING: val = this.value.get(0); break; case BOOLEAN_P_ARRAY: boolean[] arrayBooleanP = new boolean[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayBooleanP[i]=Boolean.parseBoolean(this.value.get(i)); val=arrayBooleanP; break; case BOOLEAN_ARRAY: Boolean[] arrayBoolean = new Boolean[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayBoolean[i]=Boolean.parseBoolean(this.value.get(i)); val=arrayBoolean; break; case BYTE_P_ARRAY: byte[] arrayByteP = new byte[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayByteP[i]=Byte.parseByte(this.value.get(i)); val=arrayByteP; break; case BYTE_ARRAY: Byte[] arrayByte = new Byte[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayByte[i]=Byte.parseByte(this.value.get(i)); val=arrayByte; break; case SHORT_P_ARRAY: short[] arrayShortP = new short[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayShortP[i]=Short.parseShort(this.value.get(i)); val=arrayShortP; break; case SHORT_ARRAY: Short[] arrayShort = new Short[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayShort[i]=Short.parseShort(this.value.get(i)); val=arrayShort; break; case INTEGER_P_ARRAY: int[] arrayIntP = new int[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayIntP[i]=Integer.parseInt(this.value.get(i)); val=arrayIntP; break; case INTEGER_ARRAY: Integer[] arrayInt = new Integer[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayInt[i]=Integer.parseInt(this.value.get(i)); val=arrayInt; break; case LONG_P_ARRAY: long[] arrayLongP = new long[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayLongP[i]=Long.parseLong(this.value.get(i)); val=arrayLongP; break; case LONG_ARRAY: Long[] arrayLong = new Long[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayLong[i]=Long.parseLong(this.value.get(i)); val=arrayLong; break; case FLOAT_P_ARRAY: float[] arrayFloatP = new float[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayFloatP[i]=Float.parseFloat(this.value.get(i)); val=arrayFloatP; break; case FLOAT_ARRAY: Float[] arrayFloat = new Float[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayFloat[i]=Float.parseFloat(this.value.get(i)); val=arrayFloat; break; case DOUBLE_P_ARRAY: double[] arrayDoubleP = new double[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayDoubleP[i]=Double.parseDouble(this.value.get(i)); val=arrayDoubleP; break; case DOUBLE_ARRAY: Double[] arrayDouble = new Double[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayDouble[i]=Double.parseDouble(this.value.get(i)); val=arrayDouble; break; case STRING_ARRAY: String[] arrayString = new String[this.value.size()]; for(int i=0;i<this.value.size();i++) arrayString[i]=this.value.get(i); val=arrayString; break; } f[index].set(this.o, val); ...
Et la, je voudrais ajouter une vérification sur le range de val... Donc je dois l'ajouter 50 millions de fois... Vraiment pénible...
Help please![]()
Partager