JString::operator QByteArray() const { QByteArray val; JNIEnv* env = *JVM::instance(); const char* chars = env->GetStringUTFChars(jstring(m_object), 0); val = chars; env->ReleaseStringUTFChars(jstring(m_object), chars); return val; }
void watcher(void) { Json *jv, *jtop; vlong id; char *text, *created_at, *user; char *buf; int fd, n, i; char fname[64]; buf = malloc(BUFSIZ); if(buf == nil) sysfatal("malloc: %r"); for(;;){ fetch(fname); fd = open(fname, OREAD); n = readn(fd, buf, BUFSIZ); if(n <= 0){ fprint(2, "read failed.\n"); sleep(INTERVAL); continue; } /*print("(read %d bytes in %s)\n", n, fname);*/ buf[n] = 0; jtop = parsejson(buf); if(jtop == nil){ fprint(2, "parsejson failed.\n"); sleep(INTERVAL); continue; } jv = jtop->value[0]; /* FIXME */ for(i = jv->len - 1; i >= 0; i--){ id = jint(jlookup(jv->value[i], "id")); if(id <= lastid) continue; text = jstring(jlookup(jv->value[i], "text")); created_at = jstring(jlookup(jv->value[i], "created_at")); user = jstring(jlookup(jv->value[i], "from_user")); print("[%s] %s (%s)\n", user, text, created_at); lastid = id; } jclose(jv); close(fd); sleep(INTERVAL); } }
jobjectArray GetFiles(IFileDialogPtr pDialog, BOOL isCancelled, jint type) { JNIEnv* env = GetEnv(); JLClass cls(env, env->FindClass("java/lang/String")); if (isCancelled) { return env->NewObjectArray(0, cls, NULL); } jobjectArray ret = NULL; OLE_TRY if (type == com_sun_glass_ui_CommonDialogs_Type_SAVE) { ret = env->NewObjectArray(1, cls, NULL); IShellItemPtr pFile; OLE_HRT( pDialog->GetResult(&pFile) ); OLE_CHECK_NOTNULLSP(pFile) env->SetObjectArrayElement(ret, 0, jstring(JLString(env, CreateJString(env, pFile)))); CheckAndClearException(env); return ret; } IFileOpenDialogPtr pOpenDialog(pDialog); OLE_CHECK_NOTNULLSP(pOpenDialog) IShellItemArrayPtr pFiles; OLE_HRT( pOpenDialog->GetResults(&pFiles) ); OLE_CHECK_NOTNULLSP(pFiles) DWORD count = 0; OLE_HRT( pFiles->GetCount(&count) ); ret = env->NewObjectArray(count, cls, NULL); for (DWORD i = 0; i < count; i++) { IShellItemPtr pFile; OLE_HRT( pFiles->GetItemAt(i, &pFile) ); OLE_CHECK_NOTNULLSP(pFile) env->SetObjectArrayElement(ret, i, jstring(JLString(env, CreateJString(env, pFile)))); CheckAndClearException(env); } OLE_CATCH return ret; }
status_t android_hardware_display_DisplayViewport_toNative(JNIEnv* env, jobject viewportObj, DisplayViewport* viewport) { viewport->displayId = env->GetIntField(viewportObj, gDisplayViewportClassInfo.displayId); viewport->orientation = env->GetIntField(viewportObj, gDisplayViewportClassInfo.orientation); viewport->deviceWidth = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceWidth); viewport->deviceHeight = env->GetIntField(viewportObj, gDisplayViewportClassInfo.deviceHeight); jstring uniqueId = jstring(env->GetObjectField(viewportObj, gDisplayViewportClassInfo.uniqueId)); if (uniqueId != nullptr) { viewport->uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str()); } jobject logicalFrameObj = env->GetObjectField(viewportObj, gDisplayViewportClassInfo.logicalFrame); viewport->logicalLeft = env->GetIntField(logicalFrameObj, gRectClassInfo.left); viewport->logicalTop = env->GetIntField(logicalFrameObj, gRectClassInfo.top); viewport->logicalRight = env->GetIntField(logicalFrameObj, gRectClassInfo.right); viewport->logicalBottom = env->GetIntField(logicalFrameObj, gRectClassInfo.bottom); jobject physicalFrameObj = env->GetObjectField(viewportObj, gDisplayViewportClassInfo.physicalFrame); viewport->physicalLeft = env->GetIntField(physicalFrameObj, gRectClassInfo.left); viewport->physicalTop = env->GetIntField(physicalFrameObj, gRectClassInfo.top); viewport->physicalRight = env->GetIntField(physicalFrameObj, gRectClassInfo.right); viewport->physicalBottom = env->GetIntField(physicalFrameObj, gRectClassInfo.bottom); return OK; }
bool NativeInputApplicationHandle::updateInfo() { JNIEnv* env = AndroidRuntime::getJNIEnv(); jobject obj = env->NewLocalRef(mObjWeak); if (!obj) { releaseInfo(); return false; } if (!mInfo) { mInfo = new InputApplicationInfo(); } jstring nameObj = jstring(env->GetObjectField(obj, gInputApplicationHandleClassInfo.name)); if (nameObj) { const char* nameStr = env->GetStringUTFChars(nameObj, NULL); mInfo->name.setTo(nameStr); env->ReleaseStringUTFChars(nameObj, nameStr); env->DeleteLocalRef(nameObj); } else { mInfo->name.setTo("<null>"); } mInfo->dispatchingTimeout = env->GetLongField(obj, gInputApplicationHandleClassInfo.dispatchingTimeoutNanos); env->DeleteLocalRef(obj); return true; }
JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_x_print_GDIClient_getMediaNames (JNIEnv *env, jclass owner, jstring printerName) { const char *name = env->GetStringUTFChars(printerName, 0); jobjectArray result = NULL; PRINTER_INFO_2 *info = GetPrinterInfo(name); if (info != NULL) { int numNamesFirst = DeviceCapabilities(name, info->pPortName, DC_PAPERNAMES, NULL, NULL); if (numNamesFirst > 0) { char *buffer = new char[65 * numNamesFirst]; int numNames = DeviceCapabilities(name, info->pPortName, DC_PAPERNAMES, (LPSTR)buffer, NULL); if (numNames == numNamesFirst) { jclass jstring_class = env->FindClass("java/lang/String"); result = env->NewObjectArray(numNames, jstring_class, jstring()); for (int i = 0; i < numNames; i++) { char *name = buffer + i*64; jstring jname = env->NewStringUTF(name); env->SetObjectArrayElement(result, i, jname); env->DeleteLocalRef(jname); } } delete[] buffer; } free(info); } return result; }
QString JString::str() const { if (isNull()) { //qDebug() << "WARNING: JString::str() on a null string"; return QString(); } QString val; JNIEnv* env = *JVM::instance(); const char* chars = env->GetStringUTFChars(jstring(m_object), 0); val = QString::fromUtf8(chars); env->ReleaseStringUTFChars(jstring(m_object), chars); return val; }
static std::string executeJSCallWithProxy( jobject executor, const std::string& methodName, const std::vector<folly::dynamic>& arguments) { static auto executeJSCall = jni::findClassStatic(EXECUTOR_BASECLASS)->getMethod<jstring(jstring, jstring)>("executeJSCall"); auto result = executeJSCall( executor, jni::make_jstring(methodName).get(), jni::make_jstring(folly::toJson(arguments).c_str()).get()); return result->toString(); }
QVariant JClass::getStaticValue(const char* name, const char* sig) const { JScope s; JNIEnv* env = *JVM::instance(); jfieldID fid = env->GetStaticFieldID(m_class, name, sig); if (!fid) throw RuntimeException(QObject::tr("Field %1 %2 not found").arg(name).arg(sig)); switch (sig[0]) { case '[': { QVariant var; jobject obj = env->GetStaticObjectField(m_class, fid); var.setValue<JArray>(JArray(obj)); return var; } case 'L': { jclass string_class = env->FindClass("java/lang/String"); jobject obj = env->GetStaticObjectField(m_class, fid); if (env->IsInstanceOf(obj, string_class)) return JString(jstring(obj)).str(); else { QVariant var; var.setValue<JObject>(JObject(obj)); return var; } } case 'Z': return (bool) env->GetStaticBooleanField(m_class, fid); case 'B': return env->GetStaticByteField(m_class, fid); case 'C': return env->GetStaticCharField(m_class, fid); case 'S': return env->GetStaticShortField(m_class, fid); case 'I': return env->GetStaticIntField(m_class, fid); case 'J': return (qlonglong) env->GetStaticLongField(m_class, fid); case 'F': return env->GetStaticFloatField(m_class, fid); case 'D': return env->GetStaticDoubleField(m_class, fid); default: throw RuntimeException(QObject::tr("Unknown Java data type: %1").arg(sig[0])); } }
String JniLocalization::GetLocale() { jmethodID mid = GetMethodID("GetLocale", "()Ljava/lang/String;"); if (mid) { jobject obj = GetEnvironment()->CallStaticObjectMethod(GetJavaClass(), mid); char str[256] = {0}; CreateStringFromJni(GetEnvironment(), jstring(obj), str); String locale = str; return locale; } return "en"; }
JNIEXPORT jobjectArray JNICALL Java_org_apache_harmony_x_print_Win32PrintServiceProvider_findPrintServices (JNIEnv *env, jclass owner) { jobjectArray result = NULL; DWORD services = 0; char ** names = GetPrintServices(&services); if (services > 0) { jclass jstring_class = env->FindClass("java/lang/String"); result = env->NewObjectArray(services, jstring_class, jstring()); for (DWORD i = 0; i < services; i++) { jstring name = env->NewStringUTF(names[i]); env->SetObjectArrayElement(result, i, name); env->DeleteLocalRef(name); } } return result; }
int register_com_sqlcrypt_database_CursorWindow(JNIEnv * env) { jclass clazz; FIND_CLASS(clazz, "com/sqlcrypt/database/CharArrayBuffer"); GET_FIELD_ID(gCharArrayBufferClassInfo.data, clazz, "data", "[C"); GET_FIELD_ID(gCharArrayBufferClassInfo.sizeCopied, clazz, "sizeCopied", "I"); gEmptyString = jstring(env->NewGlobalRef(env->NewStringUTF(""))); LOG_FATAL_IF(!gEmptyString, "Unable to create empty string"); return AndroidRuntime::registerNativeMethods(env, "com/sqlcrypt/database/CursorWindow", sMethods, NELEM(sMethods)); }
static void nativeRegisterCustomFunction(JNIEnv* env, jclass clazz, jlong connectionPtr, jobject functionObj) { SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); jstring nameStr = jstring(env->GetObjectField( functionObj, gSQLiteCustomFunctionClassInfo.name)); jint numArgs = env->GetIntField(functionObj, gSQLiteCustomFunctionClassInfo.numArgs); jobject functionObjGlobal = env->NewGlobalRef(functionObj); const char* name = env->GetStringUTFChars(nameStr, NULL); int err = sqlite3_create_function_v2(connection->db, name, numArgs, SQLITE_UTF16, reinterpret_cast<void*>(functionObjGlobal), &sqliteCustomFunctionCallback, NULL, NULL, &sqliteCustomFunctionDestructor); env->ReleaseStringUTFChars(nameStr, name); if (err != SQLITE_OK) { ALOGE("sqlite3_create_function returned %d", err); env->DeleteGlobalRef(functionObjGlobal); throw_sqlite3_exception(env, connection->db); return; } }
std::string ModuleHolder::getName() const { static auto method = getClass()->getMethod<jstring()>("getName"); return method(self())->toStdString(); }
size_t JString::size() const { JNIEnv* env = *JVM::instance(); return env->GetStringLength(jstring(m_object)); }
QVariant JClass::callStatic(const char* name, const char* sig, QList<QVariant> args) { JScope s; JNIEnv* env = *JVM::instance(); jmethodID mid = env->GetStaticMethodID(m_class, name, sig); if (!mid) throw RuntimeException(QObject::tr("Method %1 %2 not found").arg(name).arg(sig)); JValue vals[args.size()]; jvalue jargs[args.size()]; for(int i=0;i<args.size();i++) { vals[i] = variantToValue(args[i]); jargs[i] = vals[i]; } const char* rvtype = strchr(sig, ')'); if (!rvtype) throw RuntimeException(QObject::tr("Invalid method return type").arg(name).arg(sig)); rvtype++; QVariant retval; switch (*rvtype) { case 'V': env->CallStaticVoidMethodA(m_class, mid, jargs); break; case '[': { jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); retval.setValue<JArray>(JArray(obj)); break; } case 'L': { jclass string_class = env->FindClass("java/lang/String"); jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); if (obj && env->IsInstanceOf(obj, string_class) && !strcmp(rvtype+1, "java/lang/String;")) retval = JString(jstring(obj)).str(); else { QVariant var; var.setValue<JObject>(JObject(obj)); retval = var; } break; } case 'Z': retval = (bool) env->CallStaticBooleanMethodA(m_class, mid, jargs); break; case 'B': retval = env->CallStaticByteMethodA(m_class, mid, jargs); break; case 'C': retval = env->CallStaticCharMethodA(m_class, mid, jargs); break; case 'S': retval = env->CallStaticShortMethodA(m_class, mid, jargs); break; case 'I': retval = env->CallStaticIntMethodA(m_class, mid, jargs); break; case 'J': retval = (qlonglong) env->CallStaticLongMethodA(m_class, mid, jargs); break; case 'F': retval = env->CallStaticFloatMethodA(m_class, mid, jargs); break; case 'D': retval = env->CallStaticDoubleMethodA(m_class, mid, jargs); break; default: throw RuntimeException(QObject::tr("Unknown Java data type: %1").arg(*rvtype)); } JObject ex = env->ExceptionOccurred(); if (!ex.isNull()) { env->ExceptionClear(); ex.call("printStackTrace"); QString message = ex.call("getMessage", JSignature().retString()).toString(); QString className = ex.getClass().getClassName(); throw JException(message, className, ex); } return retval; }
main() { char buff[100]; int cn, r, k, t, dt, lp, x, rx, ry; int oldx, oldy, newx, newy; int offx, offy; Point jstring(); char *getstring(); Point p; int nonstop = NONSTOP; local(); request(MOUSE); /* random number seed is derived from position of dmd layer */ srand(mouse.xy.x); request(KBD); for ( ;; ) { lp = dt = 0; if ( nonstop == 0 ) { /* ask for lp parameter */ jmoveto(Pt(0,0)); p = jstring("loops="); lp = getnum(p); jmoveto(Pt(0,0)); jstring("loops="); } if ( lp < 0 ) exit(); if ( lp == 0 ) lp = rand() % 31 + 1; if ( nonstop == 0 ) { /* ask for dt parameter */ jmoveto(Pt(0,0)); p = jstring("delta="); dt = getnum(p); jmoveto(Pt(0,0)); jstring("delta="); } if ( dt <= 0 ) dt = rand() % 358 + 1; /* clear screen */ jrectf(Jrect,F_CLR); t=0; oldx = offx = XMAX / 2; oldy = offy = YMAX / 2; cn = 1; /* draw rose */ do { t=(t+dt)%360; x=(lp*t)%360; r = Isin(x); rx=muldiv(r,XMAX-1,1024); ry=muldiv(r,YMAX-1,1024); newx = offx + muldiv(rx,Icos(t),1024) / 2; newy = offy + muldiv(ry,Isin(t),1024) / 2; jsegment(Pt(oldx,oldy),Pt(newx,newy),F_STORE); oldx=newx; oldy=newy; /* give up the CPU every 10 times around */ if ( cn++ > 10 ) { cn = 0; sleep(4); } } while ( t != 0 && (k=kbdchar()) != 'q' ) ; if ( nonstop == 1 ) { /* in nonstop mode, any key aborts */ if ( k != -1 ) exit(); /* sleep 2 seconds between random patterns */ sleep(120); } } }
std::string JavaNativeModule::getName() { static auto getNameMethod = wrapper_->getClass()->getMethod<jstring()>("getName"); return getNameMethod(wrapper_)->toStdString(); }
bool NativeInputWindowHandle::updateInfo() { JNIEnv* env = AndroidRuntime::getJNIEnv(); jobject obj = env->NewLocalRef(mObjWeak); if (!obj) { releaseInfo(); return false; } if (!mInfo) { mInfo = new InputWindowInfo(); } jobject inputChannelObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.inputChannel); if (inputChannelObj) { mInfo->inputChannel = android_view_InputChannel_getInputChannel(env, inputChannelObj); env->DeleteLocalRef(inputChannelObj); } else { mInfo->inputChannel.clear(); } jstring nameObj = jstring(env->GetObjectField(obj, gInputWindowHandleClassInfo.name)); if (nameObj) { const char* nameStr = env->GetStringUTFChars(nameObj, NULL); mInfo->name.setTo(nameStr); env->ReleaseStringUTFChars(nameObj, nameStr); env->DeleteLocalRef(nameObj); } else { mInfo->name.setTo("<null>"); } mInfo->layoutParamsFlags = env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsFlags); mInfo->layoutParamsPrivateFlags = env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsPrivateFlags); mInfo->layoutParamsType = env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsType); mInfo->dispatchingTimeout = env->GetLongField(obj, gInputWindowHandleClassInfo.dispatchingTimeoutNanos); mInfo->frameLeft = env->GetIntField(obj, gInputWindowHandleClassInfo.frameLeft); mInfo->frameTop = env->GetIntField(obj, gInputWindowHandleClassInfo.frameTop); mInfo->frameRight = env->GetIntField(obj, gInputWindowHandleClassInfo.frameRight); mInfo->frameBottom = env->GetIntField(obj, gInputWindowHandleClassInfo.frameBottom); mInfo->scaleFactor = env->GetFloatField(obj, gInputWindowHandleClassInfo.scaleFactor); jobject regionObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.touchableRegion); if (regionObj) { SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj); mInfo->touchableRegion.set(*region); env->DeleteLocalRef(regionObj); } else { mInfo->touchableRegion.setEmpty(); } mInfo->visible = env->GetBooleanField(obj, gInputWindowHandleClassInfo.visible); mInfo->canReceiveKeys = env->GetBooleanField(obj, gInputWindowHandleClassInfo.canReceiveKeys); mInfo->hasFocus = env->GetBooleanField(obj, gInputWindowHandleClassInfo.hasFocus); mInfo->hasWallpaper = env->GetBooleanField(obj, gInputWindowHandleClassInfo.hasWallpaper); mInfo->paused = env->GetBooleanField(obj, gInputWindowHandleClassInfo.paused); mInfo->layer = env->GetIntField(obj, gInputWindowHandleClassInfo.layer); mInfo->ownerPid = env->GetIntField(obj, gInputWindowHandleClassInfo.ownerPid); mInfo->ownerUid = env->GetIntField(obj, gInputWindowHandleClassInfo.ownerUid); mInfo->inputFeatures = env->GetIntField(obj, gInputWindowHandleClassInfo.inputFeatures); mInfo->displayId = env->GetIntField(obj, gInputWindowHandleClassInfo.displayId); env->DeleteLocalRef(obj); return true; }
String::String(jobject data) : String(jstring(data)) { }