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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/***************************************************************************
* jvmextension.cpp
* This file is part of the KDE project
*
* copyright (C)2007 by Vincent Verhoeven <verhoevenv@gmail.com>
* copyright (C)2007 by Sebastian Sauer <mail@dipe.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
***************************************************************************/
#include "jvmextension.h"
#include "jvminterpreter.h"
#include "jvmclasswriter.h"
#include "jvmvariant.h"
#include "jvmfunction.h"
//#include <kross/core/metatype.h>
//#include <QMap>
#include <QString>
#include <QPointer>
#include <QFile>
#include <QMetaObject>
#include <QMetaMethod>
#include <QHash>
#include <QVarLengthArray>
using namespace Kross;
namespace Kross {
/// @internal d-pointer class.
class JVMExtension::Private {
public:
/// The wrapped QObject.
QPointer<QObject> m_object;
/// The provided Java Object
jobject javaobj;
/// For debugging.
QString debuginfo;
/// The cached list of methods.
QHash<QByteArray, int> m_methods;
/// The cached list of properties.
QHash<QByteArray, int> m_properties;
/// The cached list of enumerations.
QHash<QByteArray, int> m_enumerations;
};
}
JVMExtension::JVMExtension(QObject* object)
: d(new Private())
{
d->m_object = object;
QString name = object->objectName();
d->debuginfo = object ? QString("name=%1 class=%2").arg(name).arg(object->metaObject()->className()) : "NULL";
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug(QString("JVMExtension Ctor %1").arg(d->debuginfo));
#endif
QByteArray ba;
QDataStream data(&ba, QIODevice::WriteOnly);
JVMClassWriter writer(this);
writer.writeInterface(data);
d->javaobj = JVMInterpreter::addExtension(name, this, ba, object);
//TODO what we could do here is to create a class on the fly and register native
//callbacks using the env->RegisterNatives method to be able to provide a Java
//wrapper that calls slots and get/set properties fo the QObject.
const QMetaObject* metaobject = d->m_object->metaObject();
{ // initialize methods.
const int count = metaobject->methodCount();
for(int i = 0; i < count; ++i) {
QMetaMethod member = metaobject->method(i);
const QString signature = member.signature();
const QByteArray name = signature.left(signature.indexOf('(')).toLatin1();
if(! d->m_methods.contains(name))
d->m_methods.insert(name, i);
}
}
{ // initialize properties
const int count = metaobject->propertyCount();
for(int i = 0; i < count; ++i) {
QMetaProperty prop = metaobject->property(i);
d->m_properties.insert(prop.name(), i);
if(prop.isWritable())
d->m_properties.insert(QByteArray(prop.name()).append('='), i);
}
}
{ // initialize enumerations
const int count = metaobject->enumeratorCount();
for(int i = 0; i < count; ++i) {
QMetaEnum e = metaobject->enumerator(i);
const int kc = e.keyCount();
for(int k = 0; k < kc; ++k) {
const QByteArray name = e.key(k);
d->m_enumerations.insert(name, e.value(k));
}
}
}
}
JVMExtension::~JVMExtension()
{
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension Dtor %1").arg(d->debuginfo) );
#endif
delete d;
}
QObject* JVMExtension::object() const
{
return d->m_object;
}
jobject JVMExtension::javaobject() const
{
return d->javaobj;
}
jobject JVMExtension::callQMethod(JNIEnv* env, jstring method, int argc, jobject args[])
{
QByteArray mname = JavaType<QString>::toVariant(method,env).toLatin1();
int methodindex = d->m_methods[mname];
QMetaMethod metamethod = d->m_object->metaObject()->method(methodindex);
QList<QByteArray> typelist = metamethod.parameterTypes();
bool hasreturnvalue = strcmp(metamethod.typeName(),"") != 0;
int typelistcount = typelist.count();
QVarLengthArray<int> types( typelistcount + 1 );
QVarLengthArray<int> metatypes( typelistcount + 1 );
// set the return type
if(hasreturnvalue) {
types[0] = QVariant::nameToType( metamethod.typeName() );
if( types[0] == QVariant::Invalid || types[0] == QVariant::UserType ) {
metatypes[0] = QMetaType::type( metamethod.typeName() );
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension::callQMethod return typeName=%1 typeId=%2").arg(metamethod.typeName()).arg(metatypes[0]) );
#endif
}
else {
metatypes[0] = QMetaType::Void; //FIXME: disable before release
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension::callQMethod return typeName=%1 typeId=%2 (with metatype=QMetaType::Void)").arg(metamethod.typeName()).arg(metatypes[0]) );
#endif
}
}
else {
types[0] = QVariant::Invalid; //FIXME: disable before release
metatypes[0] = QMetaType::Void;
}
// set the arguments types
for(int idx = 1; idx <= typelistcount; ++idx) {
const char* typeName = typelist[idx - 1].constData();
types[idx] = QVariant::nameToType(typeName);
if( types[idx] == QVariant::Invalid || types[idx] == QVariant::UserType ) {
metatypes[idx] = QMetaType::type(typeName);
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension::callQMethod argument typeName=%1 typeId=%2").arg(metamethod.typeName()).arg(metatypes[idx]) );
#endif
}
else {
metatypes[idx] = QMetaType::Void; //FIXME: disable before release
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension::callQMethod argument typeName=%1 typeId=%2 set metatype=QMetaType::Void").arg(metamethod.typeName()).arg(metatypes[idx]) );
#endif
}
}
typelistcount = types.count();
QVarLengthArray<MetaType*> variantargs( typelistcount );
QVarLengthArray<void*> voidstarargs( typelistcount );
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("JVMExtension::callQMethod signature=%1 typeName=%2 argc=%3 typelistcount=%4").arg(metamethod.signature()).arg(metamethod.typeName()).arg(argc).arg(typelistcount) );
for(int i = 0; i < types.count(); ++i)
krossdebug( QString(" argument index=%1 typeId=%2 typeName=%3 metaTypeId=%4").arg(i).arg(types[i]).arg(QVariant::typeToName( (QVariant::Type)types[i] )).arg(metatypes[i]) );
#endif
Q_ASSERT(argc >= typelistcount - 1); //typelistcount also contains the return type
// set the return value
if(hasreturnvalue)
{
MetaType* returntype = JVMMetaTypeFactory::create( env, types[0], metatypes[0] );
variantargs[0] = returntype;
voidstarargs[0] = returntype->toVoidStar();
}
else
{
variantargs[0] = 0;
voidstarargs[0] = (void*)0;
}
// set the arguments values
for(int idx = 1; idx < typelistcount; ++idx)
{
//krossdebug( QString("-----------> %1").arg( STR2CSTR( rb_inspect(argv[idx]) ) ) );
MetaType* metatype = JVMMetaTypeFactory::create( env, types[idx], metatypes[idx], args[idx - 1] );
if(! metatype) { // Seems JVMMetaTypeFactory::create returned an invalid JavaType.
krosswarning( QString("JVMExtension::callMetaMethod Aborting cause JVMMetaTypeFactory::create returned NULL.") );
for(int i = 0; i < idx; ++i) // Clear already allocated instances.
delete variantargs[i];
return 0; // abort execution.
}
variantargs[idx] = metatype;
voidstarargs[idx] = metatype->toVoidStar();
}
// call the method now
int r = d->m_object->qt_metacall(QMetaObject::InvokeMetaMethod, methodindex, &voidstarargs[0]);
#ifdef KROSS_JVM_EXTENSION_DEBUG
krossdebug( QString("RESULT nr=%1").arg(r) );
#else
Q_UNUSED(r);
#endif
// free the arguments
for(int idx = 1; idx < typelistcount; ++idx)
{
delete variantargs[idx];
}
// eval the return-value
if(hasreturnvalue)
{
QVariant result(variantargs[0]->typeId(), variantargs[0]->toVoidStar());
#ifdef KROSS_JVM_EXTENSION_DEBUG
QMetaMethod metamethod = d->m_object->metaObject()->method(methodindex);
krossdebug( QString("JVMExtension::callQMethod Returnvalue typeId=%1 metamethod.typename=%2 variant.toString=%3 variant.typeName=%4").arg(variantargs[0]->typeId()).arg(metamethod.typeName()).arg(result.toString()).arg(result.typeName()) );
#endif
// free the return argument
delete variantargs[0];
// return the result
return result.isNull() ? 0 : JavaType<QVariant>::toJObject(result, env);
}
return 0;
}
jboolean JVMExtension::doConnect(JNIEnv *env, jstring signal, jobject receiver, jobject method){
QObject* qsender = d->m_object;
QByteArray qsignal = JavaType<QString>::toVariant(signal, env).toLatin1();
QObject* qreceiver = new JVMFunction(qsender, qsignal, receiver, method, env);
QByteArray qmethod = qsignal;
// Dirty hack to replace SIGNAL() and SLOT() macros. If the user doesn't
// defined them explicit, we assume it's wanted to connect from a signal to
// a slot. This seems to be the most flexible solution so far...
if( ! qsignal.startsWith('1') && ! qsignal.startsWith('2') )
qsignal.prepend('2'); // prepending 2 means SIGNAL(...)
if( ! qmethod.startsWith('1') && ! qmethod.startsWith('2') )
qmethod.prepend('1'); // prepending 1 means SLOT(...)
if(! QObject::connect(qsender, qsignal, qreceiver, qmethod, Qt::DirectConnection) ) {
krosswarning( QString("JVMExtension::doConnect Failed to connect").toLatin1().constData() );
return JNI_FALSE;
}
return JNI_TRUE;
}
bool JVMExtension::isJVMExtension(jobject obj, JNIEnv* env){
if(obj == NULL)
return false;
jclass cl = env->GetObjectClass(obj);
jclass KQExt = env->FindClass("org/kde/kdebindings/java/krossjava/KrossQExtension");
return (env->IsAssignableFrom(cl, KQExt) == JNI_TRUE);
}
/*
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
*/
|