-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaProxy.hpp
111 lines (87 loc) · 3.65 KB
/
JavaProxy.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2013-2014 Josh Blum
// 2019 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
#pragma once
#include <Pothos/Config.hpp>
#include <Pothos/Proxy.hpp>
#include <Pothos/Callable.hpp>
#include <jni.h>
#include <vector>
class JavaProxyHandle;
/***********************************************************************
* custom java environment overload
**********************************************************************/
class JavaProxyEnvironment :
public Pothos::ProxyEnvironment
{
public:
JavaProxyEnvironment(const Pothos::ProxyEnvironmentArgs &);
Pothos::Proxy makeHandle(jvalue value, char sig);
Pothos::Proxy makeHandle(jobject obj)
{
jvalue value; value.l = obj;
return this->makeHandle(value, 'L');
}
std::shared_ptr<JavaProxyHandle> getHandle(const Pothos::Proxy &proxy);
std::string getName(void) const
{
return "java";
}
Pothos::Proxy findProxy(const std::string &name);
std::string getErrorString(void);
jclass FindClass(const char *name);
jobject forName(const char *name);
jobject getPrimitiveClassType(const char *name);
jmethodID GetMethodID(jclass cls, const char *name, const char *sig);
jmethodID GetStaticMethodID(jclass cls, const char *name, const char *sig);
jmethodID GetMethodID(const char *cls, const char *name, const char *sig);
jmethodID GetStaticMethodID(const char *cls, const char *name, const char *sig);
std::string jstringToString(jobject str);
std::string getClassName(jobject cls);
jvalue CallMethodA(const char retType, jobject obj, jmethodID method, jvalue *args);
jvalue CallStaticMethodA(const char retType, jclass cls, jmethodID method, jvalue *args);
void serialize(const Pothos::Proxy &, std::ostream &);
Pothos::Proxy deserialize(std::istream &);
Pothos::Object convertProxyToObject(const Pothos::Proxy &proxy_);
JavaVM *jvm; /* pointer to open virtual machine */
JNIEnv *env; /* pointer to native method interface */
};
/***********************************************************************
* custom java class handler overload
**********************************************************************/
class JavaProxyHandle : public Pothos::ProxyHandle
{
public:
JavaProxyHandle(std::shared_ptr<JavaProxyEnvironment> env, jvalue value, char sig);
Pothos::ProxyEnvironment::Sptr getEnvironment(void) const
{
return env;
}
Pothos::Proxy call(const std::string &name, const Pothos::Proxy *args, const size_t numArgs);
int compareTo(const Pothos::Proxy &proxy) const;
size_t hashCode(void) const;
std::string toString(void) const;
std::string getClassName(void) const;
std::vector<std::string> getInheritance(void) const;
std::shared_ptr<JavaProxyEnvironment> env;
//actual value held by the handler:
//This will be a jclass returned by findObject
//or something returned by a call (method or constructor).
jvalue value;
//character signature type for jvalue: I, Z, J, etc...
char sig;
//get character signature from the class Type
char getSigFromClassType(jobject classType) const;
//convert internal jvalue which may be a primitive to equivalent jobject
jobject toJobject(void) const;
// A list of classes that make up this object's inheritance.
std::vector<std::string> inheritance;
// How many superclasses to climb up to get the return value for
// getClassName().
size_t getClassNameDepth;
inline bool hasSuperclass(void) const
{
return (getClassNameDepth < (inheritance.size()-1));
}
Pothos::Proxy getProxyWithSuperclassName(void) const;
};