From 983111eb06ce7d46a2099736c291abcc3010a2df Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Wed, 2 Jul 2003 10:37:00 +0000 Subject: [PATCH] added check that static fields are final. --- contrib/j2ee/docs/J2eeConfig.xml | 1 + .../checks/j2ee/FinalStaticCheck.java | 67 +++++++++++++++++++ .../checks/j2ee/SessionBeanCheck.java | 3 +- .../checks/j2ee/messages.properties | 1 + .../tools/checkstyle/InputEntityBean.java | 4 ++ contrib/j2ee/src/tests/AllTests.java | 1 + .../j2ee/src/tests/FinalStaticCheckTest.java | 17 +++++ 7 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/FinalStaticCheck.java create mode 100644 contrib/j2ee/src/tests/FinalStaticCheckTest.java diff --git a/contrib/j2ee/docs/J2eeConfig.xml b/contrib/j2ee/docs/J2eeConfig.xml index 754383cb1..88f806aa1 100644 --- a/contrib/j2ee/docs/J2eeConfig.xml +++ b/contrib/j2ee/docs/J2eeConfig.xml @@ -15,6 +15,7 @@ + diff --git a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/FinalStaticCheck.java b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/FinalStaticCheck.java new file mode 100644 index 000000000..8e9a58db1 --- /dev/null +++ b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/FinalStaticCheck.java @@ -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()); + } + } +} diff --git a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/SessionBeanCheck.java b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/SessionBeanCheck.java index 758fc449a..a86a528ec 100644 --- a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/SessionBeanCheck.java +++ b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/SessionBeanCheck.java @@ -31,8 +31,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; *
  • It contains a public constructor with no parameters.
  • *
  • It must not define the finalize method.
  • * - * 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 diff --git a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/messages.properties b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/messages.properties index a5b40c74e..f08ad2cb0 100644 --- a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/messages.properties +++ b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/messages.properties @@ -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. diff --git a/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java b/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java index a80985f89..d315765d4 100644 --- a/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java +++ b/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java @@ -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) */ diff --git a/contrib/j2ee/src/tests/AllTests.java b/contrib/j2ee/src/tests/AllTests.java index 8d71fc1ea..cb1b2cfb5 100644 --- a/contrib/j2ee/src/tests/AllTests.java +++ b/contrib/j2ee/src/tests/AllTests.java @@ -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$ diff --git a/contrib/j2ee/src/tests/FinalStaticCheckTest.java b/contrib/j2ee/src/tests/FinalStaticCheckTest.java new file mode 100644 index 000000000..c39e82670 --- /dev/null +++ b/contrib/j2ee/src/tests/FinalStaticCheckTest.java @@ -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); + } +}