Began on working on the second assignment. I got bored and wanted to go work on the XML parser.
- Author
- Maarten Vangeneugden
- Date
- Dec. 11, 2015, 2:15 p.m.
- Hash
- 480a1a63f145edc213dde0103ad8f56c19246ec5
- Parent
- 5d5c7f9a70db685e9e0bdcbdc8e98a0bfab6496e
- Modified file
- 07/Reflection.java
07/Reflection.java ¶
14 additions and 1 deletion.
View changes Hide changes
1 |
1 |
import java.lang.reflect.*; |
2 |
2 |
/* Reflection.java - Opdracht 1 of OOP2, introspection. |
3 |
- | Copyright 2015 Maarten Vangeneugden |
+ |
3 |
Copyright 2015 Maarten Vangeneugden |
4 |
4 |
|
5 |
5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
6 |
6 |
you may not use this file except in compliance with the License. |
7 |
7 |
You may obtain a copy of the License at |
8 |
8 |
|
9 |
9 |
https://www.apache.org/licenses/LICENSE-2.0 |
10 |
10 |
|
11 |
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
14 |
See the License for the specific language governing permissions and |
15 |
15 |
limitations under the License.*/ |
16 |
16 |
/** |
17 |
17 |
* MethodPrinter - Class that allows the user to pass a class name via the command line, after which a list of its public members and methods is shown. |
18 |
18 |
* @author Maarten Vangeneugden - 1438256 |
19 |
19 |
*/ |
20 |
20 |
class MethodPrinter { |
21 |
21 |
public static void main(String[] args) { |
22 |
22 |
System.out.println("Class name?"); |
23 |
23 |
Scanner scanner = new Scanner(System.in); |
24 |
24 |
String name = scanner.nextLine(); |
25 |
25 |
|
26 |
26 |
try { |
27 |
27 |
Class cls = Class.forName(name); |
28 |
28 |
Method methods[] = cls.getDeclaredMethods(); |
29 |
29 |
Field fields[] = cls.getDeclaredFields(); |
30 |
30 |
|
31 |
31 |
System.out.println("Methods:"); |
32 |
32 |
for (Method method : methods) { |
33 |
33 |
System.out.println(method.toString()); |
34 |
34 |
} |
35 |
35 |
System.out.println("Fields:"); |
36 |
36 |
for (Field field : fields) { |
37 |
37 |
System.out.println(field.toString()); |
38 |
38 |
} |
39 |
39 |
|
40 |
40 |
} catch(Exception e){ |
41 |
41 |
e.printStackTrace(); |
42 |
42 |
} |
43 |
43 |
} |
44 |
44 |
} |
45 |
45 |
|
+ |
46 |
/** |
+ |
47 |
* MethodExecutor - Allows to execute methods from an arbitrary class via the command line. This only works for classes with empty constructors, static methods or static classes. |
+ |
48 |
* @author Maarten Vangeneugden - 1438256 |
+ |
49 |
*/ |
+ |
50 |
class MethodExecutor { |
+ |
51 |
public static void main(String[] args) { |
+ |
52 |
System.out.println("What method to invoke?"); |
+ |
53 |
Scanner scanner = new scanner(System.in); |
+ |
54 |
String method = scanner.nextLine(); |
+ |
55 |
|
+ |
56 |
} |
+ |
57 |
} |
+ |
58 |