ClassVisitor
- Java course
- Expert
- Lesson 39/46
- 6 min
Write method and field scanning with ClassVisitor.
Implementing ClassVisitor
Since ClassVisitor is an interface, concrete classes that implement it catch the events. ClassWriter is one of them: it collects visit events in a StringBuilder and gives text output with toString. This corresponds to the writer role in ASM.
To implement an interface a class uses the implements keyword and writes all methods. The visitor then produces the output of the traversal.
Collecting with StringBuilder
StringBuilder is a class that joins text pieces efficiently. You chain additions with the append method: sb.append("Class: ").append(n).append("\n");. At the end toString returns the whole text.
ClassWriter calls sb.append(...) on every visit event. When toString is called it returns the accumulated text. So the traversal result becomes a single output.
Practice Write the ClassWriter class
Add a class ClassWriter implements ClassVisitor to the structure above; it has a StringBuilder sb = new StringBuilder(); field. Its visitClass(String n) method does sb.append("Class: ").append(n).append("\n"); and visitMethod(String n) does sb.append("Method: ").append(n).append("\n");. A String toString() method returns sb.toString().trim(). In main create ClassReader reader = new ClassReader("Main", new String[]{"main"}); and ClassWriter writer = new ClassWriter();, call reader.accept(writer); and print with System.out.println(writer.toString());.
Starter code
interface ClassVisitor {
void visitClass(String name);
void visitMethod(String name);
}
class ClassReader {
String name;
String[] methods;
ClassReader(String name, String[] methods) {
this.name = name;
this.methods = methods;
}
void accept(ClassVisitor v) {
v.visitClass(name);
for (String m : methods) v.visitMethod(m);
}
}
public class Main {
public static void main(String[] args) {
ClassReader reader = new ClassReader("Main", new String[]{"main"});
reader.accept(new ClassVisitor() {
public void visitClass(String n) { System.out.println("Class: " + n); }
public void visitMethod(String n) { System.out.println("Method: " + n); }
});
}
}Sample solution
interface ClassVisitor {
void visitClass(String name);
void visitMethod(String name);
}
class ClassReader {
String name;
String[] methods;
ClassReader(String name, String[] methods) {
this.name = name;
this.methods = methods;
}
void accept(ClassVisitor v) {
v.visitClass(name);
for (String m : methods) v.visitMethod(m);
}
}
class ClassWriter implements ClassVisitor {
StringBuilder sb = new StringBuilder();
public void visitClass(String n) { sb.append("Class: ").append(n).append("\n"); }
public void visitMethod(String n) { sb.append("Method: ").append(n).append("\n"); }
public String toString() { return sb.toString().trim(); }
}
public class Main {
public static void main(String[] args) {
ClassReader reader = new ClassReader("Main", new String[]{"main"});
ClassWriter writer = new ClassWriter();
reader.accept(writer);
System.out.println(writer.toString());
}
}Expected output
Class: Main Method: main
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the role of ClassWriter?
- It compiles the class
- It manages threads
- It collects garbage
- It turns visit events into text Correct answer
Explanation: ClassWriter collects events in a StringBuilder.
2 Complete the implements declaration
class ClassWriter ____ ClassVisitor {}
class ClassWriter implements ClassVisitor {}
Explanation: ClassWriter implements ClassVisitor applies the interface.
3 Match the ClassWriter elements
| Term | Meaning |
|---|---|
implements |
Interface implementation |
StringBuilder |
Text joining |
append |
Chained add |
toString |
Return text |