private PyObject instrumentObject(Test test,
PyDispatcher pyDispatcher,
Object o)
throws NotWrappableTypeException {
//对Object o 进行判定。如果是python对象。。。。
if (o instanceof PyObject) {
// Jython object.
if (o instanceof PyInstance) {
final PyInstance pyInstance = (PyInstance)o;
final PyClass pyClass =
m_versionAdapter .getClassForInstance(pyInstance);
return new InstrumentedPyInstance(
test, pyClass, pyInstance, pyDispatcher);
}
else if (o instanceof PyFunction) {
return new InstrumentedPyJavaInstanceForPyFunctions(
test, (PyFunction)o, pyDispatcher);
}
else if (o instanceof PyMethod) {
return new InstrumentedPyJavaInstanceForPyMethods(
test, (PyMethod)o, pyDispatcher);
}
else if (o instanceof PyReflectedFunction) {
return new InstrumentedPyReflectedFunction(
test, (PyReflectedFunction)o, pyDispatcher);
}
else {
// Fail, rather than guess a generic approach.
throw new NotWrappableTypeException( "Unknown PyObject: " +
o.getClass());
}
}
else if (o instanceof PyProxy) {
// Jython object that extends a Java class.
final PyInstance pyInstance = ((PyProxy)o)._getPyInstance();
final PyClass pyClass =
m_versionAdapter.getClassForInstance(pyInstance);
return new InstrumentedPyInstance(
test, pyClass, pyInstance, pyDispatcher);
}
//如果O是java class<?>,实现这段逻辑。
else if (o instanceof Class<?>) {
return new InstrumentedPyJavaClass(test, (Class<?>)o, pyDispatcher);
}
else if (o != null) {
// Java object.
// NB Jython uses Java types for some primitives and strings.
if (!o.getClass().isArray() &&
!(o instanceof Number) &&
!(o instanceof String)) {
//如果是java 对象,返回InstrumentedPyJavaInstanceForJavaInstances
return new InstrumentedPyJavaInstanceForJavaInstances(
test, o, pyDispatcher);
}
}
return null;
}
class InstrumentedPyJavaInstanceForJavaInstances
//脚本: request.GET("http://www.google.com.hk/")
public PyObject invoke( final String name, final PyObject arg1) {
//调用
return getInstrumentationHelper().dispatch(
new PyDispatcher.Callable() {
public PyObject call() {
//invoke后,脚本变成request(GET,"http://www.google.com.hk/")
//name=GET,arg1="http://www.google.com.hk/"
return InstrumentedPyJavaInstanceForJavaInstances.super.invoke(
name, arg1);
//
}
}
);
}
//class InstrumentationHelper
//invoke后的PyDispatcher.Callable() ,传入getInstrumentationHelper().dispatch
public PyObject dispatch(PyDispatcher.Callable callable) {
return m_dispatcher.dispatch(callable);
}
class PyDispatcher
public PyObject dispatch(Callable callable) {
try {
//开始计时,这里涉及到grinder的数据统计。另外分析。
//计时最小单位是一个request。
m_recorder.start();
boolean success = false;
try {
final PyObject result = callable.call();
success = true;
return result;
}
finally {
//计时结束。
m_recorder.end(success);
}
}