3

Are there any way not to define all Places in the PlaceHistoryMapper? At this moment I am using Generator in order to generate list of all places automatically, but I am not sure that this is a correct way.

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

Generator:

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("^\\[|\\]$", "");
    }
}
Slam
  • 177
  • 1
  • 8

1 Answers1

1

I'm afraid that the only way to figure out what Places and Tokenizers are in your application, without maintaining a list with them, is with a generator like you are doing.

Anyway instead of maintaining a generator I would use the @WithTokenizers annotation and let GWT generate your PlaceHistoryMapper take a look to the GWT MVP dev-guide

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

What I do in my applications is to use a script to generate activities, views, places and update gin modules and mappers based on a template.

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27