com.javonet.core.handler.Handler 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.CommandType;
import com.javonet.utils.RuntimeName;
import com.javonet.utils.exceptions.ExceptionSerializer;
import java.util.HashMap;
import java.util.Map;
public class Handler {
Map HANDLERS = new HashMap<>();
public Handler() {
HANDLERS.put(CommandType.VALUE, new ValueHandler());
HANDLERS.put(CommandType.LOAD_LIBRARY, new LoadLibraryHandler());
HANDLERS.put(CommandType.INVOKE_STATIC_METHOD, new InvokeStaticMethodHandler());
HANDLERS.put(CommandType.GET_STATIC_FIELD, new GetStaticFieldHandler());
HANDLERS.put(CommandType.SET_STATIC_FIELD, new SetStaticFieldHandler());
HANDLERS.put(CommandType.CREATE_INSTANCE, new CreateInstanceHandler());
HANDLERS.put(CommandType.GET_TYPE, new GetTypeHandler());
HANDLERS.put(CommandType.REFERENCE, new ResolveInstanceHandler());
HANDLERS.put(CommandType.INVOKE_INSTANCE_METHOD, new InvokeInstanceMethodHandler());
HANDLERS.put(CommandType.CAST, new CastHandler());
HANDLERS.put(CommandType.OPTIMIZE, new OptimizeHandler());
HANDLERS.put(CommandType.GENERATE_LIB, new GenerateLibraryHandler());
HANDLERS.put(CommandType.GET_INSTANCE_FIELD, new GetInstanceFieldHandler());
HANDLERS.put(CommandType.SET_INSTANCE_FIELD, new SetInstanceFieldHandler());
HANDLERS.put(CommandType.DESTRUCT_REFERENCE, new DestructReferenceHandler());
HANDLERS.put(CommandType.ARRAY_GET_ITEM, new ArrayGetItemHandler());
HANDLERS.put(CommandType.ARRAY_GET_SIZE, new ArrayGetSizeHandler());
HANDLERS.put(CommandType.ARRAY_GET_RANK, new ArrayGetRankHandler());
HANDLERS.put(CommandType.ARRAY_SET_ITEM, new ArraySetItemHandler());
HANDLERS.put(CommandType.ARRAY, new ArrayHandler());
HANDLERS.put(CommandType.INVOKE_GENERIC_STATIC_METHOD, new InvokeGenericStaticMethodHandler());
HANDLERS.put(CommandType.INVOKE_GENERIC_METHOD, new InvokeGenericMethodHandler());
HANDLERS.put(CommandType.GET_ENUM_ITEM, new GetEnumItemHandler());
HANDLERS.put(CommandType.GET_ENUM_NAME, new GetEnumNameHandler());
HANDLERS.put(CommandType.GET_ENUM_VALUE, new GetEnumValueHandler());
}
public Command handleCommand(Command command) {
try {
if (command.getCommandType() == CommandType.RETRIEVE_ARRAY) {
Object[] responseArray = (Object[]) HANDLERS.get(CommandType.REFERENCE).handleCommand((Command) command.getPayload()[0]);
return Command.createArrayResponse(responseArray, command.getRuntimeName());
}
Object response = HANDLERS.get(command.getCommandType()).handleCommand(command);
return ParseResponse(response, command.getRuntimeName());
} catch (Exception e) {
if (e.getCause() != null) {
return ExceptionSerializer.serializeException(e.getCause(), command);
} else {
return ExceptionSerializer.serializeException(e, command);
}
}
}
private static Command ParseResponse(Object response, RuntimeName runtimeName) {
if(response != null) {
if (isResponseSimpleType(response)) {
return Command.createResponse(response, runtimeName);
} else if (isCommandGenerateLibType(response) || isCommandExceptionType(response)) {
return (Command) response;
} else {
ReferencesCache cache = ReferencesCache.getInstance();
String uuid = cache.cacheReference(response);
return Command.createReference(uuid, runtimeName);
}
}
else {
return Command.createResponse(0, runtimeName);
}
}
private static boolean isCommandGenerateLibType(Object result) {
if (result.getClass() == Command.class) {
return ((Command) result).getCommandType().equals(CommandType.GENERATE_LIB);
} else {
return false;
}
}
private static boolean isCommandExceptionType(Object result) {
if (result.getClass() == Command.class) {
return ((Command) result).getCommandType().equals(CommandType.EXCEPTION);
} else {
return false;
}
}
private static boolean isResponseSimpleType(Object result) {
Class resultClass = result.getClass();
return resultClass.isPrimitive() ||
resultClass == String.class ||
resultClass == Long.class ||
resultClass == Integer.class ||
resultClass == Boolean.class ||
resultClass == Byte.class ||
resultClass == Float.class ||
resultClass == Double.class ||
resultClass == Short.class ||
resultClass == Character.class;
}
}