Coverage Report - org.apache.maven.shared.tools.test.ReflectiveSetter
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectiveSetter
0%
0/19
0%
0/4
1.5
ReflectiveSetter$FieldSetter
0%
0/11
N/A
1.5
ReflectiveSetter$MethodSetter
0%
0/11
N/A
1.5
ReflectiveSetter$Setter
N/A
N/A
1.5
 
 1  
 package org.apache.maven.shared.tools.test;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.lang.reflect.Field;
 23  
 import java.lang.reflect.InvocationTargetException;
 24  
 import java.lang.reflect.Method;
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.codehaus.plexus.util.ReflectionUtils;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  
 /**
 32  
  * @version $Id: ReflectiveSetter.java 677117 2008-07-16 00:29:56Z vsiveton $
 33  
  */
 34  
 public class ReflectiveSetter
 35  
 {
 36  0
     private Map cachedPropertySetters = new HashMap();
 37  
 
 38  
     private final Class targetClass;
 39  
 
 40  
     /**
 41  
      * @param targetClass
 42  
      */
 43  
     public ReflectiveSetter( Class targetClass )
 44  0
     {
 45  0
         this.targetClass = targetClass;
 46  0
     }
 47  
 
 48  
     /**
 49  
      * @param propertyName
 50  
      * @param value
 51  
      * @param target
 52  
      * @throws Throwable
 53  
      */
 54  
     public void setProperty( String propertyName, Object value, Object target )
 55  
         throws Throwable
 56  
     {
 57  
 
 58  0
         String preferredMethodName = "set" + StringUtils.capitalizeFirstLetter( propertyName );
 59  
 
 60  0
         Setter setter = null;
 61  
 
 62  0
         Method method = ReflectionUtils.getSetter( preferredMethodName, targetClass );
 63  
 
 64  0
         if ( method != null )
 65  
         {
 66  0
             setter = new MethodSetter( propertyName, method );
 67  
         }
 68  
         else
 69  
         {
 70  0
             Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( propertyName, targetClass );
 71  
 
 72  0
             setter = new FieldSetter( propertyName, field );
 73  
         }
 74  
 
 75  0
         if ( setter == null )
 76  
         {
 77  0
             throw new IllegalArgumentException( "No such property: " + propertyName + " in: " + targetClass
 78  
                 + ". Searched for: {method:" + preferredMethodName + ", method:" + propertyName + ", field:"
 79  
                 + propertyName + "}" );
 80  
         }
 81  
 
 82  0
         cachedPropertySetters.put( setter.getProperty(), setter );
 83  
 
 84  
         try
 85  
         {
 86  0
             setter.set( value, target );
 87  
         }
 88  0
         catch ( InvocationTargetException e )
 89  
         {
 90  0
             throw e.getTargetException();
 91  0
         }
 92  0
     }
 93  
 
 94  
     private interface Setter
 95  
     {
 96  
         void set( Object value, Object target )
 97  
             throws IllegalArgumentException, IllegalAccessException, InvocationTargetException;
 98  
 
 99  
         String getProperty();
 100  
     }
 101  
 
 102  
     private static class MethodSetter
 103  
         implements Setter
 104  
     {
 105  
         private Method method;
 106  
 
 107  
         private String name;
 108  
 
 109  
         MethodSetter( String name, Method method )
 110  0
         {
 111  0
             this.name = name;
 112  0
             this.method = method;
 113  0
         }
 114  
 
 115  
         /** {@inheritDoc} */
 116  
         public String getProperty()
 117  
         {
 118  0
             return name;
 119  
         }
 120  
 
 121  
         /** {@inheritDoc} */
 122  
         public void set( Object value, Object target )
 123  
             throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
 124  
         {
 125  0
             boolean wasAccessible = method.isAccessible();
 126  
 
 127  0
             method.setAccessible( true );
 128  
             try
 129  
             {
 130  0
                 method.invoke( target, new Object[] { value } );
 131  
             }
 132  
             finally
 133  
             {
 134  0
                 method.setAccessible( wasAccessible );
 135  0
             }
 136  0
         }
 137  
     }
 138  
 
 139  
     private static class FieldSetter
 140  
         implements Setter
 141  
     {
 142  
         private Field field;
 143  
 
 144  
         private String name;
 145  
 
 146  
         FieldSetter( String name, Field field )
 147  0
         {
 148  0
             this.name = name;
 149  0
             this.field = field;
 150  0
         }
 151  
 
 152  
         /** {@inheritDoc} */
 153  
         public String getProperty()
 154  
         {
 155  0
             return name;
 156  
         }
 157  
 
 158  
         /** {@inheritDoc} */
 159  
         public void set( Object value, Object target )
 160  
             throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
 161  
         {
 162  0
             boolean wasAccessible = field.isAccessible();
 163  
 
 164  0
             field.setAccessible( true );
 165  
             try
 166  
             {
 167  0
                 field.set( target, value );
 168  
             }
 169  
             finally
 170  
             {
 171  0
                 field.setAccessible( wasAccessible );
 172  0
             }
 173  0
         }
 174  
     }
 175  
 }