svoid tok_swappableFunctions(LS tok) { jreplace(tok, "swappable static", "static swappable"); jreplace(tok, "swappable protected", "protected swappable"); jreplace(tok, "pswappable ", "persistently swappable $2"); jreplace(tok, "persistent swappable ", "persistently swappable $3"); jreplace(tok, "transient swappable ", "swappable $3"); int i; while ((i = jfind(tok, "swappable (")) >= 0) { int iModEnd = i; bool persistently = eqGet(tok, i-2, "persistently"); if (persistently) iModEnd -= 2; bool atOverride = subListEquals(tok, iModEnd-4, "@", "", "Override"); if (atOverride) iModEnd -= 4; int iMod = leftScanModifiers(tok, iModEnd); S modifiers = joinSubList(tok, iMod, iModEnd); S returnType = tok.get(i+2), name = tok.get(i+4); int iCurly = indexOf(tok, i, "{"); LS tokArgs = subList(tok, i+7, iCurly-2); LPairS args = tok_typesAndNamesOfParams(tokArgs); S args1 = join(tokArgs); S args2 = joinWithComma(pairsB(args)); bool isVoidMethod = eq(returnType, "void"); LS types = pairsA(args); if (!isVoidMethod) types.add(returnType); S typeParams = joinWithComma(map tok_toNonPrimitiveTypes(types)); // TODO: modifiers S base = name + "_base", fallback = name + "_fallback"; S mods = modifiers + (persistently || containsJavaToken(modifiers, "static") ? "" : "transient "); if (atOverride) modifiers = "@Override " + modifiers; S src; if (isVoidMethod) if (empty(args)) // no args, returning void src = mods + "Runnable \*name*/;\n" + "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.run(); else \*base*/(\*args2*/); }\n" + "final \*modifiers*/\*returnType*/ \*fallback*/(Runnable _f, \*args1*/) { if (_f != null) _f.run(); else \*base*/(); }\n" + "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {"; else { // args, returning void S varType = "IVF\*l(args)*/<\*typeParams*/>"; src = "\*mods*/ \*varType*/ \*name*/;\n" + "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.get(\*args2*/); else \*base*/(\*args2*/); }\n" + "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { if (_f != null) _f.get(\*args2*/); else \*base*/(\*args2*/); }\n" + "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {"; } else { // zero or more args, not returning void S varType = "IF\*l(args)*/<\*typeParams*/>"; src = "\*mods*/ \*varType*/ \*name*/;\n" + "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { ret \*name*/ != null ? \*name*/.get(\*args2*/) : \*base*/(\*args2*/); }\n" + "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { ret _f != null ? _f.get(\*args2*/) : \*base*/(\*args2*/); }\n" + "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {"; } replaceTokens_reTok(tok, iMod, iCurly+1, src); } }