63f0213ef32d3e97a2affc77e47a20e435adde18
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / OFPMatchBeanInfo.java
1 package org.openflow.codec.protocol;
2
3 import java.beans.IntrospectionException;
4 import java.beans.PropertyDescriptor;
5 import java.beans.SimpleBeanInfo;
6 import java.lang.reflect.Field;
7 import java.lang.reflect.Method;
8 import java.lang.reflect.Modifier;
9 import java.util.LinkedList;
10 import java.util.List;
11
12 /**
13  * Extra info for how to treat OFPMatch as a JavaBean
14  *
15  * For some (inane!) reason, using chained setters in OFPMatch breaks a lot of
16  * the JavaBean defaults.
17  *
18  * We don't really use OFPMatch as a java bean, but there are a lot of nice XML
19  * utils that work for free if OFPMatch follows the java bean paradigm.
20  *
21  * @author Rob Sherwood (rob.sherwood@stanford.edu)
22  *
23  */
24
25 public class OFPMatchBeanInfo extends SimpleBeanInfo {
26
27     @Override
28     public PropertyDescriptor[] getPropertyDescriptors() {
29         List<PropertyDescriptor> descs = new LinkedList<PropertyDescriptor>();
30         Field[] fields = OFPMatch.class.getDeclaredFields();
31         String name;
32         for (int i = 0; i < fields.length; i++) {
33             int mod = fields[i].getModifiers();
34             if (Modifier.isFinal(mod) || // don't expose static or final fields
35                     Modifier.isStatic(mod))
36                 continue;
37
38             name = fields[i].getName();
39             Class<?> type = fields[i].getType();
40
41             try {
42                 descs.add(new PropertyDescriptor(name, name2getter(OFPMatch.class, name), name2setter(OFPMatch.class,
43                         name, type)));
44             } catch (IntrospectionException e) {
45                 e.printStackTrace();
46                 throw new RuntimeException(e);
47             }
48         }
49
50         return descs.toArray(new PropertyDescriptor[0]);
51     }
52
53     private Method name2setter(Class<OFPMatch> c, String name, Class<?> type) {
54         String mName = "set" + toLeadingCaps(name);
55         Method m = null;
56         try {
57             m = c.getMethod(mName, new Class[] { type });
58         } catch (SecurityException e) {
59
60             e.printStackTrace();
61             throw new RuntimeException(e);
62         } catch (NoSuchMethodException e) {
63             e.printStackTrace();
64             throw new RuntimeException(e);
65         }
66         return m;
67     }
68
69     private Method name2getter(Class<OFPMatch> c, String name) {
70         String mName = "get" + toLeadingCaps(name);
71         Method m = null;
72         try {
73             m = c.getMethod(mName, new Class[] {});
74         } catch (SecurityException e) {
75             e.printStackTrace();
76             throw new RuntimeException(e);
77         } catch (NoSuchMethodException e) {
78             e.printStackTrace();
79             throw new RuntimeException(e);
80         }
81         return m;
82     }
83
84     private String toLeadingCaps(String s) {
85         char[] array = s.toCharArray();
86         array[0] = Character.toUpperCase(array[0]);
87         return String.valueOf(array, 0, array.length);
88     }
89 }