*/
package org.opendaylight.controller.sal.binding.codegen;
-import com.google.common.base.Objects;
import java.lang.reflect.Field;
import java.util.Map;
-import org.eclipse.xtext.xbase.lib.Exceptions;
-import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification;
+
import org.opendaylight.yangtools.yang.binding.BaseIdentity;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.RpcService;
-@SuppressWarnings("all")
-public class RuntimeCodeHelper {
- /**
- * 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 neccessary.
- */
- public static <T extends RpcService> T getDelegate(final RpcService proxy) {
- try {
- Class<? extends RpcService> _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 get delegate from proxy");
- throw _unsupportedOperationException;
- }
- try {
- Object _get = field.get(proxy);
- return ((T) _get);
- } catch (Throwable _e) {
- throw Exceptions.sneakyThrow(_e);
- }
- } catch (Throwable _e_1) {
- throw Exceptions.sneakyThrow(_e_1);
+public final class RuntimeCodeHelper {
+ private RuntimeCodeHelper() {
+ throw new UnsupportedOperationException("Utility class should never be instantiated");
+ }
+
+ private static Field getField(final Class<?> cls, final String name) {
+ try {
+ return cls.getField(name);
+ } catch (NoSuchFieldException e) {
+ throw new IllegalArgumentException(
+ String.format("Class %s is missing field %s", cls, name), e);
+ } catch (SecurityException e) {
+ throw new IllegalStateException(String.format("Failed to examine class %s", cls), e);
+ }
}
- }
- /**
- * 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 RpcService proxy, final RpcService delegate) {
- try {
- Class<? extends RpcService> _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 RpcService> _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 Field getDelegateField(final Class<?> cls) {
+ return getField(cls, RuntimeCodeSpecification.DELEGATE_FIELD);
}
- }
- /**
- * 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);
}
- }
- public static Map<InstanceIdentifier<? extends Object>,? extends RpcService> getRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass) {
- try {
- Class<? extends RpcService> _class = target.getClass();
- String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass);
- final Field field = _class.getField(_routingTableField);
- boolean _equals = Objects.equal(field, null);
- if (_equals) {
- UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException(
- "Unable to get routing table. Table field does not exists");
- throw _unsupportedOperationException;
- }
- try {
- Object _get = field.get(target);
- return ((Map<InstanceIdentifier<? extends Object>,? extends RpcService>) _get);
- } catch (Throwable _e) {
- throw Exceptions.sneakyThrow(_e);
- }
- } catch (Throwable _e_1) {
- throw Exceptions.sneakyThrow(_e_1);
+ @SuppressWarnings("unchecked")
+ public static Map<InstanceIdentifier<? extends Object>,? extends RpcService> getRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass) {
+ final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass));
+ return (Map<InstanceIdentifier<? extends Object>,? extends RpcService>) getFieldValue(field, target);
}
- }
- public static void setRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass, final Map<InstanceIdentifier<? extends Object>,? extends RpcService> routingTable) {
- try {
- Class<? extends RpcService> _class = target.getClass();
- String _routingTableField = RuntimeCodeSpecification.getRoutingTableField(tableClass);
- final Field field = _class.getField(_routingTableField);
- boolean _equals = Objects.equal(field, null);
- if (_equals) {
- UnsupportedOperationException _unsupportedOperationException = new UnsupportedOperationException(
- "Unable to set routing table. Table field does not exists");
- throw _unsupportedOperationException;
- }
- field.set(target, routingTable);
- } catch (Throwable _e) {
- throw Exceptions.sneakyThrow(_e);
+ public static void setRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass, final Map<InstanceIdentifier<? extends Object>,? extends RpcService> routingTable) {
+ final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass));
+ setFieldValue(field, target, routingTable);
}
- }
-}
\ No newline at end of file
+}