Skip to content

Commit 4fd6b53

Browse files
add a new feature to detect any XML file beside java file and print the result in separate output file also extract the layout XML file name from the class which is using the layout XML.
1 parent 4957b14 commit 4fd6b53

File tree

7 files changed

+135
-13
lines changed

7 files changed

+135
-13
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ hs_err_pid*
2323

2424
# IntelliJ IDEA
2525
.idea
26+
*.csv
27+
*.txt

FileDetector.iml

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
66
<content url="file://$MODULE_DIR$">
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8-
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9-
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
108
<excludeFolder url="file://$MODULE_DIR$/target" />
119
</content>
1210
<orderEntry type="inheritedJdk" />

src/main/java/FileAnalyzer.java

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.github.javaparser.ast.ImportDeclaration;
44
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
55
import com.github.javaparser.ast.body.MethodDeclaration;
6+
import com.github.javaparser.ast.expr.MethodCallExpr;
7+
import com.github.javaparser.ast.type.TypeParameter;
68
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
79
import entity.ClassEntity;
810
import entity.MethodEntity;
@@ -55,6 +57,7 @@ public ClassEntity runAnalysis(Path filePath) throws IOException {
5557

5658
classEntity.setMethods(methods);
5759
classEntity.setImports(imports);
60+
5861
}
5962

6063
return classEntity;
@@ -69,16 +72,33 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {
6972
super.visit(n, arg);
7073
}
7174

75+
76+
7277
@Override
7378
public void visit(MethodDeclaration n, Void arg) {
7479
MethodEntity method = new MethodEntity(n.getNameAsString());
80+
7581
method.setTotalStatements(n.getBody().get().getStatements().size());
7682
method.setParameterCount(n.getParameters().size());
7783
method.setReturnType(n.getType().toString());
7884
method.setAccessModifier(n.getModifiers().stream().map(i -> i.asString()).collect(Collectors.joining("; ")));
7985

8086
methods.add(method);
87+
super.visit(n, arg);
88+
}
8189

90+
@Override
91+
public void visit(MethodCallExpr n, Void arg) {
92+
if (n.getName().asString().equals("setContentView")){
93+
String layerName = n.getArgument(0).toString();
94+
String [] splitLayerName = layerName.split("\\.");
95+
if (splitLayerName.length>1){
96+
classEntity.setLayoutName( splitLayerName[2]+".xml");
97+
}else {
98+
classEntity.setLayoutName( layerName);
99+
100+
}
101+
}
82102
super.visit(n, arg);
83103
}
84104

src/main/java/FileWalker.java

+36-3
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@
66

