Coverage Report - org.apache.tapestry.enhance.InjectObjectWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectObjectWorker
0%
0/24
0%
0/8
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  0
 public class InjectObjectWorker implements InjectEnhancementWorker
 34  
 {
 35  
 
 36  
     private InjectedValueProvider _provider;
 37  
 
 38  
     public void performEnhancement(EnhancementOperation op, InjectSpecification is)
 39  
     {
 40  0
         String name = is.getProperty();
 41  0
         String objectReference = is.getObject();
 42  0
         Location location = is.getLocation();
 43  
 
 44  0
         injectObject(op, objectReference, name, location);
 45  0
     }
 46  
 
 47  
     public void injectObject(EnhancementOperation op, String objectReference,
 48  
                              String propertyName, Location location)
 49  
     {
 50  0
         Defense.notNull(op, "op");
 51  0
         Defense.notNull(propertyName, "propertyName");
 52  0
         Defense.notNull(objectReference, "objectReference");
 53  
 
 54  0
         Class propertyType = op.getPropertyType(propertyName);
 55  0
         if (propertyType == null)
 56  0
             propertyType = Object.class;
 57  
 
 58  0
         op.claimReadonlyProperty(propertyName);
 59  
 
 60  0
         Object injectedValue = _provider.obtainValue(objectReference, location);
 61  
 
 62  0
         if (injectedValue == null)
 63  0
             throw new ApplicationRuntimeException(EnhanceMessages
 64  
               .locatedValueIsNull(objectReference), location, null);
 65  
 
 66  0
         if (!propertyType.isInstance(injectedValue))
 67  0
             throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(objectReference, injectedValue, propertyType),
 68  
                                                   location, null);
 69  
 
 70  0
         String fieldName = op.addInjectedField("_$" + propertyName,
 71  
                                                propertyType, injectedValue);
 72  
 
 73  0
         String methodName = EnhanceUtils.createAccessorMethodName(propertyName);
 74  
 
 75  0
         op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType,
 76  
                                                           methodName, null, null), "return " + fieldName + ";", location);
 77  0
     }
 78  
 
 79  
     public void setProvider(InjectedValueProvider provider)
 80  
     {
 81  0
         _provider = provider;
 82  0
     }
 83  
 }