|
Boost-Commit : |
From: grafik_at_[hidden]
Date: 2007-07-25 01:00:29
Author: grafik
Date: 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
New Revision: 7535
URL: http://svn.boost.org/trac/boost/changeset/7535
Log:
Code to test feasibility of redefining a Java class from within JNI.
Added:
sandbox/langbinding/libs/langbinding/test/java_jni_test/
sandbox/langbinding/libs/langbinding/test/java_jni_test/boost-build.jam
sandbox/langbinding/libs/langbinding/test/java_jni_test/build.jam
sandbox/langbinding/libs/langbinding/test/java_jni_test/native/
sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/
sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.cpp
sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.java
sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/
sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/
sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/A.java
sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/B.java
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/boost-build.jam
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/boost-build.jam 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,43 @@
+# Copyright Rene Rivera 2007.
+#
+# Distributed under the Boost Software License, Version 1.0.
+# (See accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+# For instructions see Jamfile.v2, or "bjam --help".
+
+local rule if-has-file ( file + : dir * )
+{
+ local result ;
+ if $(dir)
+ {
+ result = [ GLOB $(dir) : $(file) ] ;
+ }
+ return $(result[1]:P) ;
+}
+
+#~ Attempts to find the Boost source tree...
+
+local boost-src = [ if-has-file configure :
+ [ MATCH --boost=(.*) : $(ARGV) ]
+ $(BOOST)
+ $(.boost-build-file:D)/../boost
+ ] ;
+
+#~ Attempts to find the Boost.Build files...
+
+local boost-build-src = [ if-has-file bootstrap.jam :
+ [ MATCH --boost-build=(.*) : $(ARGV) ]
+ $(BOOST_BUILD_PATH)
+ $(BOOST_BUILD)
+ $(boost-src)/tools/build/v2
+ ] ;
+
+#~ Set some common vars to refer to the Boost sources...
+
+BOOST ?= $(boost-src) ;
+BOOST_ROOT ?= $(boost-src) ;
+
+#~ And load up Boost.Build...
+
+boost-build $(boost-build-src) ;
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/build.jam
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/build.jam 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,69 @@
+# Copyright Rene Rivera 2007.
+#
+# Distributed under the Boost Software License, Version 1.0.
+# (See accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+# Usage:
+#
+# bjam [options | properties | targets]
+#
+# Options:
+#
+# --boost=<BOOST> The directotory of a Boost source tree.
+# Default; BOOST env var (if found)
+# Default; ../boost (if found)
+#
+# --boost-build=<BOOST_BUILD>
+# The directory for the Boost.Build v2 files.
+# Default; BOOST_BUILD_PATH env var (if found)
+# Default; BOOST_BUILD env var (if found)
+# Default; <BOOST>/tools/build/v2 (if found)
+
+#~ If we have the Boost sources we can use the project...
+
+if [ GLOB $(BOOST) : [ modules.peek project : JAMFILE ] ]
+{
+ use-project /boost : $(BOOST) ;
+}
+
+path-constant JDK : "C:/Program Files/Java/jdk1.5.0_06" ;
+
+lib boost_A
+ :
+ native/boost/A.cpp
+ :
+ <include>"$(JDK)/include"
+ <include>"$(JDK)/include/win32"
+ ;
+
+install stub
+ :
+ boost_A
+ ;
+
+make native_boost_A_class
+ : native/boost/A.java
+ : @javac
+ : <dependency>stub
+ ;
+make stub_boost_A_class
+ : stub/boost/A.java
+ : @javac
+ : <dependency>stub
+ ;
+make stub_boost_B_class
+ : stub/boost/B.java
+ : @javac
+ : <dependency>stub_boost_A_class
+ ;
+
+rule javac ( targets * : sources * : properties * )
+{
+ JAVAC on $(<) = $(JDK)/bin/javac ;
+}
+
+actions javac
+{
+ "$(JAVAC)" -classpath "$(>[1]:P)/.." -d "$(>[1]:P)/.." "$(>[1])"
+}
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.cpp
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.cpp 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,49 @@
+#include <jni.h>
+#include <cstdio>
+#include <cassert>
+
+extern "C" JNIEXPORT void JNICALL foobar(JNIEnv *, jclass)
+{
+ fputs("Here we are, in foobar().",stderr);
+}
+
+namespace
+{
+ JNINativeMethod jniMethods[] = {
+ { "foo", "()V", &foobar }
+ };
+}
+
+extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+ fputs("A.cpp:JNI_OnLoad...",stderr);
+
+ JNIEnv * env = 0;
+ assert( JNI_EVERSION != vm->GetEnv((void**)&env,JNI_VERSION_1_2) );
+
+ jclass system_C = env->FindClass("java/lang/System");
+ jmethodID systemGetSecurityManager_M
+ = env->GetStaticMethodID(system_C,"getSecurityManager","()Ljava/lang/SecurityManager");
+ jclass securityManager_C = env->FindClass("java/lang/SecurityManager");
+ jmethodID securityManagerCurrentClassLoader_M
+ = env->GetMethodID(securityManager_C,"currentClassLoader","()Ljava/lang/ClassLoader");
+
+ jobject securityManager = env->CallStaticObjectMethod(system_C,systemGetSecurityManager_M);
+ jobject classLoader = env->CallObjectMethod(securityManager,securityManagerCurrentClassLoader_M);
+
+ FILE * f = std::fopen("../native/boost/A.class","rb"); assert( f );
+ std::fseek(f,0,SEEK_END);
+ long bufferLength = std::ftell(f)+1;
+ assert( bufferLength > 0 );
+ jbyte * buffer = new jbyte[bufferLength];
+ std::rewind(f);
+ assert( std::fread(buffer,1,bufferLength,f) == bufferLength );
+ std::fclose(f);
+ jclass a_C = env->DefineClass("boost/A",classLoader,buffer,bufferLength);
+ delete[] buffer;
+ assert( a_C );
+
+ assert( 0 == env->RegisterNatives(a_C,jniMethods,1) );
+
+ return JNI_VERSION_1_2;
+}
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.java
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/native/boost/A.java 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,6 @@
+package boost;
+
+public class A
+{
+ public static native void foo();
+}
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/A.java
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/A.java 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,9 @@
+package boost;
+
+public class A
+{
+ static {
+ java.lang.System.out.println("stub/boost/A/static_init...");
+ java.lang.System.loadLibrary("boost_A.dll");
+ }
+}
Added: sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/B.java
==============================================================================
--- (empty file)
+++ sandbox/langbinding/libs/langbinding/test/java_jni_test/stub/boost/B.java 2007-07-25 01:00:28 EDT (Wed, 25 Jul 2007)
@@ -0,0 +1,11 @@
+package boost;
+
+import boost.A;
+
+public class B extends boost.A
+{
+ public static void bar()
+ {
+ boost.A.foo();
+ }
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk