2017-02-28 6 views
0

私はGWTジェネレータを理解しようとしていますが、いくつかの問題に直面しています。私は発電機を使用して、このエラーに実行しているアプリでコンパイル時間を表示しようとしています -コンパイル時にGWTジェネレータ

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

これは私が私の生成メソッドを呼び出しています方法です - ここ

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

は私が持っているものです

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(); 
} 

Generatorクラス -

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

PrintWritertryCreateで作成されていることを確認する必要があると思います。返される可能性があるため、nullが返される可能性があります。一方、createSourceWriterはnullを返さないため、nullをチェックする必要はありません。

少なくともあなたがここにあるサンプルでは、​​generate-withも間違っています。それはcom.example.frontend.client.gincom.example.frontend.egenerator、(少なくともあなたのFunctionGeneratorソースに応じて)別のパッケージを持っていない必要があります。一般的には

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

を偽のエラーを防ぐに過ぎない他の理由であれば、あなたのジェネレータは、clientパッケージにすべきではありませんこれはコンパイラを遅くします(そして、は本当にスローダウンモードです)。その向こう


正しくジェネレータをマッピングすることなく、エラーの多くはそこではないでしょうが、完全なログは、問題を追跡するために多くのことを助けることができます。コンパイラーができるだけ早く失敗することを確実にするためにジェネレーターで作業する場合は、strictをオンにしてコンパイルし、最初のエラーで停止することもできます。

言ったことの全てで

、この時点で新しいジェネレータを避ける傾向にある - (彼らはあなたが更新されるたびに再実行しなければならないので)彼らはわずかスーパーのDevモードが遅くなり、彼らはサポートされません。 GWTの将来のバージョンで。注釈プロセッサ(別名APT)がこれを行うための好ましい方法ですが、あなたの場合は、プラグインを使用してantまたはmavenでクラスを生成することもできます。

関連する問題