2 w * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.java.bytecode.check;
22 import org.sonar.check.Priority;
23 import org.sonar.check.Rule;
24 import org.sonar.java.bytecode.asm.AsmClass;
25 import org.sonar.java.bytecode.asm.AsmMethod;
26 import org.sonar.squid.api.CheckMessage;
27 import org.sonar.squid.api.SourceFile;
28 import org.sonar.squid.api.SourceMethod;
30 @Rule(key = "UnusedProtectedMethod", name = "Unused protected method",
31 priority = Priority.MAJOR, description = "<p>Protected methods that are never used by any classes " +
32 "in the same project are strongly suspected to be dead code. "
33 + "Dead code means unnecessary, inoperative code that should be removed. "
34 + "This helps in maintenance by decreasing the maintained code size, "
35 + "making it easier to understand the program and preventing bugs from being introduced.</p>"
36 + "<p>In the following case, unused protected methods are not considered as dead code by Sonar :</p>"
37 + "<ul><li>Protected methods which override a method from a parent class.</li></ul>"
38 + "<ul><li>Protected methods of an abstract class.</li></ul>")
39 public class UnusedProtectedMethodCheck extends BytecodeCheck {
41 private AsmClass asmClass;
44 public void visitClass(AsmClass asmClass) {
45 this.asmClass = asmClass;
49 public void visitMethod(AsmMethod asmMethod) {
50 if (!asmMethod.isUsed() && asmMethod.isProtected() && !asmClass.isAbstract() && !SerializableContract.methodMatch(asmMethod)
51 && !asmMethod.isInherited()) {
52 CheckMessage message = new CheckMessage(this, "Protected method '" + asmMethod.getName() + "(...)' is never used.");
53 SourceMethod sourceMethod = getSourceMethod(asmMethod);
54 if (sourceMethod != null) {
55 message.setLine(sourceMethod.getStartAtLine());
57 SourceFile file = getSourceFile(asmClass);