Coverage Report - org.apache.myfaces.view.facelets.tag.MethodRule
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodRule
0%
0/12
0%
0/6
3.25
MethodRule$MethodExpressionMetadata
0%
0/14
N/A
3.25
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.view.facelets.tag;
 20  
 
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.lang.reflect.Method;
 23  
 
 24  
 import javax.el.MethodExpression;
 25  
 import javax.faces.view.facelets.FaceletContext;
 26  
 import javax.faces.view.facelets.MetaRule;
 27  
 import javax.faces.view.facelets.Metadata;
 28  
 import javax.faces.view.facelets.MetadataTarget;
 29  
 import javax.faces.view.facelets.TagAttribute;
 30  
 import javax.faces.view.facelets.TagAttributeException;
 31  
 
 32  
 /**
 33  
  * Optional Rule for binding Method[Binding|Expression] properties
 34  
  * 
 35  
  * @author Mike Kienenberger
 36  
  * @author Jacob Hookom
 37  
  */
 38  
 public final class MethodRule extends MetaRule
 39  
 {
 40  
 
 41  
     private final String methodName;
 42  
 
 43  
     private final Class<?> returnTypeClass;
 44  
 
 45  
     private final Class<?>[] params;
 46  
 
 47  
     public MethodRule(String methodName, Class<?> returnTypeClass, Class<?>[] params)
 48  0
     {
 49  0
         this.methodName = methodName;
 50  0
         this.returnTypeClass = returnTypeClass;
 51  0
         this.params = params;
 52  0
     }
 53  
 
 54  
     public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
 55  
     {
 56  0
         if (false == name.equals(this.methodName))
 57  
         {
 58  0
             return null;
 59  
         }
 60  
 
 61  0
         if (MethodExpression.class.equals(meta.getPropertyType(name)))
 62  
         {
 63  0
             Method method = meta.getWriteMethod(name);
 64  0
             if (method != null)
 65  
             {
 66  0
                 return new MethodExpressionMetadata(method, attribute, this.returnTypeClass, this.params);
 67  
             }
 68  
         }
 69  
 
 70  0
         return null;
 71  
     }
 72  
 
 73  
     private class MethodExpressionMetadata extends Metadata
 74  
     {
 75  
         private final Method _method;
 76  
 
 77  
         private final TagAttribute _attribute;
 78  
 
 79  
         private Class<?>[] _paramList;
 80  
 
 81  
         private Class<?> _returnType;
 82  
 
 83  
         public MethodExpressionMetadata(Method method, TagAttribute attribute, Class<?> returnType, 
 84  
                                         Class<?>[] paramList)
 85  0
         {
 86  0
             _method = method;
 87  0
             _attribute = attribute;
 88  0
             _paramList = paramList;
 89  0
             _returnType = returnType;
 90  0
         }
 91  
 
 92  
         public void applyMetadata(FaceletContext ctx, Object instance)
 93  
         {
 94  0
             MethodExpression expr = _attribute.getMethodExpression(ctx, _returnType, _paramList);
 95  
 
 96  
             try
 97  
             {
 98  0
                 _method.invoke(instance, new Object[] { expr });
 99  
             }
 100  0
             catch (InvocationTargetException e)
 101  
             {
 102  0
                 throw new TagAttributeException(_attribute, e.getCause());
 103  
             }
 104  0
             catch (Exception e)
 105  
             {
 106  0
                 throw new TagAttributeException(_attribute, e);
 107  0
             }
 108  0
         }
 109  
     }
 110  
 }