1st implementation of interface extender methods jdk8
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Aug 2014 19:58:05 +0200
branchjdk8
changeset 16732d3d0b72af04
parent 1672 1efb2645c7aa
child 1674 eca8e9c3ec3e
1st implementation of interface extender methods
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/Defaults.java
rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/DefaultsTest.java
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Aug 17 08:27:45 2014 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Aug 17 19:58:05 2014 +0200
     1.3 @@ -261,6 +261,19 @@
     1.4          append("\n    function fillInstOf(x) {");
     1.5          String instOfName = "$instOf_" + className;
     1.6          append("\n        Object.defineProperty(x, '").append(instOfName).append("', { value : true });");
     1.7 +        if (jc.isInterface()) {
     1.8 +            for (MethodData m : jc.getMethods()) {
     1.9 +                if ((m.getAccess() & ACC_ABSTRACT) == 0
    1.10 +                        && (m.getAccess() & ACC_STATIC) == 0
    1.11 +                        && (m.getAccess() & ACC_PRIVATE) == 0) {
    1.12 +                    final String mn = findMethodName(m, new StringBuilder());
    1.13 +                    append("\n        try {");
    1.14 +                    append("\n          Object.defineProperty(x, '").append(mn).append("', { value : c['").append(mn).append("']});");
    1.15 +                    append("\n        } catch (ignore) {");
    1.16 +                    append("\n        }");
    1.17 +                }
    1.18 +            }
    1.19 +        }
    1.20          for (String superInterface : jc.getSuperInterfaces()) {
    1.21              String intrfc = superInterface.replace('/', '_');
    1.22              append("\n      vm.").append(intrfc).append("(false)['fillInstOf'](x);");
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/Defaults.java	Sun Aug 17 19:58:05 2014 +0200
     2.3 @@ -0,0 +1,48 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.vm8;
    2.22 +
    2.23 +public interface Defaults {
    2.24 +    public static int staticValue() {
    2.25 +        return 42;
    2.26 +    }
    2.27 +    
    2.28 +    public default int value() {
    2.29 +        return 42;
    2.30 +    }
    2.31 +    
    2.32 +    public static Defaults create(boolean overriden) {
    2.33 +        class X implements Defaults {
    2.34 +        }
    2.35 +        class Y implements Defaults {
    2.36 +            @Override
    2.37 +            public int value() {
    2.38 +                return 7;
    2.39 +            }
    2.40 +        }
    2.41 +        return overriden ? new Y() : new X();
    2.42 +    }
    2.43 +    
    2.44 +    public static int defaultValue() {
    2.45 +        return create(false).value();
    2.46 +    }
    2.47 +    
    2.48 +    public static int myValue() {
    2.49 +        return create(true).value();
    2.50 +    }
    2.51 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/DefaultsTest.java	Sun Aug 17 19:58:05 2014 +0200
     3.3 @@ -0,0 +1,56 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.vm8;
    3.22 +
    3.23 +import org.testng.annotations.AfterClass;
    3.24 +import org.testng.annotations.BeforeClass;
    3.25 +import org.testng.annotations.Test;
    3.26 +
    3.27 +public class DefaultsTest {
    3.28 +    private static TestVM code;
    3.29 +    
    3.30 +    @Test public void callStatic() throws Exception {
    3.31 +        Object js = code.execCode("Value from static method",
    3.32 +            Defaults.class, "staticValue__I", 42
    3.33 +        );
    3.34 +    }
    3.35 +
    3.36 +    @Test public void defaultValue() throws Exception {
    3.37 +        Object js = code.execCode("Value from interface",
    3.38 +            Defaults.class, "defaultValue__I", 42
    3.39 +        );
    3.40 +    }
    3.41 +
    3.42 +    @Test public void overridenValue() throws Exception {
    3.43 +        Object js = code.execCode("Value from class",
    3.44 +            Defaults.class, "myValue__I", 7
    3.45 +        );
    3.46 +    }
    3.47 +
    3.48 +    @BeforeClass
    3.49 +    public static void compileTheCode() throws Exception {
    3.50 +        code = TestVM.compileClass(
    3.51 +                "org/apidesign/bck2brwsr/vm8/Defaults");
    3.52 +    }
    3.53 +
    3.54 +    @AfterClass
    3.55 +    public static void releaseTheCode() {
    3.56 +        code = null;
    3.57 +    }
    3.58 +    
    3.59 +}