我正在使用这个Xtest代码创建一个演示homeAutomation语言github source但是当生成自动代码时,我在文件AbstractHomeAutomationRuntimeModule中遇到错误,如“类型不匹配:无法从类转换为类”我正在添加用于进一步报复的图片
我在添加文件AbstractHomeAutomationRuntimeModule RulesGenerator.xtend后出现此错误
/*
* generated by Xtext 2.10.0
*/
package org.xtext.example.home.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 org.xtext.example.home.homeAutomation.Device
import org.xtext.example.home.homeAutomation.Model
import java.util.Scanner
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtend.lib.macro.declaration.Declaration
import org.xtext.example.home.homeAutomation.Rule
https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
*/
class RulesGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
val simpleClassName = resource.getURI.trimFileExtension.lastSegment
if (resource.contents?.head == null) {
return;
}
val declarations = resource.contents.head.eContents.filter(Declaration)
fsa.generateFile(simpleClassName + '.java', '''
public class «simpleClassName» {
public static void fire(String event) {
«FOR device : declarations.filter(Device)»
«FOR state : device.states»
if (event.equals("«state.name»")) {
System.out.println("«device.name» is now «state.name»!");
}
«ENDFOR»
«ENDFOR»
«FOR rule : declarations.filter(Rule)»
if (event.equals("«rule.when.name»")) {
fire("«rule.then.name»");
}
«ENDFOR»
}
public static void main(String... args) {
try («Scanner.name» scanner = new «Scanner.name»(System.in)) {
System.out.println("Welcome home!");
System.out.println("Available commands : ");
«FOR device : declarations.filter(Device)»
«FOR state : device.states»
System.out.println(" «device.name» «state.name»" );
«ENDFOR»
«ENDFOR»
System.out.println("Have fun!");
while(true) {
String command = scanner.next();
«FOR device : declarations.filter(Device)»
if (command.equalsIgnoreCase("«device.name»")) {
String secondaryCommand = scanner.next();
«FOR state : device.states»
if (secondaryCommand.equalsIgnoreCase("«state.name»")) {
fire("«state.name»");
} else
«ENDFOR»
{
System.out.println("«device.name» can only have the following states: «device.states.map[name].
join(',')».");
}
}
«ENDFOR»
if (command.equalsIgnoreCase("bye")) {
System.out.println("Ciao!");
break;
}
}
}
}
}
''')
}
def ruleMethodName(Rule device) {
'execute' + device.description.replaceAll('\\W', '_')
}
}
错误发生在此自动生成文件AbstractHomeAutomationRuntimeModule
中/*
* generated by Xtext 2.10.0
*/
package org.xtext.example.home;
import com.google.inject.Binder;
import com.google.inject.Provider;
import com.google.inject.name.Names;
import java.util.Properties;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.generator.IGenerator2;
import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.parser.ITokenToStringConverter;
import org.eclipse.xtext.parser.antlr.AntlrTokenDefProvider;
import org.eclipse.xtext.parser.antlr.AntlrTokenToStringConverter;
import org.eclipse.xtext.parser.antlr.IAntlrTokenFileProvider;
import org.eclipse.xtext.parser.antlr.ITokenDefProvider;
import org.eclipse.xtext.parser.antlr.Lexer;
import org.eclipse.xtext.parser.antlr.LexerBindings;
import org.eclipse.xtext.parser.antlr.LexerProvider;
import org.eclipse.xtext.resource.IContainer;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.containers.IAllContainersState;
import org.eclipse.xtext.resource.containers.ResourceSetBasedAllContainersStateProvider;
import org.eclipse.xtext.resource.containers.StateBasedContainerManager;
import org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider;
import org.eclipse.xtext.resource.impl.ResourceSetBasedResourceDescriptions;
import org.eclipse.xtext.scoping.IGlobalScopeProvider;
import org.eclipse.xtext.scoping.IScopeProvider;
import org.eclipse.xtext.scoping.IgnoreCaseLinking;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;
import org.eclipse.xtext.scoping.impl.DefaultGlobalScopeProvider;
import org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider;
import org.eclipse.xtext.serializer.ISerializer;
import org.eclipse.xtext.serializer.impl.Serializer;
import org.eclipse.xtext.serializer.sequencer.ISemanticSequencer;
import org.eclipse.xtext.serializer.sequencer.ISyntacticSequencer;
import org.eclipse.xtext.service.DefaultRuntimeModule;
import org.eclipse.xtext.service.SingletonBinding;
import org.xtext.example.home.generator.RulesGenerator;
import org.xtext.example.home.parser.antlr.HomeAutomationAntlrTokenFileProvider;
import org.xtext.example.home.parser.antlr.HomeAutomationParser;
import org.xtext.example.home.parser.antlr.internal.InternalHomeAutomationLexer;
import org.xtext.example.home.scoping.HomeAutomationScopeProvider;
import org.xtext.example.home.serializer.HomeAutomationSemanticSequencer;
import org.xtext.example.home.serializer.HomeAutomationSyntacticSequencer;
import org.xtext.example.home.services.HomeAutomationGrammarAccess;
import org.xtext.example.home.validation.HomeAutomationValidator;
/**
* Manual modifications go to {@link HomeAutomationRuntimeModule}.
*/
@SuppressWarnings("all")
public abstract class AbstractHomeAutomationRuntimeModule extends DefaultRuntimeModule {
protected Properties properties = null;
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/xtext/example/home/HomeAutomation.properties");
super.configure(binder);
}
public void configureLanguageName(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(Constants.LANGUAGE_NAME)).toInstance("org.xtext.example.home.HomeAutomation");
}
public void configureFileExtensions(Binder binder) {
if (properties == null || properties.getProperty(Constants.FILE_EXTENSIONS) == null)
binder.bind(String.class).annotatedWith(Names.named(Constants.FILE_EXTENSIONS)).toInstance("home");
}
// contributed by org.eclipse.xtext.xtext.generator.grammarAccess.GrammarAccessFragment2
public ClassLoader bindClassLoaderToInstance() {
return getClass().getClassLoader();
}
// contributed by org.eclipse.xtext.xtext.generator.grammarAccess.GrammarAccessFragment2
public Class<? extends IGrammarAccess> bindIGrammarAccess() {
return HomeAutomationGrammarAccess.class;
}
// contributed by org.eclipse.xtext.xtext.generator.serializer.SerializerFragment2
public Class<? extends ISemanticSequencer> bindISemanticSequencer() {
return HomeAutomationSemanticSequencer.class;
}
// contributed by org.eclipse.xtext.xtext.generator.serializer.SerializerFragment2
public Class<? extends ISyntacticSequencer> bindISyntacticSequencer() {
return HomeAutomationSyntacticSequencer.class;
}
// contributed by org.eclipse.xtext.xtext.generator.serializer.SerializerFragment2
public Class<? extends ISerializer> bindISerializer() {
return Serializer.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Class<? extends IParser> bindIParser() {
return HomeAutomationParser.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Class<? extends ITokenToStringConverter> bindITokenToStringConverter() {
return AntlrTokenToStringConverter.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Class<? extends IAntlrTokenFileProvider> bindIAntlrTokenFileProvider() {
return HomeAutomationAntlrTokenFileProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Class<? extends Lexer> bindLexer() {
return InternalHomeAutomationLexer.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Class<? extends ITokenDefProvider> bindITokenDefProvider() {
return AntlrTokenDefProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public Provider<InternalHomeAutomationLexer> provideInternalHomeAutomationLexer() {
return LexerProvider.create(InternalHomeAutomationLexer.class);
}
// contributed by org.eclipse.xtext.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment2
public void configureRuntimeLexer(Binder binder) {
binder.bind(Lexer.class)
.annotatedWith(Names.named(LexerBindings.RUNTIME))
.to(InternalHomeAutomationLexer.class);
}
// contributed by org.eclipse.xtext.xtext.generator.validation.ValidatorFragment2
@SingletonBinding(eager=true)
public Class<? extends HomeAutomationValidator> bindHomeAutomationValidator() {
return HomeAutomationValidator.class;
}
// contributed by org.eclipse.xtext.xtext.generator.scoping.ImportNamespacesScopingFragment2
public Class<? extends IScopeProvider> bindIScopeProvider() {
return HomeAutomationScopeProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.scoping.ImportNamespacesScopingFragment2
public void configureIScopeProviderDelegate(Binder binder) {
binder.bind(IScopeProvider.class).annotatedWith(Names.named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).to(ImportedNamespaceAwareLocalScopeProvider.class);
}
// contributed by org.eclipse.xtext.xtext.generator.scoping.ImportNamespacesScopingFragment2
public Class<? extends IGlobalScopeProvider> bindIGlobalScopeProvider() {
return DefaultGlobalScopeProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.scoping.ImportNamespacesScopingFragment2
public void configureIgnoreCaseLinking(Binder binder) {
binder.bindConstant().annotatedWith(IgnoreCaseLinking.class).to(false);
}
// contributed by org.eclipse.xtext.xtext.generator.exporting.QualifiedNamesFragment2
public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
return DefaultDeclarativeQualifiedNameProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.builder.BuilderIntegrationFragment2
public Class<? extends IContainer.Manager> bindIContainer$Manager() {
return StateBasedContainerManager.class;
}
// contributed by org.eclipse.xtext.xtext.generator.builder.BuilderIntegrationFragment2
public Class<? extends IAllContainersState.Provider> bindIAllContainersState$Provider() {
return ResourceSetBasedAllContainersStateProvider.class;
}
// contributed by org.eclipse.xtext.xtext.generator.builder.BuilderIntegrationFragment2
public void configureIResourceDescriptions(Binder binder) {
binder.bind(IResourceDescriptions.class).to(ResourceSetBasedResourceDescriptions.class);
}
// contributed by org.eclipse.xtext.xtext.generator.builder.BuilderIntegrationFragment2
public void configureIResourceDescriptionsPersisted(Binder binder) {
binder.bind(IResourceDescriptions.class).annotatedWith(Names.named(ResourceDescriptionsProvider.PERSISTED_DESCRIPTIONS)).to(ResourceSetBasedResourceDescriptions.class);
}
// contributed by org.eclipse.xtext.xtext.generator.generator.GeneratorFragment2
public Class<? extends IGenerator2> bindIGenerator2() {
return RulesGenerator.class;
}
}
答案 0 :(得分:0)
错误说RulesGenerator
应该实现IGenerator2
接口(不是IGenerator
)。