Skip to content

Commit b617c8f

Browse files
log file fix
1 parent 9a2a5d5 commit b617c8f

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

java-parser/src/main/java/method_analyzer/MethodLines.java

+41-31
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.github.javaparser.ast.CompilationUnit;
1818
import com.github.javaparser.Range;
1919
import com.github.javaparser.ast.Node;
20-
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
20+
import com.github.javaparser.ast.body.VariableDeclarator;
2121
import com.github.javaparser.ast.expr.SimpleName;
2222
import com.github.javaparser.ast.body.MethodDeclaration;
2323
import com.github.javaparser.ast.visitor.TreeVisitor;
@@ -30,22 +30,27 @@ public class MethodLines extends VoidVisitorAdapter<Object> {
3030

3131
private ArrayList<Node> rem = new ArrayList<>();
3232
public static ArrayList<String> acceptedNames = new ArrayList<>();
33-
private static File logFile;
34-
public static FileWriter fr;
3533
public static boolean seq;
3634

3735
private String typeToTarget;
3836
private String javaFilePath;
37+
private File logFile;
38+
private FileWriter fr;
3939

40-
MethodLines(String typeToTarget) {
40+
MethodLines(String typeToTarget, String logFile) {
4141
this.typeToTarget = typeToTarget;
42-
}
42+
/*
43+
this.logFile = new File(logFile);
4344
44-
public static void setLogPath(String path) throws IOException {
45-
MethodLines.logFile = new File(path);
46-
MethodLines.fr = new FileWriter(MethodLines.logFile, true);
45+
try {
46+
this.fr = new FileWriter(logFile, true);
47+
} catch (java.io.IOException e) {
48+
System.out.println("file " + logFile + " invalid");
49+
}
50+
*/
4751
}
4852

53+
4954
public static void set_clean_names(String fname) {
5055
File accNamesFile = new File(fname);
5156
Scanner s;
@@ -70,8 +75,19 @@ public void inspectSourceCode(File javaFile) {
7075
if (root != null) {
7176
this.visit(root.clone(), null);
7277
}
78+
79+
/*
80+
try {
81+
this.fr.close();
82+
System.out.println("Closed file");
83+
return;
84+
} catch (IOException e) {
85+
System.out.println("Can't close file");
86+
}
87+
*/
7388
}
7489

90+
7591
@Override
7692
public void visit(CompilationUnit com, Object obj) {
7793
locateTargetStatements(com, obj, this.typeToTarget);
@@ -108,8 +124,8 @@ private boolean isTypeOfTarget(String typeToTarget, Node node){
108124
case "MethodDeclaration":
109125
return (node instanceof MethodDeclaration);
110126

111-
case "VariableDeclarationExpr":
112-
return (node instanceof VariableDeclarationExpr);
127+
case "VariableDeclarator":
128+
return (node instanceof VariableDeclarator);
113129

114130
default:
115131
System.out.println("ERROR: no support for target type: " + typeToTarget);
@@ -132,18 +148,21 @@ public void process(Node node) {
132148

133149
ArrayList<String> names = MethodLines.seq ? splitNameByToken(node_name) : new ArrayList<String>(Arrays.asList(node_name));
134150

151+
/*
135152
for (int i=0; i<names.size(); i++) {
136153
String token = names.get(i);
137154
if(!acceptedNames.contains(token)){
138155
rem.add(node);
139156
break;
140157
}
141-
}
158+
}*/
142159

143160
break;
144161

145-
case "VariableDeclarationExpr":
146-
Range r = node.getRange().orElse(null);
162+
case "VariableDeclarator":
163+
VariableDeclarator vd = (VariableDeclarator) node;
164+
165+
Range r = vd.getName().getRange().orElse(null);
147166
try {
148167
fr.write(javaFilePath + " : " + typeToTarget + " : " + r.toString() + "\n");
149168
} catch (IOException e) {
@@ -180,20 +199,17 @@ private void applyManager(CompilationUnit com) {
180199
}
181200

182201
public static void main(String[] args){
183-
String inPath = "/data2/edinella/java-all-redist-fs/";
184-
Common.outputPath = "/data2/edinella/java-all-redist-g/";
202+
//String inPath = "/data2/edinella/java-all-redist-g/training/";
203+
String inPath = "/data2/edinella/java-all";
204+
Common.outputPath = "/data2/edinella/test-java-all-seq/";
185205
//Common.outputPath = "/data2/edinella/java-large-clean/";
186206

187-
String typeToTarget = "VariableDeclarationExpr";
188-
//MethodLines.seq = false;
189-
//MethodLines.set_clean_names("/home/edinella/clean_names.txt");
190207

191-
try {
192-
setLogPath(Common.outputPath + "log.txt");
193-
} catch (IOException e) {
194-
System.out.println(Common.outputPath + " can't write to!");
195-
return;
196-
}
208+
String typeToTarget = "MethodDeclaration";
209+
//String typeToTarget = "VariableDeclarator";
210+
MethodLines.seq = true;
211+
MethodLines.set_clean_names("/home/edinella/clean_seqs.txt");
212+
197213

198214
File programFolder = new File(inPath);
199215

@@ -217,12 +233,6 @@ public static void main(String[] args){
217233

218234
executor.shutdown();
219235

220-
try {
221-
MethodLines.fr.close();
222-
System.out.println("Closed file");
223-
return;
224-
} catch (IOException e) {
225-
System.out.println("Can't close file");
226-
}
236+
227237
}
228238
}

java-parser/src/main/java/method_analyzer/ThreadedManager.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ public class ThreadedManager implements Runnable {
77

88
private File file;
99
private MethodLines mn;
10+
private String logFile;
1011

1112
public ThreadedManager(File file, String typeToTarget){
1213
this.file = file;
13-
mn = new MethodLines(typeToTarget);
14+
//this.logFile = file.getPath().replace(".java", ".txt");
15+
this.mn = new MethodLines(typeToTarget, logFile);
1416
}
1517

1618
public void run() {

0 commit comments

Comments
 (0)