Reputation: 2855
I am brand new to Xtext and Xtend and am trying to learn Xtext using the Xtext tutorials in the Xtext documentation. I am running on Eclipse Photon under Java 10 with Xtext 2.14. I am starting the extended tutorial and have a problem very early on. Here is the code for my attempt at a code generator:
/*
* generated by Xtext 2.14.0
*/
package net.wiseoldbird.tutorial.domainmodel.generator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import net.wiseoldbird.tutorial.domainmodel.domainmodel.Entity
import com.google.inject.Inject
@Inject extension IQualifiedNameProvider;
class DomainmodelGenerator extends AbstractGenerator {
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
for (e: resource.allContents.toIterable.filter(Entity)) {
fsa.generateFile(e.fullyQualifiedName.toString("/") + ".java", e.compile)
}
}
}
Here is my grammar file:
grammar net.wiseoldbird.tutorial.domainmodel.Domainmodel
with org.eclipse.xtext.common.Terminals
generate domainmodel "http://www.wiseoldbird.net/tutorial/domainmodel/Domainmodel"
Domainmodel :
(elements+=AbstractElement)*;
PackageDeclaration:
'package' name=QualifiedName '{'
(elements+=AbstractElement)*
'}';
AbstractElement:
PackageDeclaration | Type | Import;
QualifiedName:
ID ('.' ID)*;
Import:
'import' importedNamespace=QualifiedNameWithWildcard;
QualifiedNameWithWildcard:
QualifiedName '.*'?;
Type:
DataType | Entity;
DataType:
'datatype' name=ID;
Entity:
'entity' name=ID ('extends' superType=[Entity|QualifiedName])? '{'
(features+=Feature)*
'}';
Feature:
(many?='many')? name=ID ':' type=[Type|QualifiedName];
My problem is that Eclipse says there is a problem with the @Inject annotation. It says that Inject cannot be resolved to an annotation type
. This is in an Eclipse Xtext project generated from the instructions in the tutorial.
How do I proceed from here?
Upvotes: 0
Views: 868
Reputation: 11868
you can inject only fields and post-init-method/constructor parameters
import com.google.inject.Inject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import org.eclipse.xtext.naming.IQualifiedNameProvider
class DomainmodelGenerator extends AbstractGenerator {
@Inject extension IQualifiedNameProvider
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
for (e : resource.allContents.toIterable.filter(Entity)) {
fsa.generateFile(e.fullyQualifiedName.toString("/") + ".java", e.compile)
}
}
def compile(Entity e) '''
package «e.eContainer.fullyQualifiedName»;
public class «e.name» {
}
'''
}
Upvotes: 1