KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > jdoql > MethodInvocation


1 package org.apache.ojb.jdo.jdoql;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.*;
19
20 /**
21  * A method invocation expression. Because invocation of methods on persistent
22  * classes is not supported, we can simply resolve the invocation to a reflection
23  * method object.
24  *
25  * TODO: What is the best way to handle these (esp. Collection.contains which
26  * does not behave like the Java method) ?
27  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
28  */

29 public class MethodInvocation extends PostfixExpression
30 {
31     /** The method name */
32     private String JavaDoc _name;
33     /** The arguments (expressions) */
34     private List _args;
35     /** The return type of the method */
36     private Class JavaDoc _returnType;
37
38     /**
39      * Creates a new method invocation object.
40      *
41      * @param base The base expression (can be <code>null</code>)
42      * @param methodName The name of the method
43      * @param args The arguments
44      */

45     public MethodInvocation(Expression base, String JavaDoc methodName, List args)
46     {
47         super(base);
48         _name = methodName;
49         _args = args;
50         for (Iterator it = args.iterator(); it.hasNext();)
51         {
52             ((Expression)it.next()).setParent(this);
53         }
54     }
55
56     /* (non-Javadoc)
57      * @see org.apache.ojb.jdo.jdoql.PostfixExpression#hasBaseExpression()
58      */

59     public boolean hasBaseExpression()
60     {
61         return _base != null;
62     }
63
64     /* (non-Javadoc)
65      * @see org.apache.ojb.jdo.jdoql.PostfixExpression#getBaseExpression()
66      */

67     public Expression getBaseExpression()
68     {
69         return _base;
70     }
71
72     /* (non-Javadoc)
73      * @see org.apache.ojb.jdo.jdoql.PostfixExpression#setBaseExpression(org.apache.ojb.jdo.jdoql.Expression)
74      */

75     public void setBaseExpression(Expression base)
76     {
77         _base = base;
78     }
79
80     /* (non-Javadoc)
81      * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
82      */

83     public void replaceChild(Expression oldChild, Expression newChild)
84     {
85         if (oldChild == _base)
86         {
87             _base.setParent(null);
88             _base = newChild;
89             _base.setParent(this);
90         }
91         else
92         {
93             Expression expr;
94
95             for (int idx = 0; idx < _args.size(); idx++)
96             {
97                 expr = (Expression)_args.get(idx);
98                 if (expr == oldChild)
99                 {
100                     expr.setParent(null);
101                     newChild.setParent(this);
102                     _args.set(idx, newChild);
103                     break;
104                 }
105             }
106         }
107     }
108
109     /**
110      * Returns the name of the accessed method.
111      *
112      * @return The method's name
113      */

114     public String JavaDoc getName()
115     {
116         return _name;
117     }
118
119     /**
120      * Returns the arguments of the invocation.
121      *
122      * @return The arguments (list of expressions)
123      */

124     public List getArguments()
125     {
126         return _args;
127     }
128
129     /* (non-Javadoc)
130      * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
131      */

132     public void accept(Visitor visitor)
133     {
134         visitor.visit(this);
135     }
136
137     /* (non-Javadoc)
138      * @see java.lang.Object#toString()
139      */

140     public String JavaDoc toString()
141     {
142         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
143
144         if (_base != null)
145         {
146             result.append(_base.toString());
147             result.append(".");
148         }
149         result.append(_name);
150         result.append("(");
151         for (Iterator it = _args.iterator(); it.hasNext();)
152         {
153             result.append(it.next().toString());
154             if (it.hasNext())
155             {
156                 result.append(", ");
157             }
158         }
159         result.append(")");
160         return result.toString();
161     }
162
163     /**
164      * Sets the return type of the method.
165      *
166      * @param type The return type
167      */

168     public void setType(Class JavaDoc type)
169     {
170         _returnType = type;
171     }
172
173     /* (non-Javadoc)
174      * @see org.apache.ojb.jdo.jdoql.Expression#getType()
175      */

176     public Class JavaDoc getType()
177     {
178         return _returnType;
179     }
180 }
181
Popular Tags