MCPcopy
hub / github.com/ccxt/ccxt / callDynamic

Method callDynamic

java/cli/src/main/java/cli/Main.java:195–247  ·  view source on GitHub ↗
(Object instance, String methodName, Object... args)

Source from the content-addressed store, hash-verified

193 }
194
195 public static Object callDynamic(Object instance, String methodName, Object... args) {
196 Class<?> clazz = instance.getClass();
197
198 try {
199 Class<?>[] paramTypes = Arrays.stream(args)
200 .map(a -> a == null ? null : a.getClass())
201 .toArray(Class<?>[]::new);
202
203 try {
204 Method m = clazz.getMethod(methodName, paramTypes);
205 return m.invoke(instance, args);
206 } catch (NoSuchMethodException ignore) {
207 // Try matching a varargs method
208 }
209
210 // Try varargs method: (Object, Object[])
211 for (Method m : clazz.getMethods()) {
212 if (!m.getName().equals(methodName)) continue;
213
214 if (m.isVarArgs()) {
215 Class<?>[] types = m.getParameterTypes();
216
217 int fixedCount = types.length - 1;
218
219 if (args.length < fixedCount) continue;
220
221 Object[] invokeArgs = new Object[types.length];
222
223 for (int i = 0; i < fixedCount; i++) {
224 invokeArgs[i] = args[i];
225 }
226
227 // Build the varargs array
228 Class<?> varType = types[fixedCount].getComponentType();
229 int varCount = args.length - fixedCount;
230
231 Object varArray = Array.newInstance(varType, varCount);
232 for (int i = 0; i < varCount; i++) {
233 Array.set(varArray, i, args[fixedCount + i]);
234 }
235
236 invokeArgs[fixedCount] = varArray;
237
238 return m.invoke(instance, invokeArgs);
239 }
240 }
241
242 throw new NoSuchMethodException("Method " + methodName + " not found");
243
244 } catch (Exception e) {
245 throw new RuntimeException("Error calling method: " + methodName, e);
246 }
247 }
248
249
250

Callers 1

mainMethod · 0.95

Calls 6

setMethod · 0.80
toArrayMethod · 0.45
mapMethod · 0.45
streamMethod · 0.45
getMethodMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected