Skip to content

Commit 5d5ee5a

Browse files
committed
init
1 parent 73698a3 commit 5d5ee5a

8 files changed

+621
-0
lines changed

Diff for: lib/utils/CSVUtil.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ProgrammingToolkit.java.lib.utils;
2+
3+
public class CSVUtil {
4+
5+
}

Diff for: lib/utils/DiffUtil.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ProgrammingToolkit.java.lib.utils;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.InputStream;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import ProgrammingToolkit.java.lib.utils.FileUtil;
10+
import io.reflectoring.diffparser.api.DiffParser;
11+
import io.reflectoring.diffparser.api.UnifiedDiffParser;
12+
import io.reflectoring.diffparser.api.model.Diff;
13+
14+
public class DiffUtil {
15+
16+
public static int getChangedLine(String diffFilePath) {
17+
try {
18+
List<String> diffFileStrList = FileUtil.readFileToStrList(diffFilePath);
19+
for (String line : diffFileStrList) {
20+
if (line.startsWith("@@")) {
21+
for (String tmp : line.split(" ")) {
22+
if (tmp.trim().startsWith("-")) {
23+
if (tmp.contains(",")) {
24+
return Integer.valueOf(tmp.trim().replace("-", "").split(",")[0]);
25+
} else {
26+
return Integer.valueOf(tmp.trim().replace("-", ""));
27+
}
28+
}
29+
}
30+
}
31+
}
32+
} catch (Exception e) {
33+
// TODO: handle exception
34+
System.out.println(diffFilePath);
35+
System.exit(0);
36+
}
37+
return -1;
38+
}
39+
40+
public static List<String> getModifiedFileList(String diffFilePath) {
41+
/**
42+
* can to be further improved in future
43+
*/
44+
List<String> modifiedFileRelPathList = new ArrayList<String>();
45+
for (String line : FileUtil.readFileToStrList(diffFilePath)) {
46+
if (line.startsWith("diff --git ")) {
47+
// Heuristic
48+
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
49+
modifiedFileRelPathList.add(fileRelPath);
50+
}
51+
}
52+
return modifiedFileRelPathList;
53+
}
54+
55+
public static void main(String[] args) {
56+
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
57+
getModifiedFileList(diffFilePath);
58+
}
59+
60+
}

