X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fcodegen%2FRuntimeCodeHelper.java;h=ae90a7739eb6fff27d8aff5265d3d6c397b5e5ad;hp=ea8b6c0972b29943c2129db6eb2b00e7096b1a9c;hb=325ce8c85b1ed89edd4aed2fc4fd2237ccc3b203;hpb=3ab0ebe1df3e7606cce0a61572f79bf12deb17c0 diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java index ea8b6c0972..ae90a7739e 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/RuntimeCodeHelper.java @@ -7,151 +7,89 @@ */ 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 getDelegate(final RpcService proxy) { - try { - Class _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 _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 _type = field.getType(); - Class _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 _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 _type = field.getType(); - Class _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 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,? extends RpcService> getRoutingTable(final RpcService target, final Class tableClass) { - try { - Class _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,? extends RpcService>) _get); - } catch (Throwable _e) { - throw Exceptions.sneakyThrow(_e); - } - } catch (Throwable _e_1) { - throw Exceptions.sneakyThrow(_e_1); + @SuppressWarnings("unchecked") + public static Map,? extends RpcService> getRoutingTable(final RpcService target, final Class tableClass) { + final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass)); + return (Map,? extends RpcService>) getFieldValue(field, target); } - } - public static void setRoutingTable(final RpcService target, final Class tableClass, final Map,? extends RpcService> routingTable) { - try { - Class _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 tableClass, final Map,? extends RpcService> routingTable) { + final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass)); + setFieldValue(field, target, routingTable); } - } -} \ No newline at end of file +}