aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionInstaller.java11
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java12
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/Metrics.java2
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java24
6 files changed, 57 insertions, 12 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionInstaller.java
index f176e1a81fc..cee17e434d7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionInstaller.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionInstaller.java
@@ -24,6 +24,8 @@ import org.sonar.api.ExtensionProvider;
import org.sonar.api.Plugin;
import org.sonar.api.batch.CoverageExtension;
import org.sonar.api.batch.InstantiationStrategy;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import java.util.List;
@@ -45,6 +47,15 @@ public final class BatchExtensionInstaller implements BatchComponent {
}
}
installExtensionProviders(module);
+ installMetrics(module);
+ }
+
+ private void installMetrics(Module module) {
+ for (Metrics metrics : module.getComponents(Metrics.class)) {
+ for (Metric metric : metrics.getMetrics()) {
+ module.addComponent(metric.getKey(), metric);
+ }
+ }
}
void installExtensionProviders(Module module) {
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
index c69b34c6b82..e78c7251f49 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
@@ -20,6 +20,9 @@
package org.sonar.batch.bootstrap;
import org.sonar.api.Plugins;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.ServerHttpClient;
import org.sonar.batch.DefaultResourceCreationLock;
@@ -59,6 +62,7 @@ public class BatchModule extends Module {
addComponent(PastSnapshotFinderByVersion.class);
addComponent(PastMeasuresLoader.class);
addComponent(PastSnapshotFinder.class);
+ addCoreMetrics();
addBatchExtensions();
}
@@ -67,6 +71,12 @@ public class BatchModule extends Module {
installer.install(this);
}
+ void addCoreMetrics() {
+ for (Metric metric : CoreMetrics.getMetrics()) {
+ addComponent(metric.getKey(), metric);
+ }
+ }
+
@Override
protected void doStart() {
ProjectTree projectTree = getComponent(ProjectTree.class);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
index 1ea854e61de..ae0c69aec40 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
@@ -104,16 +104,6 @@ public class ProjectModule extends Module {
for (Class clazz : Phases.getPhaseClasses()) {
addComponent(clazz);
}
-
- // TODO move metrics to BatchComponents
- for (Metric metric : CoreMetrics.getMetrics()) {
- addComponent(metric.getKey(), metric);
- }
- for (Metrics metricRepo : getComponents(Metrics.class)) {
- for (Metric metric : metricRepo.getMetrics()) {
- addComponent(metric.getKey(), metric);
- }
- }
}
private void addProjectPluginExtensions() {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java
index 806323bbdfb..dddea1ccc0b 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/BatchExtensionDictionnary.java
@@ -24,6 +24,7 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
@@ -86,7 +87,16 @@ public class BatchExtensionDictionnary {
}
private List<BatchExtension> getExtensions() {
- return picoContainer.getComponents(BatchExtension.class);
+ List<BatchExtension> extensions = Lists.newArrayList();
+ completeBatchExtensions(picoContainer, extensions);
+ return extensions;
+ }
+
+ private void completeBatchExtensions(PicoContainer picoContainer, List<BatchExtension> extensions) {
+ if (picoContainer!=null) {
+ extensions.addAll(picoContainer.getComponents(BatchExtension.class));
+ completeBatchExtensions(picoContainer.getParent(), extensions);
+ }
}
private <T> List<T> getFilteredExtensions(Class<T> type, Project project) {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metrics.java
index 6a29a569a51..f0217638aed 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metrics.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metrics.java
@@ -21,12 +21,14 @@ package org.sonar.api.measures;
import org.sonar.api.BatchExtension;
import org.sonar.api.ServerExtension;
+import org.sonar.api.batch.InstantiationStrategy;
import java.util.List;
/**
* @since 1.10
*/
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public interface Metrics extends BatchExtension, ServerExtension {
List<Metric> getMetrics();
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java
index 210da5a4c59..37e6e644c49 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java
@@ -21,8 +21,13 @@ package org.sonar.api.batch;
import com.google.common.collect.Lists;
import org.junit.Test;
+import org.picocontainer.Characteristics;
+import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.PicoContainer;
import org.picocontainer.containers.TransientPicoContainer;
import org.sonar.api.BatchExtension;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import java.util.ArrayList;
@@ -33,6 +38,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
import static org.mockito.Mockito.mock;
public class BatchExtensionDictionnaryTest {
@@ -47,7 +53,7 @@ public class BatchExtensionDictionnaryTest {
}
@Test
- public void getFilteredExtensions() {
+ public void testGetFilteredExtensions() {
Sensor sensor1 = new FakeSensor(), sensor2 = new FakeSensor();
Decorator decorator = mock(Decorator.class);
@@ -60,6 +66,22 @@ public class BatchExtensionDictionnaryTest {
}
@Test
+ public void shouldSearchInParentContainers() {
+ TransientPicoContainer grandParent = new TransientPicoContainer();
+ grandParent.as(Characteristics.CACHE).addComponent("ncloc", CoreMetrics.NCLOC);
+
+ DefaultPicoContainer parent = (DefaultPicoContainer)grandParent.makeChildContainer();
+ parent.as(Characteristics.CACHE).addComponent("coverage", CoreMetrics.COVERAGE);
+
+ DefaultPicoContainer child = (DefaultPicoContainer)parent.makeChildContainer();
+ child.as(Characteristics.CACHE).addComponent("complexity", CoreMetrics.COMPLEXITY);
+
+ BatchExtensionDictionnary dictionnary = new BatchExtensionDictionnary(child);
+ assertThat(dictionnary.select(Metric.class).size(), is(3));
+ assertThat(dictionnary.select(Metric.class), hasItems(CoreMetrics.NCLOC, CoreMetrics.COVERAGE, CoreMetrics.COMPLEXITY));
+ }
+
+ @Test
public void sortExtensionsByDependency() {
BatchExtension a = new MethodDependentOf(null);
BatchExtension b = new MethodDependentOf(a);