|
51 | 51 | import java.util.List; |
52 | 52 | import java.util.Map; |
53 | 53 | import java.util.Set; |
| 54 | +import java.util.function.BiPredicate; |
54 | 55 | import java.util.stream.Collectors; |
55 | 56 |
|
56 | 57 | /** |
@@ -209,6 +210,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment |
209 | 210 | noLegacyChangesByPackage, |
210 | 211 | flamingockAnnotation |
211 | 212 | ); |
| 213 | + |
| 214 | + validateAllChangesAreMappedToStages(legacyChanges, noLegacyChangesByPackage, pipeline, flamingockAnnotation.strictStageMapping()); |
| 215 | + |
212 | 216 | Serializer serializer = new Serializer(processingEnv, logger); |
213 | 217 | String setup = flamingockAnnotation.setup().toString(); |
214 | 218 | String configFile = flamingockAnnotation.configFile(); |
@@ -721,4 +725,92 @@ private void validateConfiguration(EnableFlamingock pipelineAnnotation, boolean |
721 | 725 | } |
722 | 726 | } |
723 | 727 |
|
| 728 | + /** |
| 729 | + * Validates that code-based preview changes (non-legacy) are attached to some stage. |
| 730 | + * It checks package names from the changes map against the pipeline stages' sourcesPackage |
| 731 | + * (including the SYSTEM stage). If any stage does not represent any package, a RuntimeException is thrown. |
| 732 | + * |
| 733 | + * @param legacyCodedChanges legacy code changes (kept for completeness; not used for package mapping) |
| 734 | + * @param noLegacyCodedChangesByPackage non-legacy changes grouped by source package |
| 735 | + * @param pipeline built preview pipeline |
| 736 | + * @param strictStageMapping if true, unmapped changes will throw an exception; if false, will log a warning |
| 737 | + */ |
| 738 | + private void validateAllChangesAreMappedToStages(List<CodePreviewChange> legacyCodedChanges, |
| 739 | + Map<String, List<CodePreviewChange>> noLegacyCodedChangesByPackage, |
| 740 | + PreviewPipeline pipeline, |
| 741 | + Boolean strictStageMapping) { |
| 742 | + if (noLegacyCodedChangesByPackage == null || noLegacyCodedChangesByPackage.isEmpty()) { |
| 743 | + return; |
| 744 | + } |
| 745 | + |
| 746 | + // Helper to test if a change package is covered by a stage package (exact or parent) |
| 747 | + BiPredicate<String, String> covers = (stagePkg, changePkg) -> { |
| 748 | + if (stagePkg == null || changePkg == null) return false; |
| 749 | + return changePkg.equals(stagePkg) || changePkg.startsWith(stagePkg + "."); |
| 750 | + }; |
| 751 | + |
| 752 | + List<String> unmapped = new ArrayList<>(); |
| 753 | + |
| 754 | + for (String pkg : noLegacyCodedChangesByPackage.keySet()) { |
| 755 | + if (pkg == null) { |
| 756 | + continue; |
| 757 | + } |
| 758 | + boolean matched = false; |
| 759 | + |
| 760 | + // Check system stage |
| 761 | + PreviewStage system = pipeline.getSystemStage(); |
| 762 | + if (system != null) { |
| 763 | + String sysPkg = system.getSourcesPackage(); |
| 764 | + if (covers.test(sysPkg, pkg)) { |
| 765 | + matched = true; |
| 766 | + } else if (system.getTasks() != null) { |
| 767 | + for (io.flamingock.internal.common.core.preview.AbstractPreviewTask task : system.getTasks()) { |
| 768 | + if (task instanceof CodePreviewChange) { |
| 769 | + String taskPkg = ((CodePreviewChange) task).getSourcePackage(); |
| 770 | + if (covers.test(taskPkg, pkg)) { |
| 771 | + matched = true; |
| 772 | + break; |
| 773 | + } |
| 774 | + } |
| 775 | + } |
| 776 | + } |
| 777 | + } |
| 778 | + |
| 779 | + // Check regular stages |
| 780 | + if (!matched && pipeline.getStages() != null) { |
| 781 | + for (PreviewStage stage : pipeline.getStages()) { |
| 782 | + String stagePkg = stage.getSourcesPackage(); |
| 783 | + if (covers.test(stagePkg, pkg)) { |
| 784 | + matched = true; |
| 785 | + break; |
| 786 | + } |
| 787 | + if (stage.getTasks() != null) { |
| 788 | + for (io.flamingock.internal.common.core.preview.AbstractPreviewTask task : stage.getTasks()) { |
| 789 | + if (task instanceof CodePreviewChange) { |
| 790 | + String taskPkg = ((CodePreviewChange) task).getSourcePackage(); |
| 791 | + if (covers.test(taskPkg, pkg)) { |
| 792 | + matched = true; |
| 793 | + break; |
| 794 | + } |
| 795 | + } |
| 796 | + } |
| 797 | + if (matched) break; |
| 798 | + } |
| 799 | + } |
| 800 | + } |
| 801 | + |
| 802 | + if (!matched) { |
| 803 | + unmapped.add(pkg); |
| 804 | + } |
| 805 | + } |
| 806 | + |
| 807 | + if (!unmapped.isEmpty()) { |
| 808 | + String message = "Changes are not mapped to any stage: " + String.join(", ", unmapped); |
| 809 | + if (Boolean.TRUE.equals(strictStageMapping)) { |
| 810 | + throw new RuntimeException(message); |
| 811 | + } else { |
| 812 | + logger.warn(message); |
| 813 | + } |
| 814 | + } |
| 815 | + } |
724 | 816 | } |
0 commit comments