Coverage report

  %line %branch
org.apache.commons.jelly.tags.core.InvokeTag
90% 
100% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.jelly.tags.core;
 17  
 
 18  
 import java.lang.reflect.InvocationTargetException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.beanutils.MethodUtils;
 23  
 import org.apache.commons.jelly.JellyTagException;
 24  
 import org.apache.commons.jelly.MissingAttributeException;
 25  
 import org.apache.commons.jelly.TagSupport;
 26  
 import org.apache.commons.jelly.XMLOutput;
 27  
 
 28  
 /**
 29  
   * A tag which calls a method in an object instantied by core:new
 30  
   *
 31  
   *
 32  
   * @author Rodney Waldhoff
 33  
   * @version $Revision: 1.10 $
 34  
   */
 35  2
 public class InvokeTag extends TagSupport implements ArgTagParent {
 36  
 
 37  
     /** the variable exported */
 38  
     private String var;
 39  
 
 40  
     /** the variable where the method's exception is exported */
 41  
     private String exceptionVar;
 42  
 
 43  
     /** the method to invoke */
 44  
     private String methodName;
 45  
 
 46  
     /** the object to invoke the method on */
 47  
     private Object onInstance;
 48  
 
 49  34
     private List paramTypes = new ArrayList();
 50  34
     private List paramValues = new ArrayList();
 51  
 
 52  34
     public InvokeTag() {
 53  34
     }
 54  
 
 55  
     /** Sets the name of the variable exported by this tag */
 56  
     public void setVar(String var) {
 57  4
         this.var = class="keyword">var;
 58  4
     }
 59  
 
 60  
     /** Sets the name of a variable that exports the exception thrown by
 61  
      * the method's invocation (if any)
 62  
      */
 63  
     public void setExceptionVar(String var) {
 64  2
         this.exceptionVar = var;
 65  2
     }
 66  
 
 67  
     public void setMethod(String method) {
 68  34
         this.methodName = method;
 69  34
     }
 70  
 
 71  
     public void setOn(Object instance) {
 72  34
         this.onInstance = instance;
 73  34
     }
 74  
 
 75  
     public void addArgument(Class type, Object value) {
 76  32
         paramTypes.add(type);
 77  32
         paramValues.add(value);
 78  32
     }
 79  
 
 80  
     // Tag interface
 81  
     //-------------------------------------------------------------------------
 82  
     public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
 83  34
         if ( null == methodName) {
 84  0
             throw new MissingAttributeException( "method" );
 85  
         }
 86  34
         if ( null == onInstance ) {
 87  0
             throw new MissingAttributeException( "on" );
 88  
         }
 89  
 
 90  34
         invokeBody(output);
 91  
 
 92  34
         Object[] values = paramValues.toArray();
 93  34
         Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()]));
 94  
 
 95  34
         Object result = null;
 96  
         try {
 97  34
             result = MethodUtils.invokeMethod(onInstance,methodName,values,types);
 98  30
         }
 99  
         catch (NoSuchMethodException e) {
 100  0
             throw new JellyTagException(e);
 101  
         }
 102  
         catch (IllegalAccessException e) {
 103  0
             throw new JellyTagException(e);
 104  
         }
 105  
         catch (InvocationTargetException e) {
 106  4
             if(null != exceptionVar) {
 107  2
                 context.setVariable(exceptionVar,e.getTargetException());
 108  
             } else {
 109  2
                 throw new JellyTagException("method " + methodName +
 110  
                     " threw exception: "+ e.getTargetException().getMessage(),
 111  
                     e.getTargetException() );
 112  
             }
 113  2
         }
 114  
         finally {
 115  2
             paramTypes.clear();
 116  34
             paramValues.clear();
 117  
         }
 118  
 
 119  32
         ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class));
 120  32
         if(null != parentArg) {
 121  8
             parentArg.setValue(result);
 122  
         }
 123  32
         if(null != var) {
 124  4
             context.setVariable(var, result);
 125  
         }
 126  32
     }
 127  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.