Coverage Report - org.ini4j.AbstractBeanInvocationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractBeanInvocationHandler
100%
98/98
97%
66/68
0
 
 1  
 /**
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.ini4j;
 17  
 
 18  
 import java.beans.Introspector;
 19  
 import java.beans.PropertyChangeListener;
 20  
 import java.beans.PropertyChangeSupport;
 21  
 import java.beans.PropertyVetoException;
 22  
 import java.beans.VetoableChangeListener;
 23  
 import java.beans.VetoableChangeSupport;
 24  
 
 25  
 import java.lang.reflect.Array;
 26  
 import java.lang.reflect.InvocationHandler;
 27  
 import java.lang.reflect.Method;
 28  
 
 29  
 abstract class AbstractBeanInvocationHandler implements InvocationHandler
 30  
 {
 31  
     private static final String ADD_PREFIX = "add";
 32  18
     private static final int ADD_PREFIX_LEN = ADD_PREFIX.length();
 33  
     private static final String REMOVE_PREFIX = "remove";
 34  18
     private static final int REMOVE_PREFIX_LEN = REMOVE_PREFIX.length();
 35  
     private static final String PROPERTY_CHANGE_LISTENER = "PropertyChangeListener";
 36  
     private static final String VETOABLE_CHANGE_LISTENER = "VetoableChangeListener";
 37  
     private static final String READ_PREFIX = "get";
 38  
     private static final String READ_BOOLEAN_PREFIX = "is";
 39  
     private static final String WRITE_PREFIX = "set";
 40  
     private static final String HAS_PREFIX = "has";
 41  18
     private static final int READ_PREFIX_LEN = READ_PREFIX.length();
 42  18
     private static final int READ_BOOLEAN_PREFIX_LEN = READ_BOOLEAN_PREFIX.length();
 43  18
     private static final int WRITE_PREFIX_LEN = WRITE_PREFIX.length();
 44  18
     private static final int HAS_PREFIX_LEN = HAS_PREFIX.length();
 45  
     private PropertyChangeSupport _pcSupport;
 46  
     private Object _proxy;
 47  
     private VetoableChangeSupport _vcSupport;
 48  
 
 49  
     @SuppressWarnings("empty-statement")
 50  
     protected AbstractBeanInvocationHandler()
 51  318
     {
 52  
         ;
 53  318
     }
 54  
 
 55  
     @Override public Object invoke(Object proxy, Method method, Object[] args) throws PropertyVetoException
 56  
     {
 57  2781
         Object ret = null;
 58  2781
         String name = method.getName();
 59  
         String property;
 60  
 
 61  2781
         synchronized (this)
 62  
         {
 63  2781
             if (_proxy == null)
 64  
             {
 65  294
                 _proxy = proxy;
 66  
             }
 67  2781
         }
 68  
 
 69  2781
         if (name.startsWith(READ_PREFIX))
 70  
         {
 71  1656
             property = Introspector.decapitalize(name.substring(READ_PREFIX_LEN));
 72  1656
             ret = getProperty(property, method.getReturnType());
 73  
         }
 74  1125
         else if (name.startsWith(READ_BOOLEAN_PREFIX))
 75  
         {
 76  3
             property = Introspector.decapitalize(name.substring(READ_BOOLEAN_PREFIX_LEN));
 77  3
             ret = getProperty(property, method.getReturnType());
 78  
         }
 79  1122
         else if (name.startsWith(WRITE_PREFIX))
 80  
         {
 81  48
             property = Introspector.decapitalize(name.substring(WRITE_PREFIX_LEN));
 82  48
             setProperty(property, args[0], method.getParameterTypes()[0]);
 83  
         }
 84  1074
         else if (name.startsWith(ADD_PREFIX))
 85  
         {
 86  21
             String listener = name.substring(ADD_PREFIX_LEN);
 87  
 
 88  21
             if (listener.equals(PROPERTY_CHANGE_LISTENER))
 89  
             {
 90  9
                 addPropertyChangeListener((String) args[0], (PropertyChangeListener) args[1]);
 91  
             }
 92  12
             else if (listener.equals(VETOABLE_CHANGE_LISTENER))
 93  
             {
 94  9
                 addVetoableChangeListener((String) args[0], (VetoableChangeListener) args[1]);
 95  
             }
 96  21
         }
 97  1053
         else if (name.startsWith(REMOVE_PREFIX))
 98  
         {
 99  33
             String listener = name.substring(REMOVE_PREFIX_LEN);
 100  
 
 101  33
             if (listener.equals(PROPERTY_CHANGE_LISTENER))
 102  
             {
 103  15
                 removePropertyChangeListener((String) args[0], (PropertyChangeListener) args[1]);
 104  
             }
 105  18
             else if (listener.equals(VETOABLE_CHANGE_LISTENER))
 106  
             {
 107  15
                 removeVetoableChangeListener((String) args[0], (VetoableChangeListener) args[1]);
 108  
             }
 109  33
         }
 110  1020
         else if (name.startsWith(HAS_PREFIX))
 111  
         {
 112  1017
             property = Introspector.decapitalize(name.substring(HAS_PREFIX_LEN));
 113  1017
             ret = Boolean.valueOf(hasProperty(property));
 114  
         }
 115  
 
 116  2778
         return ret;
 117  
     }
 118  
 
 119  
     protected abstract Object getPropertySpi(String property, Class<?> clazz);
 120  
 
 121  
     protected abstract void setPropertySpi(String property, Object value, Class<?> clazz);
 122  
 
 123  
     protected abstract boolean hasPropertySpi(String property);
 124  
 
 125  
     protected synchronized Object getProperty(String property, Class<?> clazz)
 126  
     {
 127  
         Object o;
 128  
 
 129  
         try
 130  
         {
 131  1683
             o = getPropertySpi(property, clazz);
 132  1683
             if (o == null)
 133  
             {
 134  9
                 o = zero(clazz);
 135  
             }
 136  1674
             else if (clazz.isArray() && (o instanceof String[]) && !clazz.equals(String[].class))
 137  
             {
 138  15
                 String[] str = (String[]) o;
 139  
 
 140  15
                 o = Array.newInstance(clazz.getComponentType(), str.length);
 141  45
                 for (int i = 0; i < str.length; i++)
 142  
                 {
 143  30
                     Array.set(o, i, parse(str[i], clazz.getComponentType()));
 144  
                 }
 145  15
             }
 146  1659
             else if ((o instanceof String) && !clazz.equals(String.class))
 147  
             {
 148  1230
                 o = parse((String) o, clazz);
 149  
             }
 150  
         }
 151  3
         catch (Exception x)
 152  
         {
 153  3
             o = zero(clazz);
 154  1680
         }
 155  
 
 156  1683
         return o;
 157  
     }
 158  
 
 159  
     @SuppressWarnings("empty-statement")
 160  
     protected synchronized void setProperty(String property, Object value, Class<?> clazz) throws PropertyVetoException
 161  
     {
 162  
         try
 163  
         {
 164  51
             boolean pc = (_pcSupport != null) && _pcSupport.hasListeners(property);
 165  51
             boolean vc = (_vcSupport != null) && _vcSupport.hasListeners(property);
 166  51
             Object old = (pc || vc) ? getProperty(property, clazz) : null;
 167  
 
 168  51
             if (vc)
 169  
             {
 170  6
                 fireVetoableChange(property, old, value);
 171  
             }
 172  
 
 173  48
             if (clazz.equals(String.class) && !(value instanceof String))
 174  
             {
 175  3
                 value = value.toString();
 176  
             }
 177  
 
 178  48
             setPropertySpi(property, value, clazz);
 179  48
             if (pc)
 180  
             {
 181  6
                 firePropertyChange(property, old, value);
 182  
             }
 183  
         }
 184  3
         catch (PropertyVetoException x)
 185  
         {
 186  3
             throw x;
 187  48
         }
 188  48
     }
 189  
 
 190  
     protected synchronized Object getProxy()
 191  
     {
 192  6
         return _proxy;
 193  
     }
 194  
 
 195  
     protected synchronized void addPropertyChangeListener(String property, PropertyChangeListener listener)
 196  
     {
 197  9
         if (_pcSupport == null)
 198  
         {
 199  3
             _pcSupport = new PropertyChangeSupport(_proxy);
 200  
         }
 201  
 
 202  9
         _pcSupport.addPropertyChangeListener(property, listener);
 203  9
     }
 204  
 
 205  
     protected synchronized void addVetoableChangeListener(String property, VetoableChangeListener listener)
 206  
     {
 207  9
         if (_vcSupport == null)
 208  
         {
 209  3
             _vcSupport = new VetoableChangeSupport(_proxy);
 210  
         }
 211  
 
 212  9
         _vcSupport.addVetoableChangeListener(property, listener);
 213  9
     }
 214  
 
 215  
     protected synchronized void firePropertyChange(String property, Object oldValue, Object newValue)
 216  
     {
 217  9
         if (_pcSupport != null)
 218  
         {
 219  6
             _pcSupport.firePropertyChange(property, oldValue, newValue);
 220  
         }
 221  9
     }
 222  
 
 223  
     protected synchronized void fireVetoableChange(String property, Object oldValue, Object newValue) throws PropertyVetoException
 224  
     {
 225  9
         if (_vcSupport != null)
 226  
         {
 227  6
             _vcSupport.fireVetoableChange(property, oldValue, newValue);
 228  
         }
 229  6
     }
 230  
 
 231  
     protected synchronized boolean hasProperty(String property)
 232  
     {
 233  
         boolean ret;
 234  
 
 235  
         try
 236  
         {
 237  1026
             ret = hasPropertySpi(property);
 238  
         }
 239  3
         catch (Exception x)
 240  
         {
 241  3
             ret = false;
 242  1023
         }
 243  
 
 244  1026
         return ret;
 245  
     }
 246  
 
 247  
     protected Object parse(String value, Class clazz) throws IllegalArgumentException
 248  
     {
 249  1260
         return BeanTool.getInstance().parse(value, clazz);
 250  
     }
 251  
 
 252  
     protected synchronized void removePropertyChangeListener(String property, PropertyChangeListener listener)
 253  
     {
 254  15
         if (_pcSupport != null)
 255  
         {
 256  12
             _pcSupport.removePropertyChangeListener(property, listener);
 257  
         }
 258  15
     }
 259  
 
 260  
     protected synchronized void removeVetoableChangeListener(String property, VetoableChangeListener listener)
 261  
     {
 262  15
         if (_vcSupport != null)
 263  
         {
 264  12
             _vcSupport.removeVetoableChangeListener(property, listener);
 265  
         }
 266  15
     }
 267  
 
 268  
     protected Object zero(Class clazz)
 269  
     {
 270  12
         return BeanTool.getInstance().zero(clazz);
 271  
     }
 272  
 }