Diff for: lib/utils/EclipseUtil.java

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package ProgrammingToolkit.java.lib.utils;
2+
3+
import java.io.File;
4+
5+
import org.eclipse.core.resources.IFolder;
6+
import org.eclipse.core.resources.IProject;
7+
import org.eclipse.core.resources.IProjectDescription;
8+
import org.eclipse.core.resources.IWorkspaceRoot;
9+
import org.eclipse.core.resources.ResourcesPlugin;
10+
import org.eclipse.core.runtime.CoreException;
11+
import org.eclipse.core.runtime.IPlatformRunnable;
12+
import org.eclipse.jdt.core.IClasspathEntry;
13+
import org.eclipse.jdt.core.ICompilationUnit;
14+
import org.eclipse.jdt.core.IJavaProject;
15+
import org.eclipse.jdt.core.IPackageFragment;
16+
import org.eclipse.jdt.core.IPackageFragmentRoot;
17+
import org.eclipse.jdt.core.IType;
18+
import org.eclipse.jdt.core.JavaCore;
19+
import org.eclipse.jdt.launching.JavaRuntime;
20+
21+
import changeassistant.multipleexample.util.PropertyLoader;
22+
23+
public class EclipseUtil implements IPlatformRunnable {
24+
25+
public static IWorkspaceRoot createWorkspace(String workspaceDir) {
26+
// PropertyLoader.load(new File(Constants.projectDir + "datasetConfig/properties"));
27+
// PropertyLoader.props.setProperty("Project_Home_Path", workspaceDir);
28+
IWorkspaceRoot workSpace = ResourcesPlugin.getWorkspace().getRoot();
29+
return workSpace;
30+
}
31+
32+
public static IProject createNewPrject(IWorkspaceRoot root, String projectName, String fileName, String srcStr) {
33+
/**
34+
* code from
35+
*/
36+
IProject project = null;
37+
try {
38+
39+
// create a project with name
40+
project = root.getProject(projectName);
41+
42+
project.create(null);
43+
project.open(null);
44+
45+
// set the Java nature
46+
IProjectDescription description = project.getDescription();
47+
description.setNatureIds(new String[] { JavaCore.NATURE_ID });
48+
49+
// create the project
50+
project.setDescription(description, null);
51+
IJavaProject javaProject = JavaCore.create(project);
52+
53+
// set the build path
54+
IClasspathEntry[] buildPath = { JavaCore.newSourceEntry(project.getFullPath().append("src")) };
55+
56+
javaProject.setRawClasspath(buildPath, project.getFullPath().append("bin"), null);
57+
58+
// IFolder binFolder = project.getFolder("bin");
59+
// binFolder.create(false, true, null);
60+
// javaProject.setOutputLocation(binFolder.getFullPath(), null);
61+
62+
IFolder sourceFolder = project.getFolder("src");
63+
sourceFolder.create(false, true, null);
64+
65+
// Add folder to Java element
66+
IPackageFragmentRoot srcFolder = javaProject.getPackageFragmentRoot(sourceFolder);
67+
68+
// create package fragment
69+
IPackageFragment fragment = srcFolder.createPackageFragment("main", true, null);
70+
71+
writeSRCtoPrject(project, javaProject, fileName, srcStr);
72+
73+
} catch (CoreException e) {
74+
System.out.println("Create Project "+projectName+" failed!");
75+
e.printStackTrace();
76+
}
77+
System.out.println("Create Project "+projectName+" successfully!");
78+
return project;
79+
}
80+
81+
public static void writeSRCtoPrject(IProject project, IJavaProject javaProject, String fileName, String srcStr) {
82+
/**
83+
* code from
84+
*/
85+
try {
86+
ICompilationUnit cu = javaProject.getPackageFragmentRoot(project.getFolder("src"))
87+
.getPackageFragment("main").createCompilationUnit(fileName, srcStr, false, null);
88+
89+
} catch (CoreException e) {
90+
// TODO Auto-generated catch block
91+
e.printStackTrace();
92+
}
93+
return;
94+
}
95+
96+
// @Override
97+
// public Object run(Object args) throws Exception {
98+
// String projectName = "testP";
99+
// IWorkspaceRoot workSpace = createWorkspace(Constants.workspaceDir);
100+
// // init code string and create compilation unit
101+
// String str = "package com.programcreek;" + "\n" + "public class Test {" + "\n" + " private String name;" + "\n"
102+
// + "}";
103+
// createNewPrject(workSpace, projectName, "test.java", str);
104+
// System.out.println("Finished!");
105+
// return null;
106+
// }
107+
108+
}

