bool methodTest() { Class A(NULL, "A"); Class B(&A, "B"); ASSERT_NO_THROW(B.addMethod("nothing", doesNothing)); ASSERT_EQUALS("nothing", B.getMethod("nothing").name()); ASSERT_EQUALS("B", B.getMethod("nothing").getDeclaringClass()); Method m = B.getMethod("nothing"); Object* inst = A.newInstance(); Object* b_inst = B.newInstance(); ASSERT_THROW(MethodNotFound, m.invoke(inst)); ASSERT_NO_THROW(m.invoke(b_inst)); ASSERT_NO_THROW(A.addInstanceField("x", INT)); // set accessible must be zero but invoke must succeed ASSERT_NO_THROW(A.addMethod("goblin", goblinIsHere)); Object* obj = A.newInstance(); Field f = A.getField("x"); Class::setAccessible(true); ASSERT_EQUALS(f.getInt(obj), 0); Class::setAccessible(false); ASSERT_NO_THROW(obj->invokeMethod("goblin")); ASSERT_THROW(FieldNotAccessible, f.getInt(obj)); Class::setAccessible(true); ASSERT_EQUALS(f.getInt(obj), 7); /* delete inst; delete b_inst; delete obj;*/ return true; }
SmokeObject *SmokeObject::convertImplicitly(const SmokeType &type) const { const Class *cl = Class::fromSmokeId(type.smoke(), type.classId()); Method *meth = cl->findImplicitConverter(this); SmokeObject *converted = NULL; if (meth) { Smoke::StackItem stack[2]; stack[1].s_voidp = _ptr; meth->invoke(NULL, stack); if (meth->lastError() == Method::NoError) converted = SmokeObject::fromPtr(stack[0].s_voidp, type, true); delete meth; } return converted; }
void Object::invokeMethod(std::string name) { Class* myClass = this->getClass(); Method method = myClass->getMethod(name); method.invoke(this); }