-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCountMostImport.java
More file actions
104 lines (92 loc) · 4.06 KB
/
CountMostImport.java
File metadata and controls
104 lines (92 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Created by KL on 2016/6/29.
*/
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Ordering;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.google.common.primitives.Ints.min;
import static java.lang.Math.PI;
public class CountMostImport {
//Match files with suffix java
private static final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java");
//Most N imported classes
private static final int mostN = 1000;
private List<File> javaFiles = new ArrayList<File>();
//Scan path for java files
Path path = Paths.get("E:\\code\\work\\campus2016");
private void getJavaFiles() {
SimpleFileVisitor<Path> javaFinder = new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(matcher.matches(file))
javaFiles.add(file.toFile());
return super.visitFile(file, attrs);
}
};
try {
java.nio.file.Files.walkFileTree(path, javaFinder);
System.out.println("files = " + javaFiles);
} catch (IOException e) {
e.printStackTrace();
}
}
public List<Map.Entry<String, Long>> countMostImportClass() {
Map<String, Long> result = new HashMap<>();
getJavaFiles();
for (File f : javaFiles) {
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(f))) { // Java7 try with resources
String line = null;
while ((line = bufferedReader.readLine()) != null) {
//process perline
String trimedLine = line.trim();
if(!trimedLine.startsWith("import"))
continue;
//import statement
List<String> pieces = Splitter.on(CharMatcher.anyOf(" ;"))
.trimResults()
.omitEmptyStrings()
.splitToList(trimedLine);
if (pieces.get(pieces.size() - 1).endsWith("*"))
continue;
else if(pieces.size() == 3) {
//Static import
String methodName = pieces.get(pieces.size() - 1);
String classQualifiedName = methodName.substring(0, methodName.lastIndexOf('.'));
result.put(classQualifiedName, result.containsKey(classQualifiedName) ? result.get(classQualifiedName) + 1 : 1);
} else if(pieces.size() == 2) {
// None static
String classQualifiedName = pieces.get(pieces.size() - 1);
result.put(classQualifiedName, result.containsKey(classQualifiedName) ? result.get(classQualifiedName) + 1 : 1);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// for end
//Guava Ordering based on map.value
Ordering<Map.Entry<String, Long>> valueOrdering = Ordering.natural().onResultOf(
new Function<Map.Entry<String, Long>, Long>() {
@Override
public Long apply(Map.Entry<String, Long> entry) {
return entry.getValue();
}
}).reverse();
List< Map.Entry<String, Long>> l = valueOrdering.sortedCopy(result.entrySet());
return l.subList(0, min(l.size(), mostN));
}
public static void main(String[] args) {
System.out.println(PI);
List<Map.Entry<String, Long>> l = new CountMostImport().countMostImportClass();
System.out.println(l);
}
}