/** * Writes a loadCurrencyMapNative method for the current locale, based on its currency data and * its superclass (if any). As currencies are included in this method, their names are added to * {@code nameMap} for later use. * * <p>If no new currency data is added for this locale over its superclass, the method is omitted * entirely. * * @param writer SourceWriter instance to use for writing the class * @param currencies array of valid currency names in the order they should be listed * @param allCurrencyData map of currency codes to currency data for the current locale, including * all inherited currencies data */ private void writeCurrencyMethodNative( SourceWriter writer, String[] currencies, Map<String, CurrencyInfo> allCurrencyData) { boolean needHeader = true; for (String currencyCode : currencies) { // TODO(jat): only emit new data where it differs from superclass! CurrencyInfo currencyInfo = allCurrencyData.get(currencyCode); if (needHeader) { needHeader = false; writer.println(); writer.println("@Override"); writer.println("protected JavaScriptObject loadCurrencyMapNative() {"); writer.indent(); writer.println( "return overrideMap(super.loadCurrencyMapNative(), loadMyCurrencyMapOverridesNative());"); writer.outdent(); writer.println("}"); writer.println(); writer.println("private native JavaScriptObject loadMyCurrencyMapOverridesNative() /*-{"); writer.indent(); writer.println("return {"); writer.indent(); } writer.println("// " + currencyInfo.getDisplayName()); writer.println("\"" + quote(currencyCode) + "\": " + currencyInfo.getJson() + ","); } if (!needHeader) { writer.outdent(); writer.println("};"); writer.outdent(); writer.println("}-*/;"); } }
/** * Generate the implementation for a single locale, overriding from its parent only data that has * changed in this locale. * * @param logger * @param context * @param targetClass * @param locale * @param superClassName * @param currencies the set of currencies defined in this locale * @param allCurrencyData map of currency code -> unparsed CurrencyInfo for that code * @param defCurrencyCode default currency code for this locale * @return fully-qualified class name generated */ private String generateOneLocale( TreeLogger logger, GeneratorContext context, JClassType targetClass, GwtLocale locale, String superClassName, String[] currencies, Map<String, CurrencyInfo> allCurrencyData, String defCurrencyCode) { String packageName = targetClass.getPackage().getName(); String className = targetClass.getName().replace('.', '_') + "_" + locale.getAsString(); PrintWriter pw = context.tryCreate(logger, packageName, className); if (pw != null) { ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, className); factory.setSuperclass(superClassName); factory.addImport(CURRENCY_DATA); factory.addImport(JAVASCRIPTOBJECT); factory.addImport(HASHMAP); SourceWriter writer = factory.createSourceWriter(context, pw); if (defCurrencyCode != null) { CurrencyInfo currencyInfo = allCurrencyData.get(defCurrencyCode); if (currencyInfo == null) { // Synthesize a null info if the specified default wasn't found. currencyInfo = new CurrencyInfo(defCurrencyCode, null, null); allCurrencyData.put(defCurrencyCode, currencyInfo); } writer.println(); writer.println("@Override"); writer.println("protected CurrencyData getDefaultJava() {"); writer.println(" return " + currencyInfo.getJava() + ";"); writer.println("}"); writer.println(); writer.println("@Override"); writer.println("protected native CurrencyData getDefaultNative() /*-{"); writer.println(" return " + currencyInfo.getJson() + ";"); writer.println("}-*/;"); } if (currencies.length > 0) { writeCurrencyMethodJava(writer, currencies, allCurrencyData); writeCurrencyMethodNative(writer, currencies, allCurrencyData); writeNamesMethodJava(writer, currencies, allCurrencyData); writeNamesMethodNative(writer, currencies, allCurrencyData); } writer.commit(logger); } return packageName + "." + className; }