2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
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 = "UnusedPrivateMethod", name = "Unused private method",
31 priority = Priority.MAJOR, description = "<p>Private methods that are never executed are dead code. " +
32 "Dead code means unnecessary, inoperative code that should be removed. " +
33 "This helps in maintenance by decreasing the maintained code size, " +
34 "making it easier to understand the program and preventing bugs from being introduced.</p>" +
35 "<p>In the following two cases, private methods are not considered as dead code by Sonar :</p>" +
36 "<ul><li>Private empty constructors that are intentionally used to prevent any direct instanciation of a class.</li>" +
37 "<li>Private methods : readObject(...), writeObject(...), writeReplace(...), readResolve(...) " +
38 "which can contractually be used when implementing the Serializable interface.</li></ul>")
39 public class UnusedPrivateMethodCheck 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.isPrivate() && !asmMethod.isDefaultConstructor() && !SerializableContract.methodMatch(asmMethod)) {
51 CheckMessage message = new CheckMessage(this, "Private method '" + asmMethod.getName() + "(...)' is never used.");
52 SourceMethod sourceMethod = getSourceMethod(asmMethod);
53 if (sourceMethod != null) {
54 message.setLine(sourceMethod.getStartAtLine());
56 SourceFile file = getSourceFile(asmClass);