0

您能帮我解决GWT Generator的问题吗?让我解释。

我希望在编译期间生成一个小部件(具有预定义值的 ListBox),为此我使用GWT Generator。我的解决方案在 DevMode 中按预期工作(当我使用 运行项目时mvn gwt:run),我在屏幕上看到生成的列表框。但是当我编译代码并在“生产模式”下运行它(使用 command mvn clean gwt:compile jetty:run-war)时,我看到只有一个项目“no-value”的哑列表框。

我对问题原因有一个想法。我在我的项目中使用GIN 。尽管我用延迟绑定而不是 GIN 注入生成的列表框替换了我的空列表框,但它可能以某种方式阻止了运行时的替换。我在一个空的测试项目上尝试了我的列表框 - 在Dev ModeProduction Mode中一切都按预期工作。但它在我的工作项目中失败了。

这是我的认识:

package com.test.generated;

import com.google.gwt.user.client.ui.IsWidget;
public interface IMySelectBox extends IsWidget {}

我的空选择框:

package com.test.generated;
import com.google.gwt.user.client.ui.ListBox;

/**
 * <p>Dumb listbox. It should be replaced with generated file.</p>
 * 
 */
public class MySelectBox implements IMySelectBox {

    @Override
    public ListBox asWidget() {
        ListBox listBox = new ListBox();
        listBox.addItem("no-value","no-value");
        return listBox;
    }
}  

我的发电机:

package com.test.generated;

import java.io.PrintWriter;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;

/**
 * Generates the ListBox and populates it
 *
 */
public class SelectBoxGenerator extends Generator {

    /**
     * {@inheritDoc}
     */
    @Override
    public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {

        try {

            JClassType classType = context.getTypeOracle().getType(typeName);
            return this.getSourceWriter(classType, context, logger);

        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Generates the source code of the List box.
     */
    private String getSourceWriter(JClassType classType, GeneratorContext context, TreeLogger logger) {

        final String packageName = classType.getPackage().getName();
        final String className = classType.getSimpleSourceName() + "GeneratedImpl";

        PrintWriter printWriter = context.tryCreate(logger, packageName, className);
        if (printWriter == null) {
            // source code has already been generated, abort
            return null;
        }

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);

        // Extends 
        composer.setSuperclass(classType.getName());

        // Implements interface IMySelectBox
        composer.addImplementedInterface(IMySelectBox.class.getSimpleName());

        // Imports
        composer.addImport(ListBox.class.getName());

        // Class body
        SourceWriter src = composer.createSourceWriter(context, printWriter);

        src.println("@Override");
        src.println("public ListBox asWidget() {");     
        src.println("ListBox sb = new ListBox();");

        // ...here I generate values for the selectbox during compilation time.

        src.println("return sb;");
        src.println("}");

        src.commit(logger);

        System.out.println("Generating for: " + className);

        // return the fully qualifed name of the generated class
        return packageName + "." + className;
    }
}

这就是我在module.gwt.xml文件中声明替换的方式:

  <generate-with class="com.test.generated.SelectBoxGenerator">
    <when-type-assignable class="com.test.generated.IMySelectBox" />
  </generate-with>

我像往常一样使用我生成的 ListBox:

IMySelectBox mySelectBox = GWT.create(MySelectBox.class);
anyPanel.add(mySelectBox);

正如你所看到的,我根本不接触 GIN 的东西。我使用 GIN 来注入我的模块和视图。我在 GIN 网站上找到了第 95 期,可能与我的情况有关。

我会很高兴得到任何帮助。欢迎任何解释,提示,解决方法,建议!

非常感谢您!

4

0 回答 0