Coverage Report - org.apache.tapestry5.internal.services.AbstractComponentMethodInvocation
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractComponentMethodInvocation
94%
31/33
71%
10/14
0
 
 1  
 // Copyright 2008 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.tapestry5.internal.services;
 16  
 
 17  
 import org.apache.tapestry5.ComponentResources;
 18  
 import org.apache.tapestry5.services.ComponentMethodAdvice;
 19  
 import org.apache.tapestry5.services.ComponentMethodInvocation;
 20  
 
 21  
 public abstract class AbstractComponentMethodInvocation implements ComponentMethodInvocation
 22  
 {
 23  
     private final ComponentMethodInvocationInfo info;
 24  
 
 25  
     private final ComponentResources resources;
 26  
 
 27  200
     private int adviceIndex = 0;
 28  
 
 29  
     private Throwable thrown;
 30  
 
 31  
     private Object result;
 32  
 
 33  
     public AbstractComponentMethodInvocation(ComponentMethodInvocationInfo info, ComponentResources resources)
 34  200
     {
 35  200
         this.info = info;
 36  200
         this.resources = resources;
 37  200
     }
 38  
 
 39  
     public ComponentResources getComponentResources()
 40  
     {
 41  200
         return resources;
 42  
     }
 43  
 
 44  
     public String getMethodName()
 45  
     {
 46  4
         return info.getMethodName();
 47  
     }
 48  
 
 49  
     public Class getResultType()
 50  
     {
 51  14
         return info.getResultType();
 52  
     }
 53  
 
 54  
     public int getParameterCount()
 55  
     {
 56  16
         return info.getParameterCount();
 57  
     }
 58  
 
 59  
     public Class getParameterType(int index)
 60  
     {
 61  2
         return info.getParameterType(index);
 62  
     }
 63  
 
 64  
     /**
 65  
      * This first call is to the first advice.  When we run out of advice, we re-invoke.
 66  
      */
 67  
     public void proceed()
 68  
     {
 69  400
         if (adviceIndex >= info.getAdviceCount())
 70  
         {
 71  200
             invokeAdvisedMethod();
 72  198
             return;
 73  
         }
 74  
 
 75  200
         ComponentMethodAdvice advice = info.getAdvice(adviceIndex++);
 76  
 
 77  
         // When this advice invokes proceed(), we can advance to the next advice,
 78  
         // and then ultimately to the advised method.
 79  
 
 80  200
         advice.advise(this);
 81  198
     }
 82  
 
 83  
     /**
 84  
      * Implemented to reinvoke the method on the advised method of the component.
 85  
      */
 86  
     protected abstract void invokeAdvisedMethod();
 87  
 
 88  
     public boolean isFail()
 89  
     {
 90  8
         return thrown != null;
 91  
     }
 92  
 
 93  
     public <T extends Throwable> T getThrown(Class<T> throwableClass)
 94  
     {
 95  172
         if (throwableClass.isInstance(thrown))
 96  4
             return throwableClass.cast(thrown);
 97  
 
 98  168
         return null;
 99  
     }
 100  
 
 101  
     public void overrideThrown(Exception thrown)
 102  
     {
 103  4
         for (Class type : info.getExceptionTypes())
 104  
         {
 105  4
             if (type.isInstance(thrown))
 106  
             {
 107  4
                 this.thrown = thrown;
 108  4
                 return;
 109  
             }
 110  
         }
 111  
 
 112  0
         throw new IllegalArgumentException(
 113  
                 String.format("Exception class %s is not a declared exception type for method %s().",
 114  
                               thrown.getClass(),
 115  
                               info.getMethodName()));
 116  
     }
 117  
 
 118  
     public Object getResult()
 119  
     {
 120  198
         return result;
 121  
     }
 122  
 
 123  
     public void overrideResult(Object newResult)
 124  
     {
 125  198
         if (newResult != null)
 126  
         {
 127  198
             Class expectedType = info.getEffectiveResultType();
 128  
 
 129  198
             if (!expectedType.isInstance(newResult))
 130  
             {
 131  0
                 throw new IllegalArgumentException(
 132  
                         String.format("Invalid result value (%s) does not match return type %s for method %s.",
 133  
                                       newResult,
 134  
                                       expectedType.getName(),
 135  
                                       info.getMethodName()));
 136  
             }
 137  
         }
 138  
 
 139  198
         result = newResult;
 140  198
         thrown = null;
 141  198
     }
 142  
 }