# HG changeset patch # User Jaroslav Tulach # Date 1360753680 -3600 # Node ID 58ce0cd13d2688aeff6558e75c64c1e7d1259606 # Parent 8d7f8719d29c3317b4da1748d6eb5c72d86de63f Igor's dejsni files and tests. Modified to compile. diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/pom.xml --- a/ide/editor/pom.xml Wed Feb 13 11:12:55 2013 +0100 +++ b/ide/editor/pom.xml Wed Feb 13 12:08:00 2013 +0100 @@ -102,6 +102,11 @@ ${netbeans.version} test + + org.testng + testng + test + diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JNIHelper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JNIHelper.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,80 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * JNI Helper. + * To facilitate lookup of methods by name and signature, instead of manually parsing signatures, + * constructs the map of all methods and uses Class.getName() to generate almost-correct signatures. + */ +public class JNIHelper { + + static Method method(String clazz, String method, String signature) { + final Map methods = methodMap(JNIHelper.clazz(clazz)); + return methods.get(methodKey(method, signature)); + } + + static Class clazz(String clazz) { + try { + return Class.forName(clazz); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } + } + + static Map methodMap(final Class clazz) { + final Map map = new HashMap(); + final Method[] methods = clazz.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + final Method method = methods[i]; + map.put(methodKey(method.getName(), signature(method)), method); + } + return map; + } + + static String methodKey(String method, String signature) { + return method + '@' + signature; + } + + static String signature(final Method method) { + final Class[] parameterTypes = method.getParameterTypes(); + final StringBuilder b = new StringBuilder(); + for (int j = 0; j < parameterTypes.length; j++) { + b.append(signature(parameterTypes[j])); + } + return b.toString(); + } + + static String signature(final Class clazz) { + if (clazz == boolean.class) return "Z"; + else if (clazz == byte.class) return "B"; + else if (clazz == char.class) return "C"; + else if (clazz == double.class) return "D"; + else if (clazz == float.class) return "F"; + else if (clazz == int.class) return "I"; + else if (clazz == long.class) return "J"; + else if (clazz == short.class) return "S"; + else if (clazz == void.class) return "V"; + else if (clazz.isArray()) return clazz.getName().replace('.','/'); + else return "L" + clazz.getName().replace('.','/') + ";"; + } +} diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizer.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,70 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class JsniCommentTokenizer { + + /** + * Tokenize the contents of JSNI comment into the provided {@linkplain Sink}. + * @param in the contents of JSNI comment + * @param out the sink that consumes parsed tokens + */ + public void process(final String in, final Sink out) { + final Matcher member = Pattern.compile("@([^:]+)::([a-zA-Z_$][a-zA-Z\\d_$]*)").matcher(in); + final Matcher signature = Pattern.compile("\\(([^\\)]*)\\)").matcher(in); + + int i = 0; + while (true) { + if (member.find(i)) { + final int memberStart = member.start(); + final int memberEnd = member.end(); + if (memberStart > i) out.javascript(in.substring(i, memberStart)); + + final String clazz = member.group(1); + final String name = member.group(2); + + if (in.charAt(memberEnd) == '(') { + if (!signature.find(memberEnd)) { + throw new IllegalStateException("Expected method signature"); + } + assert signature.start() == memberEnd; + out.method(clazz, name, signature.group(1)); + i = signature.end(); + } else { + out.field(clazz, name); + i = memberEnd; + } + } else { + out.javascript(in.substring(i, in.length())); + break; + } + } + } + + + public static interface Sink { + void javascript(String s); + + void method(String clazz, String method, String signature); + + void field(String clazz, String field); + } +} diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/ManglingSink.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/ManglingSink.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,71 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +/** + * An implementation of {@linkplain JsniCommentTokenizer.Sink} that generates B2B + */ +class ManglingSink implements JsniCommentTokenizer.Sink { + + final StringBuilder out = new StringBuilder(); + + public void javascript(String s) { + out.append(s); + } + + public void method(String clazz, String method, String signature) { + out.append(mangle(clazz, method, signature)); + } + + public void field(String clazz, String field) { +// out.append(field); + out.append('_').append(field).append('(').append(')'); + } + + + @Override + public String toString() { + return out.toString(); + } + + + static String mangle(String clazz, String method, String signature) { + final StringBuilder builder = new StringBuilder(); + builder.append(method); + builder.append("__"); + builder.append(mangle(JNIHelper.signature(JNIHelper.method(clazz, method, signature).getReturnType()))); + builder.append(mangle(signature)); + return builder.toString(); + } + + + static String mangle(String txt) { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < txt.length(); i++) { + final char ch = txt.charAt(i); + switch (ch) { + case '/': sb.append('_'); break; + case '_': sb.append("_1"); break; + case ';': sb.append("_2"); break; + case '[': sb.append("_3"); break; + default: sb.append(ch); break; + } + } + return sb.toString(); + } +} diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/DejsniReaderTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/DejsniReaderTest.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,81 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.openide.filesystems.FileUtil; +import org.testng.annotations.Test; + +public class DejsniReaderTest { + + @Test + public void test1() throws Exception { + String s = "class Test {\n" + + " /** javadoc */\n" + + " public native void test() /*-{\n" + + " // body\n" + + " }-*/;\n" + + "}\n"; + + String expected = "class Test {\n" + + "\n" + + " /** javadoc */\n" + + " @org.apidesign.bck2brwsr.core.JavaScriptBody(args = { }, body = \"\\n // body\\n \")\n" + + " public native void test();\n" + + "}\n"; + + HintTest.create() + .input(s) + .classpath(FileUtil.getArchiveRoot(JavaScriptBody.class.getProtectionDomain().getCodeSource().getLocation())) + .run(JSNI2JavaScriptBody.class) + .findWarning("2:23-2:26:verifier:" + Bundle.ERR_JSNI2JavaScriptBody()) + .applyFix() + .assertCompilable() + .assertOutput(expected); + } + + + @Test + public void test2() throws Exception { + String s = "class Test {\n" + + " /** javadoc */\n" + + " @SuppressWarnings(\"unused\")\n" + + " // comment\n" + + " public native void test() /*-{\n" + + " // body\n" + + " }-*/;\n" + + "}\n"; + + String expected = "class Test {\n" + + "\n" + + " /** javadoc */\n" + + " @SuppressWarnings(\"unused\")\n" + + " @org.apidesign.bck2brwsr.core.JavaScriptBody(args = { }, body = \"\\n // body\\n \")\n" + + " public native void test();\n" + + "}\n"; + HintTest.create() + .input(s) + .classpath(FileUtil.getArchiveRoot(JavaScriptBody.class.getProtectionDomain().getCodeSource().getLocation())) + .run(JSNI2JavaScriptBody.class) + .findWarning("2:23-2:26:verifier:" + Bundle.ERR_JSNI2JavaScriptBody()) + .applyFix() + .assertCompilable() + .assertOutput(expected); + } +} \ No newline at end of file diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBodyTest.java --- a/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBodyTest.java Wed Feb 13 11:12:55 2013 +0100 +++ b/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JSNI2JavaScriptBodyTest.java Wed Feb 13 12:08:00 2013 +0100 @@ -18,9 +18,9 @@ package org.apidesign.bck2brwsr.ide.editor; import org.apidesign.bck2brwsr.core.JavaScriptBody; -import org.junit.Test; import org.netbeans.modules.java.hints.test.api.HintTest; import org.openide.filesystems.FileUtil; +import org.testng.annotations.Test; public class JSNI2JavaScriptBodyTest { diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizerTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizerTest.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,154 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class JsniCommentTokenizerTest { + + private static class MockSink implements JsniCommentTokenizer.Sink { + final List out = new ArrayList(); + + public void javascript(String s) { + out.add("J " + s); + } + + public void method(String clazz, String method, String signature) { + out.add("M " + clazz + "|" + method + "|" + signature); + } + + public void field(String clazz, String field) { + out.add("F " + clazz + "|" + field); + } + } + + + @Test + public void testProcess_nop() throws IOException { + final String in = "foo bar"; + final List expected = new ArrayList(); + expected.add("J foo bar"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + @Test + public void testProcess_read_static_field() throws IOException { + final String in = " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";"; + final List expected = new ArrayList(); + expected.add("J "); + expected.add("F com.google.gwt.examples.JSNIExample|myStaticField"); + expected.add("J = val + \" and stuff\";"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + @Test + public void testProcess_write_instance_field() throws IOException { + final String in = " x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + \" and stuff\";"; + final List expected = new ArrayList(); + expected.add("J x."); + expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField"); + expected.add("J = val + \" and stuff\";"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + @Test + public void testProcess_read_instance_field() throws IOException { + final String in = " var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;"; + final List expected = new ArrayList(); + expected.add("J var val = this."); + expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField"); + expected.add("J ;"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + + @Test + public void testProcess_static_method() throws IOException { + final String in = " @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);"; + final List expected = new ArrayList(); + expected.add("J "); + expected.add("M com.google.gwt.examples.JSNIExample|staticFoo|Ljava/lang/String;"); + expected.add("J (s);"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + + @Test + public void testProcess_instance_method() throws IOException { + final String in = " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);"; + final List expected = new ArrayList(); + expected.add("J x."); + expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;"); + expected.add("J (s);"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } + + + @Test + public void testProcess_multiline() throws IOException { + final String in = + " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);" + + " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";"; + final List expected = new ArrayList(); + expected.add("J x."); + expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;"); + expected.add("J (s); "); + expected.add("F com.google.gwt.examples.JSNIExample|myStaticField"); + expected.add("J = val + \" and stuff\";"); + + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer(); + final MockSink out = new MockSink(); + jsniCommentTokenizer.process(in, out); + + Assert.assertEquals(expected, out.out); + } +} diff -r 8d7f8719d29c -r 58ce0cd13d26 ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/ManglingSinkTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/ManglingSinkTest.java Wed Feb 13 12:08:00 2013 +0100 @@ -0,0 +1,58 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.ide.editor; + +import org.testng.Assert; +import org.testng.annotations.Test; + + +public class ManglingSinkTest { + + @Test + public void testMangle_1() { + Assert.assertEquals( + "binarySearch__I_3BIIB", + ManglingSink.mangle("java.util.Arrays", "binarySearch", "[BIIB") + ); + } + + @Test + public void testMangle_2() { + Assert.assertEquals( + "sort__V_3I", + ManglingSink.mangle("java.util.Arrays", "sort", "[I") + ); + } + + @Test + public void testMangle_3() { + Assert.assertEquals( + "binarySearch__I_3Ljava_lang_Object_2IILjava_lang_Object_2", + ManglingSink.mangle("java.util.Arrays", "binarySearch", "[Ljava/lang/Object;IILjava/lang/Object;") + ); + } + + + @Test + public void testField() { + final ManglingSink manglingSink = new ManglingSink(); + manglingSink.field(null, "value"); + + Assert.assertEquals("_value()", manglingSink.toString()); + } +}