2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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 License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.design.batch;
22 import org.sonar.api.batch.Decorator;
23 import org.sonar.api.batch.DecoratorContext;
24 import org.sonar.api.batch.DependedUpon;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.api.measures.Measure;
27 import org.sonar.api.measures.MeasureUtils;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.resources.Project;
30 import org.sonar.api.resources.Resource;
31 import org.sonar.api.resources.ResourceUtils;
32 import org.sonar.api.resources.Scopes;
34 import java.util.Collection;
35 import java.util.List;
37 public class SuspectLcom4DensityDecorator implements Decorator {
39 public boolean shouldExecuteOnProject(Project project) {
44 public final Metric generatesMetric() {
45 return CoreMetrics.SUSPECT_LCOM4_DENSITY;
48 public void decorate(Resource resource, DecoratorContext context) {
49 if (ResourceUtils.isFile(resource)) {
51 } else if (Scopes.isDirectory(resource)) {
52 decorateDirectory(context);
54 } else if (Scopes.isProject(resource)) {
55 decorateProject(context);
59 private void decorateProject(DecoratorContext context) {
63 List<DecoratorContext> children = context.getChildren();
64 boolean hasLcom4=false;
65 for (DecoratorContext child : children) {
66 int files = MeasureUtils.getValue(child.getMeasure(CoreMetrics.FILES), 0.0).intValue();
68 Measure childSuspectDensity = child.getMeasure(CoreMetrics.SUSPECT_LCOM4_DENSITY);
69 if (childSuspectDensity!=null && childSuspectDensity.getValue()!=null) {
71 total += childSuspectDensity.getValue() * files;
75 if (hasLcom4 && totalFiles > 0) {
76 context.saveMeasure(CoreMetrics.SUSPECT_LCOM4_DENSITY, (total / totalFiles));
80 private void decorateDirectory(DecoratorContext context) {
81 Collection<Measure> fileLcoms = context.getChildrenMeasures(CoreMetrics.LCOM4);
82 double files = MeasureUtils.getValue(context.getMeasure(CoreMetrics.FILES), 0.0);
83 if (!fileLcoms.isEmpty() && files>0.0) {
84 double suspectFiles = 0.0;
86 // directory children are files
87 for (Measure fileLcom : fileLcoms) {
88 if (MeasureUtils.getValue(fileLcom, 0.0) > 1.0) {
92 double density = (suspectFiles / files) * 100.0;
93 context.saveMeasure(CoreMetrics.SUSPECT_LCOM4_DENSITY, density);