We can read definition of Object from the JDK. No need to provide default dummy implementation. InstanceSub is discovered automatically.
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Sep 2012 14:35:21 +0200
changeset 192291e553464a
parent 18 361b76189f8d
child 20 0e7dd9e2e31e
We can read definition of Object from the JDK. No need to provide default dummy implementation. InstanceSub is discovered automatically.
src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
src/test/java/org/apidesign/java4browser/InstanceTest.java
src/test/java/org/apidesign/java4browser/StaticMethodTest.java
     1.1 --- a/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Thu Sep 20 14:20:01 2012 +0200
     1.2 +++ b/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Thu Sep 20 14:35:21 2012 +0200
     1.3 @@ -83,8 +83,6 @@
     1.4                  compiler.generateStaticField(v);
     1.5              }
     1.6          }
     1.7 -        out.append("function java_lang_Object(){}\n"); // XXX temporary
     1.8 -        out.append("function java_lang_Object_consV(self){}\n"); // XXX temporary
     1.9          
    1.10          final String className = jc.getName().getInternalName().replace('/', '_');
    1.11          out.append("\nfunction ").append(className);
    1.12 @@ -576,18 +574,20 @@
    1.13      }
    1.14  
    1.15      private String findMethodName(Method m) {
    1.16 -        StringBuilder out = new StringBuilder();
    1.17 +        StringBuilder tmp = new StringBuilder();
    1.18          if ("<init>".equals(m.getName())) { // NOI18N
    1.19 -            out.append("consV"); // NOI18N
    1.20 +            tmp.append("consV"); // NOI18N
    1.21 +        } else if ("<clinit>".equals(m.getName())) { // NOI18N
    1.22 +            tmp.append("classV"); // NOI18N
    1.23          } else {
    1.24 -            out.append(m.getName());
    1.25 -            outType(m.getReturnType(), out);
    1.26 +            tmp.append(m.getName());
    1.27 +            outType(m.getReturnType(), tmp);
    1.28          } 
    1.29          List<Parameter> args = m.getParameters();
    1.30          for (Parameter t : args) {
    1.31 -            outType(t.getDescriptor(), out);
    1.32 +            outType(t.getDescriptor(), tmp);
    1.33          }
    1.34 -        return out.toString();
    1.35 +        return tmp.toString();
    1.36      }
    1.37  
    1.38      private String findMethodName(CPMethodInfo mi, int[] cnt, boolean[] hasReturn) {
     2.1 --- a/src/test/java/org/apidesign/java4browser/InstanceTest.java	Thu Sep 20 14:20:01 2012 +0200
     2.2 +++ b/src/test/java/org/apidesign/java4browser/InstanceTest.java	Thu Sep 20 14:35:21 2012 +0200
     2.3 @@ -77,8 +77,7 @@
     2.4      private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
     2.5          StringBuilder sb = new StringBuilder();
     2.6          Invocable i = StaticMethodTest.compileClass(sb, 
     2.7 -            "org/apidesign/java4browser/Instance",
     2.8 -            "org/apidesign/java4browser/InstanceSub"
     2.9 +            "org/apidesign/java4browser/Instance"
    2.10          );
    2.11          
    2.12          Object ret = null;
     3.1 --- a/src/test/java/org/apidesign/java4browser/StaticMethodTest.java	Thu Sep 20 14:20:01 2012 +0200
     3.2 +++ b/src/test/java/org/apidesign/java4browser/StaticMethodTest.java	Thu Sep 20 14:35:21 2012 +0200
     3.3 @@ -20,7 +20,9 @@
     3.4  import java.io.IOException;
     3.5  import java.io.InputStream;
     3.6  import java.util.Arrays;
     3.7 +import java.util.HashSet;
     3.8  import java.util.Iterator;
     3.9 +import java.util.LinkedList;
    3.10  import java.util.Set;
    3.11  import java.util.TreeSet;
    3.12  import javax.script.Invocable;
    3.13 @@ -156,13 +158,25 @@
    3.14      }
    3.15  
    3.16      static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
    3.17 -        for (String name : names) {
    3.18 +        if (sb == null) {
    3.19 +            sb = new StringBuilder();
    3.20 +        }
    3.21 +        Set<String> processed = new HashSet<String>();
    3.22 +
    3.23 +        LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
    3.24 +        for (;;) {
    3.25 +            toProcess.removeAll(processed);
    3.26 +            if (toProcess.isEmpty()) {
    3.27 +                break;
    3.28 +            }
    3.29 +            String name = toProcess.getFirst();
    3.30 +            processed.add(name);
    3.31 +            if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
    3.32 +                continue;
    3.33 +            }
    3.34              InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
    3.35              assertNotNull(is, "Class file found");
    3.36 -            if (sb == null) {
    3.37 -                sb = new StringBuilder();
    3.38 -            }
    3.39 -            ByteCodeToJavaScript.compile(is, sb, null);
    3.40 +            ByteCodeToJavaScript.compile(is, sb, toProcess);
    3.41          }
    3.42          ScriptEngineManager sem = new ScriptEngineManager();
    3.43          ScriptEngine js = sem.getEngineByExtension("js");