001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.codemodel;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: /**
027: * JMethod invocation
028: */
029: public final class JInvocation extends JExpressionImpl implements
030: JStatement {
031:
032: /**
033: * Object expression upon which this method will be invoked, or null if
034: * this is a constructor invocation
035: */
036: private JGenerable object;
037:
038: /**
039: * Name of the method to be invoked.
040: * Either this field is set, or {@link #method}, or {@link #type} (in which case it's a
041: * constructor invocation.)
042: * This allows {@link JMethod#name(String) the name of the method to be changed later}.
043: */
044: private String name;
045:
046: private JMethod method;
047:
048: private boolean isConstructor = false;
049:
050: /**
051: * List of argument expressions for this method invocation
052: */
053: private List<JExpression> args = new ArrayList<JExpression>();
054:
055: /**
056: * If isConstructor==true, this field keeps the type to be created.
057: */
058: private JType type = null;
059:
060: /**
061: * Invokes a method on an object.
062: *
063: * @param object
064: * JExpression for the object upon which
065: * the named method will be invoked,
066: * or null if none
067: *
068: * @param name
069: * Name of method to invoke
070: */
071: JInvocation(JExpression object, String name) {
072: this ((JGenerable) object, name);
073: }
074:
075: JInvocation(JExpression object, JMethod method) {
076: this ((JGenerable) object, method);
077: }
078:
079: /**
080: * Invokes a static method on a class.
081: */
082: JInvocation(JClass type, String name) {
083: this ((JGenerable) type, name);
084: }
085:
086: JInvocation(JClass type, JMethod method) {
087: this ((JGenerable) type, method);
088: }
089:
090: private JInvocation(JGenerable object, String name) {
091: this .object = object;
092: if (name.indexOf('.') >= 0)
093: throw new IllegalArgumentException(
094: "method name contains '.': " + name);
095: this .name = name;
096: }
097:
098: private JInvocation(JGenerable object, JMethod method) {
099: this .object = object;
100: this .method = method;
101: }
102:
103: /**
104: * Invokes a constructor of an object (i.e., creates
105: * a new object.)
106: *
107: * @param c
108: * Type of the object to be created. If this type is
109: * an array type, added arguments are treated as array
110: * initializer. Thus you can create an expression like
111: * <code>new int[]{1,2,3,4,5}</code>.
112: */
113: JInvocation(JType c) {
114: this .isConstructor = true;
115: this .type = c;
116: }
117:
118: /**
119: * Add an expression to this invocation's argument list
120: *
121: * @param arg
122: * Argument to add to argument list
123: */
124: public JInvocation arg(JExpression arg) {
125: if (arg == null)
126: throw new IllegalArgumentException();
127: args.add(arg);
128: return this ;
129: }
130:
131: /**
132: * Adds a literal argument.
133: *
134: * Short for {@code arg(JExpr.lit(v))}
135: */
136: public JInvocation arg(String v) {
137: return arg(JExpr.lit(v));
138: }
139:
140: public void generate(JFormatter f) {
141: if (isConstructor && type.isArray()) {
142: // [RESULT] new T[]{arg1,arg2,arg3,...};
143: f.p("new").g(type).p('{');
144: } else {
145: if (isConstructor)
146: f.p("new").g(type).p('(');
147: else {
148: String name = this .name;
149: if (name == null)
150: name = this .method.name();
151:
152: if (object != null)
153: f.g(object).p('.').p(name).p('(');
154: else
155: f.id(name).p('(');
156: }
157: }
158:
159: f.g(args);
160:
161: if (isConstructor && type.isArray())
162: f.p('}');
163: else
164: f.p(')');
165:
166: if (type instanceof JDefinedClass
167: && ((JDefinedClass) type).isAnonymous()) {
168: ((JAnonymousClass) type).declareBody(f);
169: }
170: }
171:
172: public void state(JFormatter f) {
173: f.g(this ).p(';').nl();
174: }
175:
176: }
|