GithubHelp home page GithubHelp logo

andreicalazans / cppjni Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nokia/cppjni

0.0 1.0 0.0 100 KB

A C++-Java communication framework, which aim to simplify usage of Java code in C++ code.

License: BSD 3-Clause "New" or "Revised" License

CMake 2.23% C++ 96.81% GDB 0.05% Java 0.91%

cppjni's Introduction

cppJNI

Java Native Interface (JNI) framework wrapper, which uses new C++ features in order to add type safety.

Requirements

  • Boost 1.61 or higher (http://www.boost.org)
  • C++14 compatible compiler (clang >= 3.8, gcc >= 6.0)
  • google test for UT (>= 1.8)

Quick Start

With below silly Java class:

package jni.test;

public class JNITest {
    private int privatePrimitiveField;
    private String privateObjectField;
    
    public JNITest(int primitiveParam, String objectParam) {
        this.privatePrimitiveField = primitiveParam;
        this.privateObjectField = objectParam;
    }
    
    public int getPrimitiveField() {
        return privatePrimitiveField;
    }
    
    public String getStringField() {
        return privateObjectField;
    }
}

Get back to C++ and prepare your Java class description:

#include <jni/method_signature.hpp>

//class description is a template that must derive it's sole template parameter
template<typename T>
struct MyJNIClass: T
{
    //path to your class package
    using class_path_t = typestring_is("jni/test/MyJNIClass");

    //here you create constructor signature: it returns object of your class, obviously, and takes one integer and string in turn
    MAKE_JAVA_CONSTRUCTORS(jni::types::Object<::MyJNIClass>(jni::types::Int, jni::types::String))
    
    //similarly you can declare methods
    MAKE_JAVA_METHOD(getPrimitiveField, jni::types::Method<jni::types::Int()>)
    MAKE_JAVA_METHOD(getStringField, jni::types::Method<jni::types::String()>)
};

Create JVM:

#include <virtual_machine.hpp>
...
jni::VirtualMachine vm({"-Djava.class.path=path-to-your-jar-file"});

Create some classes and work with them:

...
auto myClass = vm.loadClass<MyJNIClass>();

auto javaObject = myClass.Construct(5, "123ads");
cout << javaObject.getPrimitiveField() << " '" << javaObject.getStringField() << "'" << endl;
...

It can't be simpler!

TODO:

* registering of native methods
* Java monitors support
* arrays support
* class field access

cppjni's People

Contributors

csatarigergely avatar michal-los avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.