Coverage Report - org.apache.tapestry.annotations.InjectAssetAnnotationWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectAssetAnnotationWorker
0%
0/21
0%
0/6
1.667
 
 1  
 // Copyright 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.annotations;
 16  
 
 17  
 import java.lang.reflect.Method;
 18  
 
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.hivemind.ClassResolver;
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.hivemind.Resource;
 23  
 import org.apache.hivemind.util.ClasspathResource;
 24  
 import org.apache.tapestry.enhance.EnhancementOperation;
 25  
 import org.apache.tapestry.enhance.EnhancementWorker;
 26  
 import org.apache.tapestry.enhance.InjectAssetWorker;
 27  
 import org.apache.tapestry.spec.IAssetSpecification;
 28  
 import org.apache.tapestry.spec.IComponentSpecification;
 29  
 
 30  
 /**
 31  
  * Injects an asset.
 32  
  * 
 33  
  * @author Howard M. Lewis Ship
 34  
  * @since 4.0
 35  
  * @see org.apache.tapestry.annotations.InjectAsset
 36  
  * @see org.apache.tapestry.enhance.InjectAssetWorker
 37  
  */
 38  
 public class InjectAssetAnnotationWorker implements EnhancementWorker
 39  
 {
 40  
     InjectAssetWorker _delegate;
 41  
     
 42  
     private ClassResolver _classResolver;
 43  
     
 44  
     InjectAssetAnnotationWorker(InjectAssetWorker delegate)
 45  0
     {
 46  0
         _delegate = delegate;
 47  0
     }
 48  
 
 49  
     public InjectAssetAnnotationWorker()
 50  
     {
 51  0
         this(new InjectAssetWorker());
 52  0
     }
 53  
     
 54  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
 55  
     {
 56  0
         Class clazz = op.getBaseClass();
 57  
         
 58  0
         Resource classResource = newClassResource(clazz);
 59  
         
 60  0
         for (Method m : clazz.getMethods())
 61  
         {
 62  0
             if (m.getAnnotation(InjectAsset.class) != null) {
 63  
                 
 64  0
                 performEnhancement(op, spec, m, 
 65  
                         AnnotationUtils.buildLocationForAnnotation(
 66  
                         m,
 67  
                         m.getAnnotation(InjectAsset.class),
 68  
                         classResource));
 69  
             }
 70  
         }
 71  0
     }
 72  
     
 73  
     private ClasspathResource newClassResource(Class clazz)
 74  
     {
 75  0
         return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/'));
 76  
     }
 77  
     
 78  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, Method method, Location location)
 79  
     {
 80  0
         InjectAsset as = method.getAnnotation(InjectAsset.class);
 81  
         
 82  0
         IAssetSpecification asset = spec.getAsset(as.value());
 83  0
         if (asset == null) {
 84  
             
 85  0
             throw new ApplicationRuntimeException(AnnotationMessages.unknownAsset(as.value(), location));
 86  
         }
 87  
         
 88  0
         String propertyName = AnnotationUtils.getPropertyName(method);
 89  
         
 90  0
         _delegate.injectAsset(op, as.value(), propertyName, location);
 91  0
     }
 92  
 
 93  
     public void setClassResolver(ClassResolver classResolver)
 94  
     {
 95  0
         _classResolver = classResolver;
 96  0
     }
 97  
 }