From 794dba0f559ec67d56a62d102161cf34b4374ab2 Mon Sep 17 00:00:00 2001 From: Shaokai Lin Date: Tue, 25 Jul 2023 19:17:36 +0200 Subject: [PATCH] Fix error when there is no main reactor --- core/src/main/java/org/lflang/ast/ASTUtils.java | 6 +++--- core/src/main/java/org/lflang/generator/LFGenerator.java | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/lflang/ast/ASTUtils.java b/core/src/main/java/org/lflang/ast/ASTUtils.java index 93c689b810..f89922f59b 100644 --- a/core/src/main/java/org/lflang/ast/ASTUtils.java +++ b/core/src/main/java/org/lflang/ast/ASTUtils.java @@ -35,6 +35,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Predicate; import java.util.regex.Matcher; @@ -154,14 +155,13 @@ public static List getAllReactors(Resource resource) { * @param resource the resource to extract reactors from * @return An iterable over all reactors found in the resource */ - public static Reactor getMainReactor(Resource resource) { + public static Optional getMainReactor(Resource resource) { return StreamSupport.stream( IteratorExtensions.toIterable(resource.getAllContents()).spliterator(), false) .filter(Reactor.class::isInstance) .map(Reactor.class::cast) .filter(it -> it.isMain()) - .findFirst() - .get(); + .findFirst(); } /** diff --git a/core/src/main/java/org/lflang/generator/LFGenerator.java b/core/src/main/java/org/lflang/generator/LFGenerator.java index 410051f835..84a13f6fc8 100644 --- a/core/src/main/java/org/lflang/generator/LFGenerator.java +++ b/core/src/main/java/org/lflang/generator/LFGenerator.java @@ -8,6 +8,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.xtext.generator.AbstractGenerator; @@ -158,7 +159,9 @@ protected void cleanIfNeeded(LFGeneratorContext context) { * connections, etc.). */ private void runVerifierIfPropertiesDetected(Resource resource, LFGeneratorContext lfContext) { - Reactor main = ASTUtils.getMainReactor(resource); + Optional mainOpt = ASTUtils.getMainReactor(resource); + if (mainOpt.isEmpty()) return; + Reactor main = mainOpt.get(); final MessageReporter messageReporter = lfContext.getErrorReporter(); List properties = AttributeUtils.getAttributes(main).stream()