【问题标题】:How to get rid of PlaceHistoryMapper如何摆脱 PlaceHistoryMapper
【发布时间】:2013-03-16 08:53:09
【问题描述】:

有什么办法不在PlaceHistoryMapper中定义所有Places? 目前我正在使用Generator 来自动生成所有地点的列表,但我不确定这是不是正确的方法。

public class AppPlaceHistoryMapper extends AbstractPlaceHistoryMapper<Object> {

    @Override
    protected PrefixAndToken getPrefixAndToken(Place place) {
        if (place instanceof AbstractPlace) {
            return new PrefixAndToken(((AbstractPlace) place).getName(), ((AbstractPlace) place).getTokenizer()
                    .getToken((AbstractPlace) place));
        }
        throw new RuntimeException("Invalid place: " + place);
    }

    /**
     * This method will be overrided by the gwt-generated class, so any changes made in it will not be executed
     * 
     * @see PlaceMapperGenerator
     */
    @Override
    protected PlaceTokenizer<?> getTokenizer(String prefix) {
        AbstractPlace[] places = {/* List of places generated by PlaceMapperGenerator */};
        for (AbstractPlace p : places) {
            if (p.getName().equals(prefix)) {
                return p.getTokenizer();
            }
        }
        throw new RuntimeException("Unable to find place for provided prefix: " + prefix);
    }
}

生成器:

public class PlaceMapperGenerator extends Generator {

    // @formatter:off
    private static final String GENERATED_METHOD_TEMPLATE =
            "protected com.google.gwt.place.shared.PlaceTokenizer<?> getTokenizer(String prefix) {" +
                "AbstractPlace[] places = { %s };" +
                "for (AbstractPlace p : places) {" +
                    "if (p.getName().equals(prefix)) {" +
                        "return p.getTokenizer();" +
                    "}" +
                "}" +
                "throw new RuntimeException(\"Unable to find place for provided prefix: \" + prefix);" +
            "}"
    ; // @formatter:on

    @Override
    public String generate(TreeLogger logger, GeneratorContext context, String typeName) {
        JClassType type;
        try {
            type = context.getTypeOracle().getType(typeName);
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
        String implTypeName = type.getSimpleSourceName() + "Impl";
        String implPackageName = type.getPackage().getName();
        ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(implPackageName,
                implTypeName);
        composerFactory.setSuperclass(AppPlaceHistoryMapper.class.getName());
        @SuppressWarnings("resource")
        PrintWriter printWriter = context.tryCreate(logger, implPackageName, implTypeName);
        if (printWriter != null) {
            SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);
            sourceWriter.print(GENERATED_METHOD_TEMPLATE, getPlaces(context));
            sourceWriter.commit(logger);
            printWriter.close();
        }
        return composerFactory.getCreatedClassName();
    }

    private static String getPlaces(GeneratorContext context) {
        JPackage[] packages = context.getTypeOracle().getPackages();
        List<String> places = new ArrayList<String>();
        for (JPackage p : packages) {
            if (p.getName().startsWith(AbstractPlace.class.getPackage().getName())) {
                JClassType[] types = p.getTypes();
                for (JClassType type : types) {
                    if (type.getSuperclass() != null
                            && type.getSuperclass().getQualifiedSourceName().equals(AbstractPlace.class.getName())) {
                        places.add("new " + type.getQualifiedSourceName() + "()");
                    }
                }
            }
        }
        return places.toString().replaceAll("^\\[|\\]$", "");
    }
}

【问题讨论】:

    标签: java gwt gwt-places


    【解决方案1】:

    恐怕要弄清楚应用程序中的 Places 和 Tokenizer 是什么,而不用维护它们的列表,唯一的方法就是使用像你正在做的生成器。

    无论如何,我不会使用生成器,而是使用 @WithTokenizers 注释并让 GWT 生成您的 PlaceHistoryMapper 看看 GWT MVP dev-guide

    @WithTokenizers({HelloPlace.Tokenizer.class, GoodbyePlace.Tokenizer.class})
    public interface AppPlaceHistoryMapper extends PlaceHistoryMapper {}
    

    我在我的应用程序中所做的是使用脚本来生成活动、视图、地点并基于模板更新 gin 模块和映射器。

    【讨论】:

    • 好吧,我的问题是如何避免使用这种方法。
    • 我误解了你的问题,我以为你想摆脱 PlaceHistoryMapper 中的所有代码。编辑了我的回复。
    猜你喜欢
    • 2014-01-28
    • 2013-10-31
    • 2020-10-19
    • 2019-12-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多