2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.codegen;
10 import java.lang.reflect.Field;
13 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.binding.RpcService;
17 public final class RuntimeCodeHelper {
18 private RuntimeCodeHelper() {
19 throw new UnsupportedOperationException("Utility class should never be instantiated");
22 private static Field getField(final Class<?> cls, final String name) {
24 return cls.getField(name);
25 } catch (NoSuchFieldException e) {
26 throw new IllegalArgumentException(
27 String.format("Class %s is missing field %s", cls, name), e);
28 } catch (SecurityException e) {
29 throw new IllegalStateException(String.format("Failed to examine class %s", cls), e);
33 private static Field getDelegateField(final Class<?> cls) {
34 return getField(cls, RuntimeCodeSpecification.DELEGATE_FIELD);
37 private static Object getFieldValue(final Field field, final Object obj) {
39 return field.get(obj);
40 } catch (IllegalAccessException e) {
41 throw new IllegalStateException(String.format("Failed to get field %s of object %s", field, obj), e);
45 private static void setFieldValue(final Field field, final Object obj, final Object value) {
47 field.set(obj, value);
48 } catch (IllegalAccessException e) {
49 throw new IllegalStateException(String.format("Failed to set field %s to %s", field, value), e);
54 * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
56 * Note: This method uses reflection, but access to delegate field should be
57 * avoided and called only if necessary.
59 @SuppressWarnings("unchecked")
60 public static <T extends RpcService> T getDelegate(final RpcService proxy) {
61 return (T)getFieldValue(getDelegateField(proxy.getClass()), proxy);
65 * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
67 * Note: This method uses reflection, but setting delegate field should not occur too much
68 * to introduce any significant performance hits.
70 public static void setDelegate(final Object proxy, final Object delegate) {
71 final Field field = getDelegateField(proxy.getClass());
73 if (delegate != null) {
74 final Class<?> ft = field.getType();
75 if (!ft.isAssignableFrom(delegate.getClass())) {
76 throw new IllegalArgumentException(
77 String.format("Field %s type %s is not compatible with delegate type %s",
78 field, ft, delegate.getClass()));
82 setFieldValue(field, proxy, delegate);
85 @SuppressWarnings("unchecked")
86 public static Map<InstanceIdentifier<? extends Object>,? extends RpcService> getRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass) {
87 final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass));
88 return (Map<InstanceIdentifier<? extends Object>,? extends RpcService>) getFieldValue(field, target);
91 public static void setRoutingTable(final RpcService target, final Class<? extends BaseIdentity> tableClass, final Map<InstanceIdentifier<? extends Object>,? extends RpcService> routingTable) {
92 final Field field = getField(target.getClass(), RuntimeCodeSpecification.getRoutingTableField(tableClass));
93 setFieldValue(field, target, routingTable);