KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > parser > AstValue


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
/* Generated By:JJTree: Do not edit this line. AstValue.java */
23
24 package com.sun.el.parser;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import javax.el.ELException;
30 import javax.el.ELResolver;
31 import javax.el.MethodInfo;
32 import javax.el.PropertyNotFoundException;
33
34 import com.sun.el.lang.EvaluationContext;
35 import com.sun.el.util.MessageFactory;
36 import com.sun.el.util.ReflectionUtil;
37
38 /**
39  * @author Jacob Hookom [jacob@hookom.net]
40  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
41  */

42 public final class AstValue extends SimpleNode {
43
44     protected static class Target {
45         protected Object JavaDoc base;
46
47         protected Object JavaDoc property;
48     }
49
50     public AstValue(int id) {
51         super(id);
52     }
53
54     public Class JavaDoc getType(EvaluationContext ctx) throws ELException {
55         Target t = getTarget(ctx);
56         ctx.setPropertyResolved(false);
57         return ctx.getELResolver().getType(ctx, t.base, t.property);
58     }
59
60     private final Target getTarget(EvaluationContext ctx) throws ELException {
61         // evaluate expr-a to value-a
62
Object JavaDoc base = this.children[0].getValue(ctx);
63
64         // if our base is null (we know there are more properites to evaluate)
65
if (base == null) {
66             throw new PropertyNotFoundException(MessageFactory.get(
67                     "error.unreachable.base", this.children[0].getImage()));
68         }
69
70         // set up our start/end
71
Object JavaDoc property = null;
72         int propCount = this.jjtGetNumChildren() - 1;
73         int i = 1;
74
75         // evaluate any properties before our target
76
ELResolver resolver = ctx.getELResolver();
77         if (propCount > 1) {
78             while (base != null && i < propCount) {
79                 property = this.children[i].getValue(ctx);
80                 ctx.setPropertyResolved(false);
81                 base = resolver.getValue(ctx, base, property);
82                 i++;
83             }
84             // if we are in this block, we have more properties to resolve,
85
// but our base was null
86
if (base == null || property == null) {
87                 throw new PropertyNotFoundException(MessageFactory.get(
88                         "error.unreachable.property", property));
89             }
90         }
91
92         property = this.children[i].getValue(ctx);
93
94         if (property == null) {
95             throw new PropertyNotFoundException(MessageFactory.get(
96                     "error.unreachable.property", this.children[i]));
97         }
98
99         Target t = new Target();
100         t.base = base;
101         t.property = property;
102         return t;
103     }
104
105     public Object JavaDoc getValue(EvaluationContext ctx) throws ELException {
106         Object JavaDoc base = this.children[0].getValue(ctx);
107         int propCount = this.jjtGetNumChildren();
108         int i = 1;
109         Object JavaDoc property = null;
110         ELResolver resolver = ctx.getELResolver();
111         while (base != null && i < propCount) {
112             property = this.children[i].getValue(ctx);
113             if (property == null) {
114                 return null;
115             } else {
116                 ctx.setPropertyResolved(false);
117                 base = resolver.getValue(ctx, base, property);
118             }
119             i++;
120         }
121         return base;
122     }
123
124     public boolean isReadOnly(EvaluationContext ctx) throws ELException {
125         Target t = getTarget(ctx);
126         ctx.setPropertyResolved(false);
127         return ctx.getELResolver().isReadOnly(ctx, t.base, t.property);
128     }
129
130     public void setValue(EvaluationContext ctx, Object JavaDoc value)
131             throws ELException {
132         Target t = getTarget(ctx);
133         ctx.setPropertyResolved(false);
134         ctx.getELResolver().setValue(ctx, t.base, t.property, value);
135     }
136
137     public MethodInfo getMethodInfo(EvaluationContext ctx, Class JavaDoc[] paramTypes)
138             throws ELException {
139         Target t = getTarget(ctx);
140         Method JavaDoc m = ReflectionUtil.getMethod(t.base, t.property, paramTypes);
141         return new MethodInfo(m.getName(), m.getReturnType(), m
142                 .getParameterTypes());
143     }
144
145     public Object JavaDoc invoke(EvaluationContext ctx, Class JavaDoc[] paramTypes,
146             Object JavaDoc[] paramValues) throws ELException {
147         Target t = getTarget(ctx);
148         Method JavaDoc m = ReflectionUtil.getMethod(t.base, t.property, paramTypes);
149         Object JavaDoc result = null;
150         try {
151             result = m.invoke(t.base, (Object JavaDoc[]) paramValues);
152         } catch (IllegalAccessException JavaDoc iae) {
153             throw new ELException(iae);
154         } catch (InvocationTargetException JavaDoc ite) {
155             throw new ELException(ite.getCause());
156         }
157         return result;
158     }
159 }
160
Popular Tags