001: package org.apache.ojb.jdo.jdoql;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.*;
019:
020: /**
021: * A method invocation expression. Because invocation of methods on persistent
022: * classes is not supported, we can simply resolve the invocation to a reflection
023: * method object.
024: *
025: * TODO: What is the best way to handle these (esp. Collection.contains which
026: * does not behave like the Java method) ?
027: * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
028: */
029: public class MethodInvocation extends PostfixExpression {
030: /** The method name */
031: private String _name;
032: /** The arguments (expressions) */
033: private List _args;
034: /** The return type of the method */
035: private Class _returnType;
036:
037: /**
038: * Creates a new method invocation object.
039: *
040: * @param base The base expression (can be <code>null</code>)
041: * @param methodName The name of the method
042: * @param args The arguments
043: */
044: public MethodInvocation(Expression base, String methodName,
045: List args) {
046: super (base);
047: _name = methodName;
048: _args = args;
049: for (Iterator it = args.iterator(); it.hasNext();) {
050: ((Expression) it.next()).setParent(this );
051: }
052: }
053:
054: /* (non-Javadoc)
055: * @see org.apache.ojb.jdo.jdoql.PostfixExpression#hasBaseExpression()
056: */
057: public boolean hasBaseExpression() {
058: return _base != null;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.apache.ojb.jdo.jdoql.PostfixExpression#getBaseExpression()
063: */
064: public Expression getBaseExpression() {
065: return _base;
066: }
067:
068: /* (non-Javadoc)
069: * @see org.apache.ojb.jdo.jdoql.PostfixExpression#setBaseExpression(org.apache.ojb.jdo.jdoql.Expression)
070: */
071: public void setBaseExpression(Expression base) {
072: _base = base;
073: }
074:
075: /* (non-Javadoc)
076: * @see org.apache.ojb.jdo.jdoql.Expression#replaceChild(org.apache.ojb.jdo.jdoql.Expression, org.apache.ojb.jdo.jdoql.Expression)
077: */
078: public void replaceChild(Expression oldChild, Expression newChild) {
079: if (oldChild == _base) {
080: _base.setParent(null);
081: _base = newChild;
082: _base.setParent(this );
083: } else {
084: Expression expr;
085:
086: for (int idx = 0; idx < _args.size(); idx++) {
087: expr = (Expression) _args.get(idx);
088: if (expr == oldChild) {
089: expr.setParent(null);
090: newChild.setParent(this );
091: _args.set(idx, newChild);
092: break;
093: }
094: }
095: }
096: }
097:
098: /**
099: * Returns the name of the accessed method.
100: *
101: * @return The method's name
102: */
103: public String getName() {
104: return _name;
105: }
106:
107: /**
108: * Returns the arguments of the invocation.
109: *
110: * @return The arguments (list of expressions)
111: */
112: public List getArguments() {
113: return _args;
114: }
115:
116: /* (non-Javadoc)
117: * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
118: */
119: public void accept(Visitor visitor) {
120: visitor.visit(this );
121: }
122:
123: /* (non-Javadoc)
124: * @see java.lang.Object#toString()
125: */
126: public String toString() {
127: StringBuffer result = new StringBuffer();
128:
129: if (_base != null) {
130: result.append(_base.toString());
131: result.append(".");
132: }
133: result.append(_name);
134: result.append("(");
135: for (Iterator it = _args.iterator(); it.hasNext();) {
136: result.append(it.next().toString());
137: if (it.hasNext()) {
138: result.append(", ");
139: }
140: }
141: result.append(")");
142: return result.toString();
143: }
144:
145: /**
146: * Sets the return type of the method.
147: *
148: * @param type The return type
149: */
150: public void setType(Class type) {
151: _returnType = type;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.apache.ojb.jdo.jdoql.Expression#getType()
156: */
157: public Class getType() {
158: return _returnType;
159: }
160: }
|