Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to call default method on java8 #169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Requires Ant version 1.2

<available property="jdk15"
classname="java.lang.reflect.ParameterizedType" />
<available property="jdk18"
classname="java.util.stream.Stream" />

<target name="compile" depends="compile-most,compile-jdk15">
<target name="compile" depends="compile-most,compile-jdk15,compile-jdk18">
</target>

<target name="shell" depends="compile">
Expand All @@ -30,13 +32,15 @@ Requires Ant version 1.2
<javac srcdir="src"
destdir="${classes}"
includes="org/**/*.java"
excludes="org/**/jdk15/*.java"
deprecation="on"
debug="${debug}"
includeAntRuntime="false"
encoding="UTF-8"
target="${target-jvm}"
source="${source-level}" />
source="${source-level}">
<exclude name="org/**/jdk15/*.java"/>
<exclude name="org/**/jdk18/*.java"/>
</javac>
<copy todir="${classes}">
<fileset dir="src" includes="org/**/*.properties" />
<filterset>
Expand All @@ -50,6 +54,19 @@ Requires Ant version 1.2
<javac srcdir="src"
destdir="${classes}"
includes="org/**/jdk15/*.java"
excludes="org/**/jdk18/*.java"
deprecation="on"
debug="${debug}"
includeAntRuntime="false"
encoding="UTF-8"
target="${target-jvm}"
source="${source-level}" />
</target>

<target name="compile-jdk18" if="jdk18">
<javac srcdir="src"
destdir="${classes}"
includes="org/**/jdk18/*.java"
deprecation="on"
debug="${debug}"
includeAntRuntime="false"
Expand Down
3 changes: 2 additions & 1 deletion src/org/mozilla/javascript/JavaAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ public static byte[] createAdapterCode(ObjToIntMap functionNames,
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
int mods = method.getModifiers();
if (Modifier.isStatic(mods) || Modifier.isFinal(mods)) {
if (Modifier.isStatic(mods) || Modifier.isFinal(mods) ||
VMBridge.instance.isDefaultMethod(method)) {
continue;
}
String methodName = method.getName();
Expand Down
5 changes: 5 additions & 0 deletions src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ private static void discoverAccessibleMethods(Class<?> clazz,
}
}
}
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> intface : interfaces) {
discoverAccessibleMethods(intface, map, includeProtected,
includePrivate);
}
clazz = clazz.getSuperclass();
} catch (SecurityException e) {
// Some security settings (i.e., applets) disallow
Expand Down
5 changes: 5 additions & 0 deletions src/org/mozilla/javascript/VMBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private static VMBridge makeInstance()
{
String[] classNames = {
"org.mozilla.javascript.VMBridge_custom",
"org.mozilla.javascript.jdk18.VMBridge_jdk18",
"org.mozilla.javascript.jdk15.VMBridge_jdk15",
"org.mozilla.javascript.jdk13.VMBridge_jdk13",
"org.mozilla.javascript.jdk11.VMBridge_jdk11",
Expand Down Expand Up @@ -146,4 +147,8 @@ public Iterator<?> getJavaIterator(Context cx, Scriptable scope, Object obj) {
}
return null;
}

public boolean isDefaultMethod(Method method) {
return false;
}
}
21 changes: 21 additions & 0 deletions src/org/mozilla/javascript/jdk18/VMBridge_jdk18.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.jdk18;

import java.lang.reflect.Method;

public class VMBridge_jdk18 extends org.mozilla.javascript.jdk15.VMBridge_jdk15
{
public VMBridge_jdk18() throws SecurityException, InstantiationException {
super();
}

@Override
public boolean isDefaultMethod(Method method) {
return method.isDefault();
}
}
19 changes: 19 additions & 0 deletions testsrc/jstests/java8/call-default-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("testsrc/assert.js");

function startsWith(str, prefix) {
return str.lastIndexOf(prefix, prefix.length) === 0;
}

if (startsWith(''+java.util.stream.Stream, '[JavaClass')) {
var p = new java.util.function.Predicate({
test: function(t) {
return true;
}
});
assertInstanceof(p.negate(), java.util.function.Predicate);
assertFalse(p.negate().test(0));
assertInstanceof(p.isEqual(null), java.util.function.Predicate);
assertTrue(p.isEqual(null).test(null));
}

"success";
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.tests.java8;

import org.mozilla.javascript.drivers.RhinoTest;
import org.mozilla.javascript.drivers.ScriptTestsBase;

@RhinoTest("testsrc/jstests/java8/call-default-method.js")
public class CallDefaultMethodTest extends ScriptTestsBase
{
}