1 2 package net.firstpartners.nounit.reader.bytecode; 3 4 27 28 import java.io.IOException ; 29 import java.text.StringCharacterIterator ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 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 46 public class ByteCodeMethodSnippetFactory 47 extends AbstractByteCodeSnippetFactory 48 implements ISnippetFactory { 49 50 53 private ClassFile innerClassFile; 54 55 59 public ByteCodeMethodSnippetFactory(ClassFile sourceClassFile){ 60 innerClassFile = sourceClassFile; 61 } 62 63 67 public Snippets getSnippets() throws NoUnitException 68 { 69 70 Snippets snippetSet = new Snippets(); 72 HashSet methodSet = new HashSet (); 73 74 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 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 94 private HashSet addMethods(HashSet c , ClassFile infoSource) 95 throws NoUnitException , InvalidByteCodeException,IOException { 96 97 int thisDescriptorInPool; 99 String thisName; 100 String thisAccess; 101 SnippetMethod thisMethod; 102 MethodInfo[] methodArray ; 103 CPInfo[] classPieces; 104 HashMap parameterInfo; 105 106 Snippets calls; ByteCodeCallsSnippetFactory myCallsFactory; 109 110 if (infoSource==null) {return c;} 112 113 methodArray = infoSource.getMethods(); 115 classPieces= infoSource.getConstantPool(); 116 117 118 for (int a=0;a<methodArray.length;a++) { 120 121 thisName=methodArray[a].getName(); 123 thisAccess=methodArray[a].getAccessFlagsVerbose(); 124 125 thisDescriptorInPool= methodArray[a].getDescriptorIndex(); 127 parameterInfo = getParameterInfo(thisDescriptorInPool, infoSource); 128 129 myCallsFactory = new ByteCodeCallsSnippetFactory(methodArray[a],infoSource); 131 calls = myCallsFactory.getSnippets(); 132 133 thisName = super.tidyNames(thisName); 135 136 thisMethod = new SnippetMethod(thisName, 138 thisAccess, 139 parameterInfo, 140 calls); 141 c.add(thisMethod); 142 } 143 144 return c; 145 } 146 147 156 private HashMap getParameterInfo(int constantPoolRef,ClassFile infoSource) 157 throws InvalidByteCodeException { 158 159 161 String thisParams = infoSource.getConstantPoolEntryName(constantPoolRef); 162 HashMap parameters = splitTagVerboseIntoHashMap(thisParams); 163 164 return parameters; 165 166 } 167 168 173 public HashMap splitTagVerboseIntoHashMap(String inTagVerbose){ 174 175 int place=0; 177 char thisLetter; 178 StringBuffer tmpBuffer = new StringBuffer (); 179 StringCharacterIterator loopString = new StringCharacterIterator (inTagVerbose); 180 HashMap splitValues = new HashMap (); 181 182 while (loopString.getIndex()<loopString.getEndIndex()) { 184 185 thisLetter = loopString.next(); 186 187 if (thisLetter==';') { 189 190 splitValues.put(String.valueOf(place), 191 tmpBuffer.toString()); 192 tmpBuffer=new StringBuffer (); 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 |