回答編集履歴
4
Point5を追加
test
CHANGED
@@ -316,4 +316,22 @@
|
|
316
316
|
|
317
317
|
}
|
318
318
|
|
319
|
+
class Point5 {
|
320
|
+
|
321
|
+
String x="aaa";
|
322
|
+
|
323
|
+
double y = 2;
|
324
|
+
|
325
|
+
public Point5(String x,double y)
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
this.x = x;
|
330
|
+
|
331
|
+
this.y = y;
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
319
337
|
```
|
3
回答を追加
test
CHANGED
File without changes
|
2
回答を追加
test
CHANGED
@@ -93,3 +93,227 @@
|
|
93
93
|
}
|
94
94
|
|
95
95
|
```
|
96
|
+
|
97
|
+
---
|
98
|
+
|
99
|
+
2017/12/31追記
|
100
|
+
|
101
|
+
質問文のコメント欄の内容を受けて回答を追加。
|
102
|
+
|
103
|
+
引数の判定部分は分かりやすいようにif文にしてますが、Mapで判定するのがベターです。
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```Java
|
108
|
+
|
109
|
+
import java.lang.reflect.Constructor;
|
110
|
+
|
111
|
+
import java.lang.reflect.Field;
|
112
|
+
|
113
|
+
import java.lang.reflect.InvocationTargetException;
|
114
|
+
|
115
|
+
import java.util.ArrayList;
|
116
|
+
|
117
|
+
import java.util.List;
|
118
|
+
|
119
|
+
import java.util.Scanner;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
public class Q107070 {
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
|
128
|
+
|
129
|
+
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
130
|
+
|
131
|
+
try {
|
132
|
+
|
133
|
+
Class<?> c = inputFindClass();
|
134
|
+
|
135
|
+
Constructor<?>[] ctors = c.getDeclaredConstructors();
|
136
|
+
|
137
|
+
Constructor<?> ctor = ctors[0];
|
138
|
+
|
139
|
+
List<Object> params= getParams(ctor.getParameterTypes());
|
140
|
+
|
141
|
+
// コンストラクタを呼び出してインスタンスを生成
|
142
|
+
|
143
|
+
Object obj = ctor.newInstance(params.toArray());
|
144
|
+
|
145
|
+
for (Field fieled : c.getDeclaredFields()) {
|
146
|
+
|
147
|
+
fieled.setAccessible(true);
|
148
|
+
|
149
|
+
String fieldName = fieled.getName();
|
150
|
+
|
151
|
+
Class<?> fieldType = fieled.getType();
|
152
|
+
|
153
|
+
String fieldTypeStr = fieldType.getName();
|
154
|
+
|
155
|
+
System.out.print("Type : " + fieldTypeStr + ", Name : " + fieldName + ", Value : ");
|
156
|
+
|
157
|
+
try {
|
158
|
+
|
159
|
+
Object value = fieled.get(obj);
|
160
|
+
|
161
|
+
System.out.println(value);
|
162
|
+
|
163
|
+
} catch (IllegalArgumentException | IllegalAccessException e) {
|
164
|
+
|
165
|
+
System.err.println(e);
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
} catch (ClassNotFoundException e) {
|
172
|
+
|
173
|
+
System.err.println("クラスが見つかりません.");
|
174
|
+
|
175
|
+
System.err.println(e);
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
public static Class<?> inputFindClass() throws ClassNotFoundException {
|
184
|
+
|
185
|
+
System.out.println("フィールドとフィールドの値を表示します.");
|
186
|
+
|
187
|
+
System.out.println("検査するクラスを入力してください");
|
188
|
+
|
189
|
+
System.out.print(">");
|
190
|
+
|
191
|
+
try (Scanner sc = new Scanner(System.in)) {
|
192
|
+
|
193
|
+
String className = sc.nextLine();
|
194
|
+
|
195
|
+
return Class.forName(className);
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
public static List<Object> getParams(Class<?> parameterTypes[]) throws InstantiationException,IllegalAccessException {
|
204
|
+
|
205
|
+
List<Object>params = new ArrayList<>();
|
206
|
+
|
207
|
+
for(Class<?> p:parameterTypes){
|
208
|
+
|
209
|
+
if (p.isPrimitive()){
|
210
|
+
|
211
|
+
if (p.equals(boolean.class)) {
|
212
|
+
|
213
|
+
params.add(false);
|
214
|
+
|
215
|
+
continue;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
if (p.equals(byte.class)) {
|
220
|
+
|
221
|
+
params.add(0);
|
222
|
+
|
223
|
+
continue;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
if (p.equals(short.class)) {
|
228
|
+
|
229
|
+
params.add(0);
|
230
|
+
|
231
|
+
continue;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
if (p.equals(int.class)) {
|
236
|
+
|
237
|
+
params.add(0);
|
238
|
+
|
239
|
+
continue;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
if (p.equals(char.class)) {
|
244
|
+
|
245
|
+
params.add('\u0000');
|
246
|
+
|
247
|
+
continue;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
if (p.equals(float.class)) {
|
252
|
+
|
253
|
+
params.add(0f);
|
254
|
+
|
255
|
+
continue;
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
if (p.equals(double.class)) {
|
260
|
+
|
261
|
+
params.add(0d);
|
262
|
+
|
263
|
+
continue;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
params.add(p.newInstance());
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
return params;
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
class Point {
|
282
|
+
|
283
|
+
double x1, y1, x;
|
284
|
+
|
285
|
+
double y = 2.0;
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
class Point2 {
|
290
|
+
|
291
|
+
double x1=2, y1, x;
|
292
|
+
|
293
|
+
double y = 2.0;
|
294
|
+
|
295
|
+
public Point2(double v) { this.x1 = v; }
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
class Point3 {
|
300
|
+
|
301
|
+
int x1, y1, x;
|
302
|
+
|
303
|
+
int y = 2;
|
304
|
+
|
305
|
+
public Point3(int v) { this.x1 = v; }
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
class Point4 {
|
310
|
+
|
311
|
+
String x1="aaa", y1, x;
|
312
|
+
|
313
|
+
int y = 2;
|
314
|
+
|
315
|
+
public Point4(String v) { this.x1 = v; }
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
```
|
1
複数例外catchに変更
test
CHANGED
@@ -18,45 +18,33 @@
|
|
18
18
|
|
19
19
|
try {
|
20
20
|
|
21
|
-
|
21
|
+
Class<?> c = inputFindClass();
|
22
22
|
|
23
|
-
|
23
|
+
// コンストラクタを呼び出してインスタンスを生成
|
24
24
|
|
25
|
-
|
25
|
+
Object obj = c.getDeclaredConstructor().newInstance();
|
26
26
|
|
27
|
-
|
27
|
+
for (Field fieled : c.getDeclaredFields()) {
|
28
28
|
|
29
|
-
|
29
|
+
fieled.setAccessible(true);
|
30
30
|
|
31
|
-
|
31
|
+
String fieldName = fieled.getName();
|
32
32
|
|
33
|
-
|
33
|
+
Class<?> fieldType = fieled.getType();
|
34
34
|
|
35
|
-
|
35
|
+
String fieldTypeStr = fieldType.getName();
|
36
36
|
|
37
|
-
|
37
|
+
System.out.print("Type : " + fieldTypeStr + ", Name : " + fieldName + ", Value : ");
|
38
38
|
|
39
|
-
|
39
|
+
try {
|
40
40
|
|
41
|
-
|
41
|
+
Object value = fieled.get(obj);
|
42
42
|
|
43
|
-
|
43
|
+
System.out.println(value);
|
44
44
|
|
45
|
-
|
45
|
+
} catch (IllegalArgumentException | IllegalAccessException e) {
|
46
46
|
|
47
|
-
System.out.print("Type : " + fieldTypeStr + ", Name : " + fieldName + ", Value : ");
|
48
|
-
|
49
|
-
try {
|
50
|
-
|
51
|
-
Object value = fieled.get(obj);
|
52
|
-
|
53
|
-
System.out.println(value);
|
54
|
-
|
55
|
-
} catch (Exception e) {
|
56
|
-
|
57
|
-
|
47
|
+
System.err.println(e);
|
58
|
-
|
59
|
-
}
|
60
48
|
|
61
49
|
}
|
62
50
|
|
@@ -72,6 +60,26 @@
|
|
72
60
|
|
73
61
|
}
|
74
62
|
|
63
|
+
|
64
|
+
|
65
|
+
public static Class<?> inputFindClass() throws ClassNotFoundException {
|
66
|
+
|
67
|
+
System.out.println("フィールドとフィールドの値を表示します.");
|
68
|
+
|
69
|
+
System.out.println("検査するクラスを入力してください");
|
70
|
+
|
71
|
+
System.out.print(">");
|
72
|
+
|
73
|
+
try (Scanner sc = new Scanner(System.in)) {
|
74
|
+
|
75
|
+
String className = sc.nextLine();
|
76
|
+
|
77
|
+
return Class.forName(className);
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
75
83
|
}
|
76
84
|
|
77
85
|
|
@@ -80,7 +88,7 @@
|
|
80
88
|
|
81
89
|
double x1, y1, x;
|
82
90
|
|
83
|
-
double y = 2.0;//値を取得できてるのかの確認値
|
91
|
+
double y = 2.0;// 値を取得できてるのかの確認値
|
84
92
|
|
85
93
|
}
|
86
94
|
|