GWT 如何使用Generator

原文在这里  http://groups.google.com/group/Google-Web-Toolkit/msg/ae249ea67c2c3435?pli=1

记录在这里是方便以后可以很容易找到

 

/**
 *
 */
package com.gwt.web.client .generator;

import java.util.Map;

/**
 * @author
 *
 */
public interface PropertyMap extends Map{

}

 

/**
 *
 */
package com.gwt.web.server .generator;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

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;

/**
 * @author
 *
 */
public class PropertyMapGenerator extends Generator {
    private static final Logger log = Logger
            .getLogger(PropertyMapGenerator.class.getCanonicalName());
    private String className;
    private String typeName;
    private String packageName;

    /*
     * (non-Javadoc)
     *
     * @see
     * com.google.gwt.core.ext.Generator#generate(com.google.gwt.core.ext.TreeLogger
     * , com.google.gwt.core.ext.GeneratorContext, java.lang.String)
     */
    @Override
    public String generate(TreeLogger logger, GeneratorContext context,
            String typeName) throws UnableToCompleteException {
        log.log(Level.FINE, "generate() called with [" + typeName + "].");
        this.typeName = typeName;
        TypeOracle typeOracle = context.getTypeOracle();
        try {
            JClassType classType = typeOracle.getType(typeName);
            packageName = classType.getPackage().getName();
            className = classType.getSimpleSourceName() + "Wrapper";
            generateClass(logger, context);
        } catch (Exception ex) {
            log.log(Level.SEVERE, "Failed due to [" + ex.getMessage() + "]", ex);
        }
        return packageName + "." + className;
    }

    private void generateClass(TreeLogger logger, GeneratorContext context) {
        PrintWriter writer = context.tryCreate(logger, packageName, className);
        if (writer == null) {
            // source code already generated, just return;
            return;
        }

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(
                packageName, className);

        composer.setSuperclass("java.util.HashMap");
        SourceWriter sourceWriter = composer
                .createSourceWriter(context, writer);
        generateConstructor(sourceWriter);
        sourceWriter.outdent();
        sourceWriter.println("}");
        context.commit(logger, writer);
    }

    private void generateConstructor(SourceWriter sourceWriter) {
        // init resource bundle
        ResourceBundle bundle = ResourceBundle.getBundle(typeName);

        // start constructor source generation
        sourceWriter.println("public " + className + "() { ");
        sourceWriter.indent();
        sourceWriter.println("super();");

        // add statements to pub key/value pairs from the resrouce bundle
        for (Enumeration<String> keys = bundle.getKeys(); keys
                .hasMoreElements();) {
            String key = keys.nextElement();
            sourceWriter.println("put(\"" + Generator.escape(key) + "\", \""
                    + bundle.getString(key) + "\");");
        }

        // end constructor source generation
        sourceWriter.outdent();
        sourceWriter.println("}");
    }

}

 

在module.xml 文件里

    <generate-with class="com.gwt.web.server.generator.PropertyMapGenerator">
        <when-type-assignable class="com.gwt.web.client.generator.PropertyMap" />
    </generate-with>

 

如何使用

package com.gwt.web.client.resource;

import com.gwt.web.client.generator.PropertyMap;

/**
 * @author
 *
 */
public interface MyPropertyMap extends PropertyMap {

}

同一package下面的同名property文件的内容

message = Hello, World;

 

Map properties = GWT.create(MyPropertyMap.class);

//这里如果试图使用propertymap运行时会出错。如果在设置superclass的时候设置为PropertyMap,编译会出错:

11:01:22.273 [ERROR] [gwt_start] Line 3: The type PropertyMap cannot be the superclass of MyPropertyMapWrapper; a superclass must be a class 。 在这里消除这中诡异的和java语法不一致的方法是不适用PropertyMap,这个只是为了方便说明。另外一个方式应该可以通过从hashmap继承,而不是通过interface。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值