Skip to content

Commit

Permalink
refact: unsafe synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Grousset committed Sep 7, 2024
1 parent 5e4b740 commit 71b0de1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
Expand All @@ -38,15 +34,15 @@
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

public class DartSensor implements Sensor {

private static final Logger LOGGER = Loggers.get(DartSensor.class);
private static final int EXECUTOR_TIMEOUT = 10000;

@Override
public void describe(SensorDescriptor sensorDescriptor) {
public void describe(@Nonnull SensorDescriptor sensorDescriptor) {
sensorDescriptor
.onlyOnLanguage(Dart.KEY)
.name("Dart sensor")
Expand All @@ -63,46 +59,32 @@ public void execute(SensorContext sensorContext) {
FilePredicate dartAndTest = sensorContext.fileSystem().predicates().and(hasDart, isTest);
final Charset charset = sensorContext.fileSystem().encoding();

final ExecutorService executorService = Executors.newWorkStealingPool();

for(InputFile inf : sensorContext.fileSystem().inputFiles(dartAndMain)){
for (InputFile inf : sensorContext.fileSystem().inputFiles(dartAndMain)) {

executorService.execute(() -> {
// Visit source files
try {
final AntlrContext antlrContext = AntlrContext.fromInputFile(inf, charset);
ParseTreeItemVisitor visitor = new CustomTreeVisitor(new HighlighterVisitor(),
new SourceLinesVisitor(), new CyclomaticComplexityVisitor());
visitor.fillContext(sensorContext, antlrContext);
} catch (IOException e) {
LOGGER.warn("Unexpected error while analyzing file " + inf.filename(), e);
}
});
// Visit source files
try {
final AntlrContext antlrContext = AntlrContext.fromInputFile(inf, charset);
ParseTreeItemVisitor visitor = new CustomTreeVisitor(new HighlighterVisitor(),
new SourceLinesVisitor(), new CyclomaticComplexityVisitor());
visitor.fillContext(sensorContext, antlrContext);
} catch (IOException e) {
LOGGER.warn("Unexpected error while analyzing file " + inf.filename(), e);
}

}

for(InputFile inf : sensorContext.fileSystem().inputFiles(dartAndTest)){
for (InputFile inf : sensorContext.fileSystem().inputFiles(dartAndTest)) {

executorService.execute(() -> {
// Visit test files (for syntax highlighting only)
try {
final AntlrContext antlrContext = AntlrContext.fromInputFile(inf, charset);
ParseTreeItemVisitor visitor = new CustomTreeVisitor(new HighlighterVisitor());
visitor.fillContext(sensorContext, antlrContext);
} catch (IOException e) {
LOGGER.warn("Unexpected error while analyzing file " + inf.filename(), e);
}
});
// Visit test files (for syntax highlighting only)
try {
final AntlrContext antlrContext = AntlrContext.fromInputFile(inf, charset);
ParseTreeItemVisitor visitor = new CustomTreeVisitor(new HighlighterVisitor());
visitor.fillContext(sensorContext, antlrContext);
} catch (IOException e) {
LOGGER.warn("Unexpected error while analyzing file " + inf.filename(), e);
}

}

try {
executorService.shutdown();
executorService.awaitTermination(EXECUTOR_TIMEOUT, TimeUnit.SECONDS);
executorService.shutdownNow();
} catch (final InterruptedException e) {
LOGGER.warn("Unexpected error while running waiting for executor service to finish", e);
Thread.currentThread().interrupt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public void apply(ParseTree tree) {
}

if (Dart2Parser.ConditionalExpressionContext.class.equals(classz)) {
Dart2Parser.ConditionalExpressionContext conditionalExpressionContext = (Dart2Parser.ConditionalExpressionContext) tree;

int countOr = ( tree.getText().split("\\|\\|", -1).length ) - 1;
int countAnd = ( tree.getText().split("&&", -1).length ) - 1;
complexity = complexity + countOr + countAnd;
Expand All @@ -63,7 +61,7 @@ public void apply(ParseTree tree) {
@Override
public void fillContext(SensorContext context, AntlrContext antlrContext) {
final InputFile file = antlrContext.getFile();
synchronized (context) {
synchronized (CyclomaticComplexityVisitor.class) {
try {
context.<Integer>newMeasure().on(file).forMetric(CoreMetrics.COMPLEXITY).withValue(complexity)
.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void fillContext(final SensorContext context, final AntlrContext antrlFil

}
}
synchronized (context) {
synchronized (HighlighterVisitor.class) {

try {
newHighlightning.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void fillContext(SensorContext context, AntlrContext antlrContext) {
}
}

synchronized (context) {
synchronized (SourceLinesVisitor.class) {

try {
context.<Integer>newMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(locs).save();
Expand Down

0 comments on commit 71b0de1

Please sign in to comment.