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;
021
022 import org.sonar.api.CoreProperties;
023 import org.sonar.api.batch.*;
024 import org.sonar.api.checks.AnnotationCheckFactory;
025 import org.sonar.api.checks.NoSonarFilter;
026 import org.sonar.api.profiles.RulesProfile;
027 import org.sonar.api.resources.InputFile;
028 import org.sonar.api.resources.Java;
029 import org.sonar.api.resources.JavaFile;
030 import org.sonar.api.resources.Project;
031 import org.sonar.java.api.JavaUtils;
032
033 import java.io.File;
034 import java.nio.charset.Charset;
035 import java.util.Collection;
036 import java.util.Collections;
037 import java.util.List;
038
039 @Phase(name = Phase.Name.PRE)
040 @DependsUpon(JavaUtils.BARRIER_BEFORE_SQUID)
041 @DependedUpon(value = JavaUtils.BARRIER_AFTER_SQUID)
042 public class SquidSensor implements Sensor {
043
044 private NoSonarFilter noSonarFilter;
045 private RulesProfile profile;
046 private ProjectClasspath projectClasspath;
047 private ResourceCreationLock lock;
048
049 public SquidSensor(RulesProfile profile, NoSonarFilter noSonarFilter, ProjectClasspath projectClasspath, ResourceCreationLock lock) {
050 this.noSonarFilter = noSonarFilter;
051 this.profile = profile;
052 this.projectClasspath = projectClasspath;
053 this.lock = lock;
054 }
055
056 public boolean shouldExecuteOnProject(Project project) {
057 return Java.KEY.equals(project.getLanguageKey());
058 }
059
060 public void analyse(Project project, SensorContext context) {
061 analyzeMainSources(project, context);
062 browseTestSources(project, context);
063 lock.lock();
064 }
065
066 private void analyzeMainSources(Project project, SensorContext context) {
067 boolean analyzePropertyAccessors = project.getConfiguration().getBoolean(SquidPluginProperties.SQUID_ANALYSE_ACCESSORS_PROPERTY,
068 SquidPluginProperties.SQUID_ANALYSE_ACCESSORS_DEFAULT_VALUE);
069 String fieldNamesToExcludeFromLcom4Computation = project.getConfiguration().getString(
070 SquidPluginProperties.FIELDS_TO_EXCLUDE_FROM_LCOM4_COMPUTATION,
071 SquidPluginProperties.FIELDS_TO_EXCLUDE_FROM_LCOM4_COMPUTATION_DEFAULT_VALUE);
072 Charset charset = project.getFileSystem().getSourceCharset();
073
074 AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, SquidConstants.REPOSITORY_KEY, SquidRuleRepository.getCheckClasses());
075
076 SquidExecutor squidExecutor = new SquidExecutor(analyzePropertyAccessors, fieldNamesToExcludeFromLcom4Computation, factory, charset);
077 squidExecutor.scan(getMainSourceFiles(project), getBytecodeFiles(project));
078 squidExecutor.save(project, context, noSonarFilter);
079 squidExecutor.flush();
080 }
081
082 private void browseTestSources(Project project, SensorContext context) {
083 for (InputFile testFile : project.getFileSystem().testFiles(Java.KEY)) {
084 context.index(JavaFile.fromRelativePath(testFile.getRelativePath(), true));
085 }
086 }
087
088 private List<InputFile> getMainSourceFiles(Project project) {
089 return project.getFileSystem().mainFiles(Java.KEY);
090 }
091
092 /**
093 * Visibility has been relaxed to make the code testable.
094 *
095 * @return collection of jar-files and directories with classes for analysis
096 */
097 protected Collection<File> getBytecodeFiles(Project project) {
098 if (project.getConfiguration().getBoolean(CoreProperties.DESIGN_SKIP_DESIGN_PROPERTY, CoreProperties.DESIGN_SKIP_DESIGN_DEFAULT_VALUE)) {
099 return Collections.emptyList();
100 }
101 return projectClasspath.getElements();
102 }
103
104 @Override
105 public String toString() {
106 return getClass().getSimpleName();
107 }
108 }