aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-squid-java-plugin
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/sonar-squid-java-plugin')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/DITCheck.java31
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java15
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java44
3 files changed, 83 insertions, 7 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/DITCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/DITCheck.java
new file mode 100644
index 00000000000..28489477799
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/check/DITCheck.java
@@ -0,0 +1,31 @@
+package org.sonar.java.bytecode.check;
+
+import org.sonar.check.IsoCategory;
+import org.sonar.check.Rule;
+import org.sonar.check.RuleProperty;
+import org.sonar.java.bytecode.asm.AsmClass;
+import org.sonar.squid.api.CheckMessage;
+import org.sonar.squid.api.SourceClass;
+import org.sonar.squid.measures.Metric;
+
+@Rule(key = "DIT", isoCategory = IsoCategory.Maintainability)
+public class DITCheck extends BytecodeCheck {
+
+ @RuleProperty
+ private Integer threshold;
+
+ @Override
+ public void visitClass(AsmClass asmClass) {
+ SourceClass sourceClass = getSourceClass(asmClass);
+ int dit = sourceClass.getInt(Metric.DIT);
+ if (dit > threshold) {
+ CheckMessage message = new CheckMessage(this, "Depth of inheritance exceeds " + threshold + ".");
+ message.setLine(sourceClass.getStartAtLine());
+ getSourceFile(asmClass).log(message);
+ }
+ }
+
+ public void setThreshold(int threshold) {
+ this.threshold = threshold;
+ }
+}
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
index 74078394435..1c28b34a6b5 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidRuleRepository.java
@@ -1,5 +1,8 @@
package org.sonar.plugins.squid;
+import java.util.Arrays;
+import java.util.List;
+
import org.sonar.api.resources.Java;
import org.sonar.api.rules.AnnotationRuleParser;
import org.sonar.api.rules.Rule;
@@ -7,12 +10,10 @@ import org.sonar.api.rules.RuleRepository;
import org.sonar.java.ast.check.UndocumentedApiCheck;
import org.sonar.java.bytecode.check.ArchitectureCheck;
import org.sonar.java.bytecode.check.CallToDeprecatedMethodCheck;
+import org.sonar.java.bytecode.check.DITCheck;
import org.sonar.java.bytecode.check.UnusedPrivateMethodCheck;
import org.sonar.java.bytecode.check.UnusedProtectedMethodCheck;
-import java.util.Arrays;
-import java.util.List;
-
public final class SquidRuleRepository extends RuleRepository {
private AnnotationRuleParser ruleParser;
@@ -29,10 +30,10 @@ public final class SquidRuleRepository extends RuleRepository {
public static List<Class> getCheckClasses() {
return Arrays.asList(
- (Class) CallToDeprecatedMethodCheck.class,
- UnusedPrivateMethodCheck.class,
- UnusedProtectedMethodCheck.class,
- ArchitectureCheck.class,
+ // Bytecode checks
+ (Class) CallToDeprecatedMethodCheck.class, UnusedPrivateMethodCheck.class, UnusedProtectedMethodCheck.class,
+ ArchitectureCheck.class, DITCheck.class,
+ // AST checks
UndocumentedApiCheck.class);
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java
new file mode 100644
index 00000000000..ad7865b373a
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/check/DITCheckTest.java
@@ -0,0 +1,44 @@
+package org.sonar.java.bytecode.check;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.sonar.java.ast.SquidTestUtils.getFile;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.sonar.java.ast.JavaAstScanner;
+import org.sonar.java.bytecode.BytecodeScanner;
+import org.sonar.java.squid.JavaSquidConfiguration;
+import org.sonar.squid.Squid;
+import org.sonar.squid.api.CheckMessage;
+import org.sonar.squid.api.SourceFile;
+
+public class DITCheckTest {
+
+ private static Squid squid;
+
+ @BeforeClass
+ public static void setup() {
+ squid = new Squid(new JavaSquidConfiguration());
+ squid.register(JavaAstScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/src"));
+ DITCheck check = new DITCheck();
+ check.setThreshold(1);
+ squid.registerVisitor(check);
+ squid.register(BytecodeScanner.class).scanDirectory(getFile("/bytecode/unusedProtectedMethod/bin"));
+ }
+
+ @Test
+ public void testDepthOfInheritanceExceedsThreshold() {
+ SourceFile file = (SourceFile) squid.search("UnusedProtectedMethod.java");
+ assertThat(file.getCheckMessages().size(), is(1));
+ CheckMessage message = file.getCheckMessages().iterator().next();
+ assertThat(message.getLine(), is(7));
+ }
+
+ @Test
+ public void testDepthOfInheritanceNotExceedsThreshold() {
+ SourceFile file = (SourceFile) squid.search("Job.java");
+ assertThat(file.getCheckMessages().size(), is(0));
+ }
+
+}