KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > editor > ConstantAdder


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.editor;
22
23 import proguard.classfile.*;
24 import proguard.classfile.constant.*;
25 import proguard.classfile.constant.visitor.ConstantVisitor;
26
27 /**
28  * This ConstantVisitor adds all constants that it visits to the constant pool
29  * of a given target class.
30  *
31  * @author Eric Lafortune
32  */

33 public class ConstantAdder
34 implements ConstantVisitor
35 {
36     private ProgramClass targetClass;
37     private int constantIndex;
38
39     private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor();
40
41
42     /**
43      * Sets the class to which visited constants will be copied.
44      */

45     public void setTargetClass(ProgramClass targetClass)
46     {
47         this.targetClass = targetClass;
48     }
49
50
51     /**
52      * Returns the class to which visited constants will be copied.
53      */

54     public ProgramClass getTargetClass()
55     {
56         return targetClass;
57     }
58
59
60     /**
61      * Returns the index of the most recently created constant in the constant
62      * pool of the target class.
63      */

64     public int getConstantIndex()
65     {
66         return constantIndex;
67     }
68
69
70     // Implementations for ConstantVisitor.
71

72     public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
73     {
74         constantIndex =
75             constantPoolEditor.addIntegerConstant(targetClass,
76                                                   integerConstant.getValue());
77     }
78
79
80     public void visitLongConstant(Clazz clazz, LongConstant longConstant)
81     {
82         constantIndex =
83             constantPoolEditor.addLongConstant(targetClass,
84                                                longConstant.getValue());
85     }
86
87
88     public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
89     {
90         constantIndex =
91             constantPoolEditor.addFloatConstant(targetClass,
92                                                 floatConstant.getValue());
93     }
94
95
96     public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
97     {
98         constantIndex =
99             constantPoolEditor.addDoubleConstant(targetClass,
100                                                  doubleConstant.getValue());
101     }
102
103
104     public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
105     {
106         constantIndex =
107             constantPoolEditor.addStringConstant(targetClass,
108                                                  stringConstant.getString(clazz),
109                                                  stringConstant.referencedClass,
110                                                  stringConstant.referencedMember);
111     }
112
113
114     public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant)
115     {
116         constantIndex =
117             constantPoolEditor.addUtf8Constant(targetClass,
118                                                utf8Constant.getString());
119     }
120
121
122     public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant)
123     {
124         // First add the referenced class constant, with its own referenced class.
125
clazz.constantPoolEntryAccept(fieldrefConstant.u2classIndex, this);
126
127         // Then add the actual field reference constant, with its referenced
128
// class and class member.
129
constantIndex =
130             constantPoolEditor.addFieldrefConstant(targetClass,
131                                                    constantIndex,
132                                                    fieldrefConstant.getName(clazz),
133                                                    fieldrefConstant.getType(clazz),
134                                                    fieldrefConstant.referencedClass,
135                                                    fieldrefConstant.referencedMember);
136     }
137
138
139     public void visitInterfaceMethodrefConstant(Clazz clazz, InterfaceMethodrefConstant interfaceMethodrefConstant)
140     {
141         // First add the referenced class constant, with its own referenced class.
142
clazz.constantPoolEntryAccept(interfaceMethodrefConstant.u2classIndex, this);
143
144         // Then add the actual interface method reference constant, with its
145
// referenced class and class member.
146
constantIndex =
147             constantPoolEditor.addInterfaceMethodrefConstant(targetClass,
148                                                              constantIndex,
149                                                              interfaceMethodrefConstant.getName(clazz),
150                                                              interfaceMethodrefConstant.getType(clazz),
151                                                              interfaceMethodrefConstant.referencedClass,
152                                                              interfaceMethodrefConstant.referencedMember);
153     }
154
155
156     public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant)
157     {
158         // First add the referenced class constant, with its own referenced class.
159
clazz.constantPoolEntryAccept(methodrefConstant.u2classIndex, this);
160
161         // Then add the actual method reference constant, with its referenced
162
// class and class member.
163
constantIndex =
164             constantPoolEditor.addMethodrefConstant(targetClass,
165                                                     constantIndex,
166                                                     methodrefConstant.getName(clazz),
167                                                     methodrefConstant.getType(clazz),
168                                                     methodrefConstant.referencedClass,
169                                                     methodrefConstant.referencedMember);
170     }
171
172
173     public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
174     {
175         // Add the class constant, with its referenced class..
176
constantIndex =
177             constantPoolEditor.addClassConstant(targetClass,
178                                                 classConstant.getName(clazz),
179                                                 classConstant.referencedClass);
180     }
181
182
183     public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant)
184     {
185         constantIndex =
186             constantPoolEditor.addNameAndTypeConstant(targetClass,
187                                                       nameAndTypeConstant.getName(clazz),
188                                                       nameAndTypeConstant.getType(clazz));
189     }
190 }
191
Popular Tags