Skip to content

Commit 9497ebc

Browse files
committed
Add support for Java 8.
Support for java -source 1.8 Also change Travis to use 3x3 test matrix (jdk x nvm).
1 parent 3527d4f commit 9497ebc

File tree

12 files changed

+108
-12
lines changed

12 files changed

+108
-12
lines changed

.travis.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
language: node_js
2-
node_js:
3-
- "0.11"
4-
- "0.10"
5-
- "0.8"
1+
language: java
2+
jdk:
3+
- oraclejdk8
4+
- oraclejdk7
5+
- openjdk6
6+
env:
7+
- NODE_VERSION="0.11"
8+
- NODE_VERSION="0.10"
9+
- NODE_VERSION="0.8"
10+
before_install:
11+
- nvm install $NODE_VERSION
12+
before_script:
13+
- npm install
14+
script:
15+
- npm test
616
notifications:
717
email:
818
on_success: "never"

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ npm test
5858

5959
_NOTE: You will need node-gyp installed using "npm install -g node-gyp"_
6060

61+
### Java 1.8 support
62+
63+
Manual compilation for Java 1.8 support requires additional steps:
64+
65+
```bash
66+
./compile-java-code.sh
67+
./compile-java8-code.sh
68+
node-gyp configure build
69+
npm test
70+
npm test8
71+
```
72+
73+
Java 1.8 language features can be used in Java classes only if a Java 1.8 JRE is available. The script compile-java8-code.sh is used only to compile java classes used in the 'test8' unit tests, but these classes are checked into the test8/ directory. Note that unit tests in the test8/ directory will pass (by design) if run against a Java 1.6 or 1.7 JRE, provided that a java.lang.UnsupportedClassVersionError is caught with the message 'Unsupported major.minor version 52.0' (the expected behavior when Java 1.8 language features are used in an older JRE).
74+
6175
## Installation node-webkit
6276

6377
```bash
@@ -208,7 +222,7 @@ __Example__
208222
var Test = java.import('Test');
209223
Test.someStaticMethodSync(5);
210224
console.log(Test.someStaticField);
211-
225+
212226
var value1 = Test.NestedEnum.Value1;
213227
214228
var test = new Test();

compile-java8-code.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash -e
2+
3+
# This script must be run on a Mac due to its reliance on /usr/libexec/java_home
4+
# to find a JDK 1.8 installation. Note that this script will work correctly on
5+
# a mac with JDK 1.8 installed, even if JAVA_HOME currently points to a 1.7
6+
# or earlier JDK.
7+
# This script is run manually by maintainers of this project, who add the
8+
# the generated .class files to source control.
9+
10+
JAVA_VERSION=1.8
11+
JDK8_HOME=$(/usr/libexec/java_home -v ${JAVA_VERSION})
12+
13+
cd test8
14+
${JDK8_HOME}/bin/javac -source ${JAVA_VERSION} *.java

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"nodeunit": "0.9.0"
3636
},
3737
"scripts": {
38-
"test": "nodeunit test",
38+
"test": "nodeunit test test8",
3939
"postinstall": "node postInstall.js"
4040
},
4141
"main": "./index.js"

src/java.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ v8::Local<v8::Value> Java::createJVM(JavaVM** jvm, JNIEnv** env) {
161161
}
162162

163163
JNI_GetDefaultJavaVMInitArgs(&args);
164-
args.version = JNI_VERSION_1_6;
164+
args.version = JNI_BEST_VERSION;
165165
args.ignoreUnrecognized = false;
166166
args.options = vmOptions;
167167
args.nOptions = vmOptionsCount;
@@ -1050,6 +1050,12 @@ NAN_METHOD(Java::instanceOf) {
10501050
void EIO_CallJs(uv_work_t* req) {
10511051
}
10521052

1053+
static std::string int_to_string(int i) {
1054+
char buf[32];
1055+
snprintf(buf, sizeof(buf), "%d", i);
1056+
return std::string(buf);
1057+
}
1058+
10531059
#if NODE_MINOR_VERSION >= 10
10541060
void EIO_AfterCallJs(uv_work_t* req, int status) {
10551061
#else
@@ -1062,10 +1068,10 @@ void EIO_AfterCallJs(uv_work_t* req) {
10621068
dynamicProxyData->result = NULL;
10631069

10641070
JNIEnv* env;
1065-
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_VERSION_1_6);
1071+
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_BEST_VERSION);
10661072
if (ret != JNI_OK) {
10671073
dynamicProxyData->throwableClass = "java/lang/IllegalStateException";
1068-
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + ret;
1074+
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + int_to_string(ret);
10691075
dynamicProxyData->done = DYNAMIC_PROXY_JS_ERROR;
10701076
return;
10711077
}

src/java.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#include <string>
99
#include <nan.h>
1010

11+
#ifdef JNI_VERSION_1_8
12+
#define JNI_BEST_VERSION JNI_VERSION_1_8
13+
#else
14+
#define JNI_BEST_VERSION JNI_VERSION_1_6
15+
#endif
16+
1117
class Java : public node::ObjectWrap {
1218
public:
1319
static void Init(v8::Handle<v8::Object> target);

src/utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ std::string javaMethodCallToString(JNIEnv *env, jobject obj, jmethodID methodId,
127127

128128
JNIEnv* javaGetEnv(JavaVM* jvm, jobject classLoader) {
129129
JNIEnv *env = NULL;
130-
int ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
130+
int ret = jvm->GetEnv((void**)&env, JNI_BEST_VERSION);
131131

132132
if (ret == JNI_EDETACHED) {
133133
JavaVMAttachArgs attachArgs;
134-
attachArgs.version = JNI_VERSION_1_6;
134+
attachArgs.version = JNI_BEST_VERSION;
135135
attachArgs.name = NULL;
136136
attachArgs.group = NULL;
137137
jvm->AttachCurrentThread((void**)&env, &attachArgs);

test8/TestLambda$IntegerMath.class

195 Bytes
Binary file not shown.

test8/TestLambda.class

1.17 KB
Binary file not shown.

test8/TestLambda.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class TestLambda
2+
{
3+
public TestLambda() {}
4+
5+
interface IntegerMath {
6+
int op(int a, int b);
7+
}
8+
9+
public int testLambdaAddition(Integer x, Integer y) {
10+
IntegerMath addition = (a, b) -> a + b;
11+
return addition.op(x, y);
12+
}
13+
14+
public int testLambdaSubtraction(Integer x, Integer y) {
15+
IntegerMath subtraction = (a, b) -> a - b;
16+
return subtraction.op(x, y);
17+
}
18+
}

0 commit comments

Comments
 (0)