private static <RETURN> Future<RETURN> internalCallRemoteMethod(final Object argument, final RSBRemote remote, final Class<? extends RETURN> returnClass) throws CouldNotPerformException { String methodName = "?"; try { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); if (stackTrace == null) { throw new NotAvailableException("method stack"); } else if (stackTrace.length == 0) { throw new InvalidStateException("Could not detect method stack!"); } try { for (int i = 0; i < stackTrace.length; i++) { if (stackTrace[i].getMethodName().equals(INTERNAL_CALL_REMOTE_METHOD_NAME)) { methodName = stackTrace[i + 2].getMethodName(); break; } } } catch (NullPointerException | IndexOutOfBoundsException ex) { throw new CouldNotPerformException("Could not detect method name!"); } return remote.callMethodAsync(methodName, argument); } catch (CouldNotPerformException ex) { throw new CouldNotPerformException("Could not call remote Message[" + methodName + "]", ex); } }