1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package proguard.classfile.attribute.visitor;
22 
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.*;
25 import proguard.classfile.util.SimplifiedVisitor;
26 import proguard.classfile.visitor.*;
27 
28 /**
29  * This ClassVisitor, MemberVisitor, and AttributeVisitor lets a given
30  * AttributeVisitor visit all Attribute objects of the program classes,
31  * program class members, or code attributes, respectively, that it visits.
32  *
33  * @author Eric Lafortune
34  */
35 public class AllAttributeVisitor
36 extends      SimplifiedVisitor
37 implements   ClassVisitor,
38              MemberVisitor,
39              AttributeVisitor
40 {
41     private final boolean          deep;
42     private final AttributeVisitor attributeVisitor;
43 
44 
45     /**
46      * Creates a new shallow AllAttributeVisitor.
47      * @param attributeVisitor the AttributeVisitor to which visits will be
48      *                         delegated.
49      */
AllAttributeVisitor(AttributeVisitor attributeVisitor)50     public AllAttributeVisitor(AttributeVisitor attributeVisitor)
51     {
52         this(false, attributeVisitor);
53     }
54 
55 
56     /**
57      * Creates a new optionally deep AllAttributeVisitor.
58      * @param deep             specifies whether the attributes contained
59      *                         further down the class structure should be
60      *                         visited too.
61      * @param attributeVisitor the AttributeVisitor to which visits will be
62      *                         delegated.
63      */
AllAttributeVisitor(boolean deep, AttributeVisitor attributeVisitor)64     public AllAttributeVisitor(boolean          deep,
65                                AttributeVisitor attributeVisitor)
66     {
67         this.deep             = deep;
68         this.attributeVisitor = attributeVisitor;
69     }
70 
71 
72     // Implementations for ClassVisitor.
73 
visitProgramClass(ProgramClass programClass)74     public void visitProgramClass(ProgramClass programClass)
75     {
76         programClass.attributesAccept(attributeVisitor);
77 
78         // Visit the attributes further down the class structure, if required.
79         if (deep)
80         {
81             programClass.fieldsAccept(this);
82             programClass.methodsAccept(this);
83             programClass.attributesAccept(this);
84         }
85     }
86 
87 
visitLibraryClass(LibraryClass libraryClass)88     public void visitLibraryClass(LibraryClass libraryClass) {}
89 
90 
91     // Implementations for MemberVisitor.
92 
visitProgramMember(ProgramClass programClass, ProgramMember programMember)93     public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
94     {
95         programMember.attributesAccept(programClass, attributeVisitor);
96 
97         // Visit the attributes further down the member structure, if required.
98         if (deep)
99         {
100             programMember.attributesAccept(programClass, this);
101         }
102     }
103 
104 
visitLibraryMember(LibraryClass programClass, LibraryMember programMember)105     public void visitLibraryMember(LibraryClass programClass, LibraryMember programMember) {}
106 
107 
108     // Implementations for AttributeVisitor.
109 
visitAnyAttribute(Clazz clazz, Attribute attribute)110     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
111 
112 
visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)113     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
114     {
115         codeAttribute.attributesAccept(clazz, method, attributeVisitor);
116     }
117 }
118