com.javonet.core.handler.InvokeGenericStaticMethodHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javonet-java-sdk Show documentation
Show all versions of javonet-java-sdk Show documentation
Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology.
It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products.
Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today!
For more information check out our guides at https://www.javonet.com/guides/v2/
package com.javonet.core.handler;
import com.javonet.utils.Command;
import com.javonet.utils.exceptions.JavonetArgumentsMismatchException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class InvokeGenericStaticMethodHandler extends AbstractHandler {
private final int requiredArgumentsCount = 2;
@Override
public Object process(Command command) throws Exception {
if (command.getPayload().length < requiredArgumentsCount)
throw new JavonetArgumentsMismatchException(this.getClass().getName(), requiredArgumentsCount);
Method foundMethod;
Class> classToLoad = (Class>) command.getPayload()[0];
String methodToInvoke = command.getPayload()[1].toString();
Object[] arguments = Arrays.stream(command.getPayload()).skip(2).toArray();
Class>[] parameterTypes = new Class[arguments.length];
Arrays.fill(parameterTypes, Object.class);
try{
foundMethod = classToLoad.getMethod(methodToInvoke, parameterTypes);
}
catch (NoSuchMethodException e) {
Method[] methods = classToLoad.getMethods();
StringBuilder message = new StringBuilder("Method " + methodToInvoke + " with arguments " + Arrays.toString(parameterTypes) + " not found in class " + classToLoad.getName() + ". Available methods:\n");
for (Method method : methods) {
message.append(method.getName()).append(" with arguments ").append(Arrays.toString(method.getParameterTypes())).append("\n");
}
throw new NoSuchMethodException(message.toString());
}
return foundMethod.invoke(null, arguments);
}
}