KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > reader > bytecode > ByteCodeMethodSnippetFactory


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

27
28 import java.io.IOException JavaDoc;
29 import java.text.StringCharacterIterator JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32
33 import net.firstpartners.nounit.reader.ISnippetFactory;
34 import net.firstpartners.nounit.snippet.SnippetMethod;
35 import net.firstpartners.nounit.snippet.Snippets;
36 import net.firstpartners.nounit.utility.NoUnitException;
37
38 import org.gjt.jclasslib.structures.CPInfo;
39 import org.gjt.jclasslib.structures.ClassFile;
40 import org.gjt.jclasslib.structures.InvalidByteCodeException;
41 import org.gjt.jclasslib.structures.MethodInfo;
42
43 /**
44  * Reads Class Files (Byte Code) and returns them as snippets
45  */

46 public class ByteCodeMethodSnippetFactory
47     extends AbstractByteCodeSnippetFactory
48     implements ISnippetFactory {
49     
50     /**
51      * Inner Store of the classfile being read
52      */

53     private ClassFile innerClassFile;
54         
55     /**
56      * Constructor - Get (and store) source class file
57      * @param sourceClassFile - Compiled Byte Code to read
58      */

59     public ByteCodeMethodSnippetFactory(ClassFile sourceClassFile){
60         innerClassFile = sourceClassFile;
61     }
62     
63     /**
64      * Get a set of snippets from the current class source
65     * @return snippetSet Collection of ISnippets describing the file
66      */

67     public Snippets getSnippets() throws NoUnitException
68     {
69         
70         //Local Variables
71
Snippets snippetSet = new Snippets();
72         HashSet JavaDoc methodSet = new HashSet JavaDoc();
73        
74         //Add Information
75
try {
76             methodSet= addMethods(methodSet,innerClassFile);
77         } catch (InvalidByteCodeException ibce) {
78             throw new NoUnitException(ibce,"Could not read from Source class file");
79        } catch (IOException JavaDoc ie) {
80             throw new NoUnitException(ie,"Could not open Source class file");
81         }
82         snippetSet.add(methodSet);
83         
84         return snippetSet;
85         
86     }
87     
88     /**
89      * Add the Information about methods to the collection
90      * @param c - Hashset to update
91      * @param infoSource - ClassFile
92      * @return c , updated Hashset
93      */

94     private HashSet JavaDoc addMethods(HashSet JavaDoc c , ClassFile infoSource)
95         throws NoUnitException , InvalidByteCodeException,IOException JavaDoc {
96         
97         //local variables
98
int thisDescriptorInPool;
99         String JavaDoc thisName;
100         String JavaDoc thisAccess;
101         SnippetMethod thisMethod;
102         MethodInfo[] methodArray ;
103         CPInfo[] classPieces;
104         HashMap JavaDoc parameterInfo;
105         
106         //For working out what methods this one calls ...
107
Snippets calls; //info on what this method calls
108
ByteCodeCallsSnippetFactory myCallsFactory;
109         
110         //Check for null
111
if (infoSource==null) {return c;}
112         
113         //Get the source information
114
methodArray = infoSource.getMethods();
115         classPieces= infoSource.getConstantPool();
116         
117         
118         // Loop through and add Method Snippets to Hashset
119
for (int a=0;a<methodArray.length;a++) {
120             
121             //Get the information about this method
122
thisName=methodArray[a].getName();
123            thisAccess=methodArray[a].getAccessFlagsVerbose();
124            
125                 //Parameter Info
126
thisDescriptorInPool= methodArray[a].getDescriptorIndex();
127                 parameterInfo = getParameterInfo(thisDescriptorInPool, infoSource);
128            
129                 //Calls (other Methods) info
130
myCallsFactory = new ByteCodeCallsSnippetFactory(methodArray[a],infoSource);
131                 calls = myCallsFactory.getSnippets();
132                 
133            //Clean Name
134
thisName = super.tidyNames(thisName);
135                 
136            //Build and add this file to the class
137
thisMethod = new SnippetMethod(thisName,
138                                             thisAccess,
139                                             parameterInfo,
140                                             calls);
141            c.add(thisMethod);
142         }
143         
144         return c;
145     }
146     
147     /**
148      * getsParameter info (as HashMap) given set of ClassPieces and pointer
149      * to one we'return intersted in.
150      * @param constantPoolRef - pointer to Constant pool entry we want to read
151      * @param infoSource , ClassFile reference
152      * @return parameters as HashMap of value pairs
153      * e.g. 1,String 2,Integer 3,String etc
154      *
155      */

156     private HashMap JavaDoc getParameterInfo(int constantPoolRef,ClassFile infoSource)
157         throws InvalidByteCodeException {
158      
159         //Internal Variables
160

161         String JavaDoc thisParams = infoSource.getConstantPoolEntryName(constantPoolRef);
162         HashMap JavaDoc parameters = splitTagVerboseIntoHashMap(thisParams);
163         
164         return parameters;
165         
166     }
167     
168     /**
169      * Splits a String (from Tag info) into paramters (a hashmap)
170      * @param inTagVerbose
171      * @return splitValues
172      */

173     public HashMap JavaDoc splitTagVerboseIntoHashMap(String JavaDoc inTagVerbose){
174         
175         //Local Variables
176
int place=0;
177         char thisLetter;
178         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
179         StringCharacterIterator JavaDoc loopString = new StringCharacterIterator JavaDoc(inTagVerbose);
180         HashMap JavaDoc splitValues = new HashMap JavaDoc();
181         
182         //Loop and Split
183
while (loopString.getIndex()<loopString.getEndIndex()) {
184             
185             thisLetter = loopString.next();
186             
187             //Add or split?
188
if (thisLetter==';') {
189                 
190                 splitValues.put(String.valueOf(place),
191                                                 tmpBuffer.toString());
192                 tmpBuffer=new StringBuffer JavaDoc();
193                 place++;
194                 
195             } else {
196                 tmpBuffer.append(thisLetter);
197             }
198             
199             
200         }
201         splitValues = cleanValues(splitValues);
202         
203         return splitValues;
204     }
205     
206
207 }
208
Popular Tags