Coverage Report - org.apache.myfaces.view.facelets.tag.BeanPropertyTagRule
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertyTagRule
0%
0/9
0%
0/4
3.6
BeanPropertyTagRule$DynamicPropertyMetadata
0%
0/12
N/A
3.6
BeanPropertyTagRule$LiteralPropertyMetadata
0%
0/14
0%
0/2
3.6
 
 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.faces.view.facelets.FaceletContext;
 25  
 import javax.faces.view.facelets.MetaRule;
 26  
 import javax.faces.view.facelets.Metadata;
 27  
 import javax.faces.view.facelets.MetadataTarget;
 28  
 import javax.faces.view.facelets.TagAttribute;
 29  
 import javax.faces.view.facelets.TagAttributeException;
 30  
 
 31  
 /**
 32  
  * 
 33  
  * @author Jacob Hookom
 34  
  * @version $Id$
 35  
  */
 36  0
 public final class BeanPropertyTagRule extends MetaRule
 37  
 {
 38  0
     public final static BeanPropertyTagRule INSTANCE = new BeanPropertyTagRule();
 39  
 
 40  
     public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
 41  
     {
 42  0
         Method m = meta.getWriteMethod(name);
 43  
 
 44  
         // if the property is writable
 45  0
         if (m != null)
 46  
         {
 47  0
             if (attribute.isLiteral())
 48  
             {
 49  0
                 return new LiteralPropertyMetadata(m, attribute);
 50  
             }
 51  
             else
 52  
             {
 53  0
                 return new DynamicPropertyMetadata(m, attribute);
 54  
             }
 55  
         }
 56  
 
 57  0
         return null;
 58  
     }
 59  
     
 60  
     final static class LiteralPropertyMetadata extends Metadata
 61  
     {
 62  
 
 63  
         private final Method method;
 64  
 
 65  
         private final TagAttribute attribute;
 66  
 
 67  
         private Object[] value;
 68  
 
 69  
         public LiteralPropertyMetadata(Method method, TagAttribute attribute)
 70  0
         {
 71  0
             this.method = method;
 72  0
             this.attribute = attribute;
 73  0
         }
 74  
 
 75  
         public void applyMetadata(FaceletContext ctx, Object instance)
 76  
         {
 77  0
             if (value == null)
 78  
             {
 79  0
                 String str = this.attribute.getValue();
 80  0
                 value = new Object[] { ctx.getExpressionFactory().coerceToType(str, method.getParameterTypes()[0]) };
 81  
             }
 82  
             try
 83  
             {
 84  0
                 method.invoke(instance, this.value);
 85  
             }
 86  0
             catch (InvocationTargetException e)
 87  
             {
 88  0
                 throw new TagAttributeException(this.attribute, e.getCause());
 89  
             }
 90  0
             catch (Exception e)
 91  
             {
 92  0
                 throw new TagAttributeException(this.attribute, e);
 93  0
             }
 94  0
         }
 95  
 
 96  
     }
 97  
 
 98  0
     final static class DynamicPropertyMetadata extends Metadata
 99  
     {
 100  
 
 101  
         private final Method method;
 102  
 
 103  
         private final TagAttribute attribute;
 104  
 
 105  
         private final Class<?> type;
 106  
 
 107  
         public DynamicPropertyMetadata(Method method, TagAttribute attribute)
 108  0
         {
 109  0
             this.method = method;
 110  0
             this.type = method.getParameterTypes()[0];
 111  0
             this.attribute = attribute;
 112  0
         }
 113  
 
 114  
         public void applyMetadata(FaceletContext ctx, Object instance)
 115  
         {
 116  
             try
 117  
             {
 118  0
                 method.invoke(instance, new Object[] { attribute.getObject(ctx, type) });
 119  
             }
 120  0
             catch (InvocationTargetException e)
 121  
             {
 122  0
                 throw new TagAttributeException(attribute, e.getCause());
 123  
             }
 124  0
             catch (Exception e)
 125  
             {
 126  0
                 throw new TagAttributeException(attribute, e);
 127  0
             }
 128  0
         }
 129  
     }
 130  
 
 131  
 }