ProGuard

Package proguard.classfile.visitor

This package contains interfaces and classes for processing class files from the proguard.classfile package using the visitor pattern.

See:
          Description

Interface Summary
AttrInfoVisitor This interface specifies the methods for a visitor of AttrInfo objects.
ClassFileVisitor This interface specifies the methods for a visitor of ClassFile objects.
ClassPoolVisitor This interface specifies the methods for a visitor of ClassPool objects.
CpInfoVisitor This interface specifies the methods for a visitor of CpInfo objects.
ExceptionInfoVisitor This interface specifies the methods for a visitor of ExceptionInfo objects.
InnerClassesInfoVisitor This interface specifies the methods for a visitor of InnerClassesInfo objects.
LineNumberInfoVisitor This interface specifies the methods for a visitor of LineNumberInfo objects.
LocalVariableInfoVisitor This interface specifies the methods for a visitor of LocalVariableInfo objects.
MemberInfoVisitor This interface specifies the methods for a visitor of ProgramMemberInfo objects and LibraryMemberInfo objects.
 

Class Summary
AllAttrInfoVisitor This MemberInfoVisitor lets a given AttrInfoVisitor visit all AttrInfo objects of the program class members it visits.
AllClassFileVisitor This ClassPoolVisitor lets a given ClassFileVisitor visit all ClassFile objects of the class pools it visits.
AllCpInfoVisitor This ClassFileVisitor lets a given CpInfoVisitor visit all constant pool entries of the program class files it visits.
AllFieldVisitor This ClassFileVisitor lets a given MemberInfoVisitor visit all FieldMemberInfo objects of the class files it visits.
AllMemberInfoVisitor This ClassFileVisitor lets a given MemberInfoVisitor visit all MemberInfo objects of the class files it visits.
AllMethodVisitor This ClassFileVisitor lets a given MemberInfoVisitor visit all MethodMemberInfo objects of the class files it visits.
BottomClassFileFilter This ClassFileVisitor delegates its visits to another given ClassFileVisitor, but only when visiting class files that don't have any subclasses.
ClassFileAccessFilter This ClassFileVisitor delegates its visits to another given ClassFileVisitor, but only when the visited class file has the proper access flags.
ClassFileCleaner This ClassFileVisitor removes all visitor information of the class files it visits.
ClassFileHierarchyTraveler This ClassFileVisitor lets a given ClassFileVisitor optionally travel to the visited class, its superclass, its interfaces, and its subclasses.
ClassFileMemberInfoVisitor This MemberInfoVisitor delegates all visits to a given ClassFileVisitor.
ClassFileNameFilter This ClassFileVisitor delegates its visits to another given ClassFileVisitor, but only when the visited class file has a name that matches a given regular expression.
ClassFilePrinter This ClassFileVisitor prints out the complete internal structure of the class files it visits.
ClassPoolFiller This ClassFileVisitor collects all the class files it visits in a given class pool.
ConcreteClassFileDownTraveler This ClassFileVisitor lets a given ClassFileVisitor travel to the first concrete subclasses down in its hierarchy of abstract classes and concrete classes.
LibraryClassFileFilter This ClassFileVisitor delegates its visits to another given ClassFileVisitor, but only when visiting library class files.
LibraryMemberInfoFilter This MemberInfoVisitor delegates its visits to another given MemberInfoVisitor, but only when visiting members of library class files.
MemberInfoAccessFilter This MemberInfoVisitor delegates its visits to another given MemberInfoVisitor, but only when the visited member has the proper access flags.
MemberInfoDescriptorFilter This MemberInfoVisitor delegates its visits to another given MemberInfoVisitor, but only when the visited member has a descriptor that matches a given regular expression.
MemberInfoNameFilter This MemberInfoVisitor delegates its visits to another given MemberInfoVisitor, but only when the visited member has a name that matches a given regular expression.
MultiAttrInfoVisitor This AttrInfoVisitor delegates all visits to each AttrInfoVisitor in a given list.
MultiClassFileVisitor This ClassFileVisitor delegates all visits to each ClassFileVisitor in a given list.
MultiClassPoolVisitor This ClassPoolVisitor delegates all visits to each ClassPoolVisitor in a given list.
MultiMemberInfoVisitor This MemberInfoVisitor delegates all visits to each MemberInfoVisitor in a given list.
NamedClassFileVisitor This class visits ClassFile objects with the given name.
NamedFieldVisitor This class visits ProgramMemberInfo objects referring to fields, identified by a name and descriptor pair.
NamedMethodVisitor This class visits ProgramMemberInfo objects referring to methods, identified by a name and descriptor pair.
ProgramClassFileFilter This ClassFileVisitor delegates its visits to another given ClassFileVisitor, but only when visiting program class files.
ProgramMemberInfoFilter This MemberInfoVisitor delegates its visits to another given MemberInfoVisitor, but only when visiting members of program class files.
ReferencedClassFileVisitor This CpInfoVisitor lets a given ClassFileVisitor visit all the referenced class files of the constant pool entries that it visits.
SimpleClassFilePrinter This ClassFileVisitor and MemberInfoVisitor prints out the class names of the class files it visits, and the full class member descriptions of the class members it visits.
VariableClassFileVisitor This ClassFileVisitor delegates all method calls to a ClassFileVisitor that can be changed at any time.
VariableMemberInfoVisitor This MemberInfoVisitor delegates all method calls to a MemberInfoVisitor that can be changed at any time.
 

Package proguard.classfile.visitor Description

This package contains interfaces and classes for processing class files from the proguard.classfile package using the visitor pattern. Cfr., for instance, "Design Patterns, Elements of Reusable OO Software", by Gamma, Helm, Johnson, and Vlissider.

Why the visitor pattern? Class files frequently contain lists of elements of various mixed types: class items, constant pool entries, attributes,... These lists and types are largely fixed; they won't change much in future releases of the Java class file specifications. On the other hand, the kinds of operations that we may wish to perform on the class files may change and expand. We want to separate the objects and the operations performed upon them. This is a good place to use the visitor pattern.

Visitor interfaces avoid having to do series of instanceof tests on the elements of a list, followed by type casts and the proper operations. Every list element is a visitor accepter. When its accept method is called by a visitor, it calls its corresponding visitX method in the visitor, passing itself as an argument. This technique is called double-dispatch.

As already mentioned, the main advantage is avoiding lots of instanceof tests and type casts. Also, implementing a visitor interface ensures you're handling all possible visitor accepter types. Each type has its own method, which you simply have to implement.

A disadvantage is that the visitor methods always get the same names, specified by the visitor interface. These names aren't descriptive at all, making code harder to read. It's the visitor classes that describe the operations now.

Also, the visitor methods always have the same parameters and return values, as specified by the visitor interfaces. Passing additional parameters is done by means of extra fields in the visitor, which is somewhat of a kludge.

Because objects (the visitor accepters) and the operations performed upon them (the visitors) are now separated, it becomes harder to associate some state with the objects. For convenience, we always provide an extra visitor info field in visitor accepters, in which visitors can put any temporary information they want.


ProGuard