From f31b6a79623c3f09c95ae558c19e8d574215b1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20K=C3=BChne?= Date: Sun, 2 Mar 2008 22:18:36 +0000 Subject: [PATCH] incorporated patch #1892273 (NoFinalizer check) and added better unit tests, one bugfix, docs and i18n --- .../checks/coding/NoFinalizerCheck.java | 59 +++++++++++++++++++ .../checks/coding/messages.properties | 1 + .../checks/coding/messages_de.properties | 1 + .../checkstyle/coding/InputHasFinalizer.java | 27 +++++++++ .../checks/coding/NoFinalizerCheckTest.java | 38 ++++++++++++ src/xdocs/config_coding.xml | 32 +++++++++- src/xdocs/releasenotes.xml | 6 ++ 7 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputHasFinalizer.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java new file mode 100644 index 000000000..6fab6642f --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2008 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.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Checks that no method having zero parameters is defined + * using the name finalize. + * + * @author fqian@google.com (Feng Qian) + * @author smckay@google.com (Steve McKay) + * @author lkuehne + */ +public class NoFinalizerCheck extends Check +{ + + @Override + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.METHOD_DEF}; + } + + @Override + public void visitToken(DetailAST aAST) + { + final DetailAST mid = aAST.findFirstToken(TokenTypes.IDENT); + final String methodName = mid.getText(); + + if (methodName.equals("finalize")) { + + final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); + final boolean hasEmptyParamList = + !params.branchContains(TokenTypes.PARAMETER_DEF); + + if (hasEmptyParamList) { + log(aAST.getLineNo(), "avoid.finalizer.method"); + } + } + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties index c5163da3c..76ad1b1e1 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties @@ -1,5 +1,6 @@ array.trailing.comma=Array should contain trailing comma. assignment.inner.avoid=Inner assignments should be avoided. +avoid.finalizer.method=Avoid using finalizer method. covariant.equals=covariant equals without overriding equals(java.lang.Object). declaration.order.constructor=Constructor definition in wrong order. declaration.order.method=Method definition in wrong order. diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties index 6a8b64400..38207b472 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties @@ -1,5 +1,6 @@ array.trailing.comma=Array sollte mit einem Komma abheschlossen werden. assignment.inner.avoid=Innere Zuweisungen sollten vermieden werden. +avoid.finalizer.method=Die Verwendung von finalizer Methoden sollte vermieden werden. covariant.equals=Kovariante Definition von equals() ohne equals(java.lang.Object) zu überschreiben. declaration.order.constructor=Konstruktordefinition in falscher Reihenfolge. declaration.order.method=Methodendefinition in falscher Reihenfolge. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputHasFinalizer.java b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputHasFinalizer.java new file mode 100644 index 000000000..df18827ff --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputHasFinalizer.java @@ -0,0 +1,27 @@ +package com.puppycrawl.tools.checkstyle.coding; + +public class InputHasFinalizer +{ + public void finalize() + { + // It's not enough to check if the METHOD_DEF branch contains a PARAMETER_DEF, as that would + // treat this method as having a parameter. + Runnable runnable = new Runnable() { + + public void run() { + reallyFinalize("hi"); + } + + // generates a PARAMETER_DEF AST inside the METHOD_DEF of finalize() + private void reallyFinalize(String s) + { + } + }; + runnable.run(); + } + + // should not be reported by NoFinalizer check + public void finalize(String x) + { + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java new file mode 100644 index 000000000..da1416ded --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java @@ -0,0 +1,38 @@ +package com.puppycrawl.tools.checkstyle.checks.coding; + + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import org.junit.Test; + +/** + * NoFinalizerCheck test. + * + * @author smckay@google.com (Steve McKay) + */ +public class NoFinalizerCheckTest + extends BaseCheckTestSupport +{ + @Test + public void testHasFinalizer() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(NoFinalizerCheck.class); + final String[] expected = { + "5: Avoid using finalizer method.", + }; + verify(checkConfig, getPath("coding/InputHasFinalizer.java"), expected); + } + + @Test + public void testHasNoFinalizer() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(NoFinalizerCheck.class); + final String[] expected = { + }; + verify(checkConfig, getPath("coding/InputIllegalThrowsCheck.java"), expected); + } +} diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml index 9eb9e43ee..2cddb79df 100755 --- a/src/xdocs/config_coding.xml +++ b/src/xdocs/config_coding.xml @@ -1263,6 +1263,36 @@ if ("something".equals(x)) +
+ +

+ Verifies there are no finalize() methods + defined in a class. +

+
+ + +

+ To configure the check: +

+ +<module name="NoFinalizer"/> + +
+ + +

+ com.puppycrawl.tools.checkstyle.checks.coding +

+
+ + +

+ TreeWalker +

+
+
+

@@ -1272,7 +1302,7 @@ if ("something".equals(x))

Reference: Object.clone(). + href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#clone()">Object.clone().

diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index d97573f3d..f14cd8cd2 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -19,6 +19,12 @@

New Features: