|
| 1 | +import java.io.*; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.HashSet; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Jiyoung Song |
| 7 | + * This class generates .java files that getnerate sliced bytecode |
| 8 | + */ |
| 9 | +public class SlicedBytecodeGen { |
| 10 | + |
| 11 | + /** |
| 12 | + * @param slicedFile - the name of sliced file generated from hammacher's javaslicer |
| 13 | + * @return slicedLinesInFile - hashmap that contains sliced line numbers for every class and method |
| 14 | + * @throws IOException |
| 15 | + */ |
| 16 | + public HashMap<String, HashSet<Integer>> getSlicedLinesPerFile (String slicedFile) throws IOException{ |
| 17 | + HashMap<String, HashSet<Integer>> slicedLinesInFile = new HashMap<String, HashSet<Integer>>(); |
| 18 | + BufferedReader bufferedReader = new BufferedReader(new FileReader(new File((slicedFile+".txt")))); |
| 19 | + String line = ""; |
| 20 | + while ((line = bufferedReader.readLine()) != null) { |
| 21 | + String[] classNameAndLineNum = line.split(":"); |
| 22 | + String[] lineNumAndInst = classNameAndLineNum[1].split(" "); |
| 23 | + if (slicedLinesInFile.containsKey(classNameAndLineNum[0])) { |
| 24 | + HashSet<Integer> lineNumbers = slicedLinesInFile.get(classNameAndLineNum[0]); |
| 25 | + lineNumbers.add(Integer.parseInt(lineNumAndInst[0])); |
| 26 | + } else { |
| 27 | + HashSet<Integer> lineNumbers = new HashSet<Integer>(); |
| 28 | + lineNumbers.add(Integer.parseUnsignedInt(lineNumAndInst[0])); |
| 29 | + slicedLinesInFile.put(classNameAndLineNum[0],lineNumbers); |
| 30 | + } |
| 31 | + } |
| 32 | + return slicedLinesInFile; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * |
| 37 | + * @param slicedFile |
| 38 | + * @param subjectFiles - the name list of files that is target of slicing |
| 39 | + * @throws IOException |
| 40 | + */ |
| 41 | + public void genSlicedClassFileGenerator (String slicedFile, String[] subjectFiles) throws IOException { |
| 42 | + for(String subjectFile: subjectFiles) { |
| 43 | + PrintWriter printWriter = new PrintWriter(new File((subjectFile+".java"))); |
| 44 | + printWriter.println("import java.io.FileOutputStream;"); |
| 45 | + BufferedReader bufferedReader = new BufferedReader(new FileReader(new File((subjectFile+".txt")))); |
| 46 | + boolean isSliced = true; |
| 47 | + String line = ""; |
| 48 | + while ((line = bufferedReader.readLine()) != null) { |
| 49 | + if (line.contains("public static byte[] dump () throws Exception {")) { |
| 50 | + printWriter.println("public static void main(final String args[]) throws Exception {"); |
| 51 | + printWriter.println("FileOutputStream fos = new FileOutputStream(\"SlicingExample.class\");"); |
| 52 | + printWriter.println("fos.write(dump());"); |
| 53 | + printWriter.println("fos.close();"); |
| 54 | + printWriter.println("}"); |
| 55 | + isSliced = true; |
| 56 | + } else if (line.contains("mv.visitLineNumber")) { |
| 57 | + |
| 58 | + } |
| 59 | + if (isSliced) |
| 60 | + printWriter.println(line); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments