- /**
- * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
- *
- * Note: This method uses reflection, but setting delegate field should not occur too much
- * to introduce any significant performance hits.
- */
- public static void setDelegate(final Object proxy, final Object delegate) {
- try {
- Class<? extends Object> _class = proxy.getClass();
- final Field field = _class.getField(RuntimeCodeSpecification.DELEGATE_FIELD);
- boolean _equals = Objects.equal(field, null);
- if (_equals) {
- UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException("Unable to set delegate to proxy");
- throw _unsupportedOperationException;
- }
- boolean _or = false;
- boolean _equals_1 = Objects.equal(delegate, null);
- if (_equals_1) {
- _or = true;
- } else {
- Class<? extends Object> _type = field.getType();
- Class<? extends Object> _class_1 = delegate.getClass();
- boolean _isAssignableFrom = _type.isAssignableFrom(_class_1);
- _or = (_equals_1 || _isAssignableFrom);
- }
- if (_or) {
- field.set(proxy, delegate);
- } else {
- IllegalArgumentException _illegalArgumentException = new IllegalArgumentException("delegate class is not assignable to proxy");
- throw _illegalArgumentException;
- }
- } catch (Throwable _e) {
- throw Exceptions.sneakyThrow(_e);
+ private static Object getFieldValue(final Field field, final Object obj) {
+ try {
+ return field.get(obj);
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException(String.format("Failed to get field %s of object %s", field, obj), e);
+ }
+ }
+
+ private static void setFieldValue(final Field field, final Object obj, final Object value) {
+ try {
+ field.set(obj, value);
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException(String.format("Failed to set field %s to %s", field, value), e);
+ }
+ }
+
+ /**
+ * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
+ *
+ * Note: This method uses reflection, but access to delegate field should be
+ * avoided and called only if necessary.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T extends RpcService> T getDelegate(final RpcService proxy) {
+ return (T)getFieldValue(getDelegateField(proxy.getClass()), proxy);
+ }
+
+ /**
+ * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
+ *
+ * Note: This method uses reflection, but setting delegate field should not occur too much
+ * to introduce any significant performance hits.
+ */
+ public static void setDelegate(final Object proxy, final Object delegate) {
+ final Field field = getDelegateField(proxy.getClass());
+
+ if (delegate != null) {
+ final Class<?> ft = field.getType();
+ if (!ft.isAssignableFrom(delegate.getClass())) {
+ throw new IllegalArgumentException(
+ String.format("Field %s type %s is not compatible with delegate type %s",
+ field, ft, delegate.getClass()));
+ }
+ }
+
+ setFieldValue(field, proxy, delegate);