Coverage Report - org.apache.commons.clazz.reflect.common.ReflectedAccessorPairProperty
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectedAccessorPairProperty
0%
0/41
0%
0/6
1.5
 
 1  
 /*
 2  
  * Copyright 2002-2004 The Apache Software Foundation
 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.apache.commons.clazz.reflect.common;
 17  
 
 18  
 import java.lang.reflect.Field;
 19  
 import java.lang.reflect.Method;
 20  
 
 21  
 import org.apache.commons.clazz.Clazz;
 22  
 import org.apache.commons.clazz.ClazzAccessException;
 23  
 import org.apache.commons.clazz.common.ClazzFeatureSupport;
 24  
 import org.apache.commons.clazz.reflect.ReflectedProperty;
 25  
 
 26  
 /**
 27  
  * 
 28  
  * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
 29  
  * @version $Id: ReflectedAccessorPairProperty.java 155436 2005-02-26 13:17:48Z dirkv $
 30  
  */
 31  
 public class ReflectedAccessorPairProperty extends ClazzFeatureSupport 
 32  
         implements ReflectedProperty 
 33  
 {
 34  
     private String name;
 35  0
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
 36  0
     private String[] aliases = EMPTY_STRING_ARRAY;
 37  
     private Class type;
 38  
     private Clazz clazz;
 39  
     private Field field;
 40  
     private Method readMethod;
 41  
     private Method writeMethod;
 42  
     
 43  
     /**
 44  
      * Constructor for ReflectedClazzProperty.
 45  
      */
 46  
     public ReflectedAccessorPairProperty(Clazz declaringClazz, String name) {
 47  0
         super(declaringClazz);
 48  0
         this.name = name;
 49  0
     }
 50  
 
 51  
     /**
 52  
      * @see org.apache.commons.clazz.ClazzProperty#getName()
 53  
      */
 54  
     public String getName() {
 55  0
         return name;
 56  
     }
 57  
     
 58  
     /**
 59  
      * Returns the aliases.
 60  
      * @return String[]
 61  
      */
 62  
     public String[] getAliases() {
 63  0
         return aliases;
 64  
     }
 65  
 
 66  
     /**
 67  
      * Sets the aliases.
 68  
      * @param aliases The aliases to set
 69  
      */
 70  
     public void setAliases(String[] aliases) {
 71  0
         this.aliases = aliases;
 72  0
     }
 73  
     
 74  
     /**
 75  
      * @see org.apache.commons.clazz.ClazzProperty#getClazz()
 76  
      */
 77  
     public Clazz getClazz() {
 78  0
         if (clazz == null) {
 79  0
             clazz = getDeclaringClazz().
 80  
                 getClazzLoader().getClazzForName(type.getName());
 81  
         }
 82  0
         return clazz;
 83  
     }
 84  
     
 85  
     /**
 86  
      * @see org.apache.commons.clazz.ClazzProperty#getContentClazz()
 87  
      */
 88  
     public Clazz getContentClazz() {
 89  0
         return null;
 90  
     }
 91  
 
 92  
     /**
 93  
      * @see org.apache.commons.clazz.ClazzProperty#getKeyClazz()
 94  
      */
 95  
     public Clazz getKeyClazz() {
 96  0
         return null;
 97  
     }
 98  
 
 99  
     /**
 100  
      * @see org.apache.commons.clazz.ClazzProperty#isReadOnly()
 101  
      */
 102  
     public boolean isReadOnly() {
 103  0
         return false;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Returns the field.
 108  
      * @return Field
 109  
      */
 110  
     public Field getField() {
 111  0
         return field;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Returns the readMethod.
 116  
      * @return Method
 117  
      */
 118  
     public Method getReadMethod() {
 119  0
         return readMethod;
 120  
     }
 121  
 
 122  
 
 123  
     /**
 124  
      * Sets the field.
 125  
      * @param field The field to set
 126  
      */
 127  
     public void setField(Field field) {
 128  0
         this.field = field;
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Sets the readMethod.
 133  
      * @param readMethod The readMethod to set
 134  
      */
 135  
     public void setReadMethod(Method readMethod) {
 136  0
         this.readMethod = readMethod;
 137  0
     }
 138  
     
 139  
     /**
 140  
      * Returns the writeMethod.
 141  
      * @return Method
 142  
      */
 143  
     public Method getWriteMethod() {
 144  0
         return writeMethod;
 145  
     }
 146  
     
 147  
     /**
 148  
      * Sets the writeMethod.
 149  
      * @param writeMethod The writeMethod to set
 150  
      */
 151  
     public void setWriteMethod(Method writeMethod) {
 152  0
         this.writeMethod = writeMethod;
 153  0
     }
 154  
     
 155  
     /**
 156  
      * @see org.apache.commons.clazz.ClazzProperty#get(java.lang.Object)
 157  
      */
 158  
     public Object get(Object instance) {
 159  0
         if (readMethod == null) {
 160  0
             throw new ClazzAccessException(
 161  
                 "Cannot read property " + name + ": no read method");
 162  
         }
 163  
         try {
 164  0
             return readMethod.invoke(instance, null);
 165  
         }
 166  0
         catch (Exception ex) {
 167  0
             throw new ClazzAccessException(
 168  
                 "Cannot get property : "
 169  
                     + name
 170  
                     + ": cannot invoke method: "
 171  
                     + readMethod.getName(),
 172  
                 ex);
 173  
         }
 174  
     }
 175  
 
 176  
     /**
 177  
      * @see org.apache.commons.clazz.ClazzProperty#set(Object, Object)
 178  
      */
 179  
     public void set(Object instance, Object value) {
 180  0
         if (writeMethod == null) {
 181  0
             throw new ClazzAccessException(
 182  
                 "Cannot modify property " + name + ": no write method");
 183  
         }
 184  
         try {
 185  0
             writeMethod.invoke(instance, new Object[]{value});
 186  
         }
 187  0
         catch (Exception ex) {
 188  0
             throw new ClazzAccessException(
 189  
                 "Cannot set property : "
 190  
                     + name
 191  
                     + ": cannot invoke method: "
 192  
                     + writeMethod.getName(),
 193  
                 ex);
 194  0
         }
 195  0
     }
 196  
 
 197  
 
 198  
     /**
 199  
      * Returns the type.
 200  
      * @return Class
 201  
      */
 202  
     public Class getType() {
 203  0
         return type;
 204  
     }
 205  
 
 206  
     /**
 207  
      * Sets the type.
 208  
      * @param type The type to set
 209  
      */
 210  
     public void setType(Class type) {
 211  0
         this.type = type;
 212  0
     }
 213  
     
 214  
     /**
 215  
      * @see org.apache.commons.clazz.ClazzProperty#isCollection()
 216  
      */
 217  
     public boolean isCollection() {
 218  0
         return false;
 219  
     }
 220  
 
 221  
     /**
 222  
      * @see org.apache.commons.clazz.ClazzProperty#isMap()
 223  
      */
 224  
     public boolean isMap() {
 225  0
         return false;
 226  
     }
 227  
 
 228  
 }