From cc857f0940faa52f5f183464434968fdf024194c Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Mon, 30 Jun 2003 13:26:14 +0000 Subject: [PATCH] EntityBean finder checks. --- contrib/j2ee/docs/J2eeConfig.xml | 2 + .../j2ee/EntityBeanFindByPrimaryKeyCheck.java | 48 +++++++++++++++++ .../checks/j2ee/EntityBeanFinderCheck.java | 52 +++++++++++++++++++ .../tools/checkstyle/InputEntityBean.java | 4 ++ contrib/j2ee/src/tests/AllTests.java | 2 + .../EntityBeanFindByPrimaryKeyCheckTest.java | 19 +++++++ .../src/tests/EntityBeanFinderCheckTest.java | 20 +++++++ 7 files changed, 147 insertions(+) create mode 100644 contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFindByPrimaryKeyCheck.java create mode 100644 contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFinderCheck.java create mode 100644 contrib/j2ee/src/tests/EntityBeanFindByPrimaryKeyCheckTest.java create mode 100644 contrib/j2ee/src/tests/EntityBeanFinderCheckTest.java diff --git a/contrib/j2ee/docs/J2eeConfig.xml b/contrib/j2ee/docs/J2eeConfig.xml index 42e8469da..fd3eed15e 100644 --- a/contrib/j2ee/docs/J2eeConfig.xml +++ b/contrib/j2ee/docs/J2eeConfig.xml @@ -9,6 +9,8 @@ + + diff --git a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFindByPrimaryKeyCheck.java b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFindByPrimaryKeyCheck.java new file mode 100644 index 000000000..cadb68da3 --- /dev/null +++ b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFindByPrimaryKeyCheck.java @@ -0,0 +1,48 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Checks that an EntityBean defines method ejbFindByPrimaryKey. + * @author Rick Giles + */ +public class EntityBeanFindByPrimaryKeyCheck + extends AbstractBeanCheck +{ + /** + * @see com.puppycrawl.tools.checkstyle.api.Check + */ + public void visitToken(DetailAST aAST) + { + if (Utils.hasImplements(aAST, "javax.ejb.EntityBean") + && !Utils.isAbstract(aAST) + && !Utils.hasPublicMethod(aAST, "ejbFindByPrimaryKey", false, 1)) + { + final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); + log( + aAST.getLineNo(), + nameAST.getColumnNo(), + "missingmethod.bean", + new Object[] {"Entity bean", "ejbFindByPrimaryKey"}); + } + } +} diff --git a/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFinderCheck.java b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFinderCheck.java new file mode 100644 index 000000000..f2a4bac56 --- /dev/null +++ b/contrib/j2ee/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/j2ee/EntityBeanFinderCheck.java @@ -0,0 +1,52 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + * Checks that an EntityBean ejbFind method satisfies these requirements: + *
    + *
  • The access control modifier must be public.
  • + *
  • The return type must not be void.
  • + *
  • The method modifier cannot be final + * or static.
  • + *
+ * @author Rick Giles + */ +public class EntityBeanFinderCheck + extends AbstractMethodCheck +{ + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void visitToken(DetailAST aAST) + { + final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); + final String name = nameAST.getText(); + if (name.startsWith("ejbFind") + && Utils.implementsEntityBean(aAST)) + { + checkMethod(aAST); + if (Utils.isVoid(aAST)) { + log(nameAST.getLineNo(), nameAST.getColumnNo(), + "voidmethod.bean", name); + } + } + } +} 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 acee51804..07baa543a 100644 --- a/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java +++ b/contrib/j2ee/src/testinputs/com/puppycrawl/tools/checkstyle/InputEntityBean.java @@ -91,6 +91,10 @@ final class FinalEntityBean protected static final void ejbCreate(int i) { } + + protected static final void ejbFindSomething() + { + } /** * @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 a8b3a75bc..dc23241a4 100644 --- a/contrib/j2ee/src/tests/AllTests.java +++ b/contrib/j2ee/src/tests/AllTests.java @@ -18,6 +18,8 @@ public class AllTests { //$JUnit-BEGIN$ suite.addTest(new TestSuite(EntityBeanCheckTest.class)); suite.addTest(new TestSuite(EntityBeanEjbCreateCheckTest.class)); + suite.addTest(new TestSuite(EntityBeanFindByPrimaryKeyCheckTest.class)); + suite.addTest(new TestSuite(EntityBeanFinderCheckTest.class)); suite.addTest(new TestSuite(MessageBeanCheckTest.class)); suite.addTest(new TestSuite(SessionBeanCheckTest.class)); suite.addTest(new TestSuite(SessionBeanEjbCreateCheckTest.class)); diff --git a/contrib/j2ee/src/tests/EntityBeanFindByPrimaryKeyCheckTest.java b/contrib/j2ee/src/tests/EntityBeanFindByPrimaryKeyCheckTest.java new file mode 100644 index 000000000..b40741540 --- /dev/null +++ b/contrib/j2ee/src/tests/EntityBeanFindByPrimaryKeyCheckTest.java @@ -0,0 +1,19 @@ +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.j2ee.EntityBeanFindByPrimaryKeyCheck; + +public class EntityBeanFindByPrimaryKeyCheckTest + extends BaseCheckTestCase +{ + public void testDefault() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(EntityBeanFindByPrimaryKeyCheck.class); + final String[] expected = { + "13:14: Entity bean has no ejbFindByPrimaryKey method.", + "83:13: Entity bean has no ejbFindByPrimaryKey method.", + }; + verify(checkConfig, getPath("InputEntityBean.java"), expected); + } +} diff --git a/contrib/j2ee/src/tests/EntityBeanFinderCheckTest.java b/contrib/j2ee/src/tests/EntityBeanFinderCheckTest.java new file mode 100644 index 000000000..5f92ec0da --- /dev/null +++ b/contrib/j2ee/src/tests/EntityBeanFinderCheckTest.java @@ -0,0 +1,20 @@ +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.j2ee.EntityBeanFinderCheck; + +public class EntityBeanFinderCheckTest extends BaseCheckTestCase +{ + public void testDefault() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(EntityBeanFinderCheck.class); + final String[] expected = { + "95:33: Method ejbFindSomething has illegal modifier final.", + "95:33: Method ejbFindSomething has illegal modifier static.", + "95:33: Method ejbFindSomething is not public.", + "95:33: Void ejbFindSomething method.", + }; + verify(checkConfig, getPath("InputEntityBean.java"), expected); + } +}