001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.plugins.squid.decorators; 021 022 import org.sonar.api.batch.Decorator; 023 import org.sonar.api.batch.DecoratorContext; 024 import org.sonar.api.batch.DependedUpon; 025 import org.sonar.api.batch.DependsUpon; 026 import org.sonar.api.measures.*; 027 import org.sonar.api.resources.Java; 028 import org.sonar.api.resources.Project; 029 import org.sonar.api.resources.Resource; 030 import org.sonar.api.resources.Scopes; 031 import org.sonar.java.api.JavaMethod; 032 033 /** 034 * @since 2.6 035 */ 036 public final class FunctionComplexityDistributionBuilder implements Decorator { 037 038 private static final Number[] LIMITS = { 1, 2, 4, 6, 8, 10, 12 }; 039 040 @DependsUpon 041 public Metric dependOnComplexity() { 042 return CoreMetrics.COMPLEXITY; 043 } 044 045 @DependedUpon 046 public Metric generatesFunctionComplexityDistribution() { 047 return CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION; 048 } 049 050 public void decorate(Resource resource, DecoratorContext context) { 051 if (shouldExecuteOn(resource, context)) { 052 RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, LIMITS); 053 for (DecoratorContext childContext : context.getChildren()) { 054 if (childContext.getResource() instanceof JavaMethod) { 055 JavaMethod javaMethod = (JavaMethod) childContext.getResource(); 056 Measure complexity = childContext.getMeasure(CoreMetrics.COMPLEXITY); 057 if (!javaMethod.isAccessor() && complexity != null) { 058 builder.add(complexity.getValue()); 059 } 060 } 061 } 062 Measure measure = builder.build(true); 063 measure.setPersistenceMode(PersistenceMode.MEMORY); 064 context.saveMeasure(measure); 065 } 066 } 067 068 boolean shouldExecuteOn(Resource resource, DecoratorContext context) { 069 return Scopes.isProgramUnit(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null; 070 } 071 072 public boolean shouldExecuteOnProject(Project project) { 073 return Java.KEY.equals(project.getLanguageKey()); 074 } 075 }