import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
-
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.batch.CheckProject;
import org.sonar.api.batch.DependedUpon;
Object extensionToEvaluate;
if (extension instanceof SensorWrapper) {
extensionToEvaluate = ((SensorWrapper) extension).wrappedSensor();
+ } else if (extension instanceof PostJobWrapper) {
+ extensionToEvaluate = ((PostJobWrapper) extension).wrappedPostJob();
} else {
extensionToEvaluate = extension;
}
private <T> List<T> getFilteredExtensions(Class<T> type, @Nullable DefaultInputModule module, @Nullable ExtensionMatcher matcher) {
List<T> result = new ArrayList<>();
- for (Object extension : getExtensions(type)) {
+ List<Object> candidates = new ArrayList<>();
+ candidates.addAll(getExtensions(type));
+ if (org.sonar.api.batch.Sensor.class.equals(type)) {
+ candidates.addAll(getExtensions(Sensor.class));
+ }
+ if (org.sonar.api.batch.PostJob.class.equals(type)) {
+ candidates.addAll(getExtensions(PostJob.class));
+ }
+
+ for (Object extension : candidates) {
if (org.sonar.api.batch.Sensor.class.equals(type) && extension instanceof Sensor) {
extension = new SensorWrapper((Sensor) extension, sensorContext, sensorOptimizer);
}
+ if (org.sonar.api.batch.PostJob.class.equals(type) && extension instanceof PostJob) {
+ extension = new PostJobWrapper((PostJob) extension, postJobContext, postJobOptimizer);
+ }
if (shouldKeep(type, extension, module, matcher)) {
result.add((T) extension);
}
}
- if (org.sonar.api.batch.Sensor.class.equals(type)) {
- // Retrieve new Sensors and wrap then in SensorWrapper
- for (Sensor sensor : getExtensions(Sensor.class)) {
- org.sonar.api.batch.Sensor extension = new SensorWrapper(sensor, sensorContext, sensorOptimizer);
- if (shouldKeep(type, extension, module, matcher)) {
- result.add((T) extension);
- }
- }
- }
- if (org.sonar.api.batch.PostJob.class.equals(type)) {
- // Retrieve new PostJob and wrap then in PostJobWrapper
- for (PostJob postJob : getExtensions(PostJob.class)) {
- org.sonar.api.batch.PostJob extension = new PostJobWrapper(postJob, postJobContext, postJobOptimizer);
- if (shouldKeep(type, extension, module, matcher)) {
- result.add((T) extension);
- }
- }
- }
return result;
}
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.postjob.PostJobContext;
+import org.sonar.api.batch.postjob.PostJobDescriptor;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.resources.Project;
import org.sonar.core.platform.ComponentContainer;
}
@Test
- public void dependsUponPhase() {
- BatchExtension pre = new PreSensor();
- BatchExtension analyze = new GeneratesSomething("something");
- BatchExtension post = new PostSensor();
+ public void dependsUponPhaseForNewSensors() {
+ PreSensor pre = new PreSensor();
+ NormalSensor normal = new NormalSensor();
+ PostSensor post = new PostSensor();
- ScannerExtensionDictionnary selector = newSelector(analyze, post, pre);
- List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));
+ ScannerExtensionDictionnary selector = newSelector(normal, post, pre);
+ List<org.sonar.api.batch.sensor.Sensor> extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.sensor.Sensor.class, null, true, null));
+ assertThat(extensions).containsExactly(pre, normal, post);
- assertThat(extensions).hasSize(3);
- assertThat(extensions.get(0)).isEqualTo(pre);
- assertThat(extensions.get(1)).isEqualTo(analyze);
- assertThat(extensions.get(2)).isEqualTo(post);
+ List<Sensor> oldExtensions = Lists.newArrayList(selector.select(Sensor.class, null, true, null));
+ assertThat(oldExtensions).extracting("wrappedSensor").containsExactly(pre, normal, post);
+ }
+
+ @Test
+ public void dependsUponPhaseForNewPostJob() {
+ PrePostJob pre = new PrePostJob();
+ NormalPostJob normal = new NormalPostJob();
+
+ ScannerExtensionDictionnary selector = newSelector(normal, pre);
+ List<org.sonar.api.batch.postjob.PostJob> extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.postjob.PostJob.class, null, true, null));
+ assertThat(extensions).containsExactly(pre, normal);
+
+ List<PostJob> oldExtensions = Lists.newArrayList(selector.select(PostJob.class, null, true, null));
+ assertThat(oldExtensions).extracting("wrappedPostJob").containsExactly(pre, normal);
}
@Test
public void dependsUponInheritedPhase() {
- BatchExtension pre = new PreSensorSubclass();
- BatchExtension analyze = new GeneratesSomething("something");
- BatchExtension post = new PostSensorSubclass();
+ PreSensorSubclass pre = new PreSensorSubclass();
+ NormalSensor normal = new NormalSensor();
+ PostSensorSubclass post = new PostSensorSubclass();
- ScannerExtensionDictionnary selector = newSelector(analyze, post, pre);
- List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null));
+ ScannerExtensionDictionnary selector = newSelector(normal, post, pre);
+ List extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.sensor.Sensor.class, null, true, null));
- assertThat(extensions).containsExactly(pre, analyze, post);
+ assertThat(extensions).containsExactly(pre, normal, post);
}
@Test
}
}
+ class NormalSensor implements org.sonar.api.batch.sensor.Sensor {
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ }
+
+ @Override
+ public void execute(org.sonar.api.batch.sensor.SensorContext context) {
+ }
+
+ }
+
@Phase(name = Phase.Name.PRE)
- class PreSensor implements BatchExtension {
+ class PreSensor implements org.sonar.api.batch.sensor.Sensor {
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ }
+
+ @Override
+ public void execute(org.sonar.api.batch.sensor.SensorContext context) {
+ }
}
}
@Phase(name = Phase.Name.POST)
- class PostSensor implements BatchExtension {
+ class PostSensor implements org.sonar.api.batch.sensor.Sensor {
+
+ @Override
+ public void describe(SensorDescriptor descriptor) {
+ }
+
+ @Override
+ public void execute(org.sonar.api.batch.sensor.SensorContext context) {
+ }
}
public void executeOn(Project project, SensorContext context) {
}
}
+
+ class NormalPostJob implements org.sonar.api.batch.postjob.PostJob {
+
+ @Override
+ public void describe(PostJobDescriptor descriptor) {
+ }
+
+ @Override
+ public void execute(PostJobContext context) {
+ }
+
+ }
+
+ @Phase(name = Phase.Name.PRE)
+ class PrePostJob implements org.sonar.api.batch.postjob.PostJob {
+
+ @Override
+ public void describe(PostJobDescriptor descriptor) {
+ }
+
+ @Override
+ public void execute(PostJobContext context) {
+ }
+
+ }
+
}