How to code a checkStyle method in Java?

75 views Asked by At

Our teacher asked us to make a method which takes a Class as a parameter then checks the fields and methods if they follow the rules of Checkstyle (Upper case, lower case, final attribute, etc.)

He gave us a piece of code to start with but I don't know what to do next.

public class CheckStyle {
    static String  valider(Class c){
        Field[] tattribut = c.getDeclaredFields();
        String name = c.getName();
        Method[] tmethod=c.getDeclaredMethods();
    }   

    public static void main(String[] args) {
        String error = CheckStyle.valider(a.class);
        System.out.println(error);
    }
}
1

There are 1 answers

1
roeygol On

Try to use some of this code:

public class SomeClass {    
    private String aaa;
    private String bbb;
    private double ccc;    
}

public static void main(String[] args){    
    List<Field> privateFields = new ArrayList<>();
    Field[] allFields = SomeClass.class.getDeclaredFields();
    for (Field field : allFields) {
        if (Modifier.isPrivate(field.getModifiers())) {
            privateFields.add(field);
        }
    }
}

Then go over your fields to see if they are in the correct format you need and ect.