incorporated patch #1892273 (NoFinalizer check) and added better unit tests, one bugfix, docs and i18n
This commit is contained in:
parent
41f0339d5c
commit
f31b6a7962
|
|
@ -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 <em>finalize</em>.
|
||||
*
|
||||
* @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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1263,6 +1263,36 @@ if ("something".equals(x))
|
|||
</subsection>
|
||||
</section>
|
||||
|
||||
<section name="NoFinalizer">
|
||||
<subsection name="Description">
|
||||
<p>
|
||||
Verifies there are no <span class="code">finalize()</span> methods
|
||||
defined in a class.
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Example">
|
||||
<p>
|
||||
To configure the check:
|
||||
</p>
|
||||
<source>
|
||||
<module name="NoFinalizer"/>
|
||||
</source>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Package">
|
||||
<p>
|
||||
com.puppycrawl.tools.checkstyle.checks.coding
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Parent Module">
|
||||
<p>
|
||||
<a href="config.html#treewalker">TreeWalker</a>
|
||||
</p>
|
||||
</subsection>
|
||||
</section>
|
||||
|
||||
<section name="SuperClone">
|
||||
<subsection name="Description">
|
||||
<p>
|
||||
|
|
@ -1272,7 +1302,7 @@ if ("something".equals(x))
|
|||
|
||||
<p>
|
||||
Reference: <a
|
||||
href="http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Object.html#clone()">Object.clone()</a>.
|
||||
href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#clone()">Object.clone()</a>.
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,12 @@
|
|||
|
||||
<p>New Features:</p>
|
||||
<ul>
|
||||
<li>
|
||||
New check <a
|
||||
href="config_coding.html#NoFinalizer">NoFinalizer</a>
|
||||
for ensuring that a class does not define a finalize() method.
|
||||
Thanks to Feng Qian and Steve McKay for providing patch #1892273.
|
||||
</li>
|
||||
<li>
|
||||
New check <a
|
||||
href="config_whitespace.html#GenericWhitespace">GenericWhitespace</a>
|
||||
|
|
|
|||
Loading…
Reference in New Issue