001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 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.core.sensors; 021 022 import java.util.Arrays; 023 import java.util.List; 024 025 import org.sonar.api.batch.Decorator; 026 import org.sonar.api.batch.DecoratorContext; 027 import org.sonar.api.batch.DependedUpon; 028 import org.sonar.api.batch.DependsUpon; 029 import org.sonar.api.measures.CoreMetrics; 030 import org.sonar.api.measures.Measure; 031 import org.sonar.api.measures.MeasureUtils; 032 import org.sonar.api.measures.Metric; 033 import org.sonar.api.resources.Project; 034 import org.sonar.api.resources.Resource; 035 036 public class CommentDensityDecorator implements Decorator { 037 038 @DependsUpon 039 public List<Metric> dependsUponMetrics() { 040 return Arrays.asList(CoreMetrics.NCLOC, CoreMetrics.COMMENT_LINES, CoreMetrics.PUBLIC_API, CoreMetrics.PUBLIC_UNDOCUMENTED_API); 041 } 042 043 @DependedUpon 044 public List<Metric> generatesMetrics() { 045 return Arrays.asList(CoreMetrics.COMMENT_LINES_DENSITY, CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY); 046 } 047 048 public boolean shouldExecuteOnProject(Project project) { 049 return true; 050 } 051 052 public void decorate(Resource resource, DecoratorContext context) { 053 saveCommentsDensity(context); 054 savePublicApiDensity(context); 055 } 056 057 private void saveCommentsDensity(DecoratorContext context) { 058 if (context.getMeasure(CoreMetrics.COMMENT_LINES_DENSITY) != null) { 059 return; 060 } 061 062 Measure ncloc = context.getMeasure(CoreMetrics.NCLOC); 063 Measure comments = context.getMeasure(CoreMetrics.COMMENT_LINES); 064 if (MeasureUtils.hasValue(ncloc) && MeasureUtils.hasValue(comments) && (comments.getValue() + ncloc.getValue()) > 0) { 065 double val = 100.0 * (comments.getValue() / (comments.getValue() + ncloc.getValue())); 066 context.saveMeasure(new Measure(CoreMetrics.COMMENT_LINES_DENSITY, val)); 067 } 068 } 069 070 private void savePublicApiDensity(DecoratorContext context) { 071 if (context.getMeasure(CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY) != null) { 072 return; 073 } 074 075 Measure publicApi = context.getMeasure(CoreMetrics.PUBLIC_API); 076 Measure publicUndocApi = context.getMeasure(CoreMetrics.PUBLIC_UNDOCUMENTED_API); 077 078 if (MeasureUtils.hasValue(publicApi) && MeasureUtils.hasValue(publicUndocApi) && publicApi.getValue() > 0) { 079 double documentedAPI = publicApi.getValue() - publicUndocApi.getValue(); 080 Double value = 100.0 * (documentedAPI / publicApi.getValue()); 081 context.saveMeasure(new Measure(CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY, value)); 082 } 083 } 084 085 @Override 086 public String toString() { 087 return getClass().getSimpleName(); 088 } 089 }