Skip to content

Commit

Permalink
bug修复
Browse files Browse the repository at this point in the history
  • Loading branch information
SongXueZhi committed May 19, 2021
1 parent c19893b commit fa7eb76
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 39 deletions.
14 changes: 7 additions & 7 deletions miner/src/main/java/miner/PotentialBFCDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ public List<PotentialRFC> detectPotentialBFC() throws Exception {
// 获取所有的commit,我们需要对所有的commit进行分析
Iterable<RevCommit> commits = git.log().all().call();
// 开始迭代每一个commit
boolean a = true;
boolean a = false;
for (RevCommit commit : commits) {
// // a 用于从失败的节点重新开始
// if (commit.getName().equals("2fa8ee07e55b476f778a63633f556bbf6b023ab7")) {
// a = true;
// }
if (commit.getName().equals("1219227605e360e72863d133087e2086cdad7fbc")) {
a = true;
}
if (a) {
detect(commit, potentialRFCs);
}
// if (potentialRFCs.size() > 1) {
// break;
// }
if (potentialRFCs.size() >= 1) {
break;
}
countAll++;
}
FileUtilx.log("总共分析了" + countAll + "条commit\n");
Expand Down
2 changes: 2 additions & 0 deletions miner/src/main/java/miner/RelatedTestCaseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void parseTestCases(PotentialRFC pRFC) throws Exception {
code);
if (!isTestSuite(code)) {
file.setType(Type.TEST_RELATE);
pRFC.getTestRelates().add(file);

} else {
file.setType(Type.TEST_SUITE);
file.setQualityClassName(CompilationUtil.getQualityClassName(code));
Expand Down
13 changes: 8 additions & 5 deletions miner/src/main/java/miner/migrate/BICFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public String searchBIC(PotentialRFC pRFC) {
}

new SycFileCleanup().cleanDirectoryOnFilter(bfcFile, Arrays.asList(bfcName, arr[a + 1], arr[a]));// 删除在regression定义以外的项目文件
return arr[a + 1] + "," + arr[a] + "," + sj.toString();
return arr[a + 1] + "," + arr[a] + "," + sj.toString() + "," + pRFC.fileMap.get(bfcName) + ","
+ pRFC.fileMap.get(arr[a + 1]) + "," + pRFC.fileMap.get(arr[a]);
}
}

