Skip to content

Commit

Permalink
fix: 🐛 Catch Exception during Pattern load (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Jun 9, 2023
1 parent ca9b37e commit ae517c0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.keksdose.spoon.code_solver.analyzer.spoon;

import com.google.common.flogger.FluentLogger;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -8,13 +9,15 @@
import spoon.support.compiler.VirtualFile;

public class TemplateHelper {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

private TemplateHelper() {}

public static CtType<?> fromResource(String resource) {
ClassLoader classLoader = TemplateHelper.class.getClassLoader();
URL resourceUrl = classLoader.getResource(resource);
if (resourceUrl == null) {
logger.atSevere().log("Resource not found: %s", resource);
throw new IllegalArgumentException("Resource not found: " + resource);
}
try {
Expand All @@ -25,6 +28,7 @@ public static CtType<?> fromResource(String resource) {
var model = launcher.buildModel();
return model.getAllTypes().iterator().next();
} catch (Exception e) {
logger.atSevere().withCause(e).log("Error while loading resource %s", resource);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static xyz.keksdose.spoon.code_solver.history.MarkdownString.fromMarkdown;

import com.google.common.flogger.FluentLogger;
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult;
import io.github.martinwitt.laughing_train.domain.value.Position;
import io.github.martinwitt.laughing_train.domain.value.RuleId;
Expand All @@ -21,6 +22,7 @@
import xyz.keksdose.spoon.code_solver.transformations.BadSmell;

public class UnnecessaryToStringCall implements AbstractSpoonRuleAnalyzer {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

private static final List<TemplateMatcher> templates = createPattern();
private static final BadSmell UNNECESSARY_TO_STRING_CALL = new BadSmell() {
Expand Down Expand Up @@ -72,17 +74,22 @@ private Position createPosition(CtElement ctElement) {
}

private static List<TemplateMatcher> createPattern() {
List<TemplateMatcher> templates = new ArrayList<>();
var templateType = TemplateHelper.fromResource("patternDB/UnnecessaryToStringCall");
for (CtMethod<?> method : templateType.getMethods()) {
if (method.getSimpleName().startsWith("matcher")) {
var root = method.getElements(new TypeFilter<>(CtReturn.class))
.get(0)
.getReturnedExpression();
templates.add(new TemplateMatcher(root));
try {
List<TemplateMatcher> templates = new ArrayList<>();
var templateType = TemplateHelper.fromResource("patternDB/UnnecessaryToStringCall");
for (CtMethod<?> method : templateType.getMethods()) {
if (method.getSimpleName().startsWith("matcher")) {
var root = method.getElements(new TypeFilter<>(CtReturn.class))
.get(0)
.getReturnedExpression();
templates.add(new TemplateMatcher(root));
}
}
return templates;
} catch (Exception e) {
logger.atSevere().withCause(e).log("Could not load patternDB/UnnecessaryToStringCall");
}
return templates;
return new ArrayList<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ private void mineRandomRepo() {
var checkoutResult = checkoutProject(project);
if (checkoutResult instanceof ProjectResult.Error) {
logger.atWarning().log("Failed to checkout project %s", project);
registry.counter("mining.checkout.error").increment();
mineRandomRepo();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import io.github.martinwitt.laughing_train.data.SpoonPatternAnalyzerResult;
import io.smallrye.health.api.AsyncHealthCheck;
import io.smallrye.mutiny.Uni;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Readiness;
import xyz.keksdose.spoon.code_solver.analyzer.spoon.SpoonAnalyzer;
Expand All @@ -21,17 +17,14 @@ public class SpoonPatternAnalyzer {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

final Config config;
final ThreadPoolManager threadPoolManager;
final ProjectConfigService projectConfigService;
final AnalyzerResultPersistenceService analyzerResultPersistenceService;

SpoonPatternAnalyzer(
Config config,
ThreadPoolManager threadPoolManager,
ProjectConfigService projectConfigService,
AnalyzerResultPersistenceService analyzerResultPersistenceService) {
this.config = config;
this.threadPoolManager = threadPoolManager;
this.projectConfigService = projectConfigService;
this.analyzerResultPersistenceService = analyzerResultPersistenceService;
}
Expand All @@ -52,26 +45,6 @@ public SpoonPatternAnalyzerResult analyze(AnalyzerRequest request) {
}
}

@ApplicationScoped
static class ThreadPoolManager {
@SuppressWarnings("NullAway")
ExecutorService service;

@PostConstruct
void setup() {
service = Executors.newFixedThreadPool(1);
}

@PreDestroy
void close() {
service.shutdown();
}

public ExecutorService getService() {
return service;
}
}

@Readiness
@ApplicationScoped
private static class HealthCheck implements AsyncHealthCheck {
Expand Down

0 comments on commit ae517c0

Please sign in to comment.