From 8d6ec5feb1419765f6ee2fbd31400e7a1952d4a0 Mon Sep 17 00:00:00 2001 From: Johannes Dicks Date: Wed, 3 Feb 2016 17:17:19 +0100 Subject: [PATCH] #358 : LibraryExtensions refactored to allow standalone execution (without eclipse extensions) --- .../sct/domain/extension/DomainRegistry.java | 2 +- .../core/extensions/ILibraryDescriptor.java | 14 +++ .../core/extensions/LibraryExtensions.java | 109 ++++++++++-------- .../ui/quickfix/SGenQuickfixProvider.java | 6 +- .../SGenTemplateProposalProvider.java | 6 +- .../genmodel/ui/wizard/ModelCreator.java | 10 +- .../genmodel/scoping/SGenScopeProvider.java | 6 +- .../validation/SGenJavaValidator.java | 12 +- .../genmodel/test/HelpIntegrationTest.java | 7 +- 9 files changed, 101 insertions(+), 71 deletions(-) create mode 100644 plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java diff --git a/plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/DomainRegistry.java b/plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/DomainRegistry.java index 9cb1155f81..0a209f6117 100644 --- a/plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/DomainRegistry.java +++ b/plugins/org.yakindu.sct.domain/src/org/yakindu/sct/domain/extension/DomainRegistry.java @@ -46,7 +46,7 @@ public class DomainRegistry { private DomainRegistry() { } - private static List descriptors = null; + private static List descriptors; private static final class ConfigElementDomainDescriptor implements IDomainDescriptor { diff --git a/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java b/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java new file mode 100644 index 0000000000..c5b7bf3359 --- /dev/null +++ b/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/ILibraryDescriptor.java @@ -0,0 +1,14 @@ +package org.yakindu.sct.generator.core.extensions; + +import org.eclipse.emf.common.util.URI; +import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; + +public interface ILibraryDescriptor { + + URI getURI(); + + String getLibraryId(); + + IDefaultFeatureValueProvider createFeatureValueProvider(); + +} \ No newline at end of file diff --git a/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java b/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java index d06a703e8f..ef6fe906f4 100644 --- a/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java +++ b/plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/extensions/LibraryExtensions.java @@ -10,9 +10,6 @@ */ package org.yakindu.sct.generator.core.extensions; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Lists.newArrayList; - import java.util.List; import org.eclipse.core.runtime.CoreException; @@ -22,9 +19,9 @@ import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; import org.yakindu.sct.model.sgen.FeatureTypeLibrary; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; /** * @@ -40,25 +37,47 @@ public class LibraryExtensions { public static final String GLOBAL_ID = "ALL"; - public static class LibraryDescriptor { + private static List descriptors; + + private static class LibraryDescriptor implements ILibraryDescriptor { private final IConfigurationElement configElement; LibraryDescriptor(IConfigurationElement configElement) { this.configElement = configElement; } + /* + * (non-Javadoc) + * + * @see + * org.yakindu.sct.generator.core.extensions.ILibraryDescriptor#getURI() + */ + @Override public URI getURI() { return URI.createURI(configElement.getAttribute(ATTRIBUTE_URI)); } - + + /* + * (non-Javadoc) + * + * @see org.yakindu.sct.generator.core.extensions.ILibraryDescriptor# + * getLibraryId() + */ + @Override public String getLibraryId() { return configElement.getAttribute(ATTRIBUTE_LIBRARY_ID); } + /* + * (non-Javadoc) + * + * @see org.yakindu.sct.generator.core.extensions.ILibraryDescriptor# + * createFeatureValueProvider() + */ + @Override public IDefaultFeatureValueProvider createFeatureValueProvider() { try { - return (IDefaultFeatureValueProvider) configElement - .createExecutableExtension(DEFAULT_PROVIDER); + return (IDefaultFeatureValueProvider) configElement.createExecutableExtension(DEFAULT_PROVIDER); } catch (CoreException e) { e.printStackTrace(); } @@ -66,51 +85,49 @@ public IDefaultFeatureValueProvider createFeatureValueProvider() { } } - public static Iterable getLibraryDescriptors() { - IConfigurationElement[] configurationElements = Platform - .getExtensionRegistry().getConfigurationElementsFor( - EXTENSION_POINT_ID); - return transform(newArrayList(configurationElements), - new CreateLibraryDescriptor()); + public static List getLibraryDescriptors() { + + if (descriptors == null) { + descriptors = Lists.newArrayList(); + if (Platform.isRunning()) { + initFromExtensions(); + } + } + return descriptors; + } + + protected static void initFromExtensions() { + IConfigurationElement[] configurationElements = Platform.getExtensionRegistry() + .getConfigurationElementsFor(EXTENSION_POINT_ID); + for (IConfigurationElement iConfigurationElement : configurationElements) { + descriptors.add(new LibraryDescriptor(iConfigurationElement)); + } } - - public static Iterable getLibraryDescriptors( - final List libraryIds) { - Iterable libraryDescriptor = LibraryExtensions - .getLibraryDescriptors(); - return Iterables.filter(libraryDescriptor, - new Predicate() { - public boolean apply(LibraryDescriptor input) { - for (String libId : libraryIds) { - if (input.getLibraryId().equals(libId)) { - return true; - } - } - return false; + + public static List getLibraryDescriptors(final List libraryIds) { + Iterable libraryDescriptor = getLibraryDescriptors(); + return Lists.newArrayList(Iterables.filter(libraryDescriptor, new Predicate() { + public boolean apply(ILibraryDescriptor input) { + for (String libId : libraryIds) { + if (input.getLibraryId().equals(libId)) { + return true; } - }); + } + return false; + } + })); } - - public static IDefaultFeatureValueProvider getDefaultFeatureValueProvider( - List libraryId, FeatureTypeLibrary library) { - Iterable libraryDescriptor = getLibraryDescriptors(libraryId); - for (LibraryDescriptor desc : libraryDescriptor) { - IDefaultFeatureValueProvider defaultProvider = desc - .createFeatureValueProvider(); - if (defaultProvider != null - && defaultProvider.isProviderFor(library)) { + + public static IDefaultFeatureValueProvider getDefaultFeatureValueProvider(List libraryId, + FeatureTypeLibrary library) { + List libraryDescriptor = getLibraryDescriptors(libraryId); + for (ILibraryDescriptor desc : libraryDescriptor) { + IDefaultFeatureValueProvider defaultProvider = desc.createFeatureValueProvider(); + if (defaultProvider != null && defaultProvider.isProviderFor(library)) { return defaultProvider; } } return null; } - private static final class CreateLibraryDescriptor implements - Function { - - public LibraryDescriptor apply(IConfigurationElement from) { - return new LibraryDescriptor(from); - } - } - } diff --git a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/quickfix/SGenQuickfixProvider.java b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/quickfix/SGenQuickfixProvider.java index feced34e04..4e05874968 100644 --- a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/quickfix/SGenQuickfixProvider.java +++ b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/quickfix/SGenQuickfixProvider.java @@ -24,8 +24,8 @@ import org.eclipse.xtext.validation.Issue; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.genmodel.validation.SGenJavaValidator; import org.yakindu.sct.model.sgen.FeatureConfiguration; import org.yakindu.sct.model.sgen.FeatureType; @@ -66,9 +66,9 @@ private FeatureConfiguration getDefaultFeatureConfiguration( IGeneratorDescriptor generatorDescriptor = GeneratorExtensions .getGeneratorDescriptor(model.getGeneratorId()); - Iterable libraryDescriptor = LibraryExtensions + Iterable libraryDescriptor = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { ResourceSet set = new ResourceSetImpl(); Resource resource = set.getResource(desc.getURI(), true); FeatureTypeLibrary lib = (FeatureTypeLibrary) resource diff --git a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/templates/SGenTemplateProposalProvider.java b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/templates/SGenTemplateProposalProvider.java index 80686998dd..422449e416 100644 --- a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/templates/SGenTemplateProposalProvider.java +++ b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/templates/SGenTemplateProposalProvider.java @@ -26,8 +26,8 @@ import org.eclipse.xtext.ui.editor.templates.DefaultTemplateProposalProvider; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.genmodel.services.SGenGrammarAccess; import org.yakindu.sct.model.sgen.FeatureType; import org.yakindu.sct.model.sgen.FeatureTypeLibrary; @@ -78,10 +78,10 @@ private void createFeatureConfigurationTemplates( IGeneratorDescriptor generatorDescriptor = GeneratorExtensions .getGeneratorDescriptor(model.getGeneratorId()); - Iterable libraryDescriptor = LibraryExtensions + Iterable libraryDescriptor = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { ResourceSet set = new ResourceSetImpl(); Resource resource = set.getResource(desc.getURI(), true); FeatureTypeLibrary lib = (FeatureTypeLibrary) resource diff --git a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/ModelCreator.java b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/ModelCreator.java index 2e828c8fae..542dc68b9a 100644 --- a/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/ModelCreator.java +++ b/plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/ModelCreator.java @@ -19,8 +19,8 @@ import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; import org.yakindu.sct.model.sgen.FeatureConfiguration; import org.yakindu.sct.model.sgen.FeatureType; @@ -72,9 +72,9 @@ private GeneratorEntry createGeneratorEntry(EObject eobject) { } private FeatureConfiguration createFeatureConfiguration(EObject object, FeatureType featureType) { - Iterable libraryDescriptor = LibraryExtensions.getLibraryDescriptors(descriptor + Iterable libraryDescriptor = LibraryExtensions.getLibraryDescriptors(descriptor .getLibraryIDs()); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { IDefaultFeatureValueProvider defaultProvider = desc.createFeatureValueProvider(); if (defaultProvider != null && defaultProvider.isProviderFor(featureType.getLibrary())) { return defaultProvider.createDefaultFeatureConfiguration(featureType, object); @@ -85,9 +85,9 @@ private FeatureConfiguration createFeatureConfiguration(EObject object, FeatureT public static List getFeatureTypes(IGeneratorDescriptor descriptor) { ArrayList features = Lists.newArrayList(); - Iterable libraryDescriptor = LibraryExtensions.getLibraryDescriptors(descriptor + Iterable libraryDescriptor = LibraryExtensions.getLibraryDescriptors(descriptor .getLibraryIDs()); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { ResourceSet set = new ResourceSetImpl(); Resource resource = set.getResource(desc.getURI(), true); FeatureTypeLibrary lib = (FeatureTypeLibrary) resource.getContents().get(0); diff --git a/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/scoping/SGenScopeProvider.java b/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/scoping/SGenScopeProvider.java index 68afee226a..f058ba8fd7 100644 --- a/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/scoping/SGenScopeProvider.java +++ b/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/scoping/SGenScopeProvider.java @@ -26,8 +26,8 @@ import org.eclipse.xtext.scoping.impl.SimpleScope; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.genmodel.resource.FeatureResourceDescription; import org.yakindu.sct.model.sgen.FeatureConfiguration; import org.yakindu.sct.model.sgen.GeneratorModel; @@ -144,10 +144,10 @@ protected SimpleScope getLibraryScope(Resource resource) { Iterable allElements = Lists.newArrayList(); if (generatorDescriptor != null) { - Iterable libraryDescriptor = LibraryExtensions + Iterable libraryDescriptor = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { Resource library = resourceSet.getResource(desc.getURI(), true); FeatureResourceDescription description = new FeatureResourceDescription( library); diff --git a/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/validation/SGenJavaValidator.java b/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/validation/SGenJavaValidator.java index d7f22b22e1..b027bf5599 100644 --- a/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/validation/SGenJavaValidator.java +++ b/plugins/org.yakindu.sct.generator.genmodel/src/org/yakindu/sct/generator/genmodel/validation/SGenJavaValidator.java @@ -23,8 +23,8 @@ import org.yakindu.base.base.NamedElement; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.core.features.IDefaultFeatureValueProvider; import org.yakindu.sct.model.sgen.BoolLiteral; import org.yakindu.sct.model.sgen.DeprecatableElement; @@ -203,7 +203,7 @@ public void checkRequiredFeatures(GeneratorEntry entry) { IGeneratorDescriptor generatorDescriptor = GeneratorExtensions .getGeneratorDescriptor(model.getGeneratorId()); - Iterable libraryDescriptors = LibraryExtensions + Iterable libraryDescriptors = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); Iterable requiredFeatures = filter( @@ -244,7 +244,7 @@ public void checkRequiredParameters(FeatureConfiguration configuration) { IGeneratorDescriptor generatorDescriptor = GeneratorExtensions .getGeneratorDescriptor(model.getGeneratorId()); - Iterable libraryDescriptors = LibraryExtensions + Iterable libraryDescriptors = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); Iterable requiredParameters = transform( @@ -349,10 +349,10 @@ public Iterable apply(FeatureTypeLibrary from) { }; } - private static Function getFeatureTypeLibrary() { - return new Function() { + private static Function getFeatureTypeLibrary() { + return new Function() { - public FeatureTypeLibrary apply(LibraryDescriptor from) { + public FeatureTypeLibrary apply(ILibraryDescriptor from) { return (FeatureTypeLibrary) new ResourceSetImpl() .getResource(from.getURI(), true).getContents().get(0); } diff --git a/test-plugins/org.yakindu.sct.generator.genmodel.test/src/org/yakindu/sct/generator/genmodel/test/HelpIntegrationTest.java b/test-plugins/org.yakindu.sct.generator.genmodel.test/src/org/yakindu/sct/generator/genmodel/test/HelpIntegrationTest.java index 1b78ca408b..701a36cd56 100644 --- a/test-plugins/org.yakindu.sct.generator.genmodel.test/src/org/yakindu/sct/generator/genmodel/test/HelpIntegrationTest.java +++ b/test-plugins/org.yakindu.sct.generator.genmodel.test/src/org/yakindu/sct/generator/genmodel/test/HelpIntegrationTest.java @@ -26,8 +26,8 @@ import org.junit.runner.RunWith; import org.yakindu.sct.generator.core.extensions.GeneratorExtensions; import org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor; +import org.yakindu.sct.generator.core.extensions.ILibraryDescriptor; import org.yakindu.sct.generator.core.extensions.LibraryExtensions; -import org.yakindu.sct.generator.core.extensions.LibraryExtensions.LibraryDescriptor; import org.yakindu.sct.generator.genmodel.resource.FeatureResourceDescription; import org.yakindu.sct.generator.genmodel.test.util.SGenInjectorProvider; import org.yakindu.sct.generator.genmodel.ui.help.SGenUserHelpDocumentationProvider; @@ -107,7 +107,6 @@ public void checkXpandGeneratorFeatureDocumentation() { fail("Missing generator feature(s) documentation for generator " + YAKINDU_XPAND + ": " + missingDocumentation); } - } private List getFeaturesWithoutDocumentation(String generatorId) { @@ -115,11 +114,11 @@ private List getFeaturesWithoutDocumentation(String generatorId) { IGeneratorDescriptor generatorDescriptor = GeneratorExtensions .getGeneratorDescriptor(generatorId); - Iterable libraryDescriptor = LibraryExtensions + Iterable libraryDescriptor = LibraryExtensions .getLibraryDescriptors(generatorDescriptor.getLibraryIDs()); Iterable allElements = Lists.newArrayList(); - for (LibraryDescriptor desc : libraryDescriptor) { + for (ILibraryDescriptor desc : libraryDescriptor) { Resource library = resourceSet.getResource(desc.getURI(), true); FeatureResourceDescription description = new FeatureResourceDescription( library);