Coverage Report - net.sf.json.util.PropertySetStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertySetStrategy
0%
0/5
N/A
2.2
PropertySetStrategy$1
N/A
N/A
2.2
PropertySetStrategy$DefaultPropertySetStrategy
0%
0/18
0%
0/6
2.2
 
 1  
 /*
 2  
  * Copyright 2002-2009 the original author or authors.
 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  
 
 17  
 package net.sf.json.util;
 18  
 
 19  
 import java.lang.reflect.Field;
 20  
 import java.util.Map;
 21  
 
 22  
 import net.sf.json.JSONException;
 23  
 import net.sf.json.JsonConfig;
 24  
 
 25  
 import org.apache.commons.beanutils.PropertyUtils;
 26  
 
 27  
 /**
 28  
  * Defines a custom setter to be used when setting object values.<br>
 29  
  * Specify with JsonConfig.setJsonPropertySetter().
 30  
  *
 31  
  * @author Gino Miceli <ginomiceli@users.sourceforge.net>
 32  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 33  
  */
 34  0
 public abstract class PropertySetStrategy {
 35  0
    public static final PropertySetStrategy DEFAULT = new DefaultPropertySetStrategy();
 36  
 
 37  
    public abstract void setProperty( Object bean, String key, Object value ) throws JSONException;
 38  
    
 39  
    public void setProperty( Object bean, String key, Object value, JsonConfig jsonConfig ) throws JSONException {
 40  0
       setProperty( bean, key, value );
 41  0
    }
 42  
 
 43  0
    private static final class DefaultPropertySetStrategy extends PropertySetStrategy {
 44  
       public void setProperty( Object bean, String key, Object value ) throws JSONException {
 45  0
          setProperty( bean, key, value, new JsonConfig());
 46  0
       }
 47  
       
 48  
       public void setProperty( Object bean, String key, Object value, JsonConfig jsonConfig ) throws JSONException {
 49  0
          if( bean instanceof Map ){
 50  0
             ((Map) bean).put( key, value );
 51  
          } else {
 52  0
             if( !jsonConfig.isIgnorePublicFields() ) {
 53  
                try {
 54  0
                   Field field = bean.getClass().getField( key );
 55  0
                   if( field != null ) field.set( bean, value );
 56  0
                } catch( Exception e ){
 57  0
                   _setProperty( bean, key, value );
 58  0
                }
 59  
             } else {
 60  0
                _setProperty( bean, key, value );
 61  
             }
 62  
          }
 63  0
       }
 64  
       
 65  
       private void _setProperty( Object bean, String key, Object value ) {
 66  
          try {
 67  0
             PropertyUtils.setSimpleProperty( bean, key, value );
 68  0
          } catch( Exception e ) {
 69  0
             throw new JSONException( e );
 70  0
          }
 71  0
       }
 72  
       
 73  
    }
 74  
 }