/home/kocsisg/NetBeansProjects/Lesson4/src/lesson4/ClassChecker.java |
1
2
3
4
5
6 package lesson4;
7
8 import java.lang.reflect.*;
9 import java.util.Scanner;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12
13
14
15 @author
16
17 public class ClassChecker {
18
19
20 @param args
21
22 public static void main(String[] args) {
23
24
25 Scanner sc = new Scanner(System.in);
26 Class c;
27 c = sampleproject1.Student.class;
28 System.out.println(" - What is the name of the class? ");
29 while (true) {
30 try {
31 c = Class.forName(sc.nextLine());
32 break;
33 } catch (ClassNotFoundException | NoClassDefFoundError ex) {
34 System.out.println("No such class...");
35
36 }
37 }
38
39
40 Field[] fa = c.getDeclaredFields();
41
42 if (fa.length == 3) {
43 System.out.println(" - There are exactly 3 fields in the class");
44 } else {
45 System.err.println(" - The number of fields is not 3... (" + fa.length + ")");
46 }
47
48 if (c.getFields().length == 0) {
49 System.out.println(" - There are no public fields in the class");
50 } else {
51
52 for (Field f : fa) {
53 if (Modifier.isPublic(f.getModifiers())) {
54 System.err.println(" - Field '" + f.getName() + "' is public!");
55 }
56 }
57 }
58
59
60
61 Constructor[] constArray = c.getConstructors();
62 if (constArray.length == 2) {
63 System.out.println(" - Number of constructors = 2 (OK).");
64 } else {
65 System.err.println(" - The number of constructors is not 2...");
66 }
67
68 boolean defa = false;
69 boolean parameterized = false;
70 for (Constructor con : constArray) {
71 if (con.getParameterCount() == 0) {
72 defa = true;
73 }
74 if (con.getParameterCount() == fa.length) {
75 parameterized = true;
76 }
77 }
78 if (defa) {
79 System.out.println(" - There is a default constructor in the class...");
80 } else {
81 System.err.println(" - There is no default constructor in the class...");
82 }
83
84 if (parameterized) {
85 System.out.println(" - There is a constructor in the class that may set all fields...");
86 } else {
87 System.err.println(" - No proper parameterized constructor...");
88 }
89
90
91 String methodName;
92 for (Field f : fa) {
93
94 if (f.getType().equals(boolean.class)) {
95 methodName = "is";
96 } else {
97 methodName = "get";
98 }
99
100 methodName += f.getName().toUpperCase().charAt(0) + f.getName().substring(1);
101 try {
102 c.getMethod(methodName);
103 System.out.println(" - " + methodName + " exists...");
104 } catch (NoSuchMethodException | SecurityException ex) {
105 System.err.println(" - No " + methodName + " method found...");
106 }
107 }
108
109
110 for (Field f : fa) {
111
112 methodName = "set";
113
114 methodName += f.getName().toUpperCase().charAt(0) + f.getName().substring(1);
115 try {
116 c.getMethod(methodName, f.getType());
117 System.out.println(" - " + methodName + " exists...");
118 } catch (NoSuchMethodException | SecurityException ex) {
119 System.err.println(" - No " + methodName + " method found...");
120 }
121 }
122
123
124 Method[] methodArray = c.getMethods();
125 for (Method m : methodArray){
126 try {
127 if ( m.getAnnotation((Class)Class.forName(c.getPackage().getName() + ".MarkSg")) != null)
128 System.out.println(m.getName() + " is annotated MarkSg...");
129 } catch (ClassNotFoundException ex) {
130 Logger.getLogger(ClassChecker.class.getName()).log(Level.SEVERE, null, ex);
131 }
132 }
133 }
134 }//Main class