Skip to content

Commit

Permalink
Issue checkstyle#12486: NoWhitespaceAfter shouldn't check synchronize…
Browse files Browse the repository at this point in the history
…d method
  • Loading branch information
nrmancuso committed Dec 7, 2022
1 parent 0795964 commit de45896
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
Expand Up @@ -55,6 +55,24 @@
* <pre>
* public void foo(final char @NotNull [] param) {} // No violation
* </pre>
* <p>
* Note: This check processes the
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SYNCHRONIZED">
* LITERAL_SYNCHRONIZED</a> token only when it appears as a part of a
* <a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-14.html#jls-14.19">
* synchronized statement</a>.
* </p>
* <p>
* Example:
* </p>
* <pre>
* void m1() {
* synchronized(this) {} // ok
* synchronized (this) {} // violation
* }
* synchronized &lt;T&gt; void m2() {} // ok
* synchronized&lt;T&gt; void m3() {} // ok
* </pre>
* <ul>
* <li>
* Property {@code allowLineBreaks} - Control whether whitespace is allowed
Expand Down Expand Up @@ -284,7 +302,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 !isSynchronizedMethod
&& (previousSibling == null || previousSibling.getType() != TokenTypes.ANNOTATIONS);
}

/**
Expand Down
Expand Up @@ -30,6 +30,24 @@
&lt;/p&gt;
&lt;pre&gt;
public void foo(final char @NotNull [] param) {} // No violation
&lt;/pre&gt;
&lt;p&gt;
This check processes the
&lt;a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SYNCHRONIZED"&gt;
LITERAL_SYNCHRONIZED&lt;/a&gt; token only when it appears as a part of a
&lt;a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-14.html#jls-14.19"&gt;
synchronized statement&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Example:
&lt;/p&gt;
&lt;pre&gt;
void m1() {
synchronized(this) {} // ok
synchronized (this) {} // violation
}
synchronized &amp;lt;T&amp;gt; void m2() {} // ok
synchronized&amp;lt;T&amp;gt; void m3() {} // ok
&lt;/pre&gt;</description>
<properties>
<property default-value="true" name="allowLineBreaks" type="boolean">
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,59 @@
/*
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;
}

// Note that we do not check 'synchronized' as a modifier,
// convention is to have type parameters of a generic method
// be surrounded by whitespace.
synchronized <T2> void m9() {} // ok
synchronized<T2> void m10() {} // ok
}

@Target(ElementType.METHOD)
@interface SyncAnno {
}
17 changes: 17 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -1353,6 +1353,23 @@ import static java.math.BigInteger.ZERO;
<source>
public void foo(final char @NotNull [] param) {} // No violation
</source>
<p>
Note: This check processes the <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SYNCHRONIZED">
LITERAL_SYNCHRONIZED</a> token only when it appears as a part of a
<a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-14.html#jls-14.19">
synchronized statement</a>.
</p>
<p>
Example:
</p>
<source>
void m1() {
synchronized(this) {} // ok
synchronized (this) {} // violation
}
synchronized &lt;T&gt; void m2() {} // ok
synchronized&lt;T&gt; void m3() {} // ok
</source>
</subsection>

<subsection name="Properties" id="NoWhitespaceAfter_Properties">
Expand Down

0 comments on commit de45896

Please sign in to comment.