/home/kocsisg/NetBeansProjects/Lesson4/src/lesson4/ClassChecker.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package lesson4;
  7 
  8 import java.lang.reflect.*; //use this import for replection
  9 import java.util.Scanner;
 10 import java.util.logging.Level;
 11 import java.util.logging.Logger;
 12 
 13 /**
 14  *
 15  * @author admin
 16  */
 17 public class ClassChecker {
 18 
 19     /**
 20      * @param args the command line arguments
 21      */
 22     public static void main(String[] args) {
 23         //Ex. 1,2,3. creata the projecct and add sampleProject1 and sampleProject2 to the libraries
 24         //Ex. 4. Read the name of the class to be checked 
 25         Scanner sc = new Scanner(System.in);
 26         Class c;
 27         c = sampleproject1.Student.class; //A simple test can be done this way (not asking the user for the class but provide it in the code)
 28         System.out.println(" - What is the name of the class? ");
 29         while (true) {
 30             try {
 31                 c = Class.forName(sc.nextLine());
 32                 break; //this break runs only if the Class.forName method above did not throw an Exception
 33             } catch (ClassNotFoundException | NoClassDefFoundError ex) {
 34                 System.out.println("No such class..."); //If Class.forName thwrown an Exception we hande it here, 
 35                 //and go back as a result of the while loop
 36             }//try-catch
 37         }//while
 38 
 39         //Ex. 5. Get the fields to an array
 40         Field[] fa = c.getDeclaredFields();
 41         //check the number of fields
 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         //check if there is any public field / note that getFields gets only the public Fields (inherited also), while getDeclaredFields() gets all the fields (but not inherited)
 48         if (c.getFields().length == 0) {
 49             System.out.println(" - There are no public fields in the class");
 50         } else {
 51             //print out exactly what field is public
 52             for (Field f : fa) {
 53                 if (Modifier.isPublic(f.getModifiers())) {
 54                     System.err.println(" - Field '" + f.getName() + "' is public!");
 55                 }
 56             }//for
 57         }//if - else
 58 
 59         //Ex. 6. Exercise about constructors
 60         // - check the number of constructors
 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         // check the existence of the default and the parameterized constructor
 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         }//for
 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         //Ex. 7. check getters for all fields   
 91         String methodName;
 92         for (Field f : fa) {
 93             //for boolean fields the getter starts with "is" for the others it starts with "get"
 94             if (f.getType().equals(boolean.class)) {
 95                 methodName = "is";
 96             } else {
 97                 methodName = "get";
 98             }
 99             //the name of the field starts with lowercase letter, but in the method it has to start with capital letter
100             methodName += f.getName().toUpperCase().charAt(0) + f.getName().substring(1);
101             try {
102                 c.getMethod(methodName); //try to get the next getter method
103                 System.out.println(" - " + methodName + " exists...");
104             } catch (NoSuchMethodException | SecurityException ex) {
105                 System.err.println(" - No " + methodName + " method found...");
106             }
107         }//for Fields array
108 
109         //Ex. 8. check setters for all fields
110         for (Field f : fa) {
111             //The setters always start with "set"
112             methodName = "set";
113             //the name of the field starts with lowercase letter, but in the method it has to start with capital letter
114             methodName += f.getName().toUpperCase().charAt(0) + f.getName().substring(1);
115             try {
116                 c.getMethod(methodName, f.getType()); //try to get the next getter method
117                 System.out.println(" - " + methodName + " exists...");
118             } catch (NoSuchMethodException | SecurityException ex) {
119                 System.err.println(" - No " + methodName + " method found...");
120             }
121         }//for Fields array
122         
123         //Ex. 8. Print out the annotated methods
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     }//main method
134 }//Main class