Coverage Report - org.apache.tapestry.annotations.ComponentPropertyProxyWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentPropertyProxyWorker
0%
0/69
0%
0/48
7.333
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 package org.apache.tapestry.annotations;
 15  
 
 16  
 import java.beans.BeanInfo;
 17  
 import java.beans.Introspector;
 18  
 import java.beans.PropertyDescriptor;
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.Method;
 21  
 import java.lang.reflect.ParameterizedType;
 22  
 import java.lang.reflect.Type;
 23  
 import java.lang.reflect.TypeVariable;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.hivemind.ApplicationRuntimeException;
 27  
 import org.apache.tapestry.enhance.EnhanceUtils;
 28  
 import org.apache.tapestry.enhance.EnhancementOperation;
 29  
 import org.apache.tapestry.enhance.EnhancementWorker;
 30  
 import org.apache.tapestry.spec.IComponentSpecification;
 31  
 import org.apache.tapestry.spec.IPropertySpecification;
 32  
 
 33  
 
 34  
 /**
 35  
  * Performs runtime checks on persistent properties to ensure that objects being
 36  
  * managed by competing bytecode enhancement libraries (such as Hibernate) aren't
 37  
  * proxied.
 38  
  */
 39  0
 public class ComponentPropertyProxyWorker implements EnhancementWorker {
 40  
 
 41  
     private List<String> _excludedPackages;
 42  
 
 43  
     /**
 44  
      * {@inheritDoc}
 45  
      */
 46  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) {
 47  0
         for (Object o : spec.getPropertySpecificationNames()) {
 48  0
             String name = (String) o;
 49  0
             IPropertySpecification ps = spec.getPropertySpecification(name);
 50  
 
 51  0
             checkProxy(op, ps);
 52  0
         }
 53  0
     }
 54  
     
 55  
     public Class extractPropertyType(Class type, String propertyName, IPropertySpecification ps) {
 56  
         
 57  
         try {
 58  0
             BeanInfo info = Introspector.getBeanInfo(type);
 59  0
             PropertyDescriptor[] props = info.getPropertyDescriptors();
 60  
 
 61  0
             for (PropertyDescriptor prop : props) {
 62  
 
 63  0
                 if (!propertyName.equals(prop.getName())) {
 64  0
                     continue;
 65  
                 }
 66  
                 
 67  0
                 Method m = prop.getReadMethod();
 68  0
                 if (m != null && !m.getGenericReturnType().getClass().getName().equals("java.lang.Class")
 69  
                         && TypeVariable.class.isAssignableFrom(m.getGenericReturnType().getClass())) {
 70  
                     
 71  0
                     ps.setGeneric(true);
 72  0
                     TypeVariable tvar = (TypeVariable)m.getGenericReturnType();
 73  
                     
 74  
                     // try to set the actual type
 75  0
                     if (type.getGenericSuperclass() != null 
 76  
                             && ParameterizedType.class.isInstance(type.getGenericSuperclass())) {
 77  
                         
 78  0
                         ParameterizedType ptype = (ParameterizedType)type.getGenericSuperclass();
 79  0
                         if (ptype.getActualTypeArguments().length > 0) {
 80  
                             
 81  0
                             ps.setCanProxy(canProxyType((Class)ptype.getActualTypeArguments()[0]));
 82  0
                             ps.setType(((Class)tvar.getBounds()[0]).getName());
 83  
                             
 84  0
                             return (Class)tvar.getBounds()[0];
 85  
                         }
 86  
                     }
 87  
                     
 88  0
                     return null;
 89  0
                 } else if (m != null) {
 90  
                     
 91  0
                     ps.setCanProxy(canProxyType(m.getReturnType()));
 92  0
                     ps.setType(m.getReturnType().getName());
 93  
                     
 94  0
                     return m.getReturnType();
 95  
                 }
 96  
                 
 97  
                 // try write method instead
 98  
                 
 99  0
                 if (m == null && prop.getWriteMethod() == null)
 100  0
                     return null;
 101  
                 
 102  0
                 m = prop.getWriteMethod();
 103  0
                 if (m.getParameterTypes().length != 1)
 104  0
                     return null;
 105  
                 
 106  0
                 Type genParam = m.getGenericParameterTypes()[0];
 107  0
                 Class param = m.getParameterTypes()[0];
 108  
                 
 109  0
                 if (!genParam.getClass().getName().equals("java.lang.Class")
 110  
                         && TypeVariable.class.isAssignableFrom(genParam.getClass())) {
 111  
                     
 112  0
                     TypeVariable tvar = (TypeVariable)genParam;
 113  0
                     ps.setGeneric(true);
 114  
                     
 115  0
                     if (type.getGenericSuperclass() != null) {
 116  
                         
 117  0
                         ParameterizedType ptype = (ParameterizedType)type.getGenericSuperclass();
 118  0
                         if (ptype.getActualTypeArguments().length > 0) {
 119  
                             
 120  0
                             ps.setCanProxy(canProxyType((Class)ptype.getActualTypeArguments()[0]));
 121  0
                             ps.setType(((Class)tvar.getBounds()[0]).getName());
 122  
                             
 123  0
                             return (Class)tvar.getBounds()[0];
 124  
                         }
 125  
                     }
 126  
                 }
 127  
                 
 128  0
                 ps.setCanProxy(canProxyType(param));
 129  0
                 ps.setType(param.getName());
 130  0
                 return param;
 131  
             }
 132  
 
 133  0
         } catch (Throwable t) {
 134  
             
 135  0
             throw new ApplicationRuntimeException("Error reading property " + propertyName + " from base component class : " + type, t);
 136  0
         }
 137  
 
 138  0
         return null;
 139  
     }
 140  
 
 141  
     boolean canProxyType(Class type)
 142  
     {
 143  0
         if (type == null)
 144  0
             return false;
 145  
         
 146  0
         if (!EnhanceUtils.canProxyPropertyType(type))
 147  0
             return false;
 148  
         
 149  0
         for (Annotation an : type.getAnnotations()) {
 150  0
             if (isExcluded(an)) {
 151  0
                 return false;
 152  
             }
 153  
         }
 154  
         
 155  0
         return true;
 156  
     }
 157  
     
 158  
     void checkProxy(EnhancementOperation op, IPropertySpecification ps) {
 159  0
         ps.setProxyChecked(true);
 160  
 
 161  0
         if (!ps.isPersistent()) {
 162  0
             return;
 163  
         }
 164  
         
 165  0
         extractPropertyType(op.getBaseClass(), ps.getName(), ps);
 166  0
     }
 167  
     
 168  
     boolean isExcluded(Annotation annotation) {
 169  0
         for (String match : _excludedPackages) {
 170  
 
 171  0
             if (annotation.annotationType().getName().indexOf(match) > -1) {
 172  0
                 return true;
 173  
             }
 174  
         }
 175  
 
 176  0
         return false;
 177  
     }
 178  
 
 179  
     public void setExcludedPackages(List<String> packages) {
 180  0
         _excludedPackages = packages;
 181  0
     }
 182  
 }
 183