Finished MethodPrinter class. Output is as described in the assignment.
- Author
- Maarten Vangeneugden
- Date
- Dec. 11, 2015, 2:27 p.m.
- Hash
- 5d5c7f9a70db685e9e0bdcbdc8e98a0bfab6496e
- Parent
- 9ff17e25922239d1c0564ae95cca3555027942f8
- Modified file
- 07/Reflection.java
07/Reflection.java ¶
27 additions and 2 deletions.
View changes Hide changes
+ |
1 |
import java.lang.reflect.*; |
+ |
2 |
/* Reflection.java - Opdracht 1 of OOP2, introspection. |
1 |
3 |
Copyright 2015 Maarten Vangeneugden |
2 |
4 |
|
3 |
5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
6 |
you may not use this file except in compliance with the License. |
5 |
7 |
You may obtain a copy of the License at |
6 |
8 |
|
7 |
9 |
https://www.apache.org/licenses/LICENSE-2.0 |
8 |
10 |
|
9 |
11 |
Unless required by applicable law or agreed to in writing, software |
10 |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
11 |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
14 |
See the License for the specific language governing permissions and |
13 |
15 |
limitations under the License.*/ |
14 |
16 |
/** |
15 |
17 |
* Reflection - 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. |
16 |
- | * @author Maarten Vangeneugden - 1438256 |
+ |
18 |
* @author Maarten Vangeneugden - 1438256 |
17 |
19 |
*/ |
18 |
20 |
class Reflection { |
19 |
- | |
+ |
21 |
public static void main(String[] args) { |
+ |
22 |
System.out.println("Class name?"); |
+ |
23 |
Scanner scanner = new Scanner(System.in); |
+ |
24 |
String name = scanner.nextLine(); |
+ |
25 |
|
20 |
26 |
try { |
+ |
27 |
Class cls = Class.forName(name); |
+ |
28 |
Method methods[] = cls.getDeclaredMethods(); |
+ |
29 |
Field fields[] = cls.getDeclaredFields(); |
+ |
30 |
|
+ |
31 |
System.out.println("Methods:"); |
+ |
32 |
for (Method method : methods) { |
+ |
33 |
System.out.println(method.toString()); |
+ |
34 |
} |
+ |
35 |
System.out.println("Fields:"); |
+ |
36 |
for (Field field : fields) { |
+ |
37 |
System.out.println(field.toString()); |
+ |
38 |
} |
+ |
39 |
|
+ |
40 |
} catch(Exception e){ |
+ |
41 |
e.printStackTrace(); |
+ |
42 |
} |
+ |
43 |
} |
+ |
44 |
} |
+ |
45 |