KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > snippet > SnippetMethod


1
2
3 package net.firstpartners.nounit.snippet;
4
5 /**
6  * Title: NoUnit - Identify Classes that are not being unit Tested
7  *
8  * Copyright (C) 2001 Paul Browne , FirstPartners.net
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  * @author Paul Browne
26  * @version 0.7
27  */

28
29 import java.util.HashMap JavaDoc;
30
31 import net.firstpartners.nounit.snippet.xml.IXmlConstants;
32 import net.firstpartners.nounit.snippet.xml.IXmlJdomSource;
33 import net.firstpartners.nounit.snippet.xml.IXmlSource;
34
35 import org.jdom.Element;
36
37 /**
38  * Holds Information about Java Methods
39  */

40 public class SnippetMethod
41 extends AbstractSnippet
42 implements IXmlSource , IXmlJdomSource , IXmlConstants {
43     
44     
45     /**
46      * inner store of the parameters / Objects making up method signature
47      */

48     private HashMap JavaDoc innerParams;
49     
50     /**
51      * inner store of the methods called by this method
52      */

53     private Snippets innerMethodsCalled;
54     
55     /**
56      * Creates new SnippetMethod
57      * @param name of this snippet
58      * @param accessModifier
59      * @param parameters , HashMap with value pairs 1,Integer 2,Integer 3,String
60      * or whatever the method signature is
61      * @param methodsCalled (as Snippets) by this method
62      */

63     public SnippetMethod(String JavaDoc name,
64     String JavaDoc accessModifier,
65     HashMap JavaDoc parameters,
66     Snippets methodsCalled) {
67         
68         super.innerName=name;
69         super.innerAccess=accessModifier;
70         
71         //Check and set the method signature
72
if (parameters==null) {
73             this.innerParams = new HashMap JavaDoc();
74         } else {
75             this.innerParams = parameters;
76         }
77         
78         //Check and set the methods called
79
if (parameters==null) {
80             this.innerMethodsCalled = new Snippets();
81         } else {
82             this.innerMethodsCalled = methodsCalled;
83         }
84     }
85     
86     
87     /**
88      * over-ride Object.toString() to provide more information about this class
89      * @return stringDescription of Class
90      */

91     public String JavaDoc toString(){
92         
93         //Local Variables
94
StringBuffer JavaDoc stringDescription = new StringBuffer JavaDoc();
95         
96         //Build up return string
97
stringDescription.append(super.toString());
98         
99         stringDescription.append("(");
100         
101         //Loop to get method paramters
102
int counter=0;
103         Object JavaDoc tmpObject;
104         
105         while (counter>-1){
106             tmpObject=innerParams.get(String.valueOf(counter));
107             if (tmpObject==null) {
108                 counter=-1;
109             } else {
110                 counter++;
111                 stringDescription.append(tmpObject);
112                 stringDescription.append(":");
113             }
114             
115         }
116         
117         
118         stringDescription.append(")");
119         
120         
121         //return value
122

123         return stringDescription.toString();
124     }
125     
126     
127     /**
128      * Get an XML Representation of this Class (as String of XML)
129      * @return String with the XML description
130      */

131     public String JavaDoc toXml() {
132         
133         return super.toXml(getNodes());
134         
135     }
136     
137     
138     /**
139      * Get an XML Representation of this Class (as Jdom nodes)
140      * @return methodRoot with the XML description
141      */

142     public Element getNodes(){
143         
144         //Local Variables
145
int counter=0;
146         Object JavaDoc tmpObject;
147         String JavaDoc tmpString;
148         Element paramTag = new Element(ELEMENT_PARAM);
149         Element methodRoot = new Element(ELEMENT_METHOD);
150         
151         //Add Name + access info
152
methodRoot.setAttribute(ATTRIBUTE_NAME,super.innerName);
153         methodRoot.setAttribute(ATTRIBUTE_ACCESS,super.innerAccess);
154         
155         //Add Classe names that make up this method signature (under Param Tag)
156
while (counter>-1){
157             tmpObject=innerParams.get(String.valueOf(counter));
158             if (tmpObject==null) {
159                 counter=-1;
160             } else {
161                 counter++;
162                 
163                 //Create Element out of this Object
164
tmpString=(String JavaDoc)tmpObject;
165                 paramTag = new Element(ELEMENT_PARAM);
166                 paramTag.setAttribute(ATTRIBUTE_NAME,tmpString);
167                 
168                 //Now add as child of this method
169
methodRoot.addContent(paramTag);
170                 
171             }
172             
173         }
174         
175         //Now add the called methods to this mehtod root
176
methodRoot = innerMethodsCalled.addNodesTo(methodRoot);
177         
178         return methodRoot;
179     }
180     
181     
182     
183 }
184
Popular Tags