Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / Accessor.java
1 package org.opendaylight.controller.northbound.commons.query;
2
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.AccessibleObject;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Method;
7 import java.lang.reflect.Type;
8
9 /*package*/ class Accessor {
10     protected final AccessibleObject _accessorObj;
11
12     public Accessor(AccessibleObject accessor) {
13         _accessorObj = accessor;
14         _accessorObj.setAccessible(true);
15     }
16
17     public AccessibleObject getAccessibleObject() {
18         return _accessorObj;
19     }
20
21     public Annotation[] getAnnotations() {
22         return _accessorObj.getAnnotations();
23     }
24
25     public Object getValue(Object parent) throws QueryException {
26         try {
27             if (_accessorObj instanceof Field) {
28                 return ((Field)_accessorObj).get(parent);
29             } else {
30                 // assume method
31                 return ((Method)_accessorObj).invoke(parent);
32             }
33         } catch (Exception e) {
34             throw new QueryException("Failure in retrieving value", e);
35         }
36     }
37     public Type getGenericType() {
38         if (_accessorObj instanceof Field) {
39             return ((Field)_accessorObj).getGenericType();
40         } else {
41             // assume method
42             return ((Method)_accessorObj).getGenericReturnType();
43         }
44     }
45     public Class<?> getType() {
46
47         if (_accessorObj instanceof Field) {
48             return ((Field)_accessorObj).getType();
49         } else {
50             // assume method
51             return ((Method)_accessorObj).getReturnType();
52         }
53     }
54
55 }