diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheck.java deleted file mode 100755 index 88905eb56..000000000 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheck.java +++ /dev/null @@ -1,93 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2011 Oliver Burn -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//////////////////////////////////////////////////////////////////////////////// -package com.puppycrawl.tools.checkstyle.checks.coding; - -import antlr.collections.AST; -import com.puppycrawl.tools.checkstyle.api.Check; -import com.puppycrawl.tools.checkstyle.api.DetailAST; -import com.puppycrawl.tools.checkstyle.api.TokenTypes; - -/** - * Detect the double-checked locking idiom, a technique that tries to avoid - * synchronization overhead but is incorrect because of subtle artifacts of - * the java memory model. - * - * See The "Double-Checked Locking is Broken" Declaration for a - * more in depth explanation. - * - * @author Lars Kühne - */ -public class DoubleCheckedLockingCheck extends Check -{ - @Override - public int[] getDefaultTokens() - { - return new int[]{TokenTypes.LITERAL_IF}; - } - - @Override - public void visitToken(DetailAST aAST) - { - final DetailAST synchronizedAST = - getLowestParent(aAST, TokenTypes.LITERAL_SYNCHRONIZED); - if (synchronizedAST == null) { - return; - } - - final DetailAST ifAST = - getLowestParent(synchronizedAST, TokenTypes.LITERAL_IF); - if (ifAST == null) { - return; - } - - if (getIfCondition(aAST).equalsTree(getIfCondition(ifAST))) { - log(aAST.getLineNo(), aAST.getColumnNo(), - "doublechecked.locking.avoid"); - } - } - - /** - * returns the condition of an if statement. - * @param aIfAST the LITERAL_IF AST - * @return the AST that represents the condition of the if statement - */ - private AST getIfCondition(DetailAST aIfAST) - { - return aIfAST.getFirstChild().getNextSibling(); - } - - /** - * searches towards the root of the AST for a specific AST type. - * @param aAST the starting node for searching (inclusive) - * @param aTokenType the token type to search for - * @return the first token of type aTokenTye or null if no such token exists - */ - private DetailAST getLowestParent(DetailAST aAST, int aTokenType) - { - DetailAST synchronizedParent = aAST; - while ((synchronizedParent != null) - && (synchronizedParent.getType() != aTokenType)) - { - synchronizedParent = synchronizedParent.getParent(); - } - return synchronizedParent; - } -} diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputDoubleCheckedLocking.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputDoubleCheckedLocking.java deleted file mode 100755 index eb0c122da..000000000 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/InputDoubleCheckedLocking.java +++ /dev/null @@ -1,77 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// Test case file for checkstyle. -// Created: 2001 -//////////////////////////////////////////////////////////////////////////////// -package com.puppycrawl.tools.checkstyle; - -/** - * Test case for detection of double checked locking - * @author lkuehne - **/ -class InputDoubleCheckedLocking -{ - static Integer one = null; - - private static Integer getOneCorrect() - { - synchronized (InputDoubleCheckedLocking.class) - { - if (one == null) - { - one = new Integer(1); - } - } - return one; - } - - private static Integer getOneDCL() - { - if (one == null) - { - System.out.println("just to make the AST interesting"); - synchronized (InputDoubleCheckedLocking.class) - { - if (one == null) - { - one = new Integer(1); - } - } - } - return one; - } - - private static Integer getSimilarToDCL() - { - // different tests - if (one == null) - { - synchronized (InputDoubleCheckedLocking.class) - { - if (one == Integer.valueOf(2)) - { - one = new Integer(1); - } - } - } - - // no synchronization - if (one == null) - { - if (one == null) - { - one = new Integer(1); - } - } - - // no outer test - synchronized (InputDoubleCheckedLocking.class) - { - if (one == null) - { - one = new Integer(1); - } - } - return one; - } - -} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheckTest.java deleted file mode 100755 index 5af297af4..000000000 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/DoubleCheckedLockingCheckTest.java +++ /dev/null @@ -1,42 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2011 Oliver Burn -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//////////////////////////////////////////////////////////////////////////////// -package com.puppycrawl.tools.checkstyle.checks.coding; - -import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; -import com.puppycrawl.tools.checkstyle.DefaultConfiguration; -import org.junit.Test; - -/** - * @author lkuehne - */ -public class DoubleCheckedLockingCheckTest - extends BaseCheckTestSupport -{ - @Test - public void testIt() throws Exception - { - final DefaultConfiguration checkConfig = - createCheckConfig(DoubleCheckedLockingCheck.class); - final String[] expected = { - "34:17: The double-checked locking idiom is broken and should be avoided.", - }; - verify(checkConfig, getPath("InputDoubleCheckedLocking.java"), expected); - - } -} diff --git a/src/xdocs/availablechecks.xml b/src/xdocs/availablechecks.xml index ec1c4f333..a47581215 100644 --- a/src/xdocs/availablechecks.xml +++ b/src/xdocs/availablechecks.xml @@ -118,14 +118,6 @@
- The "double-checked locking" idiom (DCL) tries to avoid - the runtime cost of synchronization. An example that uses the DCL - idiom is this: -
-- The problem with the DCL idiom in Java is that it just does not work - correctly. Using it introduces bugs that are extremely hard to - track down and reproduce. The - "Double-Checked Locking is Broken" Declaration has an - in depth explanation of the exact problem which has to do with the - semantics of the Java memory model. -
- -- The DoubleCheckedLocking check will find source code where a test is - wrapped in a synchronized block that is wrapped in the same test, - like in the example above. -
-- To configure the check: -
-- com.puppycrawl.tools.checkstyle.checks.coding -
-- TreeWalker -
-diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index 6c3dbf7e3..4873a3fdd 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -35,7 +35,11 @@
Notes:
DoubleCheckedLocking check, as in Java 5
+ (and beyond), using the volatile keyword addresses
+ the issue. See
+ here
+ for more details.