1 package org.openflow.codec.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 OFPMatch as a JavaBean
15 * For some (inane!) reason, using chained setters in OFPMatch breaks a lot of
16 * the JavaBean defaults.
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.
21 * @author Rob Sherwood (rob.sherwood@stanford.edu)
25 public class OFPMatchBeanInfo extends SimpleBeanInfo {
28 public PropertyDescriptor[] getPropertyDescriptors() {
29 List<PropertyDescriptor> descs = new LinkedList<PropertyDescriptor>();
30 Field[] fields = OFPMatch.class.getDeclaredFields();
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))
38 name = fields[i].getName();
39 Class<?> type = fields[i].getType();
42 descs.add(new PropertyDescriptor(name, name2getter(OFPMatch.class, name), name2setter(OFPMatch.class,
44 } catch (IntrospectionException e) {
46 throw new RuntimeException(e);
50 return descs.toArray(new PropertyDescriptor[0]);
53 private Method name2setter(Class<OFPMatch> c, String name, Class<?> type) {
54 String mName = "set" + toLeadingCaps(name);
57 m = c.getMethod(mName, new Class[] { type });
58 } catch (SecurityException e) {
61 throw new RuntimeException(e);
62 } catch (NoSuchMethodException e) {
64 throw new RuntimeException(e);
69 private Method name2getter(Class<OFPMatch> c, String name) {
70 String mName = "get" + toLeadingCaps(name);
73 m = c.getMethod(mName, new Class[] {});
74 } catch (SecurityException e) {
76 throw new RuntimeException(e);
77 } catch (NoSuchMethodException e) {
79 throw new RuntimeException(e);
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);