Coverage Report - org.apache.tapestry.enhance.InjectObjectWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectObjectWorker
100% 
100% 
2.667
 
 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  
 
 15  
 package org.apache.tapestry.enhance;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.Location;
 19  
 import org.apache.hivemind.service.MethodSignature;
 20  
 import org.apache.hivemind.util.Defense;
 21  
 import org.apache.tapestry.services.InjectedValueProvider;
 22  
 import org.apache.tapestry.spec.InjectSpecification;
 23  
 
 24  
 import java.lang.reflect.Modifier;
 25  
 
 26  
 /**
 27  
  * Implementation for injection type "object" (the default). Adds read-only
 28  
  * properties to the enhanced class that contain objects injected from HiveMind.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  5
 public class InjectObjectWorker implements InjectEnhancementWorker
 34  
 {
 35  
 
 36  
     private InjectedValueProvider _provider;
 37  
 
 38  
     public void performEnhancement(EnhancementOperation op,
 39  
             InjectSpecification is)
 40  
     {
 41  5
         String name = is.getProperty();
 42  5
         String objectReference = is.getObject();
 43  5
         Location location = is.getLocation();
 44  
 
 45  5
         injectObject(op, objectReference, name, location);
 46  3
     }
 47  
 
 48  
     public void injectObject(EnhancementOperation op, String objectReference,
 49  
             String propertyName, Location location)
 50  
     {
 51  5
         Defense.notNull(op, "op");
 52  5
         Defense.notNull(propertyName, "propertyName");
 53  5
         Defense.notNull(objectReference, "objectReference");
 54  
 
 55  5
         Class propertyType = op.getPropertyType(propertyName);
 56  5
         if (propertyType == null)
 57  3
             propertyType = Object.class;
 58  
 
 59  5
         op.claimReadonlyProperty(propertyName);
 60  
 
 61  5
         Object injectedValue = _provider.obtainValue(objectReference, location);
 62  
         
 63  5
         if (injectedValue == null)
 64  1
             throw new ApplicationRuntimeException(EnhanceMessages
 65  
                     .locatedValueIsNull(objectReference), location, null);
 66  
         
 67  4
         if (!propertyType.isInstance(injectedValue))
 68  1
             throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(objectReference, injectedValue, propertyType),
 69  
                     location, null);
 70  
         
 71  3
         String fieldName = op.addInjectedField("_$" + propertyName,
 72  
                 propertyType, injectedValue);
 73  
         
 74  3
         String methodName = EnhanceUtils.createAccessorMethodName(propertyName);
 75  
 
 76  3
         op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType,
 77  
                 methodName, null, null), "return " + fieldName + ";", location);
 78  3
     }
 79  
 
 80  
     public void setProvider(InjectedValueProvider provider)
 81  
     {
 82  5
         _provider = provider;
 83  5
     }
 84  
 }