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