001: /*
002: * Author: Mike Atkinson
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.pretty.jdi;
010:
011: import java.text.MessageFormat;
012: import net.sourceforge.jrefactory.ast.ASTConstructorDeclaration;
013: import net.sourceforge.jrefactory.ast.ASTFormalParameters;
014: import net.sourceforge.jrefactory.ast.ASTFormalParameter;
015: import net.sourceforge.jrefactory.ast.ASTAnnotation;
016: import net.sourceforge.jrefactory.ast.ASTType;
017: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
018: import net.sourceforge.jrefactory.ast.ASTClassBodyDeclaration;
019: import net.sourceforge.jrefactory.ast.ASTVariableDeclaratorId;
020: import net.sourceforge.jrefactory.ast.ASTNameList;
021: import net.sourceforge.jrefactory.ast.ASTName;
022: import net.sourceforge.jrefactory.ast.ASTClassBody;
023: import net.sourceforge.jrefactory.ast.ASTReferenceType;
024: import net.sourceforge.jrefactory.ast.SimpleNode;
025: import net.sourceforge.jrefactory.ast.Node;
026: import org.acm.seguin.pretty.ForceJavadocComments;
027: import org.acm.seguin.pretty.JavadocTags;
028: import org.acm.seguin.pretty.PrintData;
029: import org.acm.seguin.pretty.ai.RequiredTags;
030: import org.acm.seguin.util.FileSettings;
031:
032: /**
033: * Description of the Class
034: *
035: * @author Mike Atkinson
036: * @since jRefactory 2.9.0, created October 16, 2003
037: */
038: public class ConstructorDeclaration extends BaseJDI {
039: private ASTConstructorDeclaration constructor;
040:
041: /**
042: * Constructor for the ConstructorDeclaration JavaDoc creator.
043: *
044: * @param constructor Create JavaDoc for this node.
045: */
046: public ConstructorDeclaration(ASTConstructorDeclaration constructor) {
047: super ();
048: this .constructor = constructor;
049: }
050:
051: /**
052: * Checks to see if it was printed
053: *
054: * @return true if it still needs to be printed
055: */
056: public boolean isRequired() {
057: return jdi.isRequired()
058: && (new ForceJavadocComments()).isJavaDocRequired(
059: "method", constructor);
060: }
061:
062: /**
063: * Prints all the java doc components
064: *
065: * @param printData the print data
066: */
067: public void printJavaDocComponents(PrintData printData) {
068: jdi.printJavaDocComponents(printData, bundle
069: .getString("method.tags"));
070: }
071:
072: /**
073: * Makes sure all the java doc components are present. For methods and constructors we need to do more work -
074: * checking parameters, return types, and exceptions.
075: */
076: public void finish() {
077: // Local Variables
078: int ndx;
079: int childCount;
080:
081: // Get the tags
082: JavadocTags tags = JavadocTags.get();
083:
084: // Description of the constructor
085: Object[] nameArray = new Object[1];
086: nameArray[0] = constructor.getName();
087: String descr = tags.getConstructorDescr();
088: if (isCopyConstructor()) {
089: descr = bundle
090: .getProperty(
091: "copyconstructor.descr",
092: "Copy constructor for the {0} object. Creates a copy of the {0} object parameter.");
093: }
094: String msg = MessageFormat.format(descr, nameArray);
095: jdi.require("", msg);
096:
097: // Check for parameters
098: int childNo = constructor.skipAnnotations();
099: ASTFormalParameters params = (ASTFormalParameters) constructor
100: .jjtGetChild(childNo);
101: childCount = params.jjtGetNumChildren();
102: String[] constructorParams = new String[childCount];
103: for (ndx = 0; ndx < childCount; ndx++) {
104: ASTFormalParameter nextParam = (ASTFormalParameter) params
105: .jjtGetChild(ndx);
106: childNo = nextParam.skipAnnotations();
107: ASTVariableDeclaratorId varID = (ASTVariableDeclaratorId) nextParam
108: .jjtGetChild(childNo + 1);
109: jdi.require("@param", varID.getName(),
110: getParameterDescription(bundle, tags, varID
111: .getName(), "class"));
112:
113: constructorParams[ndx] = varID.getName();
114: }
115: // sort @param tags into order of constructor parameters
116: jdi.sort("@param", constructorParams);
117:
118: // Check for exceptions
119: if ((constructor.jjtGetNumChildren() > childNo + 1)
120: && (constructor.jjtGetChild(childNo + 1) instanceof ASTNameList)) {
121: ASTNameList exceptions = (ASTNameList) constructor
122: .jjtGetChild(childNo + 1);
123: childCount = exceptions.jjtGetNumChildren();
124: for (ndx = 0; ndx < childCount; ndx++) {
125: ASTName name = (ASTName) exceptions.jjtGetChild(ndx);
126: if (!(jdi.contains("@exception", name.getName())
127: || jdi.contains("@throws", name.getName()) || jdi
128: .contains(tags.getExceptionTag(), name
129: .getName()))) {
130: jdi.require(tags.getExceptionTag(), name.getName(),
131: tags.getExceptionDescr());
132: }
133: }
134: }
135:
136: RequiredTags.getTagger().addTags(bundle, "method",
137: constructor.getName(), jdi);
138: }
139:
140: /**
141: * Gets the parameterDescription attribute of the ASTConstructorDeclaration node.
142: *
143: * @param bundle Description of Parameter
144: * @param tags Description of Parameter
145: * @param param Description of Parameter
146: * @param className Description of Parameter
147: * @return The parameterDescription value
148: */
149: private String getParameterDescription(FileSettings bundle,
150: JavadocTags tags, String param, String className) {
151: String pattern = "";
152: Object[] nameArray = new Object[6];
153: nameArray[0] = param;
154: nameArray[1] = className;
155:
156: if (isCopyConstructor()) {
157: pattern = bundle.getProperty("copyconstructor.param.descr",
158: "Object to copy.");
159: } else {
160: pattern = tags.getParamDescr();
161: }
162:
163: if (pattern == null || pattern.equals("")) {
164: return null;
165: }
166:
167: return MessageFormat.format(pattern, nameArray);
168: }
169:
170: /**
171: * Gets the copyConstructor attribute of the ASTConstructorDeclaration node.
172: *
173: * @return The copyConstructor value
174: */
175: private boolean isCopyConstructor() {
176: for (int childNo1 = 0; childNo1 < constructor
177: .jjtGetNumChildren(); childNo1++) {
178: Node child = constructor.jjtGetChild(childNo1);
179: if (child instanceof ASTFormalParameters) {
180: ASTFormalParameters params = (ASTFormalParameters) child;
181: if (params.jjtGetNumChildren() == 1) {
182: ASTFormalParameter param = (ASTFormalParameter) params
183: .jjtGetChild(0);
184: int childNo = param.skipAnnotations();
185: ASTType type = (ASTType) param.jjtGetChild(childNo);
186: if (type.jjtGetChild(0) instanceof ASTReferenceType) {
187: ASTReferenceType refType = (ASTReferenceType) type
188: .jjtGetChild(0);
189: if (refType.getArrayCount() == 0) {
190: ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) refType
191: .jjtGetChild(0);
192: //System.out.println("isCopyConstructor refType name=" + refType.getName());
193: //System.out.println("isCopyConstructor type name=" + clazz.getName());
194: ASTClassBodyDeclaration classBodyDec = (ASTClassBodyDeclaration) constructor
195: .jjtGetParent();
196: if (classBodyDec.jjtGetParent() instanceof ASTClassBody) {
197: ASTClassBody classBody = (ASTClassBody) classBodyDec
198: .jjtGetParent();
199: //System.out.println("isCopyConstructor clazz name="+ clazz.getName() + ", compare with name="+ ((SimpleNode)classBody.jjtGetParent()).getName());
200: boolean flag = (clazz.getName()
201: .equals(((SimpleNode) classBody
202: .jjtGetParent())
203: .getName()));
204: //System.out.println(" -> "+flag);
205: return flag;
206: } else {
207: return false;
208: }
209: } else {
210: return false;
211: }
212: } else {
213: return false;
214: }
215: } else {
216: return false;
217: }
218: }
219: }
220: return false;
221: }
222:
223: }
|