Skip to content

Commit b48f284

Browse files
committed
improve utils
1 parent cdd142e commit b48f284

File tree

3 files changed

+18
-80
lines changed

3 files changed

+18
-80
lines changed

lib/utils/DiffUtil.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package javaToolkit.lib.utils;
22

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.ArrayList;
5+
import java.util.HashMap;
66
import java.util.List;
7+
import java.util.Map;
78

89
public class DiffUtil {
910

@@ -84,25 +85,20 @@ public static List<Integer> getChangedLineNumListInNewVersion(String diffStr) {
8485
return changedLineNumList;
8586
}
8687

87-
public static List<String> getModifiedFileList(String diffFilePathString) {
88-
/**
89-
* can to be further improved in future
90-
*/
91-
Path diffFilePath = Paths.get(diffFilePathString);
92-
List<String> modifiedFileRelPathList = new ArrayList<String>();
88+
public static List<Map<String, String>> getModifiedFileList(Path diffFilePath) {
89+
List<Map<String, String>> modifiedFileRelPathList = new ArrayList<Map<String, String>>();
9390
for (String line : FileUtil.readFileToLineList(diffFilePath)) {
9491
if (line.startsWith("diff --git ")) {
9592
// Heuristic
96-
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
97-
modifiedFileRelPathList.add(fileRelPath);
93+
String oldFileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
94+
String newFileRelPath = line.replace("diff --git ", "").split(" ")[1].replaceFirst("b/", "").trim();
95+
Map<String, String> oldNnewMap = new HashMap<String, String>();
96+
oldNnewMap.put("old", oldFileRelPath);
97+
oldNnewMap.put("new", newFileRelPath);
98+
modifiedFileRelPathList.add(oldNnewMap);
9899
}
99100
}
100101
return modifiedFileRelPathList;
101102
}
102103

103-
public static void main(String[] args) {
104-
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
105-
getModifiedFileList(diffFilePath);
106-
}
107-
108104
}

lib/utils/FileUtil.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import java.io.BufferedReader;
44
import java.io.File;
5-
import java.io.FileInputStream;
65
import java.io.FileNotFoundException;
76
import java.io.FileReader;
87
import java.io.FilenameFilter;
98
import java.io.IOException;
10-
import java.io.InputStreamReader;
119
import java.nio.file.Files;
1210
import java.nio.file.Path;
1311
import java.nio.file.Paths;
@@ -173,36 +171,6 @@ public static void copyDirectory(Path srcDirPath, Path destDirPath) {
173171
}
174172
}
175173

176-
public static String readStringFromFile(String filePath) {
177-
try {
178-
File file = new File(filePath);
179-
if (file.isFile() && file.exists()) {
180-
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
181-
BufferedReader bufferedReader = new BufferedReader(read);
182-
StringBuilder stringBuilder = new StringBuilder();
183-
String line = null;
184-
String ls = System.getProperty("line.separator");
185-
while ((line = bufferedReader.readLine()) != null) {
186-
stringBuilder.append(line);
187-
stringBuilder.append(ls);
188-
}
189-
190-
// delete the last new line separator
191-
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
192-
bufferedReader.close();
193-
194-
String content = stringBuilder.toString();
195-
return content;
196-
} else {
197-
System.out.printf("File not found! %s\n", filePath);
198-
}
199-
} catch (Exception e) {
200-
System.out.println("Read file error!");
201-
e.printStackTrace();
202-
}
203-
return null;
204-
}
205-
206174
/**
207175
* there is no \newline at the end of each line
208176
*

lib/utils/SRCUtil.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,30 @@
11
package javaToolkit.lib.utils;
22

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.List;
65

76
public class SRCUtil {
87

9-
public static void changePackage(String oriSRCPath, String dstSRCPath, String newPackName) {
10-
List<String> srcStrList = FileUtil.readFileToLineList(Paths.get(oriSRCPath));
8+
public static String changePackage(Path SRCPath, String newPackName) {
9+
List<String> srcStrList = FileUtil.readFileToLineList(SRCPath);
1110

1211
Boolean ifchanged = false;
13-
String wrtStr = "package " + newPackName + ";";
12+
String wrtStr = "";
1413
for (int i = 0; i < srcStrList.size(); i++) {
1514
String line = srcStrList.get(i);
1615
if (line.trim().startsWith("package ") && !ifchanged) {
16+
wrtStr += ("package " + newPackName + ";\n");
1717
ifchanged = true;
1818
} else {
1919
wrtStr += (line + "\n");
2020
}
2121
}
2222

23-
FileUtil.writeStr2File(wrtStr, dstSRCPath);
24-
25-
}
26-
27-
public static void changePackage(Path srcPath, String newPackName) {
28-
List<String> srcStrList = FileUtil.readFileToLineList(srcPath);
29-
30-
Boolean ifchanged = false;
31-
String wrtStr = "";
32-
for (int i = 0; i < srcStrList.size(); i++) {
33-
String line = srcStrList.get(i);
34-
if (line.trim().startsWith("package ") && !ifchanged) {
35-
line = "package " + newPackName + ";";
36-
ifchanged = true;
37-
}
38-
wrtStr += (line + "\n");
23+
if (!ifchanged) {
24+
wrtStr = "package " + newPackName + ";\n" + wrtStr;
3925
}
40-
41-
FileUtil.writeStr2File(wrtStr, srcPath);
42-
43-
}
44-
45-
public static void main(String[] args) {
46-
/*
47-
* test purpose
48-
*/
49-
String oriPathString = "/home/appevolve/Desktop/2019_ISSTA_AE_225/run/Update_Example_Analysis+API-Usage_Update/EPR-Data/RepositoryWriter.java";
50-
String dstPathString = "/home/appevolve/Desktop/2019_ISSTA_AE_225/run/Update_Example_Analysis+API-Usage_Update/EPR-Data/RepositoryWriter_tmp.java";
51-
String newPackString = "main";
52-
changePackage(oriPathString, dstPathString, newPackString);
53-
System.out.println("Finished!");
26+
27+
return wrtStr.trim();
5428

5529
}
5630

0 commit comments

Comments
 (0)