~ubuntu-branches/ubuntu/maverick/proguard/maverick

« back to all changes in this revision

Viewing changes to src/proguard/classfile/editor/MemberReferenceFixer.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-11-13 09:42:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051113094259-432zf4yyw4890mmn
Tags: 3.4-1
* New upstream release (Closes: #338355)
* debian/control: bump standards version
* debian/copyright: update FSF address
* increase java stack size for proguard and proguardgui

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: MemberReferenceFixer.java,v 1.4 2005/06/25 22:07:51 eric Exp $
 
2
 *
 
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
 
4
 *
 
5
 * Copyright (c) 2002-2005 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.editor;
 
22
 
 
23
import proguard.classfile.*;
 
24
import proguard.classfile.attribute.*;
 
25
import proguard.classfile.attribute.annotation.*;
 
26
import proguard.classfile.visitor.*;
 
27
 
 
28
/**
 
29
 * This ClassFileVisitor fixes constant pool field and method references to
 
30
 * fields and methods whose names or descriptors have changed.
 
31
 *
 
32
 * @author Eric Lafortune
 
33
 */
 
34
public class MemberReferenceFixer
 
35
implements   ClassFileVisitor,
 
36
             CpInfoVisitor,
 
37
             MemberInfoVisitor,
 
38
             AttrInfoVisitor,
 
39
             AnnotationVisitor,
 
40
             ElementValueVisitor
 
41
{
 
42
    private static final boolean DEBUG = false;
 
43
 
 
44
 
 
45
    private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor();
 
46
    private StackSizeUpdater   stackSizeUpdater;
 
47
 
 
48
    // Parameter for the visitor methods.
 
49
    private int cpIndex;
 
50
 
 
51
    // Return values for the visitor methods.
 
52
    private boolean isInterfaceMethod;
 
53
    private boolean stackSizesMayHaveChanged;
 
54
 
 
55
 
 
56
    /**
 
57
     * Creates a new MemberReferenceFixer.
 
58
     * @param codeLength an estimate of the maximum length of all the code that
 
59
     *                   will be edited.
 
60
     */
 
61
    public MemberReferenceFixer(int codeLength)
 
62
    {
 
63
        stackSizeUpdater = new StackSizeUpdater(codeLength);
 
64
    }
 
65
 
 
66
 
 
67
    // Implementations for ClassFileVisitor.
 
68
 
 
69
    public void visitProgramClassFile(ProgramClassFile programClassFile)
 
70
    {
 
71
        stackSizesMayHaveChanged = false;
 
72
 
 
73
        // Fix the constant pool entries.
 
74
        for (int index = 1; index < programClassFile.u2constantPoolCount; index++)
 
75
        {
 
76
            CpInfo cpInfo = programClassFile.constantPool[index];
 
77
            if (cpInfo != null)
 
78
            {
 
79
                // Fix the entry, replacing it entirely if needed.
 
80
                this.cpIndex = index;
 
81
 
 
82
                cpInfo.accept(programClassFile, this);
 
83
            }
 
84
        }
 
85
 
 
86
        // Fix class members.
 
87
        programClassFile.fieldsAccept(this);
 
88
        programClassFile.methodsAccept(this);
 
89
 
 
90
        // Fix the attributes.
 
91
        programClassFile.attributesAccept(this);
 
92
    }
 
93
 
 
94
 
 
95
    public void visitLibraryClassFile(LibraryClassFile libraryClassFile)
 
96
    {
 
97
    }
 
98
 
 
99
 
 
100
    // Implementations for CpInfoVisitor.
 
101
 
 
102
    public void visitIntegerCpInfo(ClassFile classFile, IntegerCpInfo integerCpInfo) {}
 
103
    public void visitLongCpInfo(ClassFile classFile, LongCpInfo longCpInfo) {}
 
104
    public void visitFloatCpInfo(ClassFile classFile, FloatCpInfo floatCpInfo) {}
 
105
    public void visitDoubleCpInfo(ClassFile classFile, DoubleCpInfo doubleCpInfo) {}
 
106
    public void visitStringCpInfo(ClassFile classFile, StringCpInfo stringCpInfo) {}
 
107
    public void visitNameAndTypeCpInfo(ClassFile classFile, NameAndTypeCpInfo nameAndTypeCpInfo) {}
 
108
    public void visitUtf8CpInfo(ClassFile classFile, Utf8CpInfo utf8CpInfo) {}
 
109
 
 
110
 
 
111
    public void visitFieldrefCpInfo(ClassFile classFile, FieldrefCpInfo fieldrefCpInfo)
 
112
    {
 
113
        // Do we know the referenced field?
 
114
        MemberInfo referencedMemberInfo = fieldrefCpInfo.referencedMemberInfo;
 
115
        if (referencedMemberInfo != null)
 
116
        {
 
117
            ClassFile referencedClassFile = fieldrefCpInfo.referencedClassFile;
 
118
 
 
119
            // Does it have a new name or type?
 
120
            String newName = referencedMemberInfo.getName(referencedClassFile);
 
121
            String newType = referencedMemberInfo.getDescriptor(referencedClassFile);
 
122
 
 
123
            if (!fieldrefCpInfo.getName(classFile).equals(newName) ||
 
124
                !fieldrefCpInfo.getType(classFile).equals(newType))
 
125
            {
 
126
                if (DEBUG)
 
127
                {
 
128
                    debug(classFile, fieldrefCpInfo, referencedClassFile, referencedMemberInfo);
 
129
                }
 
130
 
 
131
                // Update the name and type index.
 
132
                fieldrefCpInfo.u2nameAndTypeIndex =
 
133
                    constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile,
 
134
                                                            newName,
 
135
                                                            newType);
 
136
            }
 
137
        }
 
138
    }
 
139
 
 
140
 
 
141
    public void visitInterfaceMethodrefCpInfo(ClassFile classFile, InterfaceMethodrefCpInfo interfaceMethodrefCpInfo)
 
142
    {
 
143
        // Do we know the referenced interface method?
 
144
        MemberInfo referencedMemberInfo = interfaceMethodrefCpInfo.referencedMemberInfo;
 
145
        if (referencedMemberInfo != null)
 
146
        {
 
147
            ClassFile referencedClassFile = interfaceMethodrefCpInfo.referencedClassFile;
 
148
 
 
149
            // Does it have a new name or type?
 
150
            String newName = referencedMemberInfo.getName(referencedClassFile);
 
151
            String newType = referencedMemberInfo.getDescriptor(referencedClassFile);
 
152
 
 
153
            if (!interfaceMethodrefCpInfo.getName(classFile).equals(newName) ||
 
154
                !interfaceMethodrefCpInfo.getType(classFile).equals(newType))
 
155
            {
 
156
                if (DEBUG)
 
157
                {
 
158
                    debug(classFile, interfaceMethodrefCpInfo, referencedClassFile, referencedMemberInfo);
 
159
                }
 
160
 
 
161
                // Update the name and type index.
 
162
                interfaceMethodrefCpInfo.u2nameAndTypeIndex =
 
163
                    constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile,
 
164
                                                            newName,
 
165
                                                            newType);
 
166
 
 
167
                // Remember that the stack sizes of the methods in this class
 
168
                // may have changed.
 
169
                stackSizesMayHaveChanged = true;
 
170
            }
 
171
 
 
172
            // Check if this is an interface method.
 
173
            isInterfaceMethod = true;
 
174
            classFile.constantPoolEntryAccept(interfaceMethodrefCpInfo.u2classIndex, this);
 
175
 
 
176
            // Has the method become a non-interface method?
 
177
            if (!isInterfaceMethod)
 
178
            {
 
179
                if (DEBUG)
 
180
                {
 
181
                    System.out.println("MemberReferenceFixer:");
 
182
                    System.out.println("  Class file     = "+classFile.getName());
 
183
                    System.out.println("  Ref class file = "+referencedClassFile.getName());
 
184
                    System.out.println("  Ref method     = "+interfaceMethodrefCpInfo.getName(classFile)+interfaceMethodrefCpInfo.getType(classFile));
 
185
                    System.out.println("    -> ordinary method");
 
186
                }
 
187
 
 
188
                // Replace the interface method reference by a method reference.
 
189
                ((ProgramClassFile)classFile).constantPool[this.cpIndex] =
 
190
                    new MethodrefCpInfo(interfaceMethodrefCpInfo.u2classIndex,
 
191
                                        interfaceMethodrefCpInfo.u2nameAndTypeIndex,
 
192
                                        referencedClassFile,
 
193
                                        referencedMemberInfo);
 
194
            }
 
195
        }
 
196
    }
 
