Skip to content

Commit 32694ee

Browse files
add xmlEntity class to get help obtain the value of analyzed xml file
1 parent 4fd6b53 commit 32694ee

11 files changed

+401
-96
lines changed

FileDetector.iml

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
</content>
1010
<orderEntry type="inheritedJdk" />
1111
<orderEntry type="sourceFolder" forTests="false" />
12-
<orderEntry type="library" name="Maven: com.github.javaparser:javaparser-core:3.2.4" level="project" />
13-
<orderEntry type="library" name="Maven: com.github.javaparser:java-symbol-solver-core:0.5.5" level="project" />
14-
<orderEntry type="library" name="Maven: com.github.javaparser:java-symbol-solver-logic:0.5.5" level="project" />
15-
<orderEntry type="library" name="Maven: com.javaslang:javaslang:2.0.0-beta" level="project" />
16-
<orderEntry type="library" name="Maven: io.javaslang:javaslang:2.0.3" level="project" />
17-
<orderEntry type="library" name="Maven: io.javaslang:javaslang-match:2.0.3" level="project" />
18-
<orderEntry type="library" name="Maven: com.github.javaparser:java-symbol-solver-model:0.5.5" level="project" />
19-
<orderEntry type="library" name="Maven: org.javassist:javassist:3.19.0-GA" level="project" />
20-
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
12+
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.0" level="project" />
13+
<orderEntry type="library" name="Maven: jaxen:jaxen:1.1.6" level="project" />
14+
<orderEntry type="library" name="Maven: com.github.javaparser:javaparser-symbol-solver-core:3.5.13" level="project" />
15+
<orderEntry type="library" name="Maven: com.github.javaparser:javaparser-symbol-solver-logic:3.5.13" level="project" />
16+
<orderEntry type="library" name="Maven: org.javassist:javassist:3.22.0-GA" level="project" />
17+
<orderEntry type="library" name="Maven: com.github.javaparser:javaparser-symbol-solver-model:3.5.13" level="project" />
18+
<orderEntry type="library" name="Maven: com.github.javaparser:javaparser-core:3.5.13" level="project" />
19+
<orderEntry type="library" name="Maven: com.google.guava:guava:23.4-jre" level="project" />
20+
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
21+
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.0.18" level="project" />
22+
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.1" level="project" />
23+
<orderEntry type="library" name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
2124
<orderEntry type="library" name="Maven: com.opencsv:opencsv:3.9" level="project" />
2225
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.5" level="project" />
2326
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.9.3" level="project" />

pom.xml

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
</build>
2222

2323
<dependencies>
24+
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
2425
<dependency>
25-
<groupId>com.github.javaparser</groupId>
26-
<artifactId>javaparser-core</artifactId>
27-
<version>3.2.4</version>
26+
<groupId>org.dom4j</groupId>
27+
<artifactId>dom4j</artifactId>
28+
<version>2.1.0</version>
2829
</dependency>
2930
<dependency>
3031
<groupId>com.github.javaparser</groupId>
31-
<artifactId>java-symbol-solver-core</artifactId>
32-
<version>0.5.5</version>
32+
<artifactId>javaparser-symbol-solver-core</artifactId>
33+
<version>3.5.13</version>
3334
</dependency>
3435

36+
3537
<dependency>
3638
<groupId>com.opencsv</groupId>
3739
<artifactId>opencsv</artifactId>

src/main/java/FileAnalyzer.java

+43-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import com.github.javaparser.JavaParser;
22
import com.github.javaparser.ast.CompilationUnit;
33
import com.github.javaparser.ast.ImportDeclaration;
4+
import com.github.javaparser.ast.PackageDeclaration;
45
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
56
import com.github.javaparser.ast.body.MethodDeclaration;
6-
import com.github.javaparser.ast.expr.MethodCallExpr;
7-
import com.github.javaparser.ast.type.TypeParameter;
7+
import com.github.javaparser.ast.comments.LineComment;
8+
import com.github.javaparser.ast.expr.Name;
89
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
910
import entity.ClassEntity;
1011
import entity.MethodEntity;
@@ -38,75 +39,88 @@ public static FileAnalyzer createFileAnalyzer() {
3839
return new FileAnalyzer();
3940
}
4041

