Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior |
| 3 | * University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package org.openflow.protocol; |
| 19 | |
| 20 | import java.beans.IntrospectionException; |
| 21 | import java.beans.PropertyDescriptor; |
| 22 | import java.beans.SimpleBeanInfo; |
| 23 | import java.lang.reflect.Field; |
| 24 | import java.lang.reflect.Method; |
| 25 | import java.lang.reflect.Modifier; |
| 26 | import java.util.LinkedList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Extra info for how to treat OFMatch as a JavaBean |
| 31 | * |
| 32 | * For some (inane!) reason, using chained setters in OFMatch breaks a lot of the JavaBean defaults. |
| 33 | * |
| 34 | * We don't really use OFMatch as a java bean, but there are a lot of nice XML utils that work for |
| 35 | * free if OFMatch follows the java bean paradigm. |
| 36 | * |
| 37 | * @author Rob Sherwood (rob.sherwood@stanford.edu) |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | public class OFMatchBeanInfo extends SimpleBeanInfo { |
| 42 | |
| 43 | @Override |
| 44 | public PropertyDescriptor[] getPropertyDescriptors() { |
| 45 | List<PropertyDescriptor> descs = new LinkedList<PropertyDescriptor>(); |
| 46 | Field[] fields = OFMatch.class.getDeclaredFields(); |
| 47 | String name; |
| 48 | for (int i=0; i< fields.length; i++) { |
| 49 | int mod = fields[i].getModifiers(); |
| 50 | if(Modifier.isFinal(mod) || // don't expose static or final fields |
| 51 | Modifier.isStatic(mod)) |
| 52 | continue; |
| 53 | |
| 54 | name = fields[i].getName(); |
| 55 | Class<?> type = fields[i].getType(); |
| 56 | |
| 57 | try { |
| 58 | descs.add(new PropertyDescriptor(name, |
| 59 | name2getter(OFMatch.class, name), |
| 60 | name2setter(OFMatch.class, name, type))); |
| 61 | } catch (IntrospectionException e) { |
| 62 | e.printStackTrace(); |
| 63 | throw new RuntimeException(e); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return descs.toArray(new PropertyDescriptor[0]); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | private Method name2setter(Class<OFMatch> c, String name, Class<?> type) { |
| 72 | String mName = "set" + toLeadingCaps(name); |
| 73 | Method m = null; |
| 74 | try { |
| 75 | m = c.getMethod(mName, new Class[]{ type}); |
| 76 | } catch (SecurityException e) { |
| 77 | e.printStackTrace(); |
| 78 | throw new RuntimeException(e); |
| 79 | } catch (NoSuchMethodException e) { |
| 80 | e.printStackTrace(); |
| 81 | throw new RuntimeException(e); |
| 82 | } |
| 83 | return m; |
| 84 | } |
| 85 | |
| 86 | private Method name2getter(Class<OFMatch> c, String name) { |
| 87 | String mName= "get" + toLeadingCaps(name); |
| 88 | Method m = null; |
| 89 | try { |
| 90 | m = c.getMethod(mName, new Class[]{}); |
| 91 | } catch (SecurityException e) { |
| 92 | e.printStackTrace(); |
| 93 | throw new RuntimeException(e); |
| 94 | } catch (NoSuchMethodException e) { |
| 95 | e.printStackTrace(); |
| 96 | throw new RuntimeException(e); |
| 97 | } |
| 98 | return m; |
| 99 | } |
| 100 | |
| 101 | private String toLeadingCaps(String s) { |
| 102 | char[] array = s.toCharArray(); |
| 103 | array[0] = Character.toUpperCase(array[0]); |
| 104 | return String.valueOf(array, 0, array.length); |
| 105 | } |
| 106 | } |