197
 
 
198
 
 
199
    public void visitMethodrefCpInfo(ClassFile classFile, MethodrefCpInfo methodrefCpInfo)
 
200
    {
 
201
        // Do we know the referenced method?
 
202
        MemberInfo referencedMemberInfo = methodrefCpInfo.referencedMemberInfo;
 
203
        if (referencedMemberInfo != null)
 
204
        {
 
205
            ClassFile referencedClassFile = methodrefCpInfo.referencedClassFile;
 
206
 
 
207
            // Does it have a new name or type?
 
208
            String newName = referencedMemberInfo.getName(referencedClassFile);
 
209
            String newType = referencedMemberInfo.getDescriptor(referencedClassFile);
 
210
 
 
211
            if (!methodrefCpInfo.getName(classFile).equals(newName) ||
 
212
                !methodrefCpInfo.getType(classFile).equals(newType))
 
213
            {
 
214
                if (DEBUG)
 
215
                {
 
216
                    debug(classFile, methodrefCpInfo, referencedClassFile, referencedMemberInfo);
 
217
                }
 
218
 
 
219
                // Update the name and type index.
 
220
                methodrefCpInfo.u2nameAndTypeIndex =
 
221
                    constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile,
 
222
                                                            newName,
 
223
                                                            newType);
 
224
 
 
225
                // Remember that the stack sizes of the methods in this class
 
226
                // may have changed.
 
227
                stackSizesMayHaveChanged = true;
 
228
            }
 
229
 
 
230
            // Check if this is an interface method.
 
231
            isInterfaceMethod = false;
 
232
            classFile.constantPoolEntryAccept(methodrefCpInfo.u2classIndex, this);
 
233
 
 
234
            // Has the method become an interface method?
 
235
            if (isInterfaceMethod)
 
236
            {
 
237
                if (DEBUG)
 
238
                {
 
239
                    System.out.println("MemberReferenceFixer:");
 
240
                    System.out.println("  Class file     = "+classFile.getName());
 
241
                    System.out.println("  Ref class file = "+referencedClassFile.getName());
 
242
                    System.out.println("  Ref method     = "+methodrefCpInfo.getName(classFile)+methodrefCpInfo.getType(classFile));
 
243
                    System.out.println("    -> interface method");
 
244
                }
 
245
 
 
246
                // Replace the method reference by an interface method reference.
 
247
                ((ProgramClassFile)classFile).constantPool[this.cpIndex] =
 
248
                    new InterfaceMethodrefCpInfo(methodrefCpInfo.u2classIndex,
 
249
                                                 methodrefCpInfo.u2nameAndTypeIndex,
 
250
                                                 referencedClassFile,
 
251
                                                 referencedMemberInfo);
 
252
            }
 
253
        }
 
