001: package org.andromda.cartridges.meta.metafacades;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: /**
008: * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
009: * @author Chad Brandon
010: * @since 10.12.2003
011: */
012: public class MethodData implements Comparable {
013: private String metafacadeName;
014: private String visibility;
015: private boolean isAbstract;
016: private String name;
017: private String returnTypeName;
018: private String documentation;
019: private final ArrayList arguments = new ArrayList();
020: private final ArrayList exceptions = new ArrayList();
021:
022: public MethodData(String metafacadeName, String visibility,
023: boolean isAbstract, String returnTypeName, String name,
024: String documentation) {
025: this .metafacadeName = metafacadeName;
026: this .visibility = visibility;
027: this .isAbstract = isAbstract;
028: this .name = name;
029: this .returnTypeName = returnTypeName;
030: this .documentation = documentation;
031: }
032:
033: public void addArgument(ArgumentData argument) {
034: arguments.add(argument);
035: }
036:
037: /**
038: * @return
039: */
040: public Collection getArguments() {
041: return arguments;
042: }
043:
044: public void addException(String typeName) {
045: exceptions.add(typeName);
046: }
047:
048: public Collection getExceptions() {
049: return exceptions;
050: }
051:
052: /**
053: * Gets the metafacade name.
054: *
055: * @return the name of the metafacade
056: */
057: public String getMetafacadeName() {
058: return metafacadeName;
059: }
060:
061: /**
062: * Gets the name of the method.
063: *
064: * @return the name.
065: */
066: public String getName() {
067: return name;
068: }
069:
070: /**
071: * Gets the name of the return type for this method.
072: *
073: * @return the return type name.
074: */
075: public String getReturnTypeName() {
076: return returnTypeName;
077: }
078:
079: /**
080: * Builds a string representing a declaration for this method.
081: *
082: * @param suppressAbstractDeclaration optionally suppress the "abstract" modifier
083: * @return String the declaration
084: */
085: public String buildMethodDeclaration(
086: boolean suppressAbstractDeclaration) {
087: String declaration = visibility
088: + " "
089: + ((isAbstract && !suppressAbstractDeclaration) ? "abstract "
090: : "")
091: + ((returnTypeName != null) ? (returnTypeName + " ")
092: : "") + name + "(";
093:
094: for (final Iterator iterator = arguments.iterator(); iterator
095: .hasNext();) {
096: final ArgumentData argument = (ArgumentData) iterator
097: .next();
098: declaration += (argument.getFullyQualifiedTypeName() + " " + argument
099: .getName());
100: if (iterator.hasNext()) {
101: declaration += ", ";
102: }
103: }
104: declaration += ")";
105:
106: if (exceptions.size() > 0) {
107: declaration += " throws ";
108: for (final Iterator iterator = exceptions.iterator(); iterator
109: .hasNext();) {
110: String exception = (String) iterator.next();
111: declaration += exception;
112: if (iterator.hasNext()) {
113: declaration += ", ";
114: }
115: }
116: }
117:
118: return declaration;
119: }
120:
121: /**
122: * Builds a string representing a call to the method.
123: *
124: * @return String how a call would look like
125: */
126: public String buildMethodCall() {
127: String call = getName() + "(";
128:
129: for (final Iterator iterator = arguments.iterator(); iterator
130: .hasNext();) {
131: final ArgumentData argument = (ArgumentData) iterator
132: .next();
133: call += argument.getName();
134: if (iterator.hasNext()) {
135: call += ", ";
136: }
137: }
138: call += ")";
139: return call;
140: }
141:
142: /**
143: * Builds a signature which can be used as a key into a map. Consists of the return type, the name and the f.q.
144: * types of the arguements.
145: *
146: * @return String the key that identifies this method
147: */
148: public String buildCharacteristicKey() {
149: String key = ((returnTypeName != null) ? (returnTypeName + " ")
150: : "")
151: + name + "(";
152:
153: for (final Iterator iterator = arguments.iterator(); iterator
154: .hasNext();) {
155: final ArgumentData argument = (ArgumentData) iterator
156: .next();
157: key += argument.getFullyQualifiedTypeName();
158: if (iterator.hasNext()) {
159: key += ",";
160: }
161: }
162: key += ")";
163:
164: return key;
165: }
166:
167: /**
168: * Indicates whether or not this method is abstract.
169: *
170: * @return true/false
171: */
172: public boolean isAbstract() {
173: return isAbstract;
174: }
175:
176: /**
177: * Gets the visibility of this method.
178: *
179: * @return the visibility.
180: */
181: public String getVisibility() {
182: return visibility;
183: }
184:
185: /**
186: * Gets the documentation for this method.
187: *
188: * @return the documentation.
189: */
190: public String getDocumentation() {
191: return documentation;
192: }
193:
194: /**
195: * Tells if this method returns something.
196: *
197: * @return boolean
198: */
199: public boolean isReturnTypePresent() {
200: return returnTypeName != null && !returnTypeName.equals("void");
201: }
202:
203: /**
204: * @see java.lang.Comparable#compareTo(java.lang.Object)
205: */
206: public int compareTo(final Object object) {
207: MethodData other = (MethodData) object;
208: int result = getMetafacadeName().compareTo(
209: other.getMetafacadeName());
210: return (result != 0) ? result : getName().compareTo(
211: other.getName());
212: }
213: }
|