001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * MethodInfo.java
044: *
045: * Created on April 29, 2004, 1:08 AM
046: */
047:
048: package org.netbeans.modules.visualweb.ejb.datamodel;
049:
050: import java.util.ArrayList;
051: import java.util.Iterator;
052:
053: /**
054: * To encapsulate the information for a method
055: *
056: * @author cao
057: */
058: public class MethodInfo implements java.lang.Cloneable, Comparable {
059: // Is it a business method or create method from home interface
060: private boolean isBusinessMethod = true;
061:
062: // Name of the method
063: private String name;
064:
065: // Some description
066: private String description;
067:
068: // A list of MethodParams (name,type)
069: private ArrayList parameters;
070:
071: // method return
072: private MethodReturn returnType;
073:
074: // A list of exception class names (Strings)
075: private ArrayList exceptions;
076:
077: // Full package data provider class name for this method
078: private String dataprovider;
079:
080: public MethodInfo(String name, String description,
081: ArrayList parameters, MethodReturn returnType,
082: ArrayList exceptions) {
083: this .isBusinessMethod = true;
084: this .name = name;
085: this .description = description;
086: this .parameters = parameters;
087: this .returnType = returnType;
088: this .exceptions = exceptions;
089: }
090:
091: public MethodInfo(boolean buzMethod, String name,
092: String description, ArrayList parameters,
093: MethodReturn returnType, ArrayList exceptions) {
094: this .isBusinessMethod = buzMethod;
095: this .name = name;
096: this .description = description;
097: this .parameters = parameters;
098: this .returnType = returnType;
099: this .exceptions = exceptions;
100: }
101:
102: public MethodInfo() {
103: this (null, null, null, null, null);
104: }
105:
106: public void setName(String name) {
107: this .name = name;
108: }
109:
110: public void setDescription(String description) {
111: this .description = description;
112: }
113:
114: /**
115: * @param parameters a list of MethodParam objects for the method parameters
116: */
117: public void setParameters(ArrayList parameters) {
118: this .parameters = parameters;
119: }
120:
121: public void addParameter(MethodParam parameter) {
122: if (parameters == null)
123: parameters = new ArrayList();
124:
125: parameters.add(parameter);
126: }
127:
128: public boolean isParamNameUnique(String name) {
129: for (int i = 0; i < parameters.size(); i++) {
130: MethodParam p = (MethodParam) parameters.get(i);
131: if (p.getName().equals(name))
132: return false;
133: }
134:
135: return true;
136: }
137:
138: public boolean hasNoParameters() {
139: if (parameters == null || parameters.isEmpty())
140: return true;
141: else
142: return false;
143: }
144:
145: public void setReturnType(MethodReturn returnType) {
146: this .returnType = returnType;
147: }
148:
149: public void addException(String exception) {
150: if (exceptions == null)
151: exceptions = new ArrayList();
152:
153: exceptions.add(exception);
154: }
155:
156: public void setExceptions(ArrayList exceptions) {
157: this .exceptions = exceptions;
158: }
159:
160: public void setIsBusinessMethod(boolean buz) {
161: this .isBusinessMethod = buz;
162: }
163:
164: public void setDataProvider(String dataprovider) {
165: this .dataprovider = dataprovider;
166: }
167:
168: public boolean isMethodConfigurable() {
169: if (!isBusinessMethod())
170: return false;
171:
172: // If the method has parameters and the return type is collection
173: // then this method is considered as configurable because the user can change these things
174: if ((getParameters() != null && !getParameters().isEmpty())
175: || getReturnType().isCollection())
176: return true;
177: else
178: return false;
179: }
180:
181: public boolean isBusinessMethod() {
182: return this .isBusinessMethod;
183: }
184:
185: public String getName() {
186: return this .name;
187: }
188:
189: public String getDescription() {
190: return this .description;
191: }
192:
193: public ArrayList getParameters() {
194: if (parameters == null)
195: return new ArrayList();
196: else
197: return this .parameters;
198: }
199:
200: public String getDataProvider() {
201: return this .dataprovider;
202: }
203:
204: public MethodReturn getReturnType() {
205: return this .returnType;
206: }
207:
208: public ArrayList getExceptions() {
209: return this .exceptions;
210: }
211:
212: public String getParametersAsOneStr() {
213: StringBuffer buf = new StringBuffer();
214:
215: if (parameters != null && !parameters.isEmpty()) {
216: boolean first = true;
217: for (Iterator iter = parameters.iterator(); iter.hasNext();) {
218: MethodParam p = (MethodParam) iter.next();
219:
220: if (first)
221: first = false;
222: else
223: buf.append(", ");
224:
225: buf.append(p.toString());
226: }
227: }
228:
229: return buf.toString();
230: }
231:
232: public Class[] getParameterTypes() {
233: if (parameters == null)
234: return null;
235: else {
236: Class[] types = new Class[parameters.size()];
237: for (int i = 0; i < parameters.size(); i++) {
238: MethodParam p = (MethodParam) parameters.get(i);
239:
240: try {
241: Class type = Class.forName(p.getType());
242: types[i] = type;
243: } catch (Exception e) {
244: types[i] = null;
245: }
246: }
247:
248: return types;
249: }
250: }
251:
252: public String getExceptionsAsOneStr() {
253: StringBuffer buf = new StringBuffer();
254:
255: if (exceptions != null && !exceptions.isEmpty()) {
256: boolean first = true;
257: for (Iterator iter = exceptions.iterator(); iter.hasNext();) {
258: String exception = (String) iter.next();
259:
260: if (first)
261: first = false;
262: else
263: buf.append(", ");
264:
265: buf.append(exception);
266: }
267: }
268:
269: return buf.toString();
270: }
271:
272: public Object clone() {
273: try {
274: MethodInfo methodCopy = (MethodInfo) super .clone();
275:
276: // Parameters
277: if (this .parameters != null) {
278: ArrayList pmCopy = new ArrayList();
279: for (Iterator iter = this .parameters.iterator(); iter
280: .hasNext();) {
281: MethodParam p = (MethodParam) iter.next();
282: pmCopy
283: .add(new MethodParam(p.getName(), p
284: .getType()));
285: }
286:
287: methodCopy.setParameters(pmCopy);
288: }
289:
290: // Exceptions
291: if (this .exceptions != null) {
292: ArrayList exCopy = new ArrayList();
293: for (Iterator iter = this .exceptions.iterator(); iter
294: .hasNext();) {
295: exCopy.add(new String((String) iter.next()));
296: }
297:
298: methodCopy.setExceptions(exCopy);
299: }
300:
301: return methodCopy;
302: } catch (java.lang.CloneNotSupportedException e) {
303: return null;
304: }
305: }
306:
307: public String getSignature() {
308: // NOI18N
309: StringBuffer buf = new StringBuffer();
310: buf.append("public ");
311: buf.append(getReturnType().getClassName());
312: buf.append(" ");
313: buf.append(getName());
314: buf.append("(");
315:
316: if (parameters != null && parameters.size() != 0) {
317: for (int i = 0; i < parameters.size(); i++) {
318: MethodParam p = (MethodParam) parameters.get(i);
319: if (i != 0)
320: buf.append(", ");
321:
322: buf.append(p.getType());
323: }
324: }
325:
326: buf.append(") throws ");
327:
328: // Exception. There is always at least one Exception - RemoteException
329: buf.append(getExceptionsAsOneStr());
330:
331: buf.append(" \n");
332:
333: return buf.toString();
334: }
335:
336: public String toString() {
337: // NOI18N
338: StringBuffer buf = new StringBuffer();
339: buf.append("public ");
340: buf.append(getReturnType().getClassName());
341: buf.append(" ");
342: buf.append(getName());
343: buf.append("(");
344:
345: if (parameters != null && parameters.size() != 0) {
346: for (int i = 0; i < parameters.size(); i++) {
347: MethodParam p = (MethodParam) parameters.get(i);
348: if (i != 0)
349: buf.append(", ");
350:
351: buf.append(p.toString());
352: }
353: }
354:
355: buf.append(") throws ");
356:
357: // Exception. There is always at least one Exception - RemoteException
358: buf.append(getExceptionsAsOneStr());
359:
360: buf.append(" \n");
361:
362: return buf.toString();
363: }
364:
365: // Implementing Comparable
366: public int compareTo(Object o) {
367:
368: if (o == null || !(o instanceof MethodInfo))
369: return 0;
370:
371: String theOtherName1 = ((MethodInfo) o).getName();
372:
373: return this.getName().compareTo(theOtherName1);
374: }
375:
376: }
|