Beispiel #1
0
bool Type::is_object() const {
    Class* cls = clazz();
    return is_object_proto() || is_functor()
        || (cls && cls->proto()->is_object_proto())
        || (cls && !cls->proto()->is_proto()); 
    // Note: If the user specified an invalid proto, then error out
}
Beispiel #2
0
void throw_exception(JNIEnv *env, char const *name, char const *message)
{
	safe_jni::safe_local_ref<jclass> clazz(getEnv(), getEnv()->FindClass(name));
	if (!clazz) {
		return;
	}
	getEnv()->ThrowNew(clazz.get(), message);
}
// static
jobject JHwParcel::NewObject(JNIEnv *env) {
    ScopedLocalRef<jclass> clazz(env, FindClassOrDie(env, CLASS_PATH));

    jmethodID constructID =
        GetMethodIDOrDie(env, clazz.get(), "<init>", "(Z)V");

    return env->NewObject(clazz.get(), constructID, false /* allocate */);
}
Beispiel #4
0
SEXP class__newInstance(SEXP args) {
    SEXP p = CDR(args);

    XP_Module module(CAR(p)); p = CDR(p);
    XP_Class clazz(CAR(p)); p = CDR(p);
    UNPACK_EXTERNAL_ARGS(cargs,p)
    return clazz->newInstance(cargs, nargs);
}
static jobject makeLongObject(JNIEnv *env, int64_t value) {
    ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Long"));
    CHECK(clazz.get() != NULL);

    jmethodID longConstructID = env->GetMethodID(clazz.get(), "<init>", "(J)V");
    CHECK(longConstructID != NULL);

    return env->NewObject(clazz.get(), longConstructID, value);
}
// static
void JHwParcel::InitClass(JNIEnv *env) {
    ScopedLocalRef<jclass> clazz(
            env, FindClassOrDie(env, CLASS_PATH));

    gFields.contextID =
        GetFieldIDOrDie(env, clazz.get(), "mNativeContext", "J");

    gFields.constructID = GetMethodIDOrDie(env, clazz.get(), "<init>", "(Z)V");
}
static jobject makeFloatObject(JNIEnv *env, float value) {
    ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Float"));
    CHECK(clazz.get() != NULL);

    jmethodID floatConstructID =
        env->GetMethodID(clazz.get(), "<init>", "(F)V");
    CHECK(floatConstructID != NULL);

    return env->NewObject(clazz.get(), floatConstructID, value);
}
static jobject makeIntegerObject(JNIEnv *env, int32_t value) {
    ScopedLocalRef<jclass> clazz(env, env->FindClass("java/lang/Integer"));
    CHECK(clazz.get() != NULL);

    jmethodID integerConstructID =
        env->GetMethodID(clazz.get(), "<init>", "(I)V");
    CHECK(integerConstructID != NULL);

    return env->NewObject(clazz.get(), integerConstructID, value);
}
LocalRef<jobject> newPrimitiveWrapper(const T obj) {
  using jinfo = traits::jinfo<T>;
  static_assert(jinfo::is_primitive, "Should be primitive.");

  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->FindClass(jinfo::jwrapperclass().c_str()));
  const jmethodID valueOfId = ctx->env()->GetStaticMethodID(
      clazz.get(), "valueOf", jinfo::valueOfSig().c_str());
  LocalRef<jobject> wrapper(
      ctx->env()->CallStaticObjectMethod(clazz.get(), valueOfId, obj));
  ctx->throwIfOccured();
  return wrapper;
}
void getIterable(jobject jobj, const std::function<void(jobject)> &cb) {
  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->GetObjectClass(jobj));
  const jmethodID iteratorId = ctx->env()->GetMethodID(
      clazz.get(), "iterator", "()Ljava/util/Iterator;");
  LocalRef<jobject> iter(ctx->env()->CallObjectMethod(jobj, iteratorId));
  LocalRef<jclass> iterClass(ctx->env()->GetObjectClass(iter.get()));
  const jmethodID hasNextId =
      ctx->env()->GetMethodID(iterClass.get(), "hasNext", "()Z");
  const jmethodID nextId =
      ctx->env()->GetMethodID(iterClass.get(), "next", "()Ljava/lang/Object;");
  while (ctx->env()->CallBooleanMethod(iter.get(), hasNextId)) {
    LocalRef<jobject> jobj(ctx->env()->CallObjectMethod(iter.get(), nextId));
    ctx->throwIfOccured();
    cb(jobj.get());
  }
}
static jobject NewJniObject(JNIEnv* env, const T& value, const char* javaClass)
{
  if (!value.get())
  {
    return 0;
  }

  JniLocalReference<jclass> clazz(
      env,
      env->FindClass(javaClass));
  jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");

  return env->NewObject(
      *clazz,
      method,
      JniPtrToLong(new T(value)));
}
Beispiel #12
0
std::string AndroidGetPackageName()
{
	JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

	jobject activity = (jobject)SDL_AndroidGetActivity();
	jclass clazz(env->GetObjectClass(activity));

	jmethodID method_id = env->GetMethodID(clazz, "getPackageName", "()Ljava/lang/String;");
	jstring packageName = (jstring)env->CallObjectMethod(activity,  method_id);
	const char* name = env->GetStringUTFChars(packageName, NULL);
	std::string result(name);
	env->ReleaseStringUTFChars(packageName, name);

	env->DeleteLocalRef(activity);
	env->DeleteLocalRef(clazz);

	return result;
}
Beispiel #13
0
SEXP CppMethod__invoke_notvoid(SEXP args) {
    SEXP p = CDR(args);

    // the external pointer to the class
    XP_Class clazz(CAR(p)); p = CDR(p);

    // the external pointer to the method
    SEXP met = CAR(p); p = CDR(p);

    // the external pointer to the object
    SEXP obj = CAR(p); p = CDR(p);
    CHECK_DUMMY_OBJ(obj);

    // additional arguments, processed the same way as .Call does
    UNPACK_EXTERNAL_ARGS(cargs,p)

    return clazz->invoke_notvoid(met, obj, cargs, nargs);
}
void getMap(jobject jobj, const std::function<void(jobject, jobject)> &cb) {
  const Context::Handle ctx = Context::getContext();
  LocalRef<jclass> clazz(ctx->env()->GetObjectClass(jobj));
  const jmethodID entrySetId =
      ctx->env()->GetMethodID(clazz.get(), "entrySet", "()Ljava/util/Set;");
  LocalRef<jobject> entrySet(ctx->env()->CallObjectMethod(jobj, entrySetId));
  LocalRef<jclass> mapEntryClass(ctx->env()->FindClass("java/util/Map$Entry"));
  const jmethodID getKeyId = ctx->env()->GetMethodID(
      mapEntryClass.get(), "getKey", "()Ljava/lang/Object;");
  const jmethodID getValueId = ctx->env()->GetMethodID(
      mapEntryClass.get(), "getValue", "()Ljava/lang/Object;");
  ctx->throwIfOccured();
  getIterable(entrySet.get(), [&ctx, &cb, getKeyId, getValueId](jobject jobj) {
    LocalRef<jobject> jkey(ctx->env()->CallObjectMethod(jobj, getKeyId));
    LocalRef<jobject> jvalue(ctx->env()->CallObjectMethod(jobj, getValueId));
    cb(jkey.get(), jvalue.get());
  });
}
status_t JMediaCodec::dequeueOutputBuffer(
        JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs) {
    size_t size, offset;
    int64_t timeUs;
    uint32_t flags;
    status_t err;
    if ((err = mCodec->dequeueOutputBuffer(
                    index, &offset, &size, &timeUs, &flags, timeoutUs)) != OK) {
        return err;
    }

    ScopedLocalRef<jclass> clazz(
            env, env->FindClass("android/media/MediaCodec$BufferInfo"));

    jmethodID method = env->GetMethodID(clazz.get(), "set", "(IIJI)V");
    env->CallVoidMethod(bufferInfo, method, offset, size, timeUs, flags);

    return OK;
}
Beispiel #16
0
void ExceptionTranslator::Translate(CJavascriptException const& ex)
{
  CPythonGIL python_gil;

  if (ex.m_type)
  {
    ::PyErr_SetString(ex.m_type, ex.what());
  }
  else
  {
    v8::HandleScope handle_scope(v8::Isolate::GetCurrent());

    if (!ex.Exception().IsEmpty() && ex.Exception()->IsObject())
    {
      v8::Handle<v8::Object> obj = ex.Exception()->ToObject();

      v8::Handle<v8::Value> exc_type = obj->GetHiddenValue(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "exc_type"));
      v8::Handle<v8::Value> exc_value = obj->GetHiddenValue(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "exc_value"));

      if (!exc_type.IsEmpty() && !exc_value.IsEmpty())
      {
        std::auto_ptr<py::object> type(static_cast<py::object *>(v8::Handle<v8::External>::Cast(exc_type)->Value())),
                                  value(static_cast<py::object *>(v8::Handle<v8::External>::Cast(exc_value)->Value()));

        ::PyErr_SetObject(type->ptr(), value->ptr());

        return;
      }
    }

    // Boost::Python doesn't support inherite from Python class,
    // so, just use some workaround to throw our custom exception
    //
    // http://www.language-binding.net/pyplusplus/troubleshooting_guide/exceptions/exceptions.html

    py::object impl(ex);
    py::object clazz = impl.attr("_jsclass");
    py::object err = clazz(impl);

    ::PyErr_SetObject(clazz.ptr(), py::incref(err.ptr()));
  }
}
static jobject makeByteBufferObject(
        JNIEnv *env, const void *data, size_t size) {
    jbyteArray byteArrayObj = env->NewByteArray(size);
    env->SetByteArrayRegion(byteArrayObj, 0, size, (const jbyte *)data);

    ScopedLocalRef<jclass> clazz(env, env->FindClass("java/nio/ByteBuffer"));
    CHECK(clazz.get() != NULL);

    jmethodID byteBufWrapID =
        env->GetStaticMethodID(
                clazz.get(), "wrap", "([B)Ljava/nio/ByteBuffer;");
    CHECK(byteBufWrapID != NULL);

    jobject byteBufObj = env->CallStaticObjectMethod(
            clazz.get(), byteBufWrapID, byteArrayObj);

    env->DeleteLocalRef(byteArrayObj); byteArrayObj = NULL;

    return byteBufObj;
}
Beispiel #18
0
bool Type::equals(Type const* other) const {
    // Make sure the classes are equal 
    if (clazz() != other->clazz()) {
        return false;
    }
    if (other->name() != name()) {
        return false;
    }

    // Make sure the generic parameters are the same 
    Generic* g1 = generics();
    Generic* g2 = other->generics();
    while (g1 && g2) {
        if (!g1->type()->equals(g2->type())) {
            return false;
        }
        g1 = g1->next();
        g2 = g2->next();
    }
    return true;
}
Beispiel #19
0
bool Type::is_interface() const {
    Class* cls = clazz();
    return is_interface_proto() || (cls && cls->proto()->is_interface_proto()); 
}
Beispiel #20
0
bool Type::is_value() const {
    Class* cls = clazz();
    return is_primitive() || is_value_proto() 
        || (cls && cls->proto()->is_value_proto()); 
}
Beispiel #21
0
bool Type::is_union() const {
    Class* cls = clazz();
    return is_union_proto() || (cls && cls->proto()->is_union_proto());
}
Beispiel #22
0
/**
 * The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the current class (§2.6),
 * where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant pool item at that index must be a symbolic reference to a method (§5.1),
 * which gives the name and descriptor (§4.3.3) of the method as well as a symbolic reference to the class in which the method is to be found.
 * The named method is resolved (§5.4.3.3). Finally, if the resolved method is protected (§4.6), and it is a member of a superclass of the current class,
 * and the method is not declared in the same runtime package (§5.3) as the current class,
 * then the class of objectref must be either the current class or a subclass of the current class.
 */