41-
private void initialize(){
42+
private void initialize() {
4243
methods = new ArrayList<>();
4344
imports = new ArrayList<>();
4445
}
4546

4647
public ClassEntity runAnalysis(Path filePath) throws IOException {
4748
initialize();
4849

49-
CompilationUnit compilationUnit=null;
50+
CompilationUnit compilationUnit = null;
5051

51-
if(filePath != null){
52-
classEntity = new ClassEntity(filePath);
53-
fileInputStream = new FileInputStream(classEntity.getFilePath());
54-
compilationUnit = JavaParser.parse(fileInputStream);
55-
ClassVisitor cv = new ClassVisitor();
56-
cv.visit(compilationUnit,null);
52+
classEntity = new ClassEntity(filePath);
5753

58-
classEntity.setMethods(methods);
59-
classEntity.setImports(imports);
54+
// disable to get the file details
55+
fileInputStream = new FileInputStream(classEntity.getFilePath());
56+
compilationUnit = JavaParser.parse(fileInputStream);
57+
58+
59+
FileAnalyzer.ClassVisitor classVisitor;
60+
classVisitor = new FileAnalyzer.ClassVisitor();
61+
62+
classVisitor.visit(compilationUnit, null);
63+
64+
classEntity.setMethods(methods);
65+
classEntity.setImports(imports);
6066

61-
}
6267

6368
return classEntity;
6469
}
6570

6671

6772
private class ClassVisitor extends VoidVisitorAdapter<Void> {
6873

74+
6975
@Override
70-
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
76+
public void visit(PackageDeclaration n, Void arg) {
7177
classEntity.setClassName(n.getNameAsString());
7278
super.visit(n, arg);
7379
}
7480

81+
@Override
82+
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
83+
classEntity.setClassName(n.getNameAsString());
84+
super.visit(n, arg);
7585

7686

87+
}
88+
7789
@Override
7890
public void visit(MethodDeclaration n, Void arg) {
91+
92+
7993
MethodEntity method = new MethodEntity(n.getNameAsString());
94+
if (n.getBody().isPresent()) {
95+
method.setTotalStatements(n.getBody().get().getStatements().size());
96+
method.setIsConcrete("true");
8097

81-
method.setTotalStatements(n.getBody().get().getStatements().size());
98+
}else {
99+
method.setIsConcrete("false");
100+
}
82101
method.setParameterCount(n.getParameters().size());
102+
83103
method.setReturnType(n.getType().toString());
84104
method.setAccessModifier(n.getModifiers().stream().map(i -> i.asString()).collect(Collectors.joining("; ")));
85105

106+
86107
methods.add(method);
87108
super.visit(n, arg);
88-
}
89109

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-
}
102-
super.visit(n, arg);
103110
}
104111

112+
113+
114+
105115
@Override
106116
public void visit(ImportDeclaration n, Void arg) {
117+
107118
imports.add(n.getNameAsString());
108119

109120
super.visit(n, arg);
121+
122+
110123
}
111124
}
112125
}
126+

src/main/java/FileWalker.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ private class FindXmlFilesVisitor extends SimpleFileVisitor<Path> {
6767
}
6868

6969
@Override
70-
public FileVisitResult visitFile(Path file,
71-
BasicFileAttributes attrs)
72-
throws IOException {
70+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
7371

7472
if (file.toString().toLowerCase().endsWith("."+extension)) {
7573
SecondExtFiles.add(file);

src/main/java/Main.java

+60-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import entity.ClassEntity;
2+
import entity.XmlEntity;
23

4+
import java.io.File;
5+
import java.io.FileFilter;
36
import java.io.IOException;
47
import java.nio.file.Path;
5-
import java.util.ArrayList;
68
import java.util.List;
79

810

@@ -11,44 +13,73 @@ public static void main(String[] args) throws IOException {
1113
String fileExtension = "java";
1214
String secondFileExtension = "xml";
1315

14-
final String rootDirectory = "\\\\Mac\\Home\\Desktop\\SampleTesting";
16+
1517
FileAnalyzer fileAnalyzer = FileAnalyzer.createFileAnalyzer();
18+
XmlFileAnalyzer xmlfileAnalyzer = XmlFileAnalyzer.createFileAnalyzer();
19+
1620
ResultsWriter resultsWriter = ResultsWriter.createResultsWriter();
1721
ClassEntity classEntity;
18-
ClassEntity xmlEntity;
19-
20-
//recursively identify all files with the specified extension in the specified directory
21-
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Started);
22-
FileWalker fw = new FileWalker();
23-
List<List<Path>> files = fw.getFiles(rootDirectory, true,fileExtension,secondFileExtension);
24-
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Completed);
25-
26-
27-
//foreach of the identified 'java' files, obtain details about the methods that they contain
28-
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Started);
29-
for (Path file : files.get(0)) {
30-
try {
31-
classEntity = fileAnalyzer.runAnalysis(file);
32-
resultsWriter.outputToCSV(classEntity);
33-
} catch (Exception e) {
34-
Util.writeException(e, "File: " + file.toAbsolutePath().toString());
22+
XmlEntity xmlEntity;
23+
24+
File rootDirectory = new File("G:\\Android\\Apps");
25+
FileFilter filter = new FileFilter() {
26+
@Override
27+
public boolean accept(File pathname) {
28+
return pathname.isDirectory();
3529
}
36-
}
37-
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Completed);
30+
};
31+
File[] directories = rootDirectory.listFiles( filter);
32+
33+
for (File dir: directories){
3834

39-
Util.writeOperationLogEntry("Obtain Xml details", Util.OperationStatus.Started);
35+
//recursively identify all files with the specified extension in the specified directory
4036

41-
for (Path file : files.get(1)) {
42-
try {
37+
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Started);
38+
FileWalker fw = new FileWalker();
39+
List<List<Path>> files = fw.getFiles(dir.getPath(),true,fileExtension,secondFileExtension);
40+
Util.writeOperationLogEntry("Identify all '"+fileExtension+"' test files", Util.OperationStatus.Completed);
4341

44-
xmlEntity = new ClassEntity(file);
45-
resultsWriter.outputXmlToCSV(xmlEntity);
4642

47-
} catch (Exception e) {
48-
Util.writeException(e, "XML: " + file.toAbsolutePath().toString());
43+
//foreach of the identified 'java' files, obtain details about the methods that they contain
44+
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Started);
45+
for (Path file : files.get(0)) {
46+
try {
47+
classEntity = fileAnalyzer.runAnalysis(file);
48+
resultsWriter.outputToCSV(classEntity);
49+
} catch (Exception e) {
50+
Util.writeException(e, "File: " + file.toAbsolutePath().toString());
51+
}
4952
}
53+
Util.writeOperationLogEntry("Obtain method details", Util.OperationStatus.Completed);
54+
55+
Util.writeOperationLogEntry("Obtain Xml details"+files.get(1).size(), Util.OperationStatus.Started);
56+
57+
for (Path file : files.get(1)) {
58+
try {
59+
60+
System.out.println("obtain xml"+file.toAbsolutePath().toString());
61+
62+
xmlEntity = xmlfileAnalyzer.runAnalysis(file);
63+
64+
resultsWriter.outputXmlToCSV(xmlEntity);
65+
66+
67+
68+
} catch (Exception e) {
69+
Util.writeException(e, "XML: " + file.toAbsolutePath().toString());
70+
}
71+
72+
73+
}
74+
Util.writeOperationLogEntry("Obtain Xml details", Util.OperationStatus.Completed);
75+
76+
5077
}
51-
Util.writeOperationLogEntry("Obtain Xml details", Util.OperationStatus.Completed);
78+
79+
80+
81+
82+
5283

5384

5485

0 commit comments

Comments
 (0)