2017-02-28 73 views
0

我想了解GWT发生器,但面临的问题很少。我试图使用生成并运行这个错误显示在一个应用程序的编译时间 -GWT发生器得到编译时间

Rebind result 'com.example.client.Function' must be a class 

这里是我有 -

我这是怎么叫我的生成方法 -

Function b = GWT.create(Function.class); 
label.setText(b.getBuildTime()); 

gwt.xml-

<generate-with class="example.frontend.client.gin.FunctionGenerator"> 
    <when-type-assignable class="com.example.frontend.client.gin.Function" /> 
</generate-with> 

Function.java

package com.example.frontend.client.gin; 

public interface Function{ 
    public String getBuildTime(); 
} 

生成器类 -

package com.example.frontend.egenerator; 

import java.io.PrintWriter; 
import java.util.Date; 

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.TypeOracle; 
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; 
import com.google.gwt.user.rebind.SourceWriter; 
import com.example.frontend.client.gin.Function; 

public class FunctionGenerator extends Generator { 
    private static final String IMPL_TYPE_NAME = Function.class.getSimpleName() + "Impl"; 
    private static final String IMPL_PACKAGE_NAME = Function.class.getPackage().getName(); 


    @Override 
    public String generate(final TreeLogger logger, final GeneratorContext context, final String requestedClass) throws UnableToCompleteException { 
     TypeOracle typeOracle = context.getTypeOracle(); 
     JClassType functionType = typeOracle.findType(requestedClass); 
     assert Function.class.equals(functionType.getClass()); 
     ClassSourceFileComposerFactory composerFactory = new  ClassSourceFileComposerFactory(IMPL_PACKAGE_NAME, IMPL_TYPE_NAME); 
     composerFactory.addImport(Function.class.getCanonicalName()); 
     composerFactory.addImplementedInterface(Function.class.getName()); 
     PrintWriter printWriter = context.tryCreate(logger, IMPL_PACKAGE_NAME, IMPL_TYPE_NAME); 
     SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter); 
     if(sourceWriter != null) { 
      sourceWriter.print("public String getBuildTime() {"); 
      sourceWriter.print(" return \"" + new Date() + "\" ;"); 
      sourceWriter.print("}"); 
      sourceWriter.commit(logger); 
     } 
     return IMPL_PACKAGE_NAME + "." + IMPL_TYPE_NAME; 
    } 
} 

任何想法,我缺少的是什么?

回答

2

我相信你也需要检查由tryCreate创建的PrintWriter,因为它可能会返回null。另一方面,createSourceWriter不会返回null,所以不需要检查null。

您的generate-with也是不正确的,至少对于您在这里的示例。它应该有一个不同的包(根据您的FunctionGenerator源至少),com.example.frontend.egenerator,不com.example.frontend.client.gin

<generate-with class="com.example.frontend.egenerator.FunctionGenerator"> 
    <when-type-assignable class="com.example.frontend.client.gin.Function" /> 
</generate-with> 

一般来说如果没有其他原因,而不是防止虚假的错误,您的发电机不应该在client包,这会减慢编译器的速度(并且真的是减慢超级开发模式)。


除此之外,完整的日志可以帮助很多追查问题,但不映射生成正确就不会有太大的错误。另外一定要在编译时使用strict编译生成器,以确保编译器尽快失败,并且可以在第一个错误时停止。


与所有的说的,倾向于避免在这一点上新的发电机 - 他们将略有减缓超级开发模式(因为它们必须重新运行每刷新一次),他们将不支持在未来版本的GWT中。注释处理程序(又名APT)是实现这一目的的首选方式,但在您的情况下,您可能也只能通过插件生成ant或maven类。