Skip to content

Commit

Permalink
Issue checkstyle#12486: NoWhitespaceAfter don't check synchronized me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
nrmancuso committed Dec 2, 2022
1 parent 90350a2 commit ae24c2f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Expand Up @@ -284,7 +284,10 @@ private static DetailAST getWhitespaceFollowedNode(DetailAST ast) {
*/
private static boolean shouldCheckWhitespaceAfter(DetailAST ast) {
final DetailAST previousSibling = ast.getPreviousSibling();
return previousSibling == null || previousSibling.getType() != TokenTypes.ANNOTATIONS;
final boolean isSynchronizedMethod = ast.getType() == TokenTypes.LITERAL_SYNCHRONIZED
&& ast.getFirstChild() == null;
return (previousSibling == null || previousSibling.getType() != TokenTypes.ANNOTATIONS)
&& !isSynchronizedMethod;
}

/**
Expand Down
Expand Up @@ -354,6 +354,17 @@ public void testNoWhitespaceAfterWithEmoji() throws Exception {
getPath("InputNoWhitespaceAfterWithEmoji.java"), expected);
}

@Test
public void testNoWhitespaceAfterSynchronized() throws Exception {

final String[] expected = {
"18:9: " + getCheckMessage(MSG_KEY, "synchronized"),
};

verifyWithInlineConfigParser(
getPath("InputNoWhitespaceAfterSynchronized.java"), expected);
}

/**
* Creates MOCK lexical token and returns AST node for this token.
*
Expand Down
@@ -0,0 +1,53 @@
/*
NoWhitespaceAfter
allowLineBreaks = (default)true
tokens = LITERAL_SYNCHRONIZED
*/
package com.puppycrawl.tools.checkstyle.checks.whitespace.nowhitespaceafter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

public class InputNoWhitespaceAfterSynchronized<T> {

private final Object lock = new Object();

void m1() {
synchronized (lock) {} // violation
synchronized(lock) {} // ok
}

synchronized int m2() { // ok
return 2;
}

synchronized private int[] m3() { // ok
return new int[]{2};
}

synchronized int[] m4() { // ok
return new int[]{2};
}

private final synchronized int m5() { // ok
return 2;
}

synchronized T m6() { // ok
return null;
}

@SyncAnno synchronized T m7() { // ok
return null;
}

@SyncAnno private synchronized T m8() { // ok
return null;
}
}

@Target(ElementType.METHOD)
@interface SyncAnno {
}

0 comments on commit ae24c2f

Please sign in to comment.