原文在这里? 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。
?