001: /* Generated By:JJTree: Do not edit this line. AstValue.java */
002:
003: package org.apache.el.parser;
004:
005: import java.lang.reflect.InvocationTargetException;
006: import java.lang.reflect.Method;
007:
008: import javax.el.ELException;
009: import javax.el.ELResolver;
010: import javax.el.MethodInfo;
011: import javax.el.PropertyNotFoundException;
012:
013: import org.apache.el.lang.EvaluationContext;
014: import org.apache.el.util.MessageFactory;
015: import org.apache.el.util.ReflectionUtil;
016:
017: /**
018: * @author Jacob Hookom [jacob@hookom.net]
019: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
020: */
021: public final class AstValue extends SimpleNode {
022:
023: protected static class Target {
024: protected Object base;
025:
026: protected Object property;
027: }
028:
029: public AstValue(int id) {
030: super (id);
031: }
032:
033: public Class getType(EvaluationContext ctx) throws ELException {
034: Target t = getTarget(ctx);
035: ctx.setPropertyResolved(false);
036: return ctx.getELResolver().getType(ctx, t.base, t.property);
037: }
038:
039: private final Target getTarget(EvaluationContext ctx)
040: throws ELException {
041: // evaluate expr-a to value-a
042: Object base = this .children[0].getValue(ctx);
043:
044: // if our base is null (we know there are more properites to evaluate)
045: if (base == null) {
046: throw new PropertyNotFoundException(MessageFactory.get(
047: "error.unreachable.base", this .children[0]
048: .getImage()));
049: }
050:
051: // set up our start/end
052: Object property = null;
053: int propCount = this .jjtGetNumChildren() - 1;
054: int i = 1;
055:
056: // evaluate any properties before our target
057: ELResolver resolver = ctx.getELResolver();
058: if (propCount > 1) {
059: while (base != null && i < propCount) {
060: property = this .children[i].getValue(ctx);
061: ctx.setPropertyResolved(false);
062: base = resolver.getValue(ctx, base, property);
063: i++;
064: }
065: // if we are in this block, we have more properties to resolve,
066: // but our base was null
067: if (base == null || property == null) {
068: throw new PropertyNotFoundException(MessageFactory.get(
069: "error.unreachable.property", property));
070: }
071: }
072:
073: property = this .children[i].getValue(ctx);
074:
075: if (property == null) {
076: throw new PropertyNotFoundException(MessageFactory.get(
077: "error.unreachable.property", this .children[i]));
078: }
079:
080: Target t = new Target();
081: t.base = base;
082: t.property = property;
083: return t;
084: }
085:
086: public Object getValue(EvaluationContext ctx) throws ELException {
087: Object base = this .children[0].getValue(ctx);
088: int propCount = this .jjtGetNumChildren();
089: int i = 1;
090: Object property = null;
091: ELResolver resolver = ctx.getELResolver();
092: while (base != null && i < propCount) {
093: property = this .children[i].getValue(ctx);
094: if (property == null) {
095: return null;
096: } else {
097: ctx.setPropertyResolved(false);
098: base = resolver.getValue(ctx, base, property);
099: }
100: i++;
101: }
102: return base;
103: }
104:
105: public boolean isReadOnly(EvaluationContext ctx) throws ELException {
106: Target t = getTarget(ctx);
107: ctx.setPropertyResolved(false);
108: return ctx.getELResolver().isReadOnly(ctx, t.base, t.property);
109: }
110:
111: public void setValue(EvaluationContext ctx, Object value)
112: throws ELException {
113: Target t = getTarget(ctx);
114: ctx.setPropertyResolved(false);
115: ctx.getELResolver().setValue(ctx, t.base, t.property, value);
116: }
117:
118: public MethodInfo getMethodInfo(EvaluationContext ctx,
119: Class[] paramTypes) throws ELException {
120: Target t = getTarget(ctx);
121: Method m = ReflectionUtil.getMethod(t.base, t.property,
122: paramTypes);
123: return new MethodInfo(m.getName(), m.getReturnType(), m
124: .getParameterTypes());
125: }
126:
127: public Object invoke(EvaluationContext ctx, Class[] paramTypes,
128: Object[] paramValues) throws ELException {
129: Target t = getTarget(ctx);
130: Method m = ReflectionUtil.getMethod(t.base, t.property,
131: paramTypes);
132: Object result = null;
133: try {
134: result = m.invoke(t.base, (Object[]) paramValues);
135: } catch (IllegalAccessException iae) {
136: throw new ELException(iae);
137: } catch (InvocationTargetException ite) {
138: throw new ELException(ite.getCause());
139: }
140: return result;
141: }
142: }
|