-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaStatisticsScanner.java
More file actions
executable file
·152 lines (125 loc) · 4.43 KB
/
JavaStatisticsScanner.java
File metadata and controls
executable file
·152 lines (125 loc) · 4.43 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package de.ubt.ai1.mvmt.util;
import java.util.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedList;
import java.util.List;
/**
*
* The JavaStatisticsScanner can analyze a given directory and count the number of lines of code, words, characters in Java files.
* Furthermore, it can count the number of OpenJML pre- and postconditions.
*
* @author Sandra Greiner
*
*/
public class JavaStatisticsScanner {
public static void main(String[] args) throws FileNotFoundException {
if (args.length <= 0){
System.out.println( "please specify the path of the project that should be analysed relative from to where this file is executed as argument");
return;
}
List<String> mvFiles = extractFilesFromPath(args[0]);//new LinkedList<String>();
// get multi-variant source files
long lineCT = 0;
long annotationCt = 0;
for (String f : mvFiles) {
File file = new File(f);
Scanner in = new Scanner(file);
int words = 0;
int lines = 0;
int chars = 0;
int ensures = 0;
int requires = 0;
String res = "";
while (in.hasNextLine()) {
res = in.nextLine();
if (res.length() > 0)
lines++;
String[] wordArray = res.split(" ");
for (String s : wordArray) {
if (s.length() > 1)
words++;
chars += s.length();
}
if (res.startsWith("//@ ensures"))
ensures++;
else if (res.startsWith("//@ requires"))
requires++;
}
lineCT += lines;
annotationCt += (ensures+requires);
in.close();
}
System.out.println(" ################## Summary ################## " + lineCT);
System.out.println("Number of lines: " + lineCT);
System.out.println("Number of annotations: " + (annotationCt));
}
public static List<String> extractFilesFromPath(String path) {
List<String> mvFiles = new LinkedList<String>();
// get multi-variant source files
FileIterator<Path> fiMV = new FileIterator<Path>();
try {
Files.walkFileTree(Paths.get(path), fiMV);
} catch (IOException e) {
e.printStackTrace();
}
// store the java-files
for (Path srcFile : fiMV.getRegFiles()) {
String fileName = srcFile.getFileName().toString();
if (fileName.lastIndexOf(".") > -1 && fileName.substring(fileName.lastIndexOf(".")).equals(".java")) {
mvFiles.add(srcFile.toString());
}
}
return mvFiles;
}
static class FileIterator<Path> implements FileVisitor<Path> {
private List<Path> regFiles = new LinkedList<Path>();
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
if (attr.isSymbolicLink()) {
System.out.format("Symbolic link: %s ", file);
} else if (attr.isRegularFile()) {
regFiles.add(file);
} else {
System.out.format("Other: %s ", file);
}
System.out.println("(" + attr.size() + "bytes)");
return FileVisitResult.CONTINUE;
}
public List<Path> getRegFiles() {
return regFiles;
}
public URL[] getFileURIs() {
URL[] list = new URL[regFiles.size()];
for (int i = 0; i < regFiles.size(); i++) {
Path p = regFiles.get(i);
try {
list[i] = new URL(p.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
}
}