void JavaVM::pushNewFrame(StackFrame * stackFrame)
{
	Frame * frame = stackFrame->top();
	//get a process counter and get a next instruction
	//invoke instance method on object objectref, where the method is identified by method reference index in constant pool (indexbyte1 << 8 + indexbyte2)
	u1 * ptr = &frame->_code->code[frame->_pc + 1];
	u2 method_index = getu2(ptr);
	CPBase * pConstPool = frame->_clazz->_constantPool[method_index];

	//get method
	ASSERT(pConstPool->tag == CONSTANT_Methodref);
	CONSTANT_Methodref_info_resolve * method_ref = reinterpret_cast<CONSTANT_Methodref_info_resolve *>(pConstPool);

	/*get class name*/
	std::string class_name;
	class_name.assign((const char *)method_ref->_class_index->_name_index->bytes, method_ref->_class_index->_name_index->length);
	boost::shared_ptr<JavaClass> clazz(_classHeap->getClass(class_name));

	/*get method*/
	std::string method_name, method_desc;
	frame->_clazz->getStringFromConstPool(method_ref->_name_and_type_index->name_index, method_name);
	frame->_clazz->getStringFromConstPool(method_ref->_name_and_type_index->descriptor_index, method_desc);

	printf("invoke %s.%s:%s\n", class_name.c_str(), method_name.c_str(), method_desc.c_str());
	jClass virtual_clazz(clazz);
	u2 nIndex = clazz->getMethodID(method_name, method_desc, virtual_clazz);
	
	Frame * new_f = new Frame();
	new_f->_clazz = virtual_clazz;
	new_f->_method = virtual_clazz->_methods[nIndex];
	if (!(virtual_clazz->_methods[nIndex]->getAccessFlags() & ACC_NATIVE)) // there is no code
	{
		Code_attribute * code = virtual_clazz->getMethodCodeAttribute(nIndex);
		new_f->setCode(code);
	}
	else
	{
		Code_attribute * code = new Code_attribute();
		code->max_locals = 1;
		code->max_stack = 0;
		code->code = NULL;
		new_f->setCode(code);
	}

	/*
	 * The Java virtual machine uses local variables to pass parameters on method invocation
	 * http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.1
	 */
	std::string params = countMethodStack(method_desc);
	for (std::size_t i = params.length(), j = i; i >= 0; i--, j--)
	{
		/** 
		  * The objectref and the argument values are consecutively made the values of local variables of the new frame,
		  * with objectref in local variable 0,
		  * arg1 in local variable 1 (or, if arg1 is of type long or double, in local variables 1 and 2), and so on
		  */
		new_f->_localVariable[i] = frame->_operandStack.top();
		frame->_operandStack.pop();
		
		//TODO melhorar
		if (i == 0)
			break;

		/* if it is long or double I need to add 1 in local variable */
		if(params.size() > 0 && (params[j-1] =='J' || params[j-1] =='D'))
			i--;
	}

	if (new_f->_method->getAccessFlags() & ACC_SYNCHRONIZED)
	{
		if (lockMonitor(new_f->_localVariable[0]._var._objectref))
		{
			
		}
		else
		{
			new_f->_lock = true;
			new_f->_waitingObj = new_f->_localVariable[0]._var._objectref;
			this->wait(new_f->_waitingObj, boost::bind(&Frame::unlock, new_f));
		}
	}

	//if this method has ACC_SYNCHRONIZED then I need to do monitorenter, it is implicit
	//I need to get new_f->_localVariable[0], this location in array is objref, I need to lock this object
	stackFrame->push(new_f);
}
jobject NewJniArrayList(JNIEnv* env)
{
  JniLocalReference<jclass> clazz(env, env->FindClass("java/util/ArrayList"));
  jmethodID ctor = env->GetMethodID(*clazz, "<init>", "()V");
  return env->NewObject(*clazz, ctor);
}
Beispiel #24
0
bool Type::is_functor() const {
    Class* cls = clazz();
    return is_functor_proto() || (cls && cls->proto()->is_functor_proto());
}
void JniThrowException(JNIEnv* env, const std::string& message)
{
  JniLocalReference<jclass> clazz(env,
      env->FindClass(PKG("AdblockPlusException")));
  env->ThrowNew(*clazz, message.c_str());
}
void JniAddObjectToList(JNIEnv* env, jobject list, jobject value)
{
  JniLocalReference<jclass> clazz(env, env->GetObjectClass(list));
  jmethodID add = env->GetMethodID(*clazz, "add", "(Ljava/lang/Object;)Z");
  env->CallBooleanMethod(list, add, value);
}