KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > parser > AstValue


1 /* Generated By:JJTree: Do not edit this line. AstValue.java */
2
3 package org.apache.el.parser;
4
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
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 /**
19  * @author Jacob Hookom [jacob@hookom.net]
20  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
21  */

22 public final class AstValue extends SimpleNode {
23
24     protected static class Target {
25         protected Object JavaDoc base;
26
27         protected Object JavaDoc property;
28     }
29
30     public AstValue(int id) {
31         super(id);
32     }
33
34     public Class JavaDoc 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         // evaluate expr-a to value-a
42
Object JavaDoc base = this.children[0].getValue(ctx);
43
44         // if our base is null (we know there are more properites to evaluate)
45
if (base == null) {
46             throw new PropertyNotFoundException(MessageFactory.get(
47                     "error.unreachable.base", this.children[0].getImage()));
48         }
49
50         // set up our start/end
51
Object JavaDoc property = null;
52         int propCount = this.jjtGetNumChildren() - 1;
53         int i = 1;
54
55         // evaluate any properties before our target
56
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 we are in this block, we have more properties to resolve,
65
// but our base was null
66
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 JavaDoc getValue(EvaluationContext ctx) throws ELException {
86         Object JavaDoc base = this.children[0].getValue(ctx);
87         int propCount = this.jjtGetNumChildren();
88         int i = 1;
89         Object JavaDoc 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 JavaDoc 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 JavaDoc[] paramTypes)
118             throws ELException {
119         Target t = getTarget(ctx);
120         Method JavaDoc m = ReflectionUtil.getMethod(t.base, t.property, paramTypes);
121         return new MethodInfo(m.getName(), m.getReturnType(), m
122                 .getParameterTypes());
123     }
124
125     public Object JavaDoc invoke(EvaluationContext ctx, Class JavaDoc[] paramTypes,
126             Object JavaDoc[] paramValues) throws ELException {
127         Target t = getTarget(ctx);
128         Method JavaDoc m = ReflectionUtil.getMethod(t.base, t.property, paramTypes);
129         Object JavaDoc result = null;
130         try {
131             result = m.invoke(t.base, (Object JavaDoc[]) paramValues);
132         } catch (IllegalAccessException JavaDoc iae) {
133             throw new ELException(iae);
134         } catch (InvocationTargetException JavaDoc ite) {
135             throw new ELException(ite.getCause());
136         }
137         return result;
138     }
139 }
140
Popular Tags