Diff for: lib/utils/FileUtil.java

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package ProgrammingToolkit.java.lib.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.io.FileWriter;
10+
import java.io.FilenameFilter;
11+
import java.io.IOException;
12+
import java.io.InputStreamReader;
13+
import java.io.Writer;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
import org.apache.commons.io.FileUtils;
21+
22+
public class FileUtil {
23+
24+
public static String[] getSubDirs(String rootDir) {
25+
26+
File file = new File(rootDir);
27+
String[] directories = file.list(new FilenameFilter() {
28+
@Override
29+
public boolean accept(File current, String name) {
30+
return new File(current, name).isDirectory();
31+
}
32+
});
33+
// System.out.println(Arrays.toString(directories));
34+
return directories;
35+
}
36+
37+
public static String readFile2Str(String fpath) {
38+
String content = "";
39+
try {
40+
content = new String(Files.readAllBytes(Paths.get(fpath)));
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
return content;
45+
46+
}
47+
48+
public static Boolean writeStr2File(String wStr, String fPath) {
49+
try {
50+
Files.write(Paths.get(fPath), wStr.getBytes());
51+
} catch (IOException e) {
52+
// TODO Auto-generated catch block
53+
e.printStackTrace();
54+
return false;
55+
}
56+
return true;
57+
}
58+
59+
public static boolean deleteDirectory(String dirPath) {
60+
File directoryToBeDeleted = new File(dirPath);
61+
File[] allContents = directoryToBeDeleted.listFiles();
62+
if (allContents != null) {
63+
for (File file : allContents) {
64+
deleteDirectory(file.getAbsolutePath());
65+
}
66+
}
67+
return directoryToBeDeleted.delete();
68+
}
69+
70+
public static void copyFolder(Path srcDirStr, Path destDirStr) {
71+
File source = new File(srcDirStr.toString());
72+
File dest = new File(destDirStr.toString());
73+
try {
74+
FileUtils.copyDirectory(source, dest);
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
public static void writeStringToFile(String filePath, String dataStr) {
81+
BufferedWriter bufferedWriter = null;
82+
try {
83+
File file = new File(filePath);
84+
if (!file.exists()) {
85+
file.createNewFile();
86+
}
87+
Writer writer = new FileWriter(file);
88+
bufferedWriter = new BufferedWriter(writer);
89+
bufferedWriter.write(dataStr);
90+
} catch (IOException e) {
91+
e.printStackTrace();
92+
} finally {
93+
try {
94+
if (bufferedWriter != null)
95+
bufferedWriter.close();
96+
} catch (Exception e) {
97+
System.out.println("Write file error!");
98+
e.printStackTrace();
99+
}
100+
}
101+
}
102+
103+
public static String readStringFromFile(String filePath) {
104+
try {
105+
File file = new File(filePath);
106+
if (file.isFile() && file.exists()) {
107+
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
108+
BufferedReader bufferedReader = new BufferedReader(read);
109+
StringBuilder stringBuilder = new StringBuilder();
110+
String line = null;
111+
String ls = System.getProperty("line.separator");
112+
while ((line = bufferedReader.readLine()) != null) {
113+
stringBuilder.append(line);
114+
stringBuilder.append(ls);
115+
}
116+
117+
// delete the last new line separator
118+
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
119+
bufferedReader.close();
120+
121+
String content = stringBuilder.toString();
122+
return content;
123+
} else {
124+
System.out.printf("File not found! %s\n",filePath);
125+
}
126+
} catch (Exception e) {
127+
System.out.println("Read file error!");
128+
e.printStackTrace();
129+
}
130+
return null;
131+
}
132+
133+
public static List<String> readFileToStrList(String filePath) {
134+
ArrayList<String> strList = new ArrayList<String>();
135+
try {
136+
if (!new File(filePath).exists()) {
137+
System.out.println("File not exists! " + filePath);
138+
return null;
139+
}
140+
BufferedReader reader = new BufferedReader(new FileReader(filePath));
141+
String line = reader.readLine();
142+
while (line != null) {
143+
strList.add(line);
144+
line = reader.readLine();
145+
}
146+
reader.close();
147+
} catch (FileNotFoundException e) {
148+
// TODO Auto-generated catch block
149+
System.out.println("filePath : " + filePath);
150+
e.printStackTrace();
151+
} catch (IOException e) {
152+
// TODO: handle exception
153+
System.out.println("filePath : " + filePath);
154+
e.printStackTrace();
155+
}
156+
return strList;
157+
}
158+
159+
public static void createFolder(String dirStr, Boolean deleteIfExist) {
160+
File dir = new File(dirStr);
161+
if (deleteIfExist && dir.exists()) {
162+
deleteDirectory(dir.getAbsolutePath());
163+
dir.mkdirs();
164+
} else if (!dir.exists()) {
165+
dir.mkdirs();
166+
}
167+
}
168+
169+
}

Diff for: lib/utils/JSONUtil.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ProgrammingToolkit.java.lib.utils;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
6+
7+
public class JSONUtil {
8+
9+
public static JSONObject parseJSONFromFile(String jsonFpath) {
10+
FileReader reader = null;
11+
JSONObject object = null;
12+
try {
13+
reader = new FileReader(jsonFpath);
14+
JSONParser jsonParser = new JSONParser();
15+
16+
object = (JSONObject)jsonParser.parse(reader);
17+
} catch (IOException | ParseException e) {
18+
// TODO Auto-generated catch block
19+
e.printStackTrace();
20+
}
21+
22+
return object;
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)