The members of an interface declaration or class should appear in a pre-defined order. why ?

1,573 views
Skip to first unread message

Cristiano Gavião

unread,
Aug 3, 2015, 7:04:24 PM8/3/15
to SonarQube
Hello,

I'm a bit confused.
I have the code below. I have no attributes, one private constructor and one static method.

But I'm getting the And I'm getting this warning: Move this constructor to comply with Java Code Conventions.


public final class VersionConverter {

    public static String toNewVersion(String mversion) {

        ....
        return version.toString();
    }

    private MavenVersionConverter() {

    }
}



Could someone please help to understand it?

thanks

Cristiano Gavião

unread,
Aug 4, 2015, 7:44:03 AM8/4/15
to SonarQube
It seems to be a bug.

If I move the constructor to a position before than the static method the warning is gone.

but this is not what the description of the warning says:
  • Class and instance variables
  • Constructors
  • Methods

anyone could comment?

G. Ann Campbell

unread,
Aug 4, 2015, 12:42:49 PM8/4/15
to SonarQube
This is telling you to put constructors before methods. I don't understand what's not clear...?


Ann

Cristiano Gavião

unread,
Aug 4, 2015, 4:33:15 PM8/4/15
to SonarQube

Hi.
Because the method is static, and according to the rule description and also the eclipse ide sort members function, static method should come before the constructors.
This is what is not clear.

Michael Gumowski

unread,
Aug 5, 2015, 11:21:48 AM8/5/15
to Cristiano Gavião, SonarQube
Hello,

The rule reacts as expected regarding its description, which means that it will raise an issue if members are not ordered as follow:
  1. Class (static) and instance variables
  2. Constructors
  3. Methods (including static)
In your case, you will then naturally get an issue as your (static) method is placed before the constructor.

Note that according to the Code Conventions for the Java TM Programming Language (revision date is April 20, 1999: http://www.oracle.com/technetwork/java/codeconventions-141855.html#1852), if static fields should come first (before instance fields - even if we currently do not do any difference in the implementation), static methods will definitely stay with the other methods, after the constructors. 

Regarding eclipse IDE sorting function, it is apparently applying another convention, which is not the one we follow. It doesn't mean neither that there is a bug in our implementation.
If sorting your members using eclipse causes unexpected issues on your side, note that you can also modify the sorting order to stay conform with the rule.

Regards,

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/0f750e92-0a00-4e2f-a929-a0fc87644b2a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Cristiano Gavião

unread,
Aug 5, 2015, 3:31:05 PM8/5/15
to SonarQube, cvga...@gmail.com
Hi Michael,

I understood now.

many thanks for the clarification.
Reply all
Reply to author
Forward
0 new messages