254
    }
 
255
 
 
256
 
 
257
    public void visitClassCpInfo(ClassFile classFile, ClassCpInfo classCpInfo)
 
258
    {
 
259
        // Check if this class entry refers to an interface class.
 
260
        ClassFile referencedClassFile = classCpInfo.referencedClassFile;
 
261
        if (referencedClassFile != null)
 
262
        {
 
263
            isInterfaceMethod = (referencedClassFile.getAccessFlags() & ClassConstants.INTERNAL_ACC_INTERFACE) != 0;
 
264
        }
 
265
    }
 
266
 
 
267
 
 
268
    // Implementations for MemberInfoVisitor.
 
269
 
 
270
    public void visitProgramFieldInfo(ProgramClassFile programClassFile, ProgramFieldInfo programFieldInfo)
 
271
    {
 
272
        // Fix the attributes.
 
273
        programFieldInfo.attributesAccept(programClassFile, this);
 
274
    }
 
275
 
 
276
 
 
277
    public void visitProgramMethodInfo(ProgramClassFile programClassFile, ProgramMethodInfo programMethodInfo)
 
278
    {
 
279
        // Fix the attributes.
 
280
        programMethodInfo.attributesAccept(programClassFile, this);
 
281
    }
 
282
 
 
283
 
 
284
    public void visitLibraryFieldInfo(LibraryClassFile libraryClassFile, LibraryFieldInfo libraryFieldInfo) {}
 
285
    public void visitLibraryMethodInfo(LibraryClassFile libraryClassFile, LibraryMethodInfo libraryMethodInfo) {}
 
286
 
 
287
 
 
288
    // Implementations for AttrInfoVisitor.
 
289
 
 
290
    public void visitUnknownAttrInfo(ClassFile classFile, UnknownAttrInfo unknownAttrInfo) {}
 
291
    public void visitInnerClassesAttrInfo(ClassFile classFile, InnerClassesAttrInfo innerClassesAttrInfo) {}
 
292
    public void visitConstantValueAttrInfo(ClassFile classFile, FieldInfo fieldInfo, ConstantValueAttrInfo constantValueAttrInfo) {}
 
