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