1 14 15 package com.sun.facelets.tag; 16 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 20 import com.sun.facelets.FaceletContext; 21 22 27 final class BeanPropertyTagRule extends MetaRule { 28 29 final static class LiteralPropertyMetadata extends Metadata { 30 31 private final Method method; 32 33 private final TagAttribute attribute; 34 35 private Object [] value; 36 37 public LiteralPropertyMetadata(Method method, TagAttribute attribute) { 38 this.method = method; 39 this.attribute = attribute; 40 } 41 42 public void applyMetadata(FaceletContext ctx, Object instance) { 43 if (value == null) { 44 String str = this.attribute.getValue(); 45 value = new Object [] { ctx.getExpressionFactory().coerceToType(str, 46 method.getParameterTypes()[0]) }; 47 } 48 try { 49 method.invoke(instance, this.value); 50 } catch (InvocationTargetException e) { 51 throw new TagAttributeException(this.attribute, e.getCause()); 52 } catch (Exception e) { 53 throw new TagAttributeException(this.attribute, e); 54 } 55 } 56 57 } 58 59 final static class DynamicPropertyMetadata extends Metadata { 60 61 private final Method method; 62 63 private final TagAttribute attribute; 64 65 private final Class type; 66 67 public DynamicPropertyMetadata(Method method, TagAttribute attribute) { 68 this.method = method; 69 this.type = method.getParameterTypes()[0]; 70 this.attribute = attribute; 71 } 72 73 public void applyMetadata(FaceletContext ctx, Object instance) { 74 try { 75 this.method.invoke(instance, new Object [] { this.attribute 76 .getObject(ctx, this.type) }); 77 } catch (InvocationTargetException e) { 78 throw new TagAttributeException(this.attribute, e.getCause()); 79 } catch (Exception e) { 80 throw new TagAttributeException(this.attribute, e); 81 } 82 } 83 } 84 85 public final static BeanPropertyTagRule Instance = new BeanPropertyTagRule(); 86 87 public Metadata applyRule(String name, TagAttribute attribute, 88 MetadataTarget meta) { 89 Method m = meta.getWriteMethod(name); 90 91 if (m != null) { 93 if (attribute.isLiteral()) { 94 return new LiteralPropertyMetadata(m, attribute); 95 } else { 96 return new DynamicPropertyMetadata(m, attribute); 97 } 98 } 99 100 return null; 101 } 102 103 } 104 | Popular Tags |