Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for Java class member order

We're are using PMD and Checkstyle (from developers IDE and central CI server) to check code to our coding conventions.

I was expecting for these tools to provide a rule that checks for class members order but they don't seem to have one.

By class member order I mean something like:

public static properties
public properties
protected properties
private properties

public constructors
protected constructors
private constructors

static methods

non-static methods

I could certainly write my own PMD rule but I'm not used of it and would prefer not reinventing the wheel.

Do you know any tool that could do this kind of checks for us?

like image 264
Florent Paillard Avatar asked Dec 02 '25 02:12

Florent Paillard


2 Answers

Checkstyle actually does that: DeclarationOrder

like image 162
Jean Logeart Avatar answered Dec 04 '25 16:12

Jean Logeart


You can use Checkstyle's Declaration Order Check

Checks that the parts of a class or interface declaration appear in the order suggested by the Code Conventions for the Java Programming Language.

1.Class (static) variables. First the public class variables, 

then the protected, then package level (no access modifier), and then the private.

2.Instance variables. First the public class variables, then the protected,

then package level (no access modifier), and then the private.

3.Constructors

4.Methods

Reference

like image 32
TheWhiteRabbit Avatar answered Dec 04 '25 17:12

TheWhiteRabbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!