Expand Down Expand Up @@ -168,11 +169,13 @@ public int search(String[] arr, int low, int high) {
// 查找成功条件
int statu = getTestResult(arr[middle], middle);

if (statu == TestCaseMigrater.FAL && getTestResult(arr[middle - 1], middle - 1) == TestCaseMigrater.PASS) {
if (statu == TestCaseMigrater.FAL && middle - 1 > 0
&& getTestResult(arr[middle - 1], middle - 1) == TestCaseMigrater.PASS) {
FileUtilx.log("回归+1");
return middle - 1;
}
if (statu == TestCaseMigrater.PASS && getTestResult(arr[middle + 1], middle + 1) == TestCaseMigrater.FAL) {
if (statu == TestCaseMigrater.PASS && middle + 1 < arr.length
&& getTestResult(arr[middle + 1], middle + 1) == TestCaseMigrater.FAL) {
FileUtilx.log("回归+1");
return middle;
}
Expand All @@ -184,7 +187,7 @@ public int search(String[] arr, int low, int high) {

if (left != -1 && getTestResult(arr[left], left) == TestCaseMigrater.FAL) {
// 往附近看一眼
if (getTestResult(arr[left - 1], left - 1) == TestCaseMigrater.PASS) {
if (middle - 1 > 0 && getTestResult(arr[left - 1], left - 1) == TestCaseMigrater.PASS) {
return left - 1;
}
// 左边界开始新的查找
Expand All @@ -197,7 +200,7 @@ public int search(String[] arr, int low, int high) {

if (right != -1 && getTestResult(arr[right], right) == TestCaseMigrater.PASS) {
// 往附近看一眼
if (getTestResult(arr[right + 1], right + 1) == TestCaseMigrater.FAL) {
if (middle + 1 < arr.length && getTestResult(arr[right + 1], right + 1) == TestCaseMigrater.FAL) {
return right;
}
int b = search(arr, right, high);
Expand Down
2 changes: 1 addition & 1 deletion miner/src/main/java/miner/migrate/TestCaseDeterminer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void determine(PotentialRFC pRFC) throws Exception {
}
String bfcpID = pRFC.getCommit().getParent(0).getName();
File bfcpDirectory = checkout(bfcID, bfcpID, "bfcp");// 管理每一个commit的文件路径
// pRFC.fileMap.put(bfcpID, bfcpDirectory);
pRFC.fileMap.put(bfcpID, bfcpDirectory);

// // 3.第一次尝试编译 BFCP
// if (!comiple(bfcpDirectory, false)) {
Expand Down
82 changes: 65 additions & 17 deletions miner/src/main/java/miner/migrate/TestCaseMigrater.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miner.migrate;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -21,7 +22,9 @@
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

import ast.FieldRetriever;
Expand All @@ -37,6 +40,11 @@
import utils.CompilationUtil;
import utils.FileUtilx;

/**
*
* @author sxz
*
*/
public class TestCaseMigrater extends Migrater {
public final static int PASS = 0;
public final static int FAL = 1;
Expand All @@ -60,7 +68,7 @@ public void migrate(PotentialRFC pRFC, Set<String> bicSet) {
public int migrate(PotentialRFC pRFC, String bic) throws Exception {
FileUtilx.log("bic:" + bic);
File bicDirectory = checkout(pRFC.getCommit().getName(), bic, "bic");
// pRFC.fileMap.put(bic, bicDirectory);
pRFC.fileMap.put(bic, bicDirectory);
// // 第一次编译未copy时候编译尝试
// if (!comiple(bicDirectory, false)) {
// FileUtilx.log("本身编译失败");
Expand All @@ -69,6 +77,7 @@ public int migrate(PotentialRFC pRFC, String bic) throws Exception {
File bfcDir = pRFC.fileMap.get(pRFC.getCommit().getName());
// 第一次编译成功,则开始依赖图匹配
copyTestCase(pRFC, bicDirectory, bfcDir);
copyTestRelates(pRFC, bicDirectory, bfcDir);
// 编译
if (comiple(bicDirectory, true)) {
int a = testSuite(bicDirectory, pRFC.getTestCaseFiles());
Expand Down Expand Up @@ -140,7 +149,37 @@ public int testBFCPMethod(TestFile testSuite, StringJoiner sj) throws Exception
return UNRESOLVE;
}

public void copyTestCase(PotentialRFC pRFC, File tDir, File bfcDir) throws Exception {
public void copyTestRelates(PotentialRFC pRFC, File tDir, File bfcDir) {
List<TestFile> testRelates = pRFC.getTestRelates();
for (TestFile testRelate : testRelates) {
// 测试文件是被删除则什么也不作。
if (testRelate.getNewPath().contains("/dev/null")) {
continue;
}
File file = new File(Conf.TMP_FILE + File.separator + pRFC.getCommit().getName() + File.separator
+ testRelate.getNewPath());
// 测试文件是被删除则什么也不作。

String targetPath = testRelate.getNewPath();
// 测试文件不是删除,则copy
targetPath = FileUtilx.getDirectoryFromPath(targetPath);
File file1 = new File(tDir.getAbsoluteFile() + File.separator + targetPath);
if (!file1.exists()) {
file1.mkdirs();
}
exec.exec("cp " + file.getAbsolutePath() + " " + file1.getAbsolutePath());

}
}

/**
* 当前只迁移了tessuite,因此针对testrelated需要其他的策略
*
* @param pRFC
* @param tDir
* @param bfcDir
*/
public void copyTestCase(PotentialRFC pRFC, File tDir, File bfcDir) {
List<TestFile> tcs = pRFC.getTestCaseFiles();
String[] tFiles = CodeUtil.getJavaFiles(tDir);
String[] bfcFils = CodeUtil.getJavaFiles(bfcDir);
Expand All @@ -167,6 +206,7 @@ public void copyTestCase(PotentialRFC pRFC, File tDir, File bfcDir) throws Excep
List<ImportDeclaration> imports = unitFrom.imports();
Map<String, ImportDeclaration> needToCopyImport = new HashMap<>();
for (Map.Entry<String, RelatedTestCase> entry : tc.getTestMethodMap().entrySet()) {
// 注意此处的MemberRetriever的实现并不会访问内部类中的方法,所以内部类中的方法将不会被影响
MethodDeclaration md = entry.getValue().getMethod().getMethodDeclaration();
MemberRetriever mr = new MemberRetriever();
unitTo.accept(mr);
Expand Down Expand Up @@ -196,8 +236,13 @@ public void copyTestCase(PotentialRFC pRFC, File tDir, File bfcDir) throws Excep
unitTo.recordModifications();
Document document = new Document();
TextEdit edits = unitTo.rewrite(document, null);
edits.apply(document);
FileUtils.write(new File(tDir, tpath), document.get());
try {
edits.apply(document);
FileUtils.write(new File(tDir, tpath), document.get());
} catch (MalformedTreeException | BadLocationException | IOException e) {
FileUtilx.log(e.getMessage() + "\n bfcdir: " + bfcDir + "\n tdir: " + tDir);
}

}
}
}
Expand All @@ -222,15 +267,14 @@ public boolean whetherMehothdExits(List<MethodDeclaration> mLsit, MethodDeclarat
}

public void copyAndPruneNoUsedMethod(String javaFilepath, String classFile, File tDir, File bfcDir,
Map<String, RelatedTestCase> methodMap) throws Exception {
Map<String, RelatedTestCase> methodMap) {
MethodCaller mcaller = new MethodCaller();
String content = FileUtilx.readContentFromFile(bfcDir.getAbsolutePath() + File.separator + javaFilepath);
CompilationUnit unit = CompilationUtil.parseCompliationUnit(content);
JavaClass clazz = CodeUtil.lookupClassbyFile(bfcDir.getAbsolutePath() + File.separator + classFile);
Method[] methods = clazz.getMethods();
Set<String> methodsSet = new HashSet<>();
// 获取每个方法,加每个方法在该类中的调用

for (Map.Entry<String, RelatedTestCase> entry : methodMap.entrySet()) {
Method method = CodeUtil.methodMatch(methods, entry.getValue().getMethod().getMethodDeclaration());
CallNode node = mcaller.getMethodCall(method, clazz);
Expand Down Expand Up @@ -261,15 +305,18 @@ public void copyAndPruneNoUsedMethod(String javaFilepath, String classFile, File
unit.recordModifications();
Document doc = new Document(content);
TextEdit edits = rewriter.rewriteAST(doc, null);
edits.apply(doc);
// 删除无关字段和import
String code = pruneImport(pruneField(doc.get()));

FileUtils.write(new File(tDir, javaFilepath), code);

try {
edits.apply(doc);
// 删除无关字段和import
String code;
code = pruneImport(pruneField(doc.get()));
FileUtils.write(new File(tDir, javaFilepath), code);
} catch (MalformedTreeException | BadLocationException | IOException e) {
FileUtilx.log(e.getMessage() + "\n bfcdir: " + bfcDir + "\n tdir: " + tDir);
}
}

public String pruneImport(String content) throws Exception {
public String pruneImport(String content) throws MalformedTreeException, BadLocationException {
CompilationUnit unit = CompilationUtil.parseCompliationUnit(content);
ASTRewrite rewriter = ASTRewrite.create(unit.getAST());
// 删除无用的import
Expand All @@ -295,10 +342,11 @@ public String pruneImport(String content) throws Exception {
return doc.get();
}

public String pruneField(String content) throws Exception {
public String pruneField(String content) throws MalformedTreeException, BadLocationException {
CompilationUnit unit = CompilationUtil.parseCompliationUnit(content);
ASTRewrite rewriter = ASTRewrite.create(unit.getAST());
List<TypeDeclaration> types = unit.types();
// 与MethodRetriever不同FieldRetriever可以访问到内部类中的字段声明
FieldRetriever fr = new FieldRetriever();
unit.accept(fr);
Map<FieldDeclaration, List<VariableDeclarationFragment>> filedMap = fr.fieldMap;
Expand All @@ -309,9 +357,9 @@ public String pruneField(String content) throws Exception {
for (VariableDeclarationFragment filed : vflist) {
boolean flag = false;
for (TypeDeclaration type : types) {
if (type.toString().contains(filed.getName().toString())) {
flag = true;
}
if (type.toString().contains(filed.getName().toString())) {
flag = true;
}
}
if (flag == false) {
a++;
Expand Down
19 changes: 19 additions & 0 deletions miner/src/main/java/model/PotentialRFC.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -13,6 +14,8 @@ public class PotentialRFC {
private int priority;
private List<NormalFile> normalJavaFiles;
private List<TestFile> testCaseFiles;
private List<TestFile> testSuites = new ArrayList<TestFile>();
private List<TestFile> testRelates = new ArrayList<TestFile>();
private Set<String> testCaseSet;
public Map<String, File> fileMap = new HashMap<>();

Expand Down Expand Up @@ -70,4 +73,20 @@ public void setTestCaseSet(Set<String> testCaseSet) {
this.testCaseSet = testCaseSet;
}

public List<TestFile> getTestSuites() {
return testSuites;
}

public void setTestSuites(List<TestFile> testSuites) {
this.testSuites = testSuites;
}

public List<TestFile> getTestRelates() {
return testRelates;
}

public void setTestRelates(List<TestFile> testRelates) {
this.testRelates = testRelates;
}

}
25 changes: 16 additions & 9 deletions miner/src/main/java/utils/CodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand All @@ -23,16 +24,22 @@

public class CodeUtil {

public static JavaClass lookupClassbyFile(String resourceName) throws Exception {
public static JavaClass lookupClassbyFile(String resourceName) {
byte[] b = new byte[(int) new File(resourceName).length()];
InputStream in = new FileInputStream(resourceName);
new DataInputStream(in).readFully(b);
in.close();
ClassParser parser = new ClassParser(new ByteArrayInputStream(b), resourceName);
boolean parsedClass = false;
JavaClass javaClass = parser.parse();
parsedClass = true;
return javaClass;
InputStream in;
try {
in = new FileInputStream(resourceName);
new DataInputStream(in).readFully(b);
in.close();
ClassParser parser = new ClassParser(new ByteArrayInputStream(b), resourceName);
JavaClass javaClass = parser.parse();
return javaClass;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static Method getMethodByName(JavaClass clazz, String methodName) {
Expand Down

0 comments on commit fa7eb76

Please sign in to comment.