Merge "BUG-980: stop emiting copyright"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / RuntimeCodeHelper.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.codegen;
9
10 import java.lang.reflect.Field;
11 import java.util.Map;
12
13 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.binding.RpcService;
16
17 public final class RuntimeCodeHelper {
18     private RuntimeCodeHelper() {
19         throw new UnsupportedOperationException("Utility class should never be instantiated");
20     }
21
22     private static Field getField(final Class<?> cls, final String name) {
23         try {
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);
30         }
31     }
32
33     private static Field getDelegateField(final Class<?> cls) {
34         return getField(cls, RuntimeCodeSpecification.DELEGATE_FIELD);
35     }
36
37     private static Object getFieldValue(final Field field, final Object obj) {
38         try {
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);
42         }
43     }
44
45     private static void setFieldValue(final Field field, final Object obj, final Object value) {
46         try {
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);
50         }
51     }
52
53     /**
54      * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
55      *
56      * Note: This method uses reflection, but access to delegate field should be
57      * avoided and called only if necessary.
58      */
59     @SuppressWarnings("unchecked")
60     public static <T extends RpcService> T getDelegate(final RpcService proxy) {
61         return (T)getFieldValue(getDelegateField(proxy.getClass()), proxy);
62     }
63
64     /**
65      * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
66      *
67      * Note: This method uses reflection, but setting delegate field should not occur too much
68      * to introduce any significant performance hits.
69      */
70     public static void setDelegate(final Object proxy, final Object delegate) {
71         final Field field = getDelegateField(proxy.getClass());
72
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()));
79             }
80         }
81
82         setFieldValue(field, proxy, delegate);
83     }
84
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);
89     }
90
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);
94     }
95 }