293
    public void visitExceptionsAttrInfo(ClassFile classFile, MethodInfo methodInfo, ExceptionsAttrInfo exceptionsAttrInfo) {}
 
294
    public void visitLineNumberTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LineNumberTableAttrInfo lineNumberTableAttrInfo) {}
 
295
    public void visitLocalVariableTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTableAttrInfo localVariableTableAttrInfo) {}
 
296
    public void visitLocalVariableTypeTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTypeTableAttrInfo localVariableTypeTableAttrInfo) {}
 
297
    public void visitSourceFileAttrInfo(ClassFile classFile, SourceFileAttrInfo sourceFileAttrInfo) {}
 
298
    public void visitSourceDirAttrInfo(ClassFile classFile, SourceDirAttrInfo sourceDirAttrInfo) {}
 
299
    public void visitDeprecatedAttrInfo(ClassFile classFile, DeprecatedAttrInfo deprecatedAttrInfo) {}
 
300
    public void visitSyntheticAttrInfo(ClassFile classFile, SyntheticAttrInfo syntheticAttrInfo) {}
 
301
    public void visitSignatureAttrInfo(ClassFile classFile, SignatureAttrInfo signatureAttrInfo) {}
 
302
 
 
303
 
 
304
    public void visitEnclosingMethodAttrInfo(ClassFile classFile, EnclosingMethodAttrInfo enclosingMethodAttrInfo)
 
305
    {
 
306
        MemberInfo referencedMemberInfo = enclosingMethodAttrInfo.referencedMethodInfo;
 
307
        if (referencedMemberInfo != null)
 
308
        {
 
309
            ClassFile referencedClassFile = enclosingMethodAttrInfo.referencedClassFile;
 
310
 
 
311
            // Does it have a new class?
 
312
            if (!enclosingMethodAttrInfo.getClassName(classFile).equals(referencedClassFile.getName()))
 
313
            {
 
314
                // Update the class index.
 
315
                enclosingMethodAttrInfo.u2classIndex =
 
316
                    constantPoolEditor.addClassCpInfo((ProgramClassFile)classFile,
 
317
                                                      referencedClassFile);
 
318
            }
 
319
 
 
320
            // Does it have a new name or type?
 
321
            if (!enclosingMethodAttrInfo.getName(classFile).equals(referencedMemberInfo.getName(referencedClassFile)) ||
 
322
                !enclosingMethodAttrInfo.getType(classFile).equals(referencedMemberInfo.getDescriptor(referencedClassFile)))
 
323
            {
 
324
                // Update the name and type index.
 
325
                enclosingMethodAttrInfo.u2nameAndTypeIndex =
 
326
                    constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile,
 
327
                                                            referencedMemberInfo.getName(referencedClassFile),
 
328
                                                            referencedMemberInfo.getDescriptor(referencedClassFile));
 
329
            }
 
330
        }
 
331
    }
 
332
 
 
333
 
 
334
    public void visitCodeAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo)
 
335
    {
 
336
        // Recompute the maximum stack size if necessary.
 
337
        if (stackSizesMayHaveChanged)
 
338
        {
 
339
            stackSizeUpdater.visitCodeAttrInfo(classFile, methodInfo, codeAttrInfo);
 
340
        }
 
341
 
 
342
        // Fix the nested attributes.
 
343
        codeAttrInfo.attributesAccept(classFile, methodInfo, this);
 
344
    }
 
345
 
 
346
 
 
347
    public void visitRuntimeVisibleAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleAnnotationsAttrInfo runtimeVisibleAnnotationsAttrInfo)
 
348
    {
 
349
        // Fix the annotations.
 
350
        runtimeVisibleAnnotationsAttrInfo.annotationsAccept(classFile, this);
 
351
    }
 
352
 
 
353
 
 
354
    public void visitRuntimeInvisibleAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleAnnotationsAttrInfo runtimeInvisibleAnnotationsAttrInfo)
 
355
    {
 
356
        // Fix the annotations.
 
357
        runtimeInvisibleAnnotationsAttrInfo.annotationsAccept(classFile, this);
 
358
    }
 
359
 
 
360
 
 
361
    public void visitRuntimeVisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleParameterAnnotationsAttrInfo runtimeVisibleParameterAnnotationsAttrInfo)
 
362
    {
 
363
        // Fix the annotations.
 
364
        runtimeVisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this);
 
365
    }
 
