]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3821 Restore ProjectClasspath in order to please PMD plugin
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 3 Jun 2015 14:19:16 +0000 (16:19 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 3 Jun 2015 14:19:39 +0000 (16:19 +0200)
sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java [new file with mode: 0644]

index 9ce20db1b1f7be2ef9464e95bb360278df0dda6a..1809108f70fff77e395f881d2f3fecc08dcd95fa 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.batch.scan;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.InstantiationStrategy;
+import org.sonar.api.batch.ProjectClasspath;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.fs.internal.FileMetadata;
 import org.sonar.api.batch.rule.CheckFactory;
@@ -141,6 +142,7 @@ public class ModuleScanContainer extends ComponentContainer {
       DefaultModuleFileSystem.class,
       ModuleFileSystemInitializer.class,
       ProjectFileSystemAdapter.class,
+      ProjectClasspath.class,
       QProfileVerifier.class,
 
       SensorOptimizer.class,
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java
new file mode 100644 (file)
index 0000000..e9f71cc
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.api.batch;
+
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.utils.SonarException;
+
+/**
+ * @since 2.2
+ * @deprecated since 4.5 this is some Java specific stuff that should by handled by SQ Java plugin
+ */
+@Deprecated
+public class ProjectClasspath implements BatchComponent {
+
+  private final ProjectDefinition def;
+  private final ProjectFileSystem projectFileSystem;
+  private List<File> elements;
+  private URLClassLoader classloader;
+
+  public ProjectClasspath(ProjectDefinition def, ProjectFileSystem projectFileSystem) {
+    this.def = def;
+    this.projectFileSystem = projectFileSystem;
+  }
+
+  public URLClassLoader getClassloader() {
+    if (classloader == null) {
+      classloader = createClassLoader();
+    }
+    return classloader;
+  }
+
+  /**
+   * bytecode directory + JARs (dependencies)
+   */
+  public List<File> getElements() {
+    if (elements == null) {
+      elements = createElements();
+    }
+    return elements;
+  }
+
+  protected URLClassLoader createClassLoader() {
+    try {
+      List<URL> urls = Lists.newArrayList();
+      for (File file : getElements()) {
+        urls.add(file.toURI().toURL());
+      }
+      return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
+
+    } catch (MalformedURLException e) {
+      throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e);
+    }
+  }
+
+  protected List<File> createElements() {
+    List<File> elements = Lists.newArrayList();
+    for (String path : def.getBinaries()) {
+      elements.add(projectFileSystem.resolvePath(path));
+    }
+    for (String path : def.getLibraries()) {
+      elements.add(projectFileSystem.resolvePath(path));
+    }
+    return elements;
+  }
+}