-1

I am writing checkstyle rules for my project where I want only one whitespace between method signature and opening curly braces.

I have tried with existing maven-checkstyle-plugin but it is not working.

public class CheckStyleDemo {

    public static void main(String args[]) {

      System.out.println("In Main method");
   }

 }

If I add more than one white space between main method signature and opening curly braces then it should not allow.

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
pradip318
  • 9
  • 3

1 Answers1

0

I found some Regex that just might help you with this. I found some good method matching regex expressions here. I recommend reading up on those answers. Here is my implementation:

private static boolean validateMethodSignature(String method) {
    Pattern methodPattern = Pattern.compile("(((public|private|protected|static|final|native|synchronized|abstract|transient)+\\s)+)?[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?(\\s*)\\{?[^\\}]*\\}?"
    ); // see the link i posted for more explanantion about the pattern
    Matcher matcher = methodPattern.matcher(method);
    if (matcher.matches()) {
      int spaces = matcher.group(4).length(); // i added a fourth group to extract the spaces between the signature and the opening brackets
      System.out.printf("pattern '%s' was a java method\n", method);
      System.out.printf("%d spaces between method signature and opening curly braces \n",spaces);
      return spaces == 1;
    } else {
      System.out.printf("pattern %s was not a java method \n", method);
      return false;
    }
  }

I did some testing myself:



public static void main (String[] args) {
    System.out.println(validateMethodSignature("public static void main(String args[]) {"));
    System.out.println(validateMethodSignature("public void main(String args[]) {"));
    System.out.println(validateMethodSignature("public static String main(int a, int b, String args[]) {"));
    System.out.println(validateMethodSignature("void main(String args[]) {"));
    System.out.println(validateMethodSignature("main(String args[]) {"));
    System.out.println(validateMethodSignature("void main(String args[])    {"));
    System.out.println(validateMethodSignature("some unkoksacoasjdpoj"));
  }

Output:

pattern 'public static void main(String args[]) {' was a java method
1 spaces between method signature and opening curly braces 
true
pattern 'public void main(String args[]) {' was a java method
1 spaces between method signature and opening curly braces 
true
pattern 'public static String main(int a, int b, String args[]) {' was a java method
1 spaces between method signature and opening curly braces 
true
pattern 'void main(String args[]) {' was a java method
1 spaces between method signature and opening curly braces 
true
pattern main(String args[]) { was not a java method 
false
pattern 'void main(String args[])    {' was a java method
4 spaces between method signature and opening curly braces 
false
pattern some unkoksacoasjdpoj was not a java method 
false
Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29