Skip to content

Commit

Permalink
Add support for Java 8.
Browse files Browse the repository at this point in the history
Support for java -source 1.8
Also change Travis to use 3x3 test matrix (jdk x nvm).
  • Loading branch information
jimlloyd committed Oct 21, 2014
1 parent 3527d4f commit 9497ebc
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 12 deletions.
20 changes: 15 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
language: java
jdk:
- oraclejdk8
- oraclejdk7
- openjdk6
env:
- NODE_VERSION="0.11"
- NODE_VERSION="0.10"
- NODE_VERSION="0.8"
before_install:
- nvm install $NODE_VERSION
before_script:
- npm install
script:
- npm test
notifications:
email:
on_success: "never"
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ npm test

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

### Java 1.8 support

Manual compilation for Java 1.8 support requires additional steps:

```bash
./compile-java-code.sh
./compile-java8-code.sh
node-gyp configure build
npm test
npm test8
```

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).

## Installation node-webkit

```bash
Expand Down Expand Up @@ -208,7 +222,7 @@ __Example__
var Test = java.import('Test');
Test.someStaticMethodSync(5);
console.log(Test.someStaticField);
var value1 = Test.NestedEnum.Value1;
var test = new Test();
Expand Down
14 changes: 14 additions & 0 deletions compile-java8-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -e

# This script must be run on a Mac due to its reliance on /usr/libexec/java_home
# to find a JDK 1.8 installation. Note that this script will work correctly on
# a mac with JDK 1.8 installed, even if JAVA_HOME currently points to a 1.7
# or earlier JDK.
# This script is run manually by maintainers of this project, who add the
# the generated .class files to source control.

JAVA_VERSION=1.8
JDK8_HOME=$(/usr/libexec/java_home -v ${JAVA_VERSION})

cd test8
${JDK8_HOME}/bin/javac -source ${JAVA_VERSION} *.java
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"nodeunit": "0.9.0"
},
"scripts": {
"test": "nodeunit test",
"test": "nodeunit test test8",
"postinstall": "node postInstall.js"
},
"main": "./index.js"
Expand Down
12 changes: 9 additions & 3 deletions src/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ v8::Local<v8::Value> Java::createJVM(JavaVM** jvm, JNIEnv** env) {
}

JNI_GetDefaultJavaVMInitArgs(&args);
args.version = JNI_VERSION_1_6;
args.version = JNI_BEST_VERSION;
args.ignoreUnrecognized = false;
args.options = vmOptions;
args.nOptions = vmOptionsCount;
Expand Down Expand Up @@ -1050,6 +1050,12 @@ NAN_METHOD(Java::instanceOf) {
void EIO_CallJs(uv_work_t* req) {
}

static std::string int_to_string(int i) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", i);
return std::string(buf);
}

#if NODE_MINOR_VERSION >= 10
void EIO_AfterCallJs(uv_work_t* req, int status) {
#else
Expand All @@ -1062,10 +1068,10 @@ void EIO_AfterCallJs(uv_work_t* req) {
dynamicProxyData->result = NULL;

JNIEnv* env;
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_VERSION_1_6);
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_BEST_VERSION);
if (ret != JNI_OK) {
dynamicProxyData->throwableClass = "java/lang/IllegalStateException";
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + ret;
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + int_to_string(ret);
dynamicProxyData->done = DYNAMIC_PROXY_JS_ERROR;
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/java.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <string>
#include <nan.h>

#ifdef JNI_VERSION_1_8
#define JNI_BEST_VERSION JNI_VERSION_1_8
#else
#define JNI_BEST_VERSION JNI_VERSION_1_6
#endif

class Java : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> target);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ std::string javaMethodCallToString(JNIEnv *env, jobject obj, jmethodID methodId,

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

if (ret == JNI_EDETACHED) {
JavaVMAttachArgs attachArgs;
attachArgs.version = JNI_VERSION_1_6;
attachArgs.version = JNI_BEST_VERSION;
attachArgs.name = NULL;
attachArgs.group = NULL;
jvm->AttachCurrentThread((void**)&env, &attachArgs);
Expand Down
Binary file added test8/TestLambda$IntegerMath.class
Binary file not shown.
Binary file added test8/TestLambda.class
Binary file not shown.
18 changes: 18 additions & 0 deletions test8/TestLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class TestLambda
{
public TestLambda() {}

interface IntegerMath {
int op(int a, int b);
}

public int testLambdaAddition(Integer x, Integer y) {
IntegerMath addition = (a, b) -> a + b;
return addition.op(x, y);
}

public int testLambdaSubtraction(Integer x, Integer y) {
IntegerMath subtraction = (a, b) -> a - b;
return subtraction.op(x, y);
}
}
27 changes: 27 additions & 0 deletions test8/testLambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var java = require("../testHelpers").java;

var nodeunit = require("nodeunit");
var util = require("util");

exports['Java8'] = nodeunit.testCase({
"call methods of a class that uses lambda expressions": function(test) {
try {
var TestLambda = java.import('TestLambda');
var lambda = new TestLambda();
var sum = lambda.testLambdaAdditionSync(23, 42);
test.equal(sum, 65);
var diff = lambda.testLambdaSubtractionSync(23, 42);
test.equal(diff, -19);
}
catch (err) {
var unsupportedVersion = err.toString().match(/Unsupported major.minor version 52.0/)
test.ok(unsupportedVersion);
if (unsupportedVersion)
console.log('JRE 1.8 not available');
else
console.error('Java8 test failed with unknown error:', err);
}
test.done();
}
});

1 change: 1 addition & 0 deletions testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ java.options.push("-Djava.awt.headless=true");

java.classpath.push("test/");
java.classpath.push("test/commons-lang3-3.1.jar");
java.classpath.push("test8/");

module.exports.java = java;

0 comments on commit 9497ebc

Please sign in to comment.