001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx.compiler;
006:
007: /** Represents a method in a Java source file being generated for output. <code>JavaMethods</code> are created
008: * and added to a {@link JavaFile}, which can then output Java source code. In addition to normal methods, a
009: * <code>JavaMethod</code> can represent a constructor -- constructors should be named after their containing
010: * classes and have a return type of <code>null</code>.
011: */
012: public class JavaMethod {
013: private int modifiers;
014: private String returnType;
015: private String name;
016: private JavaArgument[] arguments;
017: private String[] exceptions;
018: private StringBuffer bodyCode;
019:
020: /** Constructs a new no-argument <code>JavaMethod</code> which throws no checked exceptions. The
021: * <code>modifiers</code> parameter is a bit mask of the constants from {@link java.lang.reflect.Modifier},
022: * and the <code>returnType</code> of the method should be represented as it would appear in Java source
023: * code (<code>null</code> for a constructor). The method body is initially empty.
024: *
025: *@param modifiers the modifier keywords that should appear as part of the method's declaration
026: *@param returnType the return type of the method as it would appear in Java source code
027: *@param name the method's name
028: *@see #appendBodyCode
029: */
030: public JavaMethod(int modifiers, String returnType, String name) {
031: this (modifiers, returnType, name, null);
032: }
033:
034: /** Constructs a new <code>JavaMethod</code> which throws no checked exceptions. The <code>modifiers</code>
035: * parameter is a bit mask of the constants from {@link java.lang.reflect.Modifier}, and the
036: * <code>returnType</code> of the method should be represented as it would appear in Java source code
037: * (<code>null</code> for a constructor). The method body is initially empty.
038: *
039: *@param modifiers the modifier keywords that should appear as part of the method's declaration
040: *@param returnType the return type of the method as it would appear in Java source code
041: *@param name the method's name
042: *@param arguments the method's arguments
043: *@see #appendBodyCode
044: */
045: public JavaMethod(int modifiers, String returnType, String name,
046: JavaArgument[] arguments) {
047: this (modifiers, returnType, name, arguments, null);
048: }
049:
050: /** Constructs a new <code>JavaMethod</code>. The <code>modifiers</code> parameter is a bit mask of the
051: * constants from {@link java.lang.reflect.Modifier}, and the <code>returnType</code> and <code>exceptions</code>
052: * of the method should be represented as they would appear in Java source code (<code>null</code> for a
053: * constructor). The method body is initially empty.
054: *
055: *@param modifiers the modifier keywords that should appear as part of the method's declaration
056: *@param returnType the return type of the method as it would appear in Java source code
057: *@param name the method's name
058: *@param arguments the method's arguments
059: *@param exceptions a list of exceptions the methods can throw, as they would be represented in Java source code
060: *@see #appendBodyCode
061: */
062: public JavaMethod(int modifiers, String returnType, String name,
063: JavaArgument[] arguments, String[] exceptions) {
064: this (modifiers, returnType, name, arguments, exceptions, null);
065: }
066:
067: /** Constructs a new <code>JavaMethod</code> containing the specified body code. The <code>modifiers</code> parameter
068: * is a bit mask of the constants from {@link java.lang.reflect.Modifier}, and the <code>returnType</code> and
069: * <code>exceptions</code> of the method should be represented as they would appear in Java source code (<code>null</code>
070: * for a constructor). The method body is initially empty.
071: *
072: *@param modifiers the modifier keywords that should appear as part of the method's declaration
073: *@param returnType the return type of the method as it would appear in Java source code
074: *@param name the method's name
075: *@param arguments the method's arguments
076: *@param exceptions a list of exceptions the methods can throw, as they would be represented in Java source code
077: *@param bodyCode Java source code which should appear in the method body
078: */
079: public JavaMethod(int modifiers, String returnType, String name,
080: JavaArgument[] arguments, String[] exceptions,
081: String bodyCode) {
082: this .modifiers = modifiers;
083: this .returnType = returnType;
084: this .name = name;
085: this .arguments = arguments;
086: this .exceptions = exceptions;
087: this .bodyCode = new StringBuffer(bodyCode != null ? bodyCode
088: : "");
089: }
090:
091: /** Returns a bit mask describing the modifier keywords which should appear as part of this method's
092: * declaration. See <code>java.lang.reflect.Modifier</code> for more information on decoding this
093: * field.
094: *
095: *@return the modifier bit mask
096: */
097: public int getModifiers() {
098: return modifiers;
099: }
100:
101: /** Returns the method's return type, as it would be represented in Java source code.
102: *
103: *@return the method's return type
104: */
105: public String getReturnType() {
106: return returnType;
107: }
108:
109: /** Returns the method's name.
110: *
111: *@return the method's name
112: */
113: public String getName() {
114: return name;
115: }
116:
117: /** Returns a list of the method's arguments.
118: *
119: *@return the method's arguments
120: */
121: public JavaArgument[] getArguments() {
122: return arguments;
123: }
124:
125: /** Returns a list of exceptions the method can throw.
126: *
127: *@return the method's exceptions
128: */
129: public String[] getExceptions() {
130: return exceptions;
131: }
132:
133: /** Returns the Java source code for the method's body.
134: *
135: *@return the method's body code
136: */
137: public String getBodyCode() {
138: return bodyCode.toString();
139: }
140:
141: /** Appends additional code to the method's body.
142: *
143: *@param extraCode Java source code to append to the method's body
144: */
145: public void appendBodyCode(String extraCode) {
146: if (extraCode.length() == 0)
147: return;
148: if (bodyCode.length() > 0
149: && !bodyCode.toString().endsWith(
150: JAXXCompiler.getLineSeparator()))
151: bodyCode.append(JAXXCompiler.getLineSeparator());
152: bodyCode.append(extraCode);
153: }
154:
155: /** Returns the Java source code for this method.
156: *
157: *@return the Java source code for this method
158: */
159: public String toString() {
160: StringBuffer result = new StringBuffer();
161: result.append(JavaFile.getModifiersText(modifiers));
162: if (returnType != null) {
163: result.append(returnType);
164: result.append(' ');
165: }
166: result.append(name);
167: result.append('(');
168: if (arguments != null) {
169: for (int i = 0; i < arguments.length; i++) {
170: if (i > 0)
171: result.append(", ");
172: result.append(arguments[i].toString());
173: }
174: }
175: result.append(") {");
176: result.append(JAXXCompiler.getLineSeparator());
177: if (bodyCode != null) {
178: String formattedBodyCode = JavaFile.addIndentation(bodyCode
179: .toString().trim(), 4);
180: if (formattedBodyCode.length() > 0) {
181: result.append(formattedBodyCode);
182: result.append(JAXXCompiler.getLineSeparator());
183: }
184: }
185: result.append("}");
186: return result.toString();
187: }
188: }
|