1 2 3 package org.apache.el.parser; 4 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 8 import javax.el.ELException; 9 import javax.el.ELResolver; 10 import javax.el.MethodInfo; 11 import javax.el.PropertyNotFoundException; 12 13 import org.apache.el.lang.EvaluationContext; 14 import org.apache.el.util.MessageFactory; 15 import org.apache.el.util.ReflectionUtil; 16 17 18 22 public final class AstValue extends SimpleNode { 23 24 protected static class Target { 25 protected Object base; 26 27 protected Object property; 28 } 29 30 public AstValue(int id) { 31 super(id); 32 } 33 34 public Class getType(EvaluationContext ctx) throws ELException { 35 Target t = getTarget(ctx); 36 ctx.setPropertyResolved(false); 37 return ctx.getELResolver().getType(ctx, t.base, t.property); 38 } 39 40 private final Target getTarget(EvaluationContext ctx) throws ELException { 41 Object base = this.children[0].getValue(ctx); 43 44 if (base == null) { 46 throw new PropertyNotFoundException(MessageFactory.get( 47 "error.unreachable.base", this.children[0].getImage())); 48 } 49 50 Object property = null; 52 int propCount = this.jjtGetNumChildren() - 1; 53 int i = 1; 54 55 ELResolver resolver = ctx.getELResolver(); 57 if (propCount > 1) { 58 while (base != null && i < propCount) { 59 property = this.children[i].getValue(ctx); 60 ctx.setPropertyResolved(false); 61 base = resolver.getValue(ctx, base, property); 62 i++; 63 } 64 if (base == null || property == null) { 67 throw new PropertyNotFoundException(MessageFactory.get( 68 "error.unreachable.property", property)); 69 } 70 } 71 72 property = this.children[i].getValue(ctx); 73 74 if (property == null) { 75 throw new PropertyNotFoundException(MessageFactory.get( 76 "error.unreachable.property", this.children[i])); 77 } 78 79 Target t = new Target(); 80 t.base = base; 81 t.property = property; 82 return t; 83 } 84 85 public Object getValue(EvaluationContext ctx) throws ELException { 86 Object base = this.children[0].getValue(ctx); 87 int propCount = this.jjtGetNumChildren(); 88 int i = 1; 89 Object property = null; 90 ELResolver resolver = ctx.getELResolver(); 91 while (base != null && i < propCount) { 92 property = this.children[i].getValue(ctx); 93 if (property == null) { 94 return null; 95 } else { 96 ctx.setPropertyResolved(false); 97 base = resolver.getValue(ctx, base, property); 98 } 99 i++; 100 } 101 return base; 102 } 103 104 public boolean isReadOnly(EvaluationContext ctx) throws ELException { 105 Target t = getTarget(ctx); 106 ctx.setPropertyResolved(false); 107 return ctx.getELResolver().isReadOnly(ctx, t.base, t.property); 108 } 109 110 public void setValue(EvaluationContext ctx, Object value) 111 throws ELException { 112 Target t = getTarget(ctx); 113 ctx.setPropertyResolved(false); 114 ctx.getELResolver().setValue(ctx, t.base, t.property, value); 115 } 116 117 public MethodInfo getMethodInfo(EvaluationContext ctx, Class [] paramTypes) 118 throws ELException { 119 Target t = getTarget(ctx); 120 Method m = ReflectionUtil.getMethod(t.base, t.property, paramTypes); 121 return new MethodInfo(m.getName(), m.getReturnType(), m 122 .getParameterTypes()); 123 } 124 125 public Object invoke(EvaluationContext ctx, Class [] paramTypes, 126 Object [] paramValues) throws ELException { 127 Target t = getTarget(ctx); 128 Method m = ReflectionUtil.getMethod(t.base, t.property, paramTypes); 129 Object result = null; 130 try { 131 result = m.invoke(t.base, (Object []) paramValues); 132 } catch (IllegalAccessException iae) { 133 throw new ELException(iae); 134 } catch (InvocationTargetException ite) { 135 throw new ELException(ite.getCause()); 136 } 137 return result; 138 } 139 } 140 | Popular Tags |