Browse Source

...

JNI-44
Volker Birk 7 years ago
parent
commit
6d616ce2c3
9 changed files with 186 additions and 63 deletions
  1. +9
    -11
      src/Makefile
  2. +1
    -1
      src/gen_java_exceptions.ysl2
  3. +64
    -0
      src/gen_throw_pEp_exception.ysl2
  4. +40
    -0
      src/jniutils.cc
  5. +15
    -0
      src/jniutils.hh
  6. +22
    -0
      src/org/pEp/jniadapter/AbstractEngine.java
  7. +1
    -15
      src/org/pEp/jniadapter/Engine.java
  8. +2
    -36
      src/org_pEp_jniadapter_AbstractEngine.cc
  9. +32
    -0
      src/textutils.ysl2

+ 9
- 11
src/Makefile View File

@ -3,27 +3,25 @@ JAR=jniadapter.jar
all: $(JAR) $(LIBRARY)
JAVASOURCE=$(wildcard org/pEp/jniadapter/*.java) org/pEp/jniadapter/pEpException.java
$(JAR): $(JAVASOURCE)
javac $<
$(JAR): org/pEp/jniadapter/pEpException.java
javac org/pEp/jniadapter/*.java
jar cf $@ org/pEp/jniadapter/*.class
org_pEp_jniadapter_Engine.h: org/pEp/jniadapter/Engine.java
javah org.pEp.jniadapter.Engine
org_pEp_jniadapter_AbstractEngine.h: org/pEp/jniadapter/Engine.java
javah org.pEp.jniadapter.AbstractEngine
org_pEp_jniadapter_Engine.o: org_pEp_jniadapter_Engine.h org_pEp_jniadapter_Engine.cc throw_pEp_exception.hh
org_pEp_jniadapter_AbstractEngine.o: org_pEp_jniadapter_AbstractEngine.h org_pEp_jniadapter_AbstractEngine.cc throw_pEp_exception.hh jniutils.hh
$(LIBRARY): org_pEp_jniadapter_Engine.o throw_pEp_exception.o
ar -r $@ $<
$(LIBRARY): org_pEp_jniadapter_AbstractEngine.o throw_pEp_exception.o jniutils.o
ar -r $@ *.o
.PHONY: clean generate distclean
clean:
rm -f $(JAR) $(LIBRARY)
rm -f org_pEp_jniadapter_Engine.o
rm -f org_pEp_jniadapter_Engine.h
rm -f *.o
rm -f *.class
rm -f org_pEp_jniadapter_*.h
rm -f org/pEp/jniadapter/*.class
rm -f org/pEp/jniadapter/pEp*.java
rm -f throw_pEp_exception.*


+ 1
- 1
src/gen_java_exceptions.ysl2 View File

@ -14,7 +14,7 @@ tstylesheet {
function "exception" {
param "name";
document("org/pEp/jniadapter/{$name}.java") {
document "org/pEp/jniadapter/{$name}.java", "text" {
| package org.pEp.jniadapter;
|
| class «$name» extends pEpException { }


+ 64
- 0
src/gen_throw_pEp_exception.ysl2 View File

@ -0,0 +1,64 @@
include yslt.yml2
tstylesheet {
include ./textutils.ysl2
template "/" {
apply "namespace", 0;
document "throw_pEp_exception.hh", "text"
||
#pragma once
#include <jni.h>
namespace pEp {
namespace JNIAdapter {
jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status);
};
};
||
}
template "namespace"
||
#include <assert.h>
#include <pEp/pEpEngine.h>
#include "throw_pEp_exception.hh"
namespace pEp {
namespace JNIAdapter {
jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status)
{
jclass ex;
const char *ex_name;
switch (status) {
`` apply "enum/*", 4, mode=case
default:
assert(0);
ex_name = "Exception";
}
ex = env->FindClass(ex_name);
assert(ex);
if (ex == NULL) {
ex = env->FindClass("java/lang/NoClassDefFoundError");
assert(ex);
}
return env->ThrowNew(ex, ex_name);
}
};
};
||
template "*", mode=case {
| case `call "UCASE" with "text", "name(.)"`:
| ex_name = "org/pEp/jniadapter/`call "CamelCase" with "text", "name(.)"`";
| break;
}
}

+ 40
- 0
src/jniutils.cc View File

@ -0,0 +1,40 @@
#include "jniutils.hh"
#include <stdexcept>
#include <typeinfo>
#include <assert.h>
namespace pEp {
namespace JNIAdapter {
jfieldID getFieldID(
JNIEnv *env,
const char *classname,
const char *fieldname,
const char *signature
)
{
jclass engine = env->FindClass(classname);
assert(engine);
if (engine == NULL) {
jclass ex = env->FindClass("java/lang/NoClassDefFoundError");
assert(ex);
env->ThrowNew(ex, classname);
throw std::bad_cast();
}
jfieldID field = env->GetFieldID(engine, fieldname, signature);
assert(field);
if (field == NULL) {
jclass ex = env->FindClass("java/lang/NoSuchFieldError");
assert(ex);
env->ThrowNew(ex, fieldname);
throw std::invalid_argument(fieldname);
}
return field;
}
};
};

+ 15
- 0
src/jniutils.hh View File

@ -0,0 +1,15 @@
#pragma once
#include <jni.h>
namespace pEp {
namespace JNIAdapter {
jfieldID getFieldID(
JNIEnv *env,
const char *classname,
const char *fieldname,
const char *signature
);
};
};

+ 22
- 0
src/org/pEp/jniadapter/AbstractEngine.java View File

@ -0,0 +1,22 @@
package org.pEp.jniadapter;
abstract class AbstractEngine implements AutoCloseable {
static {
System.loadLibrary("pEpJNI");
}
protected native void init() throws pEpException;
protected native void release();
private long handle;
protected long getHandle() { return handle; }
public AbstractEngine() throws pEpException {
init();
}
public void close() {
release();
}
}

+ 1
- 15
src/org/pEp/jniadapter/Engine.java View File

@ -1,21 +1,7 @@
package org.pEp.jniadapter;
public class Engine implements AutoCloseable {
static {
System.loadLibrary("pEpJNI");
}
protected native void init() throws pEpException;
protected native void release();
private long handle;
public class Engine extends AbstractEngine {
public Engine() throws pEpException {
init();
}
public void close() {
release();
}
}

src/org_pEp_jniadapter_Engine.cc → src/org_pEp_jniadapter_AbstractEngine.cc View File


+ 32
- 0
src/textutils.ysl2 View File

@ -0,0 +1,32 @@
function "UCASE" {
param "text";
value "translate($text, 'abcdefghijklmnopqrstuvwxyz-', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_')";
}
function "lcase" {
param "text";
value "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-', 'abcdefghijklmnopqrstuvwxyz_')";
}
function "CamelCase" {
param "text";
choose {
when "contains($text, '-')" {
const "tokens", "str:tokenize($text, '-')";
for "$tokens" {
choose {
when ".='pEp'" > pEp
otherwise {
call "UCASE" with "text", "substring(., 1, 1)";
call "lcase" with "text", "substring(., 2)";
}
}
}
}
otherwise | unsupported
}
}

Loading…
Cancel
Save