From 0dd97e34723d40dd515af2b9fccde2b0fb218f8f Mon Sep 17 00:00:00 2001 From: SimonCockx <47859223+SimonCockx@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:41:41 +0100 Subject: [PATCH] Workaround for switch issue (#896) --- .../maven/RosettaStandaloneBuilder.java | 128 +++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/rosetta-maven-plugin/src/main/java/com/regnosys/rosetta/maven/RosettaStandaloneBuilder.java b/rosetta-maven-plugin/src/main/java/com/regnosys/rosetta/maven/RosettaStandaloneBuilder.java index e09492a45..76ad71725 100644 --- a/rosetta-maven-plugin/src/main/java/com/regnosys/rosetta/maven/RosettaStandaloneBuilder.java +++ b/rosetta-maven-plugin/src/main/java/com/regnosys/rosetta/maven/RosettaStandaloneBuilder.java @@ -19,18 +19,35 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; +import javax.inject.Inject; +import javax.inject.Provider; + +import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.xtext.EcoreUtil2; import org.eclipse.xtext.builder.standalone.LanguageAccess; import org.eclipse.xtext.builder.standalone.StandaloneBuilder; +import org.eclipse.xtext.common.types.access.impl.IndexedJvmTypeAccess; import org.eclipse.xtext.generator.GeneratorContext; import org.eclipse.xtext.generator.GeneratorDelegate; import org.eclipse.xtext.generator.JavaIoFileSystemAccess; +import org.eclipse.xtext.resource.XtextResourceSet; +import org.eclipse.xtext.resource.clustering.DisabledClusteringPolicy; +import org.eclipse.xtext.resource.clustering.DynamicResourceClusteringPolicy; +import org.eclipse.xtext.resource.clustering.IResourceClusteringPolicy; +import org.eclipse.xtext.resource.impl.ResourceDescriptionsData; import org.eclipse.xtext.util.CancelIndicator; +import org.eclipse.xtext.xbase.lib.IterableExtensions; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import com.regnosys.rosetta.generator.RosettaGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +55,11 @@ public class RosettaStandaloneBuilder extends StandaloneBuilder { private static final Logger LOG = LoggerFactory.getLogger(RosettaStandaloneBuilder.class); + @Inject + private Provider resourceSetProvider; + @Inject + private IndexedJvmTypeAccess jvmTypeAccess; + private LanguageAccess rosettaLanguageAccess = null; // TODO: patch Xtext to make `languages` available @SuppressWarnings("unchecked") @@ -78,7 +100,111 @@ private RosettaGenerator getRosettaGenerator() { @Override public boolean launch() { needsBeforeAllCall = true; - boolean success = super.launch(); + + // START COPY PASTE OF ORIGINAL IMPLEMENTATION + boolean needsJava = IterableExtensions.exists(getLanguages().values(), l -> l.isLinksAgainstJava()); + if (getBaseDir() == null) { + setBaseDir(System.getProperty("user.dir")); + LOG.warn("Property baseDir not set. Using '" + getBaseDir() + "'"); + } + if (needsJava) { + LOG.info("Using common types."); + } + XtextResourceSet resourceSet = resourceSetProvider.get(); + if (getEncoding() != null) { + forceDebugLog("Setting encoding."); + fileEncodingSetup(getLanguages().values(), getEncoding()); + } + LOG.info("Collecting source models."); + long startedAt = System.currentTimeMillis(); + Iterable rootsToTravers = getClassPathEntries(); + if (getClassPathLookUpFilter() != null) { + LOG.info("Class path look up filter is active."); + Pattern cpLookUpFilter = Pattern.compile(getClassPathLookUpFilter()); + rootsToTravers = Iterables.filter(getClassPathEntries(), root -> cpLookUpFilter.matcher(root).matches()); + LOG.info("Investigating " + Iterables.size(rootsToTravers) + " of " + Iterables.size(getClassPathEntries()) + + " class path entries."); + } + List sourceResourceURIs = collectResources(getSourceDirs(), resourceSet); + // PATCHED THE FOLLOWING LINE TO WORKAROUND ISSUE: https://github.com/finos/rune-dsl/issues/878 + Iterable allResourcesURIs = Iterables.concat(collectResources(rootsToTravers, resourceSet), sourceResourceURIs); + forceDebugLog("Finished collecting source models. Took: " + (System.currentTimeMillis() - startedAt) + " ms."); + Iterable allClassPathEntries = Iterables.concat(getSourceDirs(), getClassPathEntries()); + if (needsJava) { + LOG.info("Installing type provider."); + installTypeProvider(allClassPathEntries, resourceSet, null); + } + IResourceClusteringPolicy strategy = null; + if (getClusteringConfig() != null) { + LOG.info("Clustering configured."); + DynamicResourceClusteringPolicy dynamicResourceClusteringPolicy = new DynamicResourceClusteringPolicy(); + // Convert MB to byte to make it easier for the user + dynamicResourceClusteringPolicy.setMinimumFreeMemory(getClusteringConfig().getMinimumFreeMemory() * 1024 * 1024); + dynamicResourceClusteringPolicy.setMinimumClusterSize(getClusteringConfig().getMinimumClusterSize()); + dynamicResourceClusteringPolicy.setMinimumPercentFreeMemory(getClusteringConfig().getMinimumPercentFreeMemory()); + strategy = dynamicResourceClusteringPolicy; + } else { + strategy = new DisabledClusteringPolicy(); + } + // Fill index + ResourceDescriptionsData index = new ResourceDescriptionsData(new ArrayList<>()); + Iterator allResourceIterator = allResourcesURIs.iterator(); + while (allResourceIterator.hasNext()) { + List resources = new ArrayList<>(); + int clusterIndex = 0; + boolean canContinue = true; + while (allResourceIterator.hasNext() && canContinue) { + URI uri = allResourceIterator.next(); + Resource resource = resourceSet.getResource(uri, true); + resources.add(resource); + fillIndex(uri, resource, index); + clusterIndex++; + if (!strategy.continueProcessing(resourceSet, null, clusterIndex)) { + canContinue = false; + } + } + if (!canContinue) { + clearResourceSet(resourceSet); + } + } + installIndex(resourceSet, index); + // Generate Stubs + if (needsJava) { + String stubsClasses = compileStubs(generateStubs(index, sourceResourceURIs)); + LOG.info("Installing type provider for stubs."); + installTypeProvider(Iterables.concat(allClassPathEntries, Lists.newArrayList(stubsClasses)), resourceSet, + jvmTypeAccess); + } + // Validate and generate + LOG.info("Validate and generate."); + Iterator sourceResourceIterator = sourceResourceURIs.iterator(); + boolean hasValidationErrors = false; + while (sourceResourceIterator.hasNext()) { + List resources = new ArrayList<>(); + int clusterIndex = 0; + boolean canContinue = true; + while (sourceResourceIterator.hasNext() && canContinue) { + URI uri = sourceResourceIterator.next(); + Resource resource = resourceSet.getResource(uri, true); + resources.add(resource); + resource.getContents(); // full initialize + EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl); + hasValidationErrors = !validate(resource) || hasValidationErrors; + clusterIndex++; + if (!strategy.continueProcessing(resourceSet, null, clusterIndex)) { + canContinue = false; + } + } + if (isFailOnValidationError() && hasValidationErrors) { + return !hasValidationErrors; + } + generate(resources); + if (!canContinue) { + clearResourceSet(resourceSet); + } + } + boolean success = !hasValidationErrors; + // END COPY PASTE OF ORIGINAL IMPLEMENTATION LOG.info("Starting after all generation"); GeneratorContext context = new GeneratorContext();