1 package org.openflow.protocol;
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;
13 * Extra info for how to treat OFMatch as a JavaBean
15 * For some (inane!) reason, using chained setters in OFMatch breaks a lot of the JavaBean defaults.
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.
20 * @author Rob Sherwood (rob.sherwood@stanford.edu)
24 public class OFMatchBeanInfo extends SimpleBeanInfo {
27 public PropertyDescriptor[] getPropertyDescriptors() {
28 List<PropertyDescriptor> descs = new LinkedList<PropertyDescriptor>();
29 Field[] fields = OFMatch.class.getDeclaredFields();
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))
37 name = fields[i].getName();
38 Class<?> type = fields[i].getType();
41 descs.add(new PropertyDescriptor(name,
42 name2getter(OFMatch.class, name),
43 name2setter(OFMatch.class, name, type)));
44 } catch (IntrospectionException e) {
46 throw new RuntimeException(e);
50 return descs.toArray(new PropertyDescriptor[0]);
54 private Method name2setter(Class<OFMatch> c, String name, Class<?> type) {
55 String mName = "set" + toLeadingCaps(name);
58 m = c.getMethod(mName, new Class[]{ type});
59 } catch (SecurityException e) {
62 throw new RuntimeException(e);
63 } catch (NoSuchMethodException e) {
65 throw new RuntimeException(e);
70 private Method name2getter(Class<OFMatch> c, String name) {
71 String mName= "get" + toLeadingCaps(name);
74 m = c.getMethod(mName, new Class[]{});
75 } catch (SecurityException e) {
77 throw new RuntimeException(e);
78 } catch (NoSuchMethodException e) {
80 throw new RuntimeException(e);
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);