001: /*
002: * MethodInfo.java - part of the CodeAid plugin.
003: * Copyright (C) 1999 Jason Ginchereau
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.acm.seguin.completer.info;
021:
022: import java.lang.reflect.Method;
023:
024: public final class MethodInfo extends MemberInfo {
025: private String returnType;
026: private String[] parameterTypes;
027: private String[] parameterNames;
028: private String[] exceptionTypes;
029:
030: public MethodInfo(Method m) {
031: super (m);
032: returnType = m.getReturnType().getName();
033: Class[] pt = m.getParameterTypes();
034: parameterTypes = new String[pt.length];
035: for (int i = 0; i < pt.length; i++) {
036: parameterTypes[i] = pt[i].getName();
037: }
038: parameterNames = null;
039: Class[] et = m.getExceptionTypes();
040: exceptionTypes = new String[et.length];
041: for (int i = 0; i < et.length; i++) {
042: exceptionTypes[i] = et[i].getName();
043: }
044: }
045:
046: public MethodInfo(String declaringClass, int modifiers,
047: String returnType, String name, String[] parameterTypes,
048: String[] parameterNames, String[] exceptionTypes,
049: String comment) {
050: super (declaringClass, modifiers, name, comment);
051: this .returnType = returnType;
052: this .parameterTypes = parameterTypes;
053: this .parameterNames = parameterNames;
054: if (exceptionTypes == null) {
055: exceptionTypes = new String[] {};
056: }
057: this .exceptionTypes = exceptionTypes;
058: }
059:
060: public String getType() {
061: return getReturnType();
062: }
063:
064: public String getReturnType() {
065: return returnType;
066: }
067:
068: public String[] getParameterTypes() {
069: return parameterTypes;
070: }
071:
072: public String[] getParameterNames() {
073: return parameterNames;
074: }
075:
076: public String[] getExceptionTypes() {
077: return exceptionTypes;
078: }
079:
080: public String getLine() {
081: return modifiersToString(getModifiers())
082: + getReturnType()
083: + " "
084: + getName()
085: + parametersToString(parameterTypes, parameterNames,
086: exceptionTypes);
087: }
088:
089: static String parametersToString(Class[] parameterTypes,
090: String[] parameterNames, Class[] exceptionTypes) {
091: String[] pt = new String[parameterTypes.length];
092: for (int i = 0; i < parameterTypes.length; i++) {
093: pt[i] = parameterTypes[i].getName();
094: }
095: String[] et = new String[exceptionTypes.length];
096: for (int i = 0; i < exceptionTypes.length; i++) {
097: et[i] = exceptionTypes[i].getName();
098: }
099: return parametersToString(pt, parameterNames, et);
100: }
101:
102: static String parametersToString(String[] parameterTypes,
103: String[] parameterNames, String[] exceptionTypes) {
104: String p = "(";
105: for (int i = 0; i < parameterTypes.length; i++) {
106: p += parameterTypes[i];
107: if (parameterNames != null) {
108: p += " " + parameterNames[i];
109: }
110: if (i != parameterTypes.length - 1) {
111: p += ", ";
112: }
113: }
114: p += ")";
115: if (exceptionTypes.length > 0) {
116: p += " throws ";
117: for (int i = 0; i < exceptionTypes.length; i++) {
118: p += exceptionTypes[i];
119: if (i != exceptionTypes.length - 1) {
120: p += ", ";
121: }
122: }
123: }
124:
125: return p;
126: }
127:
128: public int compareTo(MemberInfo mi) {
129: int nc = getName().compareTo(mi.getName());
130: if (nc != 0) {
131: return nc;
132: }
133: if (mi instanceof MethodInfo) {
134: int rc = returnType.compareTo(((MethodInfo) mi).returnType);
135: if (rc != 0) {
136: return rc;
137: }
138: String[] opt = ((MethodInfo) mi).parameterTypes;
139: if (parameterTypes.length < opt.length) {
140: return -1;
141: } else if (parameterTypes.length > opt.length) {
142: return 1;
143: } else {
144: for (int i = 0; i < parameterTypes.length; i++) {
145: int pc = parameterTypes[i].compareTo(opt[i]);
146: if (pc != 0) {
147: return pc;
148: }
149: }
150: }
151: return 0;
152: } else {
153: return 1;
154: }
155: }
156:
157: public String toString() {
158: return getName() + "()";
159: }
160: }
|