Skip to content

Commit 9a2a5d5

Browse files
initial commit of stats
1 parent 4181182 commit 9a2a5d5

File tree

2 files changed

+204
-124
lines changed

2 files changed

+204
-124
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package method_analyzer;
22

33
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
46
import java.lang.Character;
57
import java.util.ArrayList;
68
import java.util.Arrays;
79
import java.util.List;
810
import java.util.stream.Collectors;
11+
import java.util.concurrent.TimeUnit;
912
import java.util.Scanner;
1013
import java.util.concurrent.ExecutorService;
1114
import java.util.concurrent.Executors;
1215
import java.util.concurrent.ExecutionException;
1316

1417
import com.github.javaparser.ast.CompilationUnit;
18+
import com.github.javaparser.Range;
1519
import com.github.javaparser.ast.Node;
20+
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
1621
import com.github.javaparser.ast.expr.SimpleName;
1722
import com.github.javaparser.ast.body.MethodDeclaration;
1823
import com.github.javaparser.ast.visitor.TreeVisitor;
@@ -23,126 +28,201 @@
2328
@SuppressWarnings({"WeakerAccess", "unused", "unchecked"})
2429
public class MethodLines extends VoidVisitorAdapter<Object> {
2530

26-
private ArrayList<Node> rem = new ArrayList<>();
27-
public static ArrayList<String> acceptedNames = new ArrayList<>();
28-
public static boolean seq;
29-
30-
MethodLines() { }
31-
32-
public static void set_clean_names(String fname) {
33-
File accNamesFile = new File(fname);
34-
Scanner s;
35-
36-
try{
37-
s = new Scanner(accNamesFile);
38-
} catch (java.io.FileNotFoundException exp) {
39-
System.out.println("file " + fname + " does not exist");
40-
System.out.println("Exiting");
41-
return;
42-
}
43-
44-
while (s.hasNextLine()){
45-
acceptedNames.add(s.nextLine());
46-
}
47-
}
48-
49-
public void inspectSourceCode(File javaFile) {
50-
CompilationUnit root = Common.getParseUnit(javaFile);
51-
if (root != null) {
52-
this.visit(root.clone(), null);
53-
}
54-
}
55-
56-
@Override
57-
public void visit(CompilationUnit com, Object obj) {
58-
locateTargetStatements(com, obj);
59-
applyManager(com);
60-
super.visit(com, obj);
61-
}
62-
63-
private ArrayList<String> splitNameByToken(String name) {
64-
char prev = '0';
65-
String token = "";
66-
ArrayList<String> tokens = new ArrayList<String>();
67-
68-
for( int i =0; i < name.length(); i++){
69-
char c = name.charAt(i);
70-
if (i > 0 && ((Character.isUpperCase(c) && Character.isLowerCase(prev)) || c == '_') && token.length() > 0) {
71-
tokens.add(token);
72-
token = "";
73-
}
74-
75-
prev = c;
76-
token += c;
77-
}
78-
79-
if (token.length() > 0){
80-
tokens.add(token);
81-
}
82-
83-
return tokens;
84-
}
85-
86-
private void locateTargetStatements(CompilationUnit com, Object obj) {
87-
try{
88-
new TreeVisitor() {
89-
@Override
90-
public void process(Node node) {
91-
if (node instanceof MethodDeclaration){
92-
93-
MethodDeclaration md = (MethodDeclaration) node;
94-
String node_name = md.getNameAsString();
95-
96-
ArrayList<String> names = MethodLines.seq ? splitNameByToken(node_name) : new ArrayList<String>(Arrays.asList(node_name));
97-
98-
for (int i=0; i<names.size(); i++) {
99-
String token = names.get(i);
100-
if(!acceptedNames.contains(token)){
101-
rem.add(node);
102-
break;
103-
}
104-
}
105-
106-
}
107-
}
108-
}.visitBreadthFirst(com);
109-
110-
}catch(Exception e){
111-
System.out.println(e);
112-
}
113-
}
114-
115-
private void applyManager(CompilationUnit com) {
116-
for (int i=0; i<rem.size(); i++) {
117-
try{
118-
rem.get(i).remove();
119-
}catch(Exception e){
120-
System.out.println("Can't remove"+ ((MethodDeclaration)rem.get(i)).getNameAsString());
121-
}
122-
}
123-
124-
Common.saveTransformation(com);
125-
}
126-
127-
public static void main(String[] args){
128-
String inPath = "/data2/edinella/java-large/";
129-
Common.outputPath = "/data2/edinella/java-large-clean/";
130-
131-
MethodLines.seq = false;
132-
133-
MethodLines.set_clean_names("/home/edinella/clean_names.txt");
134-
135-
File programFolder = new File(inPath);
136-
137-
File[] files = Utils.getAllSubFiles(programFolder);
138-
139-
ExecutorService executor = Executors.newFixedThreadPool(70);
140-
141-
for (File file : files) {
142-
Thread worker = new Thread(new ThreadedManager(file));
143-
executor.execute(worker);
144-
}
145-
146-
executor.shutdown();
147-
}
31+
private ArrayList<Node> rem = new ArrayList<>();
32+
public static ArrayList<String> acceptedNames = new ArrayList<>();
33+
private static File logFile;
34+
public static FileWriter fr;
35+
public static boolean seq;
36+
37+
private String typeToTarget;
38+
private String javaFilePath;
39+
40+
MethodLines(String typeToTarget) {
41+
this.typeToTarget = typeToTarget;
42+
}
43+
44+
public static void setLogPath(String path) throws IOException {
45+
MethodLines.logFile = new File(path);
46+
MethodLines.fr = new FileWriter(MethodLines.logFile, true);
47+
}
48+
49+
public static void set_clean_names(String fname) {
50+
File accNamesFile = new File(fname);
51+
Scanner s;
52+
53+
try{
54+
s = new Scanner(accNamesFile);
55+
} catch (java.io.FileNotFoundException exp) {
56+
System.out.println("file " + fname + " does not exist");
57+
System.out.println("Exiting");
58+
return;
59+
}
60+
61+
while (s.hasNextLine()){
62+
acceptedNames.add(s.nextLine());
63+
}
64+
}
65+
66+
public void inspectSourceCode(File javaFile) {
67+
this.javaFilePath = javaFile.getPath();
68+
System.out.println("Inspecting " + this.javaFilePath);
69+
CompilationUnit root = Common.getParseUnit(javaFile);
70+
if (root != null) {
71+
this.visit(root.clone(), null);
72+
}
73+
}
74+
75+
@Override
76+
public void visit(CompilationUnit com, Object obj) {
77+
locateTargetStatements(com, obj, this.typeToTarget);
78+
applyManager(com);
79+
super.visit(com, obj);
80+
}
81+
82+
private ArrayList<String> splitNameByToken(String name) {
83+
char prev = '0';
84+
String token = "";
85+
ArrayList<String> tokens = new ArrayList<String>();
86+
87+
for( int i =0; i < name.length(); i++){
88+
char c = name.charAt(i);
89+
if (i > 0 && ((Character.isUpperCase(c) && Character.isLowerCase(prev)) || c == '_') && token.length() > 0) {
90+
tokens.add(token);
91+
token = "";
92+
}
93+
94+
prev = c;
95+
token += c;
96+
}
97+
98+
if (token.length() > 0){
99+
tokens.add(token);
100+
}
101+
102+
return tokens;
103+
}
104+
105+
private boolean isTypeOfTarget(String typeToTarget, Node node){
106+
107+
switch(typeToTarget){
108+
case "MethodDeclaration":
109+
return (node instanceof MethodDeclaration);
110+
111+
case "VariableDeclarationExpr":
112+
return (node instanceof VariableDeclarationExpr);
113+
114+
default:
115+
System.out.println("ERROR: no support for target type: " + typeToTarget);
116+
return false;
117+
}
118+
}
119+
120+
private void locateTargetStatements(CompilationUnit com, Object obj, String typeToTarget) {
121+
try{
122+
new TreeVisitor() {
123+
@Override
124+
public void process(Node node) {
125+
if (isTypeOfTarget(typeToTarget, node)) {
126+
127+
switch (typeToTarget) {
128+
129+
case "MethodDeclaration":
130+
MethodDeclaration md = (MethodDeclaration) node;
131+
String node_name = md.getNameAsString();
132+
133+
ArrayList<String> names = MethodLines.seq ? splitNameByToken(node_name) : new ArrayList<String>(Arrays.asList(node_name));
134+
135+
for (int i=0; i<names.size(); i++) {
136+
String token = names.get(i);
137+
if(!acceptedNames.contains(token)){
138+
rem.add(node);
139+
break;
140+
}
141+
}
142+
143+
break;
144+
145+
case "VariableDeclarationExpr":
146+
Range r = node.getRange().orElse(null);
147+
try {
148+
fr.write(javaFilePath + " : " + typeToTarget + " : " + r.toString() + "\n");
149+
} catch (IOException e) {
150+
System.out.println(e);
151+
System.out.println("Can't write to file!");
152+
}
153+
break;
154+
155+
default:
156+
System.out.println("ERROR: no support for target type: " + typeToTarget);
157+
158+
}
159+
160+
}
161+
}
162+
163+
}.visitBreadthFirst(com);
164+
165+
}catch(Exception e){
166+
System.out.println(e);
167+
}
168+
}
169+
170+
private void applyManager(CompilationUnit com) {
171+
for (int i=0; i<rem.size(); i++) {
172+
try{
173+
rem.get(i).remove();
174+
}catch(Exception e){
175+
System.out.println("Can't remove"+ ((MethodDeclaration)rem.get(i)).getNameAsString());
176+
}
177+
}
178+
179+
Common.saveTransformation(com);
180+
}
181+
182+
public static void main(String[] args){
183+
String inPath = "/data2/edinella/java-all-redist-fs/";
184+
Common.outputPath = "/data2/edinella/java-all-redist-g/";
185+
//Common.outputPath = "/data2/edinella/java-large-clean/";
186+
187+
String typeToTarget = "VariableDeclarationExpr";
188+
//MethodLines.seq = false;
189+
//MethodLines.set_clean_names("/home/edinella/clean_names.txt");
190+
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+
}
197+
198+
File programFolder = new File(inPath);
199+
200+
File[] files = Utils.getAllSubFiles(programFolder);
201+
202+
ExecutorService executor = Executors.newFixedThreadPool(70);
203+
//ExecutorService executor = Executors.newFixedThreadPool(1);
204+
205+
for (File file : files) {
206+
Thread worker = new Thread(new ThreadedManager(file, typeToTarget));
207+
executor.execute(worker);
208+
}
209+
210+
try {
211+
while (!executor.awaitTermination(24L, TimeUnit.HOURS)) {
212+
213+
}
214+
} catch (InterruptedException e) {
215+
System.out.println("interrupted");
216+
}
217+
218+
executor.shutdown();
219+
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+
}
227+
}
148228
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ThreadedManager implements Runnable {
88
private File file;
99
private MethodLines mn;
1010

11-
public ThreadedManager(File file){
11+
public ThreadedManager(File file, String typeToTarget){
1212
this.file = file;
13-
mn = new MethodLines();
13+
mn = new MethodLines(typeToTarget);
1414
}
1515

1616
public void run() {

0 commit comments

Comments
 (0)