X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=third-party%2Fopenflowj%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenflow%2Fprotocol%2FOFMatchBeanInfo.java;fp=third-party%2Fopenflowj%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenflow%2Fprotocol%2FOFMatchBeanInfo.java;h=9f7a40e62f42aff6a7937162c7034ee94bc36b1a;hp=0000000000000000000000000000000000000000;hb=42210c03b0a4c54706320ba9f55794c0abd4d201;hpb=7576b38152b393793b1c9ec3df0ff86685f95236 diff --git a/third-party/openflowj/src/main/java/org/openflow/protocol/OFMatchBeanInfo.java b/third-party/openflowj/src/main/java/org/openflow/protocol/OFMatchBeanInfo.java new file mode 100644 index 0000000000..9f7a40e62f --- /dev/null +++ b/third-party/openflowj/src/main/java/org/openflow/protocol/OFMatchBeanInfo.java @@ -0,0 +1,90 @@ +package org.openflow.protocol; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.beans.SimpleBeanInfo; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.LinkedList; +import java.util.List; + +/** + * Extra info for how to treat OFMatch as a JavaBean + * + * For some (inane!) reason, using chained setters in OFMatch breaks a lot of the JavaBean defaults. + * + * We don't really use OFMatch as a java bean, but there are a lot of nice XML utils that work for + * free if OFMatch follows the java bean paradigm. + * + * @author Rob Sherwood (rob.sherwood@stanford.edu) + * + */ + +public class OFMatchBeanInfo extends SimpleBeanInfo { + + @Override + public PropertyDescriptor[] getPropertyDescriptors() { + List descs = new LinkedList(); + Field[] fields = OFMatch.class.getDeclaredFields(); + String name; + for (int i=0; i< fields.length; i++) { + int mod = fields[i].getModifiers(); + if(Modifier.isFinal(mod) || // don't expose static or final fields + Modifier.isStatic(mod)) + continue; + + name = fields[i].getName(); + Class type = fields[i].getType(); + + try { + descs.add(new PropertyDescriptor(name, + name2getter(OFMatch.class, name), + name2setter(OFMatch.class, name, type))); + } catch (IntrospectionException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + return descs.toArray(new PropertyDescriptor[0]); + } + + + private Method name2setter(Class c, String name, Class type) { + String mName = "set" + toLeadingCaps(name); + Method m = null; + try { + m = c.getMethod(mName, new Class[]{ type}); + } catch (SecurityException e) { + + e.printStackTrace(); + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + return m; + } + + private Method name2getter(Class c, String name) { + String mName= "get" + toLeadingCaps(name); + Method m = null; + try { + m = c.getMethod(mName, new Class[]{}); + } catch (SecurityException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + return m; + } + + private String toLeadingCaps(String s) { + char[] array = s.toCharArray(); + array[0] = Character.toUpperCase(array[0]); + return String.valueOf(array, 0, array.length); + } +}