added check that static fields are final.

This commit is contained in:
Rick Giles 2003-07-02 10:37:00 +00:00
parent 9089e7a210
commit 983111eb06
7 changed files with 92 additions and 2 deletions

View File

@ -15,6 +15,7 @@
<module name="j2ee.EntityBeanFindByPrimaryKey"/>
<module name="j2ee.EntityBeanFinder"/>
<module name="j2ee.EntityBeanMatchEjbCreate"/>
<module name="j2ee.FinalStatic"/>
<module name="j2ee.HomeInterface"/>
<module name="j2ee.LocalHomeInterface"/>
<module name="j2ee.MessageBean"/>

View File

@ -0,0 +1,67 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2003 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.j2ee;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Checks that all static fields are declared final.
* That ensures consistent runtime semantics so that EJB containers have
* the flexibility to distribute instances across multiple JVMs.
* http://www.javaworld.com/javaworld/jw-08-2000/jw-0825-ejbrestrict.html
* @author Rick Giles
*/
public class FinalStaticCheck
extends Check
{
/**
* @see com.puppycrawl.tools.checkstyle.api.Check
*/
public int[] getDefaultTokens()
{
return new int[] {TokenTypes.VARIABLE_DEF};
}
/**
* @see com.puppycrawl.tools.checkstyle.api.Check
*/
public int[] getRequiredTokens()
{
return getDefaultTokens();
}
/**
* @see com.puppycrawl.tools.checkstyle.api.Check
*/
public void visitToken(DetailAST aAST)
{
if (Utils.isInEJB(aAST)
&& Utils.isStatic(aAST)
&& !Utils.isFinal(aAST))
{
final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
log(
nameAST.getLineNo(),
nameAST.getColumnNo(),
"nonfinalstatic.bean", nameAST.getText());
}
}
}

View File

@ -31,8 +31,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* <li>It contains a <code>public</code> constructor with no parameters.</li>
* <li>It must not define the <code>finalize</code> method.</li>
* </ul>
* Reference: Enterprise JavaBeansTM Specification,Version 2.1, sections 10.6.2
* and 7.11.2.
* Reference: Enterprise JavaBeansTM Specification,Version 2.1, section 7.11.2.
* @author Rick Giles
*/
public class SessionBeanCheck

View File

@ -4,6 +4,7 @@ illegalthrows.bean=Method {0} must not throw {1}.
missingmethod.bean={0} must have method {1}.
missingthrows.bean=Method {0} must throw {1}.
nonabstract.bean={0} must be abstract.
nonfinalstatic.bean=Static field {0} should be final.
nonpublicconstructor.bean={0} must have a public constructor with no parameters.
nonpublic.bean={0} must be public.
nonvoidmethod.bean=Method {0} must be void.

View File

@ -118,6 +118,10 @@ final class FinalEntityBean
return (this);
}
static int sInt1;
static final int sInt2 = 0;
/**
* @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
*/

View File

@ -30,6 +30,7 @@ public class AllTests {
suite.addTest(new TestSuite(RemoteInterfaceCheckTest.class));
suite.addTest(new TestSuite(SessionBeanCheckTest.class));
suite.addTest(new TestSuite(SessionBeanEjbCreateCheckTest.class));
suite.addTest(new TestSuite(FinalStaticCheckTest.class));
suite.addTest(new TestSuite(ThisParameterCheckTest.class));
suite.addTest(new TestSuite(ThisReturnCheckTest.class));
//$JUnit-END$

View File

@ -0,0 +1,17 @@
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.j2ee.FinalStaticCheck;
public class FinalStaticCheckTest extends BaseCheckTestCase
{
public void testDefault()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(FinalStaticCheck.class);
final String[] expected = {
"121:16: Static field sInt1 should be final.",
};
verify(checkConfig, getPath("InputEntityBean.java"), expected);
}
}