77
public class FileWalker {
88
private List<Path> files;
9+
private List<Path> SecondExtFiles;
910

1011

11-
public List<Path> getFiles(String directoryPath, boolean recursive, String extension) throws IOException {
12+
public List<List<Path>> getFiles(String directoryPath, boolean recursive, String extension, String SecondExtension) throws IOException {
1213
files = new ArrayList<>();
14+
SecondExtFiles = new ArrayList<>();
15+
ArrayList container = new ArrayList<>();
16+
1317
Path startDir = Paths.get(directoryPath);
1418

1519
if (recursive) {
1620
Files.walkFileTree(startDir, new FindJavaFilesVisitor(extension));
21+
Files.walkFileTree(startDir, new FindXmlFilesVisitor(SecondExtension));
22+
1723
} else {
1824
Files.walk(startDir, 1)
1925
.filter(Files::isRegularFile)
2026
.forEach(filePath -> {
2127
if (filePath.toString().toLowerCase().endsWith("."+extension)) {
2228
files.add(filePath);
2329
}
30+
if (filePath.toString().toLowerCase().endsWith("."+SecondExtension)){
31+
SecondExtFiles.add(filePath);
32+
33+
}
34+
2435
});
2536
}
26-
return files;
37+
container.add(files);
38+
container.add(SecondExtFiles);
39+
40+
return container;
2741
}
2842

2943
private class FindJavaFilesVisitor extends SimpleFileVisitor<Path> {
@@ -39,7 +53,26 @@ public FileVisitResult visitFile(Path file,
3953
throws IOException {
4054

4155
if (file.toString().toLowerCase().endsWith("."+extension)) {
42-
files.add(file);
56+
files.add(file);
57+
}
58+
59+
return FileVisitResult.CONTINUE;
60+
}
61+
}
62+
private class FindXmlFilesVisitor extends SimpleFileVisitor<Path> {
63+
private String extension;
64+
65+
FindXmlFilesVisitor(String extension) {
66+
this.extension = extension;
67+
}
68+
69+
@Override
70+
public FileVisitResult visitFile(Path file,
71+
BasicFileAttributes attrs)
72+
throws IOException {
73+
74+
if (file.toString().toLowerCase().endsWith("."+extension)) {
75+
SecondExtFiles.add(file);
4376
}
4477

4578
return FileVisitResult.CONTINUE;

src/main/java/Main.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@
99
public class Main {
1010
public static void main(String[] args) throws IOException {
1111
String fileExtension = "java";
12-
final String rootDirectory = "C:\\Projects\\AndroidCodeSmells\\FileDetector\\src\\main\\java\\entity";
12+
String secondFileExtension = "xml";
13+
14+
final String rootDirectory = "\\\\Mac\\Home\\Desktop\\SampleTesting";
1315
FileAnalyzer fileAnalyzer = FileAnalyzer.createFileAnalyzer();
1416
ResultsWriter resultsWriter = ResultsWriter.createResultsWriter();
1517
ClassEntity classEntity;
18+
ClassEntity xmlEntity;
1619

1720
//recursively identify all files with the specified extension in the specified directory
1821
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Started);
1922
FileWalker fw = new FileWalker();
20-
List<Path> files = fw.getFiles(rootDirectory, true,fileExtension);
23+
List<List<Path>> files = fw.getFiles(rootDirectory, true,fileExtension,secondFileExtension);
2124
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Completed);
2225

2326

2427
//foreach of the identified 'java' files, obtain details about the methods that they contain
2528
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Started);
26-
for (Path file : files) {
29+
for (Path file : files.get(0)) {
2730
try {
2831
classEntity = fileAnalyzer.runAnalysis(file);
2932
resultsWriter.outputToCSV(classEntity);
@@ -33,6 +36,21 @@ public static void main(String[] args) throws IOException {
3336
}
3437
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Completed);
3538

39+
Util.writeOperationLogEntry("Obtain Xml details", Util.OperationStatus.Started);
40+
41+
for (Path file : files.get(1)) {
42+
try {
43+
44+
xmlEntity = new ClassEntity(file);
45+
resultsWriter.outputXmlToCSV(xmlEntity);
46+
47+
} catch (Exception e) {
48+
Util.writeException(e, "XML: " + file.toAbsolutePath().toString());
49+
}
50+
}
51+
Util.writeOperationLogEntry("Obtain Xml details", Util.OperationStatus.Completed);
52+
53+
3654

3755
resultsWriter.closeOutputFiles();
3856
}

src/main/java/ResultsWriter.java

+45-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class ResultsWriter {
1313

14-
private CSVWriter classCSVWriter, methodCSVWriter;
14+
private CSVWriter classCSVWriter, methodCSVWriter,xmlCSVWriter;
1515

1616
public static ResultsWriter createResultsWriter() throws IOException {
1717
return new ResultsWriter();
@@ -21,13 +21,31 @@ private ResultsWriter() throws IOException {
2121
String time = String.valueOf(Calendar.getInstance().getTimeInMillis());
2222
String classFileName = MessageFormat.format("{0}_{1}_{2}.{3}", "Output", "Class", time, "csv");
2323
String methodFileName = MessageFormat.format("{0}_{1}_{2}.{3}", "Output", "Method", time, "csv");
24+
String xmlFileName = MessageFormat.format("{0}_{1}_{2}.{3}", "Output", "Xml", time, "csv");
25+
2426
methodCSVWriter = new CSVWriter(new FileWriter(methodFileName), ',');
2527
classCSVWriter = new CSVWriter(new FileWriter(classFileName), ',');
28+
xmlCSVWriter = new CSVWriter(new FileWriter(xmlFileName), ',');
2629

2730
createClassFile();
2831
createMethodFile();
32+
createXmlFile();
2933
}
3034

35+
private void createXmlFile() throws IOException {
36+
List<String[]> fileLines = new ArrayList<String[]>();
37+
String[] columnNames = {
38+
"App",
39+
"Tag",
40+
"FilePath",
41+
"RelativeFilePath",
42+
"FileName"
43+
};
44+
fileLines.add(columnNames);
45+
46+
xmlCSVWriter.writeAll(fileLines, false);
47+
xmlCSVWriter.flush();
48+
}
3149
private void createClassFile() throws IOException {
3250
List<String[]> fileLines = new ArrayList<String[]>();
3351
String[] columnNames = {
@@ -39,7 +57,7 @@ private void createClassFile() throws IOException {
3957
"ClassName",
4058
"TotalImports",
4159
"TotalMethods",
42-
"TotalMethodStatements"
60+
"TotalMethodStatements","LayoutName"
4361
};
4462
fileLines.add(columnNames);
4563

@@ -61,6 +79,7 @@ private void createMethodFile() throws IOException {
6179
"TotalParameters",
6280
"ReturnType",
6381
"AccessModifier"
82+
6483
};
6584
fileLines.add(columnNames);
6685

@@ -72,10 +91,13 @@ public void outputToCSV(ClassEntity classEntity) throws IOException {
7291
outputClassDetails(classEntity);
7392
outputMethodDetails(classEntity);
7493
}
75-
94+
public void outputXmlToCSV(ClassEntity classEntity) throws IOException {
95+
outputXmlDetails(classEntity);
96+
}
7697
public void closeOutputFiles() throws IOException {
7798
classCSVWriter.close();
7899
methodCSVWriter.close();
100+
79101
}
80102

81103
private void outputMethodDetails(ClassEntity classEntity) throws IOException {
@@ -102,11 +124,11 @@ private void outputMethodDetails(ClassEntity classEntity) throws IOException {
102124
methodCSVWriter.flush();
103125
}
104126

105-
private void outputClassDetails(ClassEntity classEntity) throws IOException {
127+
private void outputClassDetails(ClassEntity classEntity) throws IOException {
106128
List<String[]> fileLines = new ArrayList<String[]>();
107129
String[] dataLine;
108130

109-
dataLine = new String[9];
131+
dataLine = new String[10];
110132
dataLine[0] = classEntity.getAppName();
111133
dataLine[1] = classEntity.getTagName();
112134
dataLine[2] = classEntity.getFilePath();
@@ -116,10 +138,28 @@ private void outputClassDetails(ClassEntity classEntity) throws IOException {
116138
dataLine[6] = String.valueOf(classEntity.getTotalImports());
117139
dataLine[7] = String.valueOf(classEntity.getTotalMethods());
118140
dataLine[8] = String.valueOf(classEntity.getTotalMethodStatement());
141+
dataLine[9] = classEntity.getLayoutName();
119142

120143
fileLines.add(dataLine);
121144

122145
classCSVWriter.writeAll(fileLines, false);
123146
classCSVWriter.flush();
124147
}
148+
149+
private void outputXmlDetails(ClassEntity classEntity) throws IOException {
150+
List<String[]> fileLines = new ArrayList<String[]>();
151+
String[] dataLine;
152+
153+
dataLine = new String[5];
154+
dataLine[0] = classEntity.getAppName();
155+
dataLine[1] = classEntity.getTagName();
156+
dataLine[2] = classEntity.getFilePath();
157+
dataLine[3] = classEntity.getRelativeFilePath();
158+
dataLine[4] = classEntity.getFileName();
159+
160+
fileLines.add(dataLine);
161+
162+
xmlCSVWriter.writeAll(fileLines, false);
163+
xmlCSVWriter.flush();
164+
}
125165
}

src/main/java/entity/ClassEntity.java

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ public class ClassEntity {
1111
private ArrayList<String> imports;
1212
private String className;
1313

14+
public String getLayoutName() {
15+
return layoutName;
16+
}
17+
18+
public void setLayoutName(String layoutName) {
19+
this.layoutName = layoutName;
20+
}
21+
22+
private String layoutName;
23+
1424
public ClassEntity(Path path) {
1525
this.path = path;
1626
}
@@ -74,6 +84,7 @@ public String getAppName() {
7484

7585
public String getTagName() {
7686
String filePath = path.toAbsolutePath().toString();
87+
7788
return filePath.split("\\\\")[4];
7889
}
7990
}

0 commit comments

Comments
 (0)