diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TRunnable.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TRunnable.java new file mode 100644 index 000000000..96ad56ab4 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TRunnable.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +/** + * + * @author Alexey Andreev + */ +public interface TRunnable { + void run(); +} diff --git a/teavm-core/src/main/java/org/teavm/dependency/DependencyChecker.java b/teavm-core/src/main/java/org/teavm/dependency/DependencyChecker.java index 4335f1dbc..9957f5c9b 100644 --- a/teavm-core/src/main/java/org/teavm/dependency/DependencyChecker.java +++ b/teavm-core/src/main/java/org/teavm/dependency/DependencyChecker.java @@ -66,17 +66,21 @@ public class DependencyChecker implements DependencyInformation { methodCache.addKeyListener(new KeyListener() { @Override public void keyAdded(MethodReference key) { MethodGraph graph = methodCache.getKnown(key); - for (DependencyListener listener : listeners) { - listener.methodAchieved(DependencyChecker.this, graph); + if (!missingMethods.containsKey(key) && !missingClasses.containsKey(key.getClassName())) { + for (DependencyListener listener : listeners) { + listener.methodAchieved(DependencyChecker.this, graph); + } + activateDependencyPlugin(graph); } - activateDependencyPlugin(graph); } }); fieldCache.addKeyListener(new KeyListener() { @Override public void keyAdded(FieldReference key) { DependencyNode node = fieldCache.getKnown(key); - for (DependencyListener listener : listeners) { - listener.fieldAchieved(DependencyChecker.this, key, node); + if (!missingFields.containsKey(key)) { + for (DependencyListener listener : listeners) { + listener.fieldAchieved(DependencyChecker.this, key, node); + } } } }); diff --git a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java index 9a9cf809a..6cbebafc4 100644 --- a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java +++ b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java @@ -51,7 +51,8 @@ public class JavaScriptBodyGenerator implements Generator { } writer.append(")").ws().append("{").indent().softNewLine(); writer.append(body).softNewLine(); - writer.outdent().append("}).call(").append(context.getParameterName(0)); + writer.outdent().append("}).call(").append(!method.hasModifier(ElementModifier.STATIC) ? + context.getParameterName(0) : "null"); for (int i = 0; i < args.size(); ++i) { writer.append(",").ws(); wrapParameter(writer, context.getParameterName(i + 1)); diff --git a/teavm-html4j/src/test/java/net/java/html/js/tests/Bodies.java b/teavm-html4j/src/test/java/net/java/html/js/tests/Bodies.java new file mode 100644 index 000000000..1fc2b0d57 --- /dev/null +++ b/teavm-html4j/src/test/java/net/java/html/js/tests/Bodies.java @@ -0,0 +1,107 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import java.util.concurrent.Callable; +import net.java.html.js.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +final class Bodies { + @JavaScriptBody(args = { "a", "b" }, body = "return a + b;") + public static native int sum(int a, int b); + + @JavaScriptBody(args = {"r"}, javacall = true, body = "r.@java.lang.Runnable::run()();") + static native void callback(Runnable r); + + @JavaScriptBody(args = {"c"}, javacall = true, body = "return c.@java.util.concurrent.Callable::call()();") + static native Object callback(Callable c); + + @JavaScriptBody(args = {"c", "v"}, javacall = true, body = "var arr = c.@java.util.concurrent.Callable::call()(); arr.push(v); return arr;") + static native Object callbackAndPush(Callable c, String v); + + @JavaScriptBody(args = { "v" }, body = "return v;") + public static native Object id(Object v); + + @JavaScriptBody(args = { "v" }, body = "return { 'x' : v };") + public static native Object instance(int v); + + @JavaScriptBody(args = "o", body = "o.x++;") + public static native void incrementX(Object o); + + @JavaScriptBody(args = "o", body = "return o.x;") + public static native int readX(Object o); + + @JavaScriptBody(args = { "c" }, javacall = true, body = + "return c.@net.java.html.js.tests.Sum::sum(II)(40, 2);" + ) + public static native int sumIndirect(Sum c); + + @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];") + public static native Object select(Object[] arr, int index); + + @JavaScriptBody(args = { "arr" }, body = "return arr.length;") + public static native int length(Object[] arr); + + @JavaScriptBody(args = { "o", "vo" }, body = "if (vo) o = o.valueOf(); return typeof o;") + public static native String typeof(Object o, boolean useValueOf); + + @JavaScriptBody(args = { "b" }, body = "return typeof b;") + public static native String typeof(boolean b); + + @JavaScriptBody(args = { "o" }, body = "return Array.isArray(o);") + public static native boolean isArray(Object o); + + @JavaScriptBody(args = { "arr", "i", "value" }, body = "arr[i] = value; return arr[i];") + public static native String modify(String[] arr, int i, String value); + + @JavaScriptBody(args = {}, body = "return true;") + public static native boolean truth(); + + @JavaScriptBody(args = { "s" }, javacall = true, body = + "return s.@net.java.html.js.tests.Sum::sum([Ljava/lang/Object;)([1, 2, 3]);" + ) + public static native int sumArr(Sum s); +} diff --git a/teavm-html4j/src/test/java/net/java/html/js/tests/Factorial.java b/teavm-html4j/src/test/java/net/java/html/js/tests/Factorial.java new file mode 100644 index 000000000..ee6ae36b2 --- /dev/null +++ b/teavm-html4j/src/test/java/net/java/html/js/tests/Factorial.java @@ -0,0 +1,62 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import net.java.html.js.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +public final class Factorial { + int minusOne(int i) { + return i - 1; + } + + @JavaScriptBody(args = { "i" }, javacall = true,body = + "if (i <= 1) return 1;\n" + + "var im1 = this.@net.java.html.js.tests.Factorial::minusOne(I)(i);\n" + + "return this.@net.java.html.js.tests.Factorial::factorial(I)(im1) * i;" + ) + native int factorial(int n); +} diff --git a/teavm-html4j/src/test/java/net/java/html/js/tests/JavaScriptBodyTests.java b/teavm-html4j/src/test/java/net/java/html/js/tests/JavaScriptBodyTests.java new file mode 100644 index 000000000..49317dab8 --- /dev/null +++ b/teavm-html4j/src/test/java/net/java/html/js/tests/JavaScriptBodyTests.java @@ -0,0 +1,339 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import static org.junit.Assert.*; +import java.io.StringReader; +import java.util.Arrays; +import java.util.concurrent.Callable; +import org.apidesign.html.boot.spi.Fn; +import org.junit.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class JavaScriptBodyTests { + @Test public void sumTwoNumbers() { + int res = Bodies.sum(5, 3); + assertEquals(8, res); + } + + @Test public void accessJsObject() { + Object o = Bodies.instance(10); + int ten = Bodies.readX(o); + assertEquals(10, ten); + } + + @Test public void callWithNoReturnType() { + Object o = Bodies.instance(10); + Bodies.incrementX(o); + int ten = Bodies.readX(o); + assertEquals(11, ten); + } + + @Test public void callbackToRunnable() { + R run = new R(); + Bodies.callback(run); + assertEquals(1, run.cnt); + } + + /*@Test public void typeOfCharacter() { + String charType = Bodies.typeof('a', false); + assertEquals("number", charType); + }*/ + + @Test public void typeOfBoolean() { + String booleanType = Bodies.typeof(true, false); + assertEquals("boolean", equals(booleanType)); + } + + @Test public void typeOfPrimitiveBoolean() { + String booleanType = Bodies.typeof(true); + assertTrue("boolean".equals(booleanType) || "number".equals(booleanType)); + } + + @Test public void typeOfInteger() { + String intType = Bodies.typeof(1, false); + assertEquals("number", equals(intType)); + } + + @Test public void typeOfString() { + String strType = Bodies.typeof("Ahoj", false); + assertEquals("string", strType); + } + + /*@Test public void typeOfDouble() { + String doubleType = Bodies.typeof(0.33, false); + assertEquals("number", doubleType); + }*/ + + @Test public void typeOfBooleanValueOf() { + String booleanType = Bodies.typeof(true, true); + assertEquals("boolean", booleanType); + } + + @Test public void typeOfIntegerValueOf() { + String intType = Bodies.typeof(1, true); + assertEquals("number", intType); + } + + @Test public void typeOfStringValueOf() { + String strType = Bodies.typeof("Ahoj", true); + assertEquals("string", strType); + } + + /*@Test public void typeOfDoubleValueOf() { + String doubleType = Bodies.typeof(0.33, true); + assertEquals("number", doubleType); + }*/ + + @Test public void computeInARunnable() { + final int[] sum = new int[2]; + class First implements Runnable { + @Override public void run() { + sum[0] = Bodies.sum(22, 20); + sum[1] = Bodies.sum(32, 10); + } + } + Bodies.callback(new First()); + assertEquals(42, sum[0]); + assertEquals(42, sum[1]); + } + + /*@Test public void doubleCallbackToRunnable() { + final R run = new R(); + final R r2 = new R(); + class First implements Runnable { + @Override public void run() { + Bodies.callback(run); + Bodies.callback(r2); + } + } + Bodies.callback(new First()); + assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt; + assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt; + } + + @Test public void identity() { + Object p = new Object(); + Object r = Bodies.id(p); + assert r == p : "The object is the same"; + } + + @Test public void encodingString() { + Object p = "Ji\n\"Hi\"\nHon"; + Object r = Bodies.id(p); + assert p.equals(r) : "The object is the same: " + p + " != " + r; + } + + @Test public void encodingBackslashString() { + Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}"; + Object r = Bodies.id(p); + assert p.equals(r) : "The object is the same: " + p + " != " + r; + } + + @Test public void nullIsNull() { + Object p = null; + Object r = Bodies.id(p); + assert r == p : "The null is the same"; + } + + @Test public void callbackWithResult() { + Callable c = new C(); + Object b = Bodies.callback(c); + assert b == Boolean.TRUE : "Should return true"; + } + + @Test public void callbackWithParameters() { + int res = Bodies.sumIndirect(new Sum()); + assert res == 42 : "Expecting 42"; + } + + @Test public void selectFromStringJavaArray() { + String[] arr = { "Ahoj", "World" }; + Object res = Bodies.select(arr, 1); + assert "World".equals(res) : "Expecting World, but was: " + res; + } + + @Test public void selectFromObjectJavaArray() { + Object[] arr = { new Object(), new Object() }; + Object res = Bodies.select(arr, 1); + assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res; + } + + @Test public void lengthOfJavaArray() { + String[] arr = { "Ahoj", "World" }; + int res = Bodies.length(arr); + assert res == 2 : "Expecting 2, but was: " + res; + } + + @Test public void isJavaArray() { + String[] arr = { "Ahoj", "World" }; + boolean is = Bodies.isArray(arr); + assert is: "Expecting it to be an array: " + is; + } + + @Test public void javaArrayInOutIsCopied() { + String[] arr = { "Ahoj", "World" }; + Object res = Bodies.id(arr); + assert res != null : "Non-null is returned"; + assert res instanceof Object[] : "Returned an array: " + res; + assert !(res instanceof String[]) : "Not returned a string array: " + res; + + Object[] ret = (Object[]) res; + assert arr.length == ret.length : "Same length: " + ret.length; + assert arr[0].equals(ret[0]) : "Same first elem"; + assert arr[1].equals(ret[1]) : "Same 2nd elem"; + } + + @Test public void modifyJavaArrayHasNoEffect() { + String[] arr = { "Ahoj", "World" }; + String value = Bodies.modify(arr, 0, "Hello"); + assert "Hello".equals(value) : "Inside JS the value is changed: " + value; + assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0]; + } + + @Test + public void callbackWithArray() { + class A implements Callable { + @Override + public String[] call() throws Exception { + return new String[] { "Hello" }; + } + } + Callable a = new A(); + Object b = Bodies.callbackAndPush(a, "World!"); + assert b instanceof Object[] : "Returns an array: " + b; + Object[] arr = (Object[]) b; + String str = Arrays.toString(arr); + assert arr.length == 2 : "Size is two " + str; + assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0]; + assert "World!".equals(arr[1]) : "World! expected: " + arr[1]; + } + + @Test public void truth() { + assert Bodies.truth() : "True is true"; + } + + @Test public void factorial2() { + assert new Factorial().factorial(2) == 2; + } + + @Test public void factorial3() { + assert new Factorial().factorial(3) == 6; + } + + @Test public void factorial4() { + assert new Factorial().factorial(4) == 24; + } + + @Test public void factorial5() { + assert new Factorial().factorial(5) == 120; + } + + @Test public void factorial6() { + assert new Factorial().factorial(6) == 720; + } + + @Test public void sumArray() { + int r = Bodies.sumArr(new Sum()); + assert r == 6 : "Sum is six: " + r; + } + + @Test public void callLater() throws Exception{ + final Fn.Presenter p = Fn.activePresenter(); + if (p == null) { + return; + } + p.loadScript(new StringReader( + "if (typeof window === 'undefined') window = {};" + )); + Later l = new Later(); + l.register(); + p.loadScript(new StringReader( + "window.later();" + )); + for (int i = 0; i < 100 && l.call != 42; i++) { + Thread.sleep(50); + } + assert l.call == 42 : "Method was called: " + l.call; + }*/ + + private static class R implements Runnable { + int cnt; + //private final Thread initThread; + + public R() { + //initThread = Thread.currentThread(); + } + + @Override + public void run() { + //assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread(); + cnt++; + } + } + + private static class C implements Callable { + @Override + public Boolean call() throws Exception { + return Boolean.TRUE; + } + } +} diff --git a/teavm-html4j/src/test/java/net/java/html/js/tests/Later.java b/teavm-html4j/src/test/java/net/java/html/js/tests/Later.java new file mode 100644 index 000000000..71293937b --- /dev/null +++ b/teavm-html4j/src/test/java/net/java/html/js/tests/Later.java @@ -0,0 +1,65 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import net.java.html.js.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +public final class Later { + volatile int call; + + @JavaScriptBody(args = { }, javacall = true, body = + "var self = this;" + + "window.later = function() {" + + " self.@net.java.html.js.tests.Later::call(I)(42);" + + "};" + ) + native void register(); + + void call(int value) { + this.call = value; + } +} diff --git a/teavm-html4j/src/test/java/net/java/html/js/tests/Sum.java b/teavm-html4j/src/test/java/net/java/html/js/tests/Sum.java new file mode 100644 index 000000000..ae18175af --- /dev/null +++ b/teavm-html4j/src/test/java/net/java/html/js/tests/Sum.java @@ -0,0 +1,63 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +/** + * + * @author Jaroslav Tulach + */ +public final class Sum { + public int sum(int a, int b) { + return a + b; + } + + public int sum(Object[] arr) { + int s = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] instanceof Number) { + s += ((Number)arr[i]).intValue(); + } + } + return s; + } +}