ByteCodeToJavaScript { static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) { if (params.startsWith("(")) { params = params.substring(1); } if (params.endsWith(")")) { params = params.substring(0, params.length() - 1); } StringBuilder sb = new StringBuilder(); final String fqnu = fqn.replace('.', '_'); final String rfqn = mangleClassName(fqnu); final String rm = mangleMethodName(method); final String srp; { StringBuilder pb = new StringBuilder(); int len = params.length(); int indx = 0; while (indx < len) { char ch = params.charAt(indx); if (ch == '[' || ch == 'L') { int column = params.indexOf(';', indx) + 1; if (column > indx) { String real = params.substring(indx, column); if ("Ljava/lang/String;".equals(real)) { pb.append("Ljava/lang/String;"); indx = column; continue; } } pb.append("Ljava/lang/Object;"); indx = column; } else { pb.append(ch); indx++; } } srp = mangleSig(pb.toString()); } final String rp = mangleSig(params); final String mrp = mangleMethodName(rp); sb.append(rfqn).append("$").append(rm). append('$').append(mrp).append("__Ljava_lang_Object_2"); if (!isStatic) { sb.append('L').append(fqnu).append("_2"); } sb.append(srp); return sb.toString(); } protected  ByteCodeToJavaScript(final Appendable out); String compile(InputStream classFile);  }
@Test public void mangleJsCallbackToAType() throws Exception { String res = ByteCodeToJavaScript.mangleJsCallbacks( "org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations", "onError", "Ljava/lang/Object;", false ); assertEquals(res, "org_1apidesign_1bck2brwsr_1vmtest_1impl_1HtmlAnnotations$onError$" + "Ljava_1lang_1Object_12__" + "Ljava_lang_Object_2Lorg_apidesign_bck2brwsr_vmtest_impl_HtmlAnnotations_2Ljava_lang_Object_2", "Pretty long method name" ); }
@Test public void mangleJsCallbackToATypeWithString() throws Exception { String res = ByteCodeToJavaScript.mangleJsCallbacks( "org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations", "onMessage", "Ljava/lang/String;", false ); assertEquals(res, "org_1apidesign_1bck2brwsr_1vmtest_1impl_1HtmlAnnotations$onMessage$" + "Ljava_1lang_1String_12__" + "Ljava_lang_Object_2Lorg_apidesign_bck2brwsr_vmtest_impl_HtmlAnnotations_2Ljava_lang_String_2", "Pretty long method name" ); }