java - 类型不匹配 : cannot convert from Class<RulesGenerator> to Class<? 扩展 IGenerator2> xtest 错误

标签 java eclipse xtext xtend

我正在使用此 Xtest 代码创建演示家庭自动化语言 github source但是当生成自动代码时,我在文件 AbstractHomeAutomationRuntimeModule 中收到错误,例如“类型不匹配:无法从类转换为类”,我添加了一张图片以供进一步引用 this is image of the error

添加文件 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;
    }

}

最佳答案

该错误表明 RulesGenerator 应该实现 IGenerator2 接口(interface)(而不是 IGenerator)。

关于java - 类型不匹配 : cannot convert from Class<RulesGenerator> to Class<? 扩展 IGenerator2> xtest 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274232/

相关文章:

java - 第一人称相机胶卷

java.io.FileInputStream,未找到源

java - Eclipse Plugin IDE 在处理过程中卡住/不刷新

java - XTEND/JAVA自动生成eclipse项目

JAVA:如何计算多个字符串日期的平均天数?

java - json 模式 validator NoClassDefFoundError : com/google/common/io/Closer

java - 当类是spring bean时,Eclipse类加载断点不会被触发

eclipse - 如何解决错误 Mwe2Launcher : Couldn't find EClass for name

java - 在 xtext 语法中定义原语

java - 删除语句中的语法错误