1 14 package com.sun.facelets.tag; 15 16 import java.lang.reflect.InvocationTargetException ; 17 import java.lang.reflect.Method ; 18 19 import javax.el.MethodExpression; 20 import javax.faces.el.MethodBinding; 21 22 import com.sun.facelets.FaceletContext; 23 import com.sun.facelets.el.LegacyMethodBinding; 24 25 31 public final class MethodRule extends MetaRule { 32 33 private final String methodName; 34 35 private final Class returnTypeClass; 36 37 private final Class [] params; 38 39 public MethodRule(String methodName, Class returnTypeClass, Class [] params) { 40 this.methodName = methodName; 41 this.returnTypeClass = returnTypeClass; 42 this.params = params; 43 } 44 45 public Metadata applyRule(String name, TagAttribute attribute, 46 MetadataTarget meta) { 47 if (false == name.equals(this.methodName)) 48 return null; 49 50 if (MethodBinding.class.equals(meta.getPropertyType(name))) { 51 Method method = meta.getWriteMethod(name); 52 if (method != null) { 53 return new MethodBindingMetadata(method, attribute, 54 this.returnTypeClass, this.params); 55 } 56 } else if (MethodExpression.class.equals(meta.getPropertyType(name))) { 57 Method method = meta.getWriteMethod(name); 58 if (method != null) { 59 return new MethodExpressionMetadata(method, attribute, 60 this.returnTypeClass, this.params); 61 } 62 } 63 64 return null; 65 } 66 67 private class MethodBindingMetadata extends Metadata { 68 private final Method _method; 69 70 private final TagAttribute _attribute; 71 72 private Class [] _paramList; 73 74 private Class _returnType; 75 76 public MethodBindingMetadata(Method method, TagAttribute attribute, 77 Class returnType, Class [] paramList) { 78 _method = method; 79 _attribute = attribute; 80 _paramList = paramList; 81 _returnType = returnType; 82 } 83 84 public void applyMetadata(FaceletContext ctx, Object instance) { 85 MethodExpression expr = _attribute.getMethodExpression(ctx, 86 _returnType, _paramList); 87 88 try { 89 _method.invoke(instance, 90 new Object [] { new LegacyMethodBinding(expr) }); 91 } catch (InvocationTargetException e) { 92 throw new TagAttributeException(_attribute, e.getCause()); 93 } catch (Exception e) { 94 throw new TagAttributeException(_attribute, e); 95 } 96 } 97 } 98 99 private class MethodExpressionMetadata extends Metadata { 100 private final Method _method; 101 102 private final TagAttribute _attribute; 103 104 private Class [] _paramList; 105 106 private Class _returnType; 107 108 public MethodExpressionMetadata(Method method, TagAttribute attribute, 109 Class returnType, Class [] paramList) { 110 _method = method; 111 _attribute = attribute; 112 _paramList = paramList; 113 _returnType = returnType; 114 } 115 116 public void applyMetadata(FaceletContext ctx, Object instance) { 117 MethodExpression expr = _attribute.getMethodExpression(ctx, 118 _returnType, _paramList); 119 120 try { 121 _method.invoke(instance, new Object [] { expr }); 122 } catch (InvocationTargetException e) { 123 throw new TagAttributeException(_attribute, e.getCause()); 124 } catch (Exception e) { 125 throw new TagAttributeException(_attribute, e); 126 } 127 } 128 } 129 } 130 | Popular Tags |