| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include "jpype.h" |
| 17 | #include "jp_arrayclass.h" |
| 18 | #include "jp_method.h" |
| 19 | #include "pyjp.h" |
| 20 | |
| 21 | JPMethod::JPMethod(JPJavaFrame& frame, |
| 22 | JPClass* claz, |
| 23 | const string& name, |
| 24 | jobject mth, |
| 25 | jmethodID mid, |
| 26 | JPMethodList& moreSpecific, |
| 27 | jint modifiers) |
| 28 | : m_Method(frame, mth) |
| 29 | { |
| 30 | m_Class = claz; |
| 31 | m_Name = name; |
| 32 | m_MethodID = mid; |
| 33 | m_MoreSpecificOverloads = moreSpecific; |
| 34 | m_Modifiers = modifiers; |
| 35 | m_ReturnType = (JPClass*) (-1); |
| 36 | } |
| 37 | |
| 38 | JPMethod::~JPMethod() |
| 39 | = default; |
| 40 | |
| 41 | void JPMethod::setParameters( |
| 42 | JPClass *returnType, |
| 43 | JPClassList&& parameterTypes) |
| 44 | { |
| 45 | m_ReturnType = returnType; |
| 46 | m_ParameterTypes = parameterTypes; |
| 47 | } |
| 48 | |
| 49 | string JPMethod::toString() const |
| 50 | { |
| 51 | return m_Name; |
| 52 | } |
| 53 | |
| 54 | JPMatch::Type matchVars(JPJavaFrame &frame, JPMethodMatch& match, JPPyObjectVector &arg, size_t start, JPClass *vartype) |
| 55 | { |
| 56 | JP_TRACE_IN("JPMethod::matchVars"); |
| 57 | auto *arraytype = dynamic_cast<JPArrayClass*>( vartype); |
| 58 | JPClass *type = arraytype->getComponentType(); |
| 59 | size_t len = arg.size(); |
| 60 | |
| 61 | JPMatch::Type lastMatch = JPMatch::_exact; |
| 62 | for (size_t i = start; i < len; i++) |
| 63 | { |
| 64 | JPMatch::Type quality = type->findJavaConversion(match[i]); |
| 65 | |
| 66 | if (quality < JPMatch::_implicit) |
| 67 | { |
| 68 | return JPMatch::_none; |
| 69 | } |
| 70 | if (quality < lastMatch) |
| 71 | { |
| 72 | lastMatch = quality; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return lastMatch; |
| 77 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 78 | } |
| 79 | |
| 80 | JPMatch::Type JPMethod::matches(JPJavaFrame &frame, JPMethodMatch& methodMatch, bool callInstance, |
| 81 | JPPyObjectVector& arg) |
| 82 | { |
| 83 | ensureTypeCache(); |
| 84 | |
| 85 | JP_TRACE_IN("JPMethod::matches"); |
| 86 | methodMatch.m_Overload = this; |
| 87 | methodMatch.m_Offset = 0; |
| 88 | methodMatch.m_Skip = 0; |
| 89 | methodMatch.m_IsVarIndirect = false; |
| 90 | methodMatch.m_Type = JPMatch::_exact; |
| 91 | |
| 92 | size_t len = arg.size(); |
| 93 | size_t tlen = m_ParameterTypes.size(); |
| 94 | JP_TRACE("Flags", isStatic(), isInstance(), callInstance); |
| 95 | JP_TRACE("arguments length", len); |
| 96 | JP_TRACE("types length", tlen); |
| 97 | if (callInstance && isStatic()) |
| 98 | { |
| 99 | JP_TRACE("Skip this"); |
| 100 | len--; |
| 101 | methodMatch.m_Offset = 1; |
| 102 | } |
| 103 | |
| 104 | if (callInstance || isInstance()) |
| 105 | { |
| 106 | JP_TRACE("Take this"); |
| 107 | methodMatch.m_Skip = 1; |
| 108 | } |
| 109 | |
| 110 | if (!JPModifier::isVarArgs(m_Modifiers)) |
| 111 | { |
| 112 | // Bypass if length does not match |
| 113 | if (len != tlen) |
| 114 | { |
| 115 | JP_TRACE("Argument length mismatch", len, tlen); |
| 116 | return methodMatch.m_Type = JPMatch::_none; |
| 117 | } |
| 118 | } else |
| 119 | { |
| 120 | JP_TRACE("Match vargs"); |
| 121 | methodMatch.m_Type = JPMatch::_none; |
| 122 | if (len < tlen - 1) |
| 123 | { |
| 124 | return methodMatch.m_Type; |
| 125 | } |
| 126 | |
| 127 | JPClass* type = m_ParameterTypes[tlen - 1]; |
| 128 | // Hard, could be direct array or an array. |
| 129 | if (len == tlen) |
| 130 | { |
| 131 | // Try direct |
| 132 | size_t last = tlen - 1 - methodMatch.m_Offset; |
| 133 | methodMatch.m_Type = type->findJavaConversion(methodMatch.m_Arguments[last]); |
| 134 | JP_TRACE("Direct vargs", methodMatch.m_Type); |
| 135 | } |
| 136 | |
| 137 | if (methodMatch.m_Type < JPMatch::_implicit && len >= tlen) |
| 138 | { |
| 139 | // Must match the array type |
| 140 | methodMatch.m_Type = matchVars(frame, methodMatch, arg, tlen - 1 + methodMatch.m_Offset, type); |
| 141 | methodMatch.m_IsVarIndirect = true; |
| 142 | JP_TRACE("Indirect vargs", methodMatch.m_Type); |
| 143 | } else if (len < tlen) |
| 144 | { |
| 145 | methodMatch.m_IsVarIndirect = true; |
| 146 | methodMatch.m_Type = JPMatch::_exact; |
| 147 | JP_TRACE("Empty vargs"); |
| 148 | } |
| 149 | len = tlen - 1; |
| 150 | |
| 151 | if (methodMatch.m_Type < JPMatch::_implicit) |
| 152 | { |
| 153 | return methodMatch.m_Type; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | JP_TRACE("Match args"); |
| 158 | for (size_t i = 0; i < len; i++) |
| 159 | { |
| 160 | size_t j = i + methodMatch.m_Offset; |
| 161 | JPClass *type = m_ParameterTypes[i]; |
| 162 | JPMatch::Type ematch = type->findJavaConversion(methodMatch.m_Arguments[j]); |
| 163 | JP_TRACE("Result", ematch); |
| 164 | if (ematch < methodMatch.m_Type) |
| 165 | { |
| 166 | methodMatch.m_Type = ematch; |
| 167 | } |
| 168 | if (methodMatch.m_Type < JPMatch::_implicit) |
| 169 | { |
| 170 | return methodMatch.m_Type; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return methodMatch.m_Type; |
| 175 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 176 | } |
| 177 | |
| 178 | void JPMethod::packArgs(JPJavaFrame &frame, JPMethodMatch &match, |
| 179 | vector<jvalue> &v, JPPyObjectVector &arg) |
| 180 | { |
| 181 | JP_TRACE_IN("JPMethod::packArgs"); |
| 182 | size_t len = arg.size(); |
| 183 | size_t tlen = m_ParameterTypes.size(); |
| 184 | JP_TRACE("skip", match.m_Skip == 1); |
| 185 | JP_TRACE("offset", match.m_Offset == 1); |
| 186 | JP_TRACE("arguments length", len); |
| 187 | JP_TRACE("types length", tlen); |
| 188 | if (match.m_IsVarIndirect) |
| 189 | { |
| 190 | JP_TRACE("Pack indirect varargs"); |
| 191 | len = tlen - 1; |
| 192 | auto* type = dynamic_cast<JPArrayClass*>( m_ParameterTypes[tlen - 1]); |
| 193 | v[tlen - 1 - match.m_Skip] = type->convertToJavaVector(frame, arg, (jsize) tlen - 1, (jsize) arg.size()); |
| 194 | } |
| 195 | |
| 196 | JP_TRACE("Pack fixed total=", len - match.m_Offset); |
| 197 | for (size_t i = match.m_Skip; i < len; i++) |
| 198 | { |
| 199 | v[i - match.m_Skip] = match.m_Arguments[i].convert(); |
| 200 | } |
| 201 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 202 | } |
| 203 | |
| 204 | JPPyObject JPMethod::invoke(JPJavaFrame& frame, JPMethodMatch& match, JPPyObjectVector& arg, bool instance) |
| 205 | { |
| 206 | JP_TRACE_IN("JPMethod::invoke"); |
| 207 | // Check if it is caller sensitive |
| 208 | if (isCallerSensitive()) |
| 209 | return invokeCallerSensitive(match, arg, instance); |
| 210 | |
| 211 | size_t alen = m_ParameterTypes.size(); |
| 212 | JPClass* retType = m_ReturnType; |
| 213 | |
| 214 | // Pack the arguments |
| 215 | vector<jvalue> v(alen + 1); |
| 216 | packArgs(frame, match, v, arg); |
| 217 | |
| 218 | // Invoke the method (arg[0] = this) |
| 219 | if (JPModifier::isStatic(m_Modifiers)) |
| 220 | { |
| 221 | JP_TRACE("invoke static", m_Name); |
| 222 | jclass claz = m_Class->getJavaClass(); |
| 223 | return retType->invokeStatic(frame, claz, m_MethodID, &v[0]); |
| 224 | } else |
| 225 | { |
| 226 | JPValue* selfObj = PyJPValue_getJavaSlot(arg[0]); |
| 227 | jobject c; |
| 228 | if (selfObj == nullptr) |
| 229 | { |
| 230 | // This only can be hit by calling an instance method as a |
| 231 | // class object. We already know it is safe to convert. |
| 232 | auto val = match.m_Arguments[0].convert(); |
| 233 | c = val.l; |
| 234 | } else |
| 235 | { |
| 236 | c = selfObj->getJavaObject(); |
| 237 | } |
| 238 | jclass clazz = nullptr; |
| 239 | if (!isAbstract() && !instance) |
| 240 | { |
| 241 | clazz = m_Class->getJavaClass(); |
| 242 | JP_TRACE("invoke nonvirtual", m_Name); |
| 243 | } else |
| 244 | { |
| 245 | JP_TRACE("invoke virtual", m_Name); |
| 246 | } |
| 247 | return retType->invoke(frame, c, clazz, m_MethodID, &v[0]); |
| 248 | } |
| 249 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 250 | } |
| 251 | |
| 252 | JPPyObject JPMethod::invokeCallerSensitive(JPMethodMatch& match, JPPyObjectVector& arg, bool instance) |
| 253 | { |
| 254 | JP_TRACE_IN("JPMethod::invokeCallerSensitive"); |
| 255 | size_t alen = m_ParameterTypes.size(); |
| 256 | JPJavaFrame frame = JPJavaFrame::outer((int) (8 + alen)); |
| 257 | JPContext *context = frame.getContext(); |
| 258 | JPClass* retType = m_ReturnType; |
| 259 | |
| 260 | // Pack the arguments |
| 261 | vector<jvalue> v(alen + 1); |
| 262 | packArgs(frame, match, v, arg); |
| 263 | |
| 264 | //Proxy the call to |
| 265 | // public static Object callMethod(Method method, Object obj, Object[] args) |
| 266 | jobject self = nullptr; |
| 267 | size_t len = alen; |
| 268 | if (!isStatic()) |
| 269 | { |
| 270 | JP_TRACE("Call instance"); |
| 271 | len--; |
| 272 | JPValue *selfObj = PyJPValue_getJavaSlot(arg[0]); |
| 273 | if (selfObj == nullptr) |
| 274 | JP_RAISE(PyExc_RuntimeError, "Null object"); // GCOVR_EXCL_LINE |
| 275 | self = selfObj->getJavaObject(); |
| 276 | } |
| 277 | |
| 278 | // Convert arguments |
| 279 | jobjectArray ja = frame.NewObjectArray((jsize) len, context->_java_lang_Object->getJavaClass(), nullptr); |
| 280 | for (jsize i = 0; i < (jsize) len; ++i) |
| 281 | { |
| 282 | JPClass *cls = m_ParameterTypes[i + match.m_Skip - match.m_Offset]; |
| 283 | if (cls->isPrimitive()) |
| 284 | { |
| 285 | auto* type = dynamic_cast<JPPrimitiveType*>( cls); |
| 286 | PyObject *u = arg[i + match.m_Skip]; |
| 287 | JPMatch conv(&frame, u); |
| 288 | JPClass *boxed = type->getBoxedClass(frame); |
| 289 | boxed->findJavaConversion(conv); |
| 290 | jvalue v = conv.convert(); |
| 291 | frame.SetObjectArrayElement(ja, i, v.l); |
| 292 | } else |
| 293 | { |
| 294 | frame.SetObjectArrayElement(ja, i, v[i].l); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Call the method |
| 299 | jobject o; |
| 300 | { |
| 301 | JPPyCallRelease call; |
| 302 | o = frame.callMethod(m_Method.get(), self, ja); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | JP_TRACE("ReturnType", retType->getCanonicalName()); |
| 307 | |
| 308 | // Deal with the return |
| 309 | if (retType->isPrimitive()) |
| 310 | { |
| 311 | JP_TRACE("Return primitive"); |
| 312 | JPClass *boxed = (dynamic_cast<JPPrimitiveType*>( retType))->getBoxedClass(frame); |
| 313 | JPValue out = retType->getValueFromObject(frame, JPValue(boxed, o)); |
| 314 | return retType->convertToPythonObject(frame, out.getValue(), false); |
| 315 | } else |
| 316 | { |
| 317 | JP_TRACE("Return object"); |
| 318 | jvalue v; |
| 319 | v.l = o; |
| 320 | return retType->convertToPythonObject(frame, v, false); |
| 321 | } |
| 322 | JP_TRACE_OUT; |
| 323 | } |
| 324 | |
| 325 | JPValue JPMethod::invokeConstructor(JPJavaFrame& frame, JPMethodMatch& match, JPPyObjectVector& arg) |
| 326 | { |
| 327 | JP_TRACE_IN("JPMethod::invokeConstructor"); |
| 328 | size_t alen = m_ParameterTypes.size(); |
| 329 | vector<jvalue> v(alen + 1); |
| 330 | packArgs(frame, match, v, arg); |
| 331 | JPPyCallRelease call; |
| 332 | return JPValue(m_Class, frame.NewObjectA(m_Class->getJavaClass(), m_MethodID, &v[0])); |
| 333 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 334 | } |
| 335 | |
| 336 | string JPMethod::matchReport(JPPyObjectVector& args) |
| 337 | { |
| 338 | ensureTypeCache(); |
| 339 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 340 | std::stringstream res; |
| 341 | |
| 342 | res << m_ReturnType->getCanonicalName() << " ("; |
| 343 | |
| 344 | bool isFirst = true; |
| 345 | for (auto & m_ParameterType : m_ParameterTypes) |
| 346 | { |
| 347 | if (isFirst && !isStatic()) |
| 348 | { |
| 349 | isFirst = false; |
| 350 | continue; |
| 351 | } |
| 352 | isFirst = false; |
| 353 | res << m_ParameterType->getCanonicalName(); |
| 354 | } |
| 355 | |
| 356 | res << ") ==> "; |
| 357 | |
| 358 | JPMethodMatch methodMatch(frame, args, false); |
| 359 | matches(frame, methodMatch, !isStatic(), args); |
| 360 | // GCOVR_EXCL_START |
| 361 | switch (methodMatch.m_Type) |
| 362 | { |
| 363 | case JPMatch::_none: |
| 364 | res << "NONE"; |
| 365 | break; |
| 366 | case JPMatch::_explicit: |
| 367 | res << "EXPLICIT"; |
| 368 | break; |
| 369 | case JPMatch::_implicit: |
| 370 | res << "IMPLICIT"; |
| 371 | break; |
| 372 | case JPMatch::_exact: |
| 373 | res << "EXACT"; |
| 374 | break; |
| 375 | default: |
| 376 | res << "UNKNOWN"; |
| 377 | break; |
| 378 | } |
| 379 | // GCOVR_EXCL_STOP |
| 380 | res << std::endl; |
| 381 | return res.str(); |
| 382 | } |
| 383 | |
| 384 | bool JPMethod::checkMoreSpecificThan(JPMethod* other) const |
| 385 | { |
| 386 | for (auto m_MoreSpecificOverload : m_MoreSpecificOverloads) |
| 387 | { |
| 388 | if (other == m_MoreSpecificOverload) |
| 389 | return true; |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | void JPMethod::ensureTypeCache() |
| 395 | { |
| 396 | if (m_ReturnType != (JPClass*) (-1)) |
| 397 | return; |
| 398 | |
| 399 | JPContext_global->getTypeManager()->populateMethod(this, m_Method.get()); |
| 400 | } |