366
 
 
367
 
 
368
    public void visitRuntimeInvisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleParameterAnnotationsAttrInfo runtimeInvisibleParameterAnnotationsAttrInfo)
 
369
    {
 
370
        // Fix the annotations.
 
371
        runtimeInvisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this);
 
372
    }
 
373
 
 
374
 
 
375
    public void visitAnnotationDefaultAttrInfo(ClassFile classFile, AnnotationDefaultAttrInfo annotationDefaultAttrInfo)
 
376
    {
 
377
        // Fix the annotation.
 
378
        annotationDefaultAttrInfo.defaultValueAccept(classFile, this);
 
379
    }
 
380
 
 
381
 
 
382
    // Implementations for AnnotationVisitor.
 
383
 
 
384
    public void visitAnnotation(ClassFile classFile, Annotation annotation)
 
385
    {
 
386
        // Fix the element values.
 
387
        annotation.elementValuesAccept(classFile, this);
 
388
    }
 
389
 
 
390
 
 
391
    // Implementations for ElementValueVisitor.
 
392
 
 
393
    public void visitConstantElementValue(ClassFile classFile, Annotation annotation, ConstantElementValue constantElementValue)
 
394
    {
 
395
        fixElementValue(classFile, annotation, constantElementValue);
 
396
    }
 
397
 
 
398
 
 
399
    public void visitEnumConstantElementValue(ClassFile classFile, Annotation annotation, EnumConstantElementValue enumConstantElementValue)
 
400
    {
 
401
        fixElementValue(classFile, annotation, enumConstantElementValue);
 
402
    }
 
403
 
 
404
 
 
405
    public void visitClassElementValue(ClassFile classFile, Annotation annotation, ClassElementValue classElementValue)
 
406
    {
 
407
        fixElementValue(classFile, annotation, classElementValue);
 
408
    }
 
409
 
 
410
 
 
411
    public void visitAnnotationElementValue(ClassFile classFile, Annotation annotation, AnnotationElementValue annotationElementValue)
 
412
    {
 
413
        fixElementValue(classFile, annotation, annotationElementValue);
 
414
 
 
415
        // Fix the annotation.
 
416
        annotationElementValue.annotationAccept(classFile, this);
 
417
    }
 
418
 
 
419
 
 
420
    public void visitArrayElementValue(ClassFile classFile, Annotation annotation, ArrayElementValue arrayElementValue)
 
421
    {
 
422
        fixElementValue(classFile, annotation, arrayElementValue);
 
423
 
 
424
        // Fix the element values.
 
425
        arrayElementValue.elementValuesAccept(classFile, annotation, this);
 
426
    }
 
427
 
 
428
 
 
429
    // Small utility methods.
 
430
 
 
431
    /**
 
432
     * Fixs the method reference of the element value, if any.
 
433
     */
 
434
    private void fixElementValue(ClassFile    classFile,
 
435
                                 Annotation   annotation,
 
436
                                 ElementValue elementValue)
 
437
    {
 
438
        // Do we know the referenced method?
 
439
        MemberInfo referencedMemberInfo = elementValue.referencedMethodInfo;
 
440
        if (referencedMemberInfo != null)
 
441
        {
 
442
            // Does it have a new name or type?
 
443
            String methodName    = elementValue.getMethodName(classFile);
 
444
            String newMethodName = referencedMemberInfo.getName(annotation.referencedClassFiles[0]);
 
445
            if (!methodName.equals(newMethodName))
 
446
            {
 
447
                // Update the element name index.
 
448
                elementValue.u2elementName =
 
449
                    constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile,
 
450
                                                     newMethodName);
 
451
            }
 
452
        }
 
453
    }
 
454
 
 
455
 
 
456
    private void debug(ClassFile  classFile,
 
457
                       RefCpInfo  refCpInfo,
 
458
                       ClassFile  referencedClassFile,
 
459
                       MemberInfo referencedMemberInfo)
 
460
    {
 
461
        System.out.println("MemberReferenceFixer:");
 
462
        System.out.println("  Class file      = "+classFile.getName());
 
463
        System.out.println("  Ref class file  = "+referencedClassFile.getName());
 
464
        System.out.println("  Ref member name = "+refCpInfo.getName(classFile));
 
465
        System.out.println("                 -> "+referencedMemberInfo.getName(referencedClassFile));
 
466
        System.out.println("  Ref descriptor  = "+refCpInfo.getType(classFile));
 
467
        System.out.println("                 -> "+referencedMemberInfo.getDescriptor(referencedClassFile));
 
468
    }
 
469
}