mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 16:58:56 +00:00
[generator] Fixed some problems with new generator
Signed-off-by: Miro Spönemann <miro.spoenemann@itemis.de>
This commit is contained in:
parent
588af7bf44
commit
f0544b47ed
6 changed files with 51 additions and 41 deletions
|
@ -55,8 +55,8 @@ class LanguageConfig2 extends CompositeGeneratorFragment2 {
|
|||
@Accessors(PUBLIC_GETTER)
|
||||
List<String> fileExtensions
|
||||
|
||||
@Accessors(PUBLIC_SETTER)
|
||||
ResourceSet forcedResourceSet
|
||||
@Accessors
|
||||
ResourceSet resourceSet
|
||||
|
||||
@Accessors
|
||||
XtextGeneratorNaming naming
|
||||
|
@ -102,7 +102,8 @@ class LanguageConfig2 extends CompositeGeneratorFragment2 {
|
|||
override initialize(Injector injector) {
|
||||
super.initialize(injector)
|
||||
|
||||
val rs = forcedResourceSet ?: resourceSetProvider.get()
|
||||
if (resourceSet === null)
|
||||
resourceSet = resourceSetProvider.get()
|
||||
for (String loadedResource : loadedResources) {
|
||||
val loadedResourceUri = URI.createURI(loadedResource)
|
||||
switch (loadedResourceUri.fileExtension) {
|
||||
|
@ -142,28 +143,28 @@ class LanguageConfig2 extends CompositeGeneratorFragment2 {
|
|||
}
|
||||
val xcoreLangURI = URI.createPlatformResourceURI('/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore', true)
|
||||
try {
|
||||
rs.getResource(xcoreLangURI, true)
|
||||
resourceSet.getResource(xcoreLangURI, true)
|
||||
} catch (WrappedException e) {
|
||||
LOG.error("Could not load XcoreLang.xcore.", e)
|
||||
val brokenResource = rs.getResource(xcoreLangURI, false)
|
||||
rs.resources.remove(brokenResource)
|
||||
val brokenResource = resourceSet.getResource(xcoreLangURI, false)
|
||||
resourceSet.resources.remove(brokenResource)
|
||||
}
|
||||
}
|
||||
}
|
||||
rs.getResource(loadedResourceUri, true)
|
||||
resourceSet.getResource(loadedResourceUri, true)
|
||||
}
|
||||
if (!rs.resources.isEmpty) {
|
||||
installIndex(rs)
|
||||
for (var i = 0, var size = rs.resources.size; i < size; i++) {
|
||||
val res = rs.resources.get(i)
|
||||
if (!resourceSet.resources.isEmpty) {
|
||||
installIndex()
|
||||
for (var i = 0, var size = resourceSet.resources.size; i < size; i++) {
|
||||
val res = resourceSet.resources.get(i)
|
||||
if (res.getContents().isEmpty())
|
||||
LOG.error("Error loading '" + res.getURI() + "'")
|
||||
else if (!res.getErrors().isEmpty())
|
||||
LOG.error("Error loading '" + res.getURI() + "':\n" + Joiner.on('\n').join(res.getErrors()))
|
||||
}
|
||||
EcoreUtil.resolveAll(rs)
|
||||
EcoreUtil.resolveAll(resourceSet)
|
||||
}
|
||||
val resource = rs.getResource(URI.createURI(uri), true) as XtextResource
|
||||
val resource = resourceSet.getResource(URI.createURI(uri), true) as XtextResource
|
||||
if (resource.contents.isEmpty) {
|
||||
throw new IllegalArgumentException("Couldn't load grammar for '" + uri + "'.")
|
||||
}
|
||||
|
@ -182,7 +183,7 @@ class LanguageConfig2 extends CompositeGeneratorFragment2 {
|
|||
naming.grammar = grammar
|
||||
}
|
||||
|
||||
private def void installIndex(ResourceSet resourceSet) {
|
||||
private def void installIndex() {
|
||||
val index = new ResourceDescriptionsData(Collections.emptyList)
|
||||
val resources = Lists.newArrayList(resourceSet.resources)
|
||||
for (resource : resources) {
|
||||
|
|
|
@ -122,17 +122,19 @@ class XtextGenerator extends AbstractWorkflowComponent2 implements IGuiceAwareGe
|
|||
}
|
||||
|
||||
protected def generateRuntimeSetup(LanguageConfig2 language) {
|
||||
templates.createRuntimeSetup(language).writeTo(projectConfig.runtimeSrc)
|
||||
templates.createRuntimeGenSetup(language).writeTo(projectConfig.runtimeSrcGen)
|
||||
if (!projectConfig.runtimeSrc.isFile(language.naming.runtimeSetup.path))
|
||||
templates.createRuntimeSetup(language).writeTo(projectConfig.runtimeSrc)
|
||||
}
|
||||
|
||||
protected def generateModules(LanguageConfig2 language) {
|
||||
templates.createRuntimeModule(language).writeTo(projectConfig.runtimeSrc)
|
||||
templates.createRuntimeGenModule(language).writeTo(projectConfig.runtimeSrcGen)
|
||||
if (projectConfig.eclipsePluginSrc !== null)
|
||||
templates.createEclipsePluginModule(language).writeTo(projectConfig.eclipsePluginSrc)
|
||||
if (!projectConfig.runtimeSrc.isFile(language.naming.runtimeModule.path))
|
||||
templates.createRuntimeModule(language).writeTo(projectConfig.runtimeSrc)
|
||||
if (projectConfig.eclipsePluginSrcGen !== null)
|
||||
templates.createEclipsePluginGenModule(language).writeTo(projectConfig.eclipsePluginSrcGen)
|
||||
if (projectConfig.eclipsePluginSrc !== null && !projectConfig.eclipsePluginSrc.isFile(language.naming.eclipsePluginModule.path))
|
||||
templates.createEclipsePluginModule(language).writeTo(projectConfig.eclipsePluginSrc)
|
||||
}
|
||||
|
||||
protected def generateExecutableExtensionFactory(LanguageConfig2 language) {
|
||||
|
|
|
@ -206,10 +206,7 @@ class XtextGeneratorTemplates {
|
|||
|
||||
def JavaFileAccess createRuntimeGenModule(LanguageConfig2 langConfig) {
|
||||
val it = langConfig.naming
|
||||
val superClass =
|
||||
if (langConfig.runtimeGenModule.superClassName !== null)
|
||||
new TypeReference(langConfig.runtimeGenModule.superClassName)
|
||||
else runtimeDefaultModule
|
||||
val superClass = langConfig.runtimeGenModule.superClass ?: runtimeDefaultModule
|
||||
val javaFile = new JavaFileAccess(runtimeGenModule, codeConfig)
|
||||
javaFile.encodingProvider = encodingProvider
|
||||
|
||||
|
@ -270,10 +267,7 @@ class XtextGeneratorTemplates {
|
|||
|
||||
def JavaFileAccess createEclipsePluginGenModule(LanguageConfig2 langConfig) {
|
||||
val it = langConfig.naming
|
||||
val superClass =
|
||||
if (langConfig.eclipsePluginGenModule.superClassName !== null)
|
||||
new TypeReference(langConfig.eclipsePluginGenModule.superClassName)
|
||||
else eclipsePluginDefaultModule
|
||||
val superClass = langConfig.eclipsePluginGenModule.superClass ?: eclipsePluginDefaultModule
|
||||
val javaFile = new JavaFileAccess(eclipsePluginGenModule, codeConfig)
|
||||
javaFile.encodingProvider = encodingProvider
|
||||
|
||||
|
@ -379,28 +373,28 @@ class XtextGeneratorTemplates {
|
|||
«ENDFOR»
|
||||
|
||||
private static final «Logger» logger = «Logger».getLogger(«activator.simpleName».class);
|
||||
|
||||
|
||||
private static «activator.simpleName» INSTANCE;
|
||||
|
||||
|
||||
private «Map»<String, «Injector»> injectors = «Collections».synchronizedMap(«Maps».<String, «Injector»> newHashMapWithExpectedSize(1));
|
||||
|
||||
|
||||
@Override
|
||||
public void start(«'org.osgi.framework.BundleContext'.typeRef» context) throws Exception {
|
||||
super.start(context);
|
||||
INSTANCE = this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void stop(«'org.osgi.framework.BundleContext'.typeRef» context) throws Exception {
|
||||
injectors.clear();
|
||||
INSTANCE = null;
|
||||
super.stop(context);
|
||||
}
|
||||
|
||||
|
||||
public static «activator.simpleName» getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
public «Injector» getInjector(String language) {
|
||||
synchronized (injectors) {
|
||||
«Injector» injector = injectors.get(language);
|
||||
|
@ -410,7 +404,7 @@ class XtextGeneratorTemplates {
|
|||
return injector;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected «Injector» createInjector(String language) {
|
||||
try {
|
||||
«Module» runtimeModule = getRuntimeModule(language);
|
||||
|
@ -424,7 +418,7 @@ class XtextGeneratorTemplates {
|
|||
throw new RuntimeException("Failed to create injector for " + language, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected Module getRuntimeModule(String grammar) {
|
||||
«FOR lang : langConfigs»
|
||||
if («lang.grammar.name.toUpperCase.replaceAll('\\.', '_')».equals(grammar)) {
|
||||
|
@ -433,7 +427,7 @@ class XtextGeneratorTemplates {
|
|||
«ENDFOR»
|
||||
throw new IllegalArgumentException(grammar);
|
||||
}
|
||||
|
||||
|
||||
protected «Module» getUiModule(String grammar) {
|
||||
«FOR lang : langConfigs»
|
||||
if («lang.grammar.name.toUpperCase.replaceAll('\\.', '_')».equals(grammar)) {
|
||||
|
@ -442,11 +436,11 @@ class XtextGeneratorTemplates {
|
|||
«ENDFOR»
|
||||
throw new IllegalArgumentException(grammar);
|
||||
}
|
||||
|
||||
|
||||
protected «Module» getSharedStateModule() {
|
||||
return new «'org.eclipse.xtext.ui.shared.SharedStateModule'.typeRef»();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
'''
|
||||
javaFile.markedAsGenerated = true
|
||||
|
|
|
@ -65,7 +65,7 @@ class GuiceModuleAccess {
|
|||
val Set<Binding> bindings = newLinkedHashSet
|
||||
|
||||
@Accessors
|
||||
String superClassName
|
||||
TypeReference superClass
|
||||
|
||||
def void add(Binding newBinding) {
|
||||
if (bindings.contains(newBinding)) {
|
||||
|
|
|
@ -41,14 +41,18 @@ class JavaFileAccess extends TextFileAccess {
|
|||
new(TypeReference typeRef, CodeConfig codeConfig) {
|
||||
this.packageName = typeRef.package
|
||||
if (typeRef.name != packageName + '.' + typeRef.simpleName)
|
||||
throw new IllegalArgumentException('Nested types cannot be serialized.')
|
||||
this.path = packageName.replace('.', '/') + '/' + typeRef.simpleName + '.java'
|
||||
throw new IllegalArgumentException('Nested type cannot be serialized: ' + typeRef)
|
||||
this.path = typeRef.path
|
||||
this.codeConfig = codeConfig
|
||||
}
|
||||
|
||||
def String importType(TypeReference typeRef) {
|
||||
var name = typeRef.simpleName
|
||||
if (!CodeGenUtil.isJavaDefaultType(name) && this.packageName != typeRef.package) {
|
||||
var packageName = typeRef.package
|
||||
val isJavaDefaultType = CodeGenUtil.isJavaDefaultType(name)
|
||||
if (isJavaDefaultType && packageName != 'java.lang') {
|
||||
name = typeRef.name
|
||||
} else if (!isJavaDefaultType && this.packageName != packageName) {
|
||||
val imported = imports.get(name)
|
||||
if (imported === null)
|
||||
imports.put(name, typeRef)
|
||||
|
|
|
@ -75,6 +75,15 @@ class TypeReference {
|
|||
return ''
|
||||
}
|
||||
|
||||
def String getPath() {
|
||||
val packageName = getPackage
|
||||
var className = name.substring(packageName.length + 1)
|
||||
val outerClassEnd = className.indexOf('.')
|
||||
if (outerClassEnd >= 0)
|
||||
className = className.substring(0, outerClassEnd)
|
||||
return packageName.replace('.', '/') + '/' + className + '.java'
|
||||
}
|
||||
|
||||
private def matches(char c1, char c2) {
|
||||
c1 == c2
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue