Skip to content

Commit f383db8

Browse files
author
ar.r.lysenko
committed
add field counter
1 parent e3c338a commit f383db8

File tree

6 files changed

+134
-58
lines changed

6 files changed

+134
-58
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.example;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.nio.file.Path;
6+
import java.util.jar.JarFile;
7+
import org.example.visitor.ClassStatisticVisitor;
8+
import org.objectweb.asm.ClassReader;
9+
import org.objectweb.asm.ClassVisitor;
10+
11+
public class Analyzer {
12+
13+
public static void main(String[] args) throws IOException {
14+
var path = Path.of("src/main/resources/sample.jar");
15+
var analyzer = new Analyzer();
16+
17+
analyzer.analyzeJar(path);
18+
}
19+
20+
public void analyzeJar(Path path) throws IOException {
21+
try (JarFile jar = new JarFile(path.toFile())) {
22+
23+
Statistic statistic = new Statistic();
24+
25+
jar.stream()
26+
.filter(entry -> entry.getName().endsWith(".class"))
27+
.forEach(entry -> {
28+
// Should be safe for Java classes, since they always in the class with same name
29+
// unlike kotlin classes
30+
String className = entry.getName().replace(".class", "");
31+
32+
try (InputStream is = jar.getInputStream(entry)) {
33+
ClassReader cr = new ClassReader(is);
34+
ClassVisitor visitor = new ClassStatisticVisitor(statistic);
35+
cr.accept(visitor, 0);
36+
} catch (IOException e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
);
41+
42+
statistic.printStatistic();
43+
}
44+
}
45+
}

src/main/java/org/example/Example.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example;
2+
3+
import org.example.util.AverageHolder;
4+
5+
public class Statistic {
6+
7+
private final AverageHolder fieldAverageHolder = new AverageHolder();
8+
9+
public void updateFieldStatistic(Integer fieldCount) {
10+
fieldAverageHolder.updateAverage(fieldCount);
11+
}
12+
13+
public void printStatistic() {
14+
System.out.print("Average: " + fieldAverageHolder.getAverage());
15+
}
16+
}

src/main/java/org/example/example/BubbleSort.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.example.util;
2+
3+
public class AverageHolder {
4+
private final Object lock = new Object();
5+
private Integer sum = 0;
6+
private Integer count = 0;
7+
8+
public Double getAverage() {
9+
return count.equals(0) ? 0.0 : sum.doubleValue() / count;
10+
}
11+
12+
public void updateAverage(Integer value) {
13+
synchronized (lock) {
14+
sum += value;
15+
count++;
16+
}
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.example.visitor;
2+
3+
import org.example.Statistic;
4+
import org.objectweb.asm.AnnotationVisitor;
5+
import org.objectweb.asm.Attribute;
6+
import org.objectweb.asm.ClassVisitor;
7+
import org.objectweb.asm.FieldVisitor;
8+
import org.objectweb.asm.MethodVisitor;
9+
import static org.objectweb.asm.Opcodes.ASM8;
10+
11+
public class ClassStatisticVisitor extends ClassVisitor {
12+
13+
private final Statistic statistic;
14+
15+
private Integer fieldCount = 0;
16+
17+
public ClassStatisticVisitor(Statistic statistic) {
18+
super(ASM8);
19+
this.statistic = statistic;
20+
}
21+
22+
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
23+
}
24+
25+
public void visitSource(String source, String debug) {
26+
}
27+
28+
public void visitOuterClass(String owner, String name, String desc) {
29+
}
30+
31+
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
32+
return super.visitAnnotation(desc, visible);
33+
}
34+
35+
public void visitAttribute(Attribute attr) {
36+
}
37+
38+
// todo also visit inner classes
39+
public void visitInnerClass(String name, String outerName, String innerName, int access) {
40+
}
41+
42+
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
43+
fieldCount++;
44+
return super.visitField(access, name, desc, signature, value);
45+
}
46+
47+
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
48+
return super.visitMethod(access, name, desc, signature, exceptions);
49+
}
50+
51+
public void visitEnd() {
52+
statistic.updateFieldStatistic(fieldCount);
53+
}
54+
}
55+

0 commit comments

Comments
 (0)