2017-02-28 42 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類。