2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.vm8;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.concurrent.Callable;
24 import org.apidesign.bck2brwsr.vmtest.Compare;
25 import org.apidesign.bck2brwsr.vmtest.VMTest;
26 import org.testng.annotations.Factory;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class LambdasTest extends LambdasSuper {
33 @Compare public String StringverifyJSTime() throws Exception {
34 return Lambdas.compound();
38 public int canCallLambda() {
40 Runnable incr = () -> {
48 public String lambdaReturnsString() throws Exception {
49 Callable<String> lambda = () -> "Hello World!";
53 private interface Convertor<P, R> {
55 public R convert(P value);
59 public int convertToLength() {
60 Convertor<String, Integer> lambda = (String s) -> s.getBytes().length;
61 return lambda.convert("buu");
64 private int meaningOfWorld = 0;
67 public int accessToInstanceVar() {
68 Runnable lambda = () -> {
72 return meaningOfWorld;
76 public int localMeaningOfWorld() {
77 int[] meansOfWorld = new int[1];
78 Runnable lambda = () -> {
82 return meansOfWorld[0];
86 public int useLocalVars() throws Exception {
95 Callable<Integer> lambda = () -> (int) ((bool ? 1 : 0) + b + s + i + l + f + d + c);
100 public String callVirtualMethod() throws Exception {
102 Callable<String> ref = foo::toUpperCase;
107 public int callInterfaceMethod() throws Exception {
108 List<String> foos = Arrays.asList("foo");
109 Callable<Integer> ref = foos::size;
114 public long callStaticMethod() throws Exception {
115 long expected = System.currentTimeMillis();
116 Callable<Long> ref = System::currentTimeMillis;
117 return ref.call() & ~0xffff;
121 public String callConstructor() throws Exception {
122 Callable<List<String>> ref = ArrayList<String>::new;
123 return ref.call().toString();
127 public String superMethodOverridenByThis() throws Exception {
128 Callable<String> ref = super::inheritedMethod;
133 String inheritedMethod() {
134 return "overridden version";
138 public String referencePrivateClassMethod() throws Exception {
139 StringBuilder sb = new StringBuilder();
141 Callable<String> ref1 = LambdasTest::privateClassMethod;
142 sb.append(ref1.call());
144 Callable<String> ref2 = this::privateInstanceMethod;
145 sb.append("\n").append(ref2.call());
147 // Normal method calls should still work after our magic
148 // of making them them accessible from the lambda classes.
149 sb.append("\n").append(privateClassMethod());
150 sb.append("\n").append(privateInstanceMethod());
152 return sb.toString();
155 private String privateInstanceMethod() {
159 private static String privateClassMethod() {
163 private String unrelatedPrivateMethod() {
167 @Factory public static Object[] create() {
168 return VMTest.create(LambdasTest.class);