aboutsummaryrefslogtreecommitdiffstats
path: root/src/proguard/classfile/editor/MemberReferenceFixer.java
blob: 4bd8af5804d89cac96f5a1685c2bc2431afa067f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package proguard.classfile.editor;

import proguard.classfile.*;
import proguard.classfile.attribute.*;
import proguard.classfile.attribute.annotation.*;
import proguard.classfile.attribute.annotation.visitor.*;
import proguard.classfile.attribute.visitor.AttributeVisitor;
import proguard.classfile.constant.*;
import proguard.classfile.constant.visitor.ConstantVisitor;
import proguard.classfile.util.*;
import proguard.classfile.visitor.*;

/**
 * This ClassVisitor fixes constant pool field and method references to fields
 * and methods whose names or descriptors have changed.
 *
 * @author Eric Lafortune
 */
public class MemberReferenceFixer
extends      SimplifiedVisitor
implements   ClassVisitor,
             ConstantVisitor,
             MemberVisitor,
             AttributeVisitor,
             AnnotationVisitor,
             ElementValueVisitor
{
    private static final boolean DEBUG = false;


    private final StackSizeUpdater stackSizeUpdater = new StackSizeUpdater();

    // Parameter for the visitor methods.
    private int constantIndex;

    // Return values for the visitor methods.
    private boolean isInterfaceMethod;
    private boolean stackSizesMayHaveChanged;


    // Implementations for ClassVisitor.

    public void visitProgramClass(ProgramClass programClass)
    {
        stackSizesMayHaveChanged = false;

        // Fix the constant pool entries.
        for (int index = 1; index < programClass.u2constantPoolCount; index++)
        {
            Constant constant = programClass.constantPool[index];
            if (constant != null)
            {
                // Fix the entry, replacing it entirely if needed.
                this.constantIndex = index;

                constant.accept(programClass, this);
            }
        }

        // Fix the class members.
        programClass.fieldsAccept(this);
        programClass.methodsAccept(this);

        // Fix the attributes.
        programClass.attributesAccept(this);
    }


    // Implementations for ConstantVisitor.

    public void visitAnyConstant(Clazz clazz, Constant constant) {}


    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
    {
        // Does the string refer to a class member, due to a
        // Class.get[Declared]{Field,Method} construct?
        Member referencedMember = stringConstant.referencedMember;
        if (referencedMember != null)
        {
            Clazz referencedClass = stringConstant.referencedClass;

            // Does it have a new name?
            String newName = referencedMember.getName(referencedClass);

            if (!stringConstant.getString(clazz).equals(newName))
            {
                if (DEBUG)
                {
                    debug(clazz, stringConstant, referencedClass, referencedMember);
                }

                // Update the name.
                stringConstant.u2stringIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);
            }
        }
    }


    public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant)
    {
        // Do we know the referenced field?
        Member referencedMember = fieldrefConstant.referencedMember;
        if (referencedMember != null)
        {
            Clazz referencedClass = fieldrefConstant.referencedClass;

            // Does it have a new name or type?
            String newName = referencedMember.getName(referencedClass);
            String newType = referencedMember.getDescriptor(referencedClass);

            if (!fieldrefConstant.getName(clazz).equals(newName) ||
                !fieldrefConstant.getType(clazz).equals(newType))
            {
                if (DEBUG)
                {
                    debug(clazz, fieldrefConstant, referencedClass, referencedMember);
                }

                // Update the name and type index.
                fieldrefConstant.u2nameAndTypeIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addNameAndTypeConstant(newName, newType);
            }
        }
    }


    public void visitInterfaceMethodrefConstant(Clazz clazz, InterfaceMethodrefConstant interfaceMethodrefConstant)
    {
        // Do we know the referenced interface method?
        Member referencedMember = interfaceMethodrefConstant.referencedMember;
        if (referencedMember != null)
        {
            Clazz referencedClass = interfaceMethodrefConstant.referencedClass;

            // Does it have a new name or type?
            String newName = referencedMember.getName(referencedClass);
            String newType = referencedMember.getDescriptor(referencedClass);

            if (!interfaceMethodrefConstant.getName(clazz).equals(newName) ||
                !interfaceMethodrefConstant.getType(clazz).equals(newType))
            {
                if (DEBUG)
                {
                    debug(clazz, interfaceMethodrefConstant, referencedClass, referencedMember);
                }

                // Update the name and type index.
                interfaceMethodrefConstant.u2nameAndTypeIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addNameAndTypeConstant(newName, newType);

                // Remember that the stack sizes of the methods in this class
                // may have changed.
                stackSizesMayHaveChanged = true;
            }

            // Check if this is an interface method.
            isInterfaceMethod = true;
            clazz.constantPoolEntryAccept(interfaceMethodrefConstant.u2classIndex, this);

            // Has the method become a non-interface method?
            if (!isInterfaceMethod)
            {
                if (DEBUG)
                {
                    System.out.println("MemberReferenceFixer:");
                    System.out.println("  Class file     = "+clazz.getName());
                    System.out.println("  Ref class      = "+referencedClass.getName());
                    System.out.println("  Ref method     = "+interfaceMethodrefConstant.getName(clazz)+interfaceMethodrefConstant.getType(clazz));
                    System.out.println("    -> ordinary method");
                }

                // Replace the interface method reference by a method reference.
                ((ProgramClass)clazz).constantPool[this.constantIndex] =
                    new MethodrefConstant(interfaceMethodrefConstant.u2classIndex,
                                          interfaceMethodrefConstant.u2nameAndTypeIndex,
                                          referencedClass,
                                          referencedMember);
            }
        }
    }


    public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant)
    {
        // Do we know the referenced method?
        Member referencedMember = methodrefConstant.referencedMember;
        if (referencedMember != null)
        {
            Clazz referencedClass = methodrefConstant.referencedClass;

            // Does it have a new name or type?
            String newName = referencedMember.getName(referencedClass);
            String newType = referencedMember.getDescriptor(referencedClass);

            if (!methodrefConstant.getName(clazz).equals(newName) ||
                !methodrefConstant.getType(clazz).equals(newType))
            {
                if (DEBUG)
                {
                    debug(clazz, methodrefConstant, referencedClass, referencedMember);
                }

                // Update the name and type index.
                methodrefConstant.u2nameAndTypeIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addNameAndTypeConstant(newName, newType);

                // Remember that the stack sizes of the methods in this class
                // may have changed.
                stackSizesMayHaveChanged = true;
            }

            // Check if this is an interface method.
            isInterfaceMethod = false;
            clazz.constantPoolEntryAccept(methodrefConstant.u2classIndex, this);

            // Has the method become an interface method?
            if (isInterfaceMethod)
            {
                if (DEBUG)
                {
                    System.out.println("MemberReferenceFixer:");
                    System.out.println("  Class file     = "+clazz.getName());
                    System.out.println("  Ref class      = "+referencedClass.getName());
                    System.out.println("  Ref method     = "+methodrefConstant.getName(clazz)+methodrefConstant.getType(clazz));
                    System.out.println("    -> interface method");
                }

                // Replace the method reference by an interface method reference.
                ((ProgramClass)clazz).constantPool[this.constantIndex] =
                    new InterfaceMethodrefConstant(methodrefConstant.u2classIndex,
                                                   methodrefConstant.u2nameAndTypeIndex,
                                                   referencedClass,
                                                   referencedMember);
            }
        }
    }


    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
    {
        // Check if this class entry is an array type.
        if (ClassUtil.isInternalArrayType(classConstant.getName(clazz)))
        {
            isInterfaceMethod = false;
        }
        else
        {
            // Check if this class entry refers to an interface class.
            Clazz referencedClass = classConstant.referencedClass;
            if (referencedClass != null)
            {
                isInterfaceMethod = (referencedClass.getAccessFlags() & ClassConstants.INTERNAL_ACC_INTERFACE) != 0;
            }
        }
    }


    // Implementations for MemberVisitor.

    public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
    {
        // Fix the attributes.
        programMember.attributesAccept(programClass, this);
    }


    // Implementations for AttributeVisitor.

    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}


    public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute)
    {
        Member referencedMember = enclosingMethodAttribute.referencedMethod;
        if (referencedMember != null)
        {
            Clazz referencedClass = enclosingMethodAttribute.referencedClass;

            // Does it have a new class?
            if (!enclosingMethodAttribute.getClassName(clazz).equals(referencedClass.getName()))
            {
                // Update the class index.
                enclosingMethodAttribute.u2classIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addClassConstant(referencedClass);
            }

            // Does it have a new name or type?
            if (!enclosingMethodAttribute.getName(clazz).equals(referencedMember.getName(referencedClass)) ||
                !enclosingMethodAttribute.getType(clazz).equals(referencedMember.getDescriptor(referencedClass)))
            {
                // Update the name and type index.
                enclosingMethodAttribute.u2nameAndTypeIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addNameAndTypeConstant(referencedMember.getName(referencedClass),
                                                                                       referencedMember.getDescriptor(referencedClass));
            }
        }
    }


    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
        // Recompute the maximum stack size if necessary.
        if (stackSizesMayHaveChanged)
        {
                stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
        }

        // Fix the nested attributes.
        codeAttribute.attributesAccept(clazz, method, this);
    }


    public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute)
    {
        // Fix the annotations.
        annotationsAttribute.annotationsAccept(clazz, this);
    }


    public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
    {
        // Fix the annotations.
        parameterAnnotationsAttribute.annotationsAccept(clazz, method, this);
    }


    public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute)
    {
        // Fix the annotation.
        annotationDefaultAttribute.defaultValueAccept(clazz, this);
    }


    // Implementations for AnnotationVisitor.

    public void visitAnnotation(Clazz clazz, Annotation annotation)
    {
        // Fix the element values.
        annotation.elementValuesAccept(clazz, this);
    }


    // Implementations for ElementValueVisitor.

    public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue)
    {
        fixElementValue(clazz, annotation, constantElementValue);
    }


    public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue)
    {
        fixElementValue(clazz, annotation, enumConstantElementValue);
    }


    public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue)
    {
        fixElementValue(clazz, annotation, classElementValue);
    }


    public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue)
    {
        fixElementValue(clazz, annotation, annotationElementValue);

        // Fix the annotation.
        annotationElementValue.annotationAccept(clazz, this);
    }


    public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue)
    {
        fixElementValue(clazz, annotation, arrayElementValue);

        // Fix the element values.
        arrayElementValue.elementValuesAccept(clazz, annotation, this);
    }


    // Small utility methods.

    /**
     * Fixes the method reference of the element value, if any.
     */
    private void fixElementValue(Clazz        clazz,
                                 Annotation   annotation,
                                 ElementValue elementValue)
    {
        // Do we know the referenced method?
        Member referencedMember = elementValue.referencedMethod;
        if (referencedMember != null)
        {
            // Does it have a new name or type?
            String methodName    = elementValue.getMethodName(clazz);
            String newMethodName = referencedMember.getName(elementValue.referencedClass);

            if (!methodName.equals(newMethodName))
            {
                // Update the element name index.
                elementValue.u2elementNameIndex =
                    new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newMethodName);
            }
        }
    }


    private void debug(Clazz          clazz,
                       StringConstant stringConstant,
                       Clazz          referencedClass,
                       Member         referencedMember)
    {
        System.out.println("MemberReferenceFixer:");
        System.out.println("  Class file      = "+clazz.getName());
        System.out.println("  Ref class       = "+referencedClass.getName());
        System.out.println("  Ref member name = "+stringConstant.getString(clazz));
        System.out.println("                 -> "+referencedMember.getName(referencedClass));
    }


    private void debug(Clazz       clazz,
                       RefConstant refConstant,
                       Clazz       referencedClass,
                       Member      referencedMember)
    {
        System.out.println("MemberReferenceFixer:");
        System.out.println("  Class file      = "+clazz.getName());
        System.out.println("  Ref class       = "+referencedClass.getName());
        System.out.println("  Ref member name = "+refConstant.getName(clazz));
        System.out.println("                 -> "+referencedMember.getName(referencedClass));
        System.out.println("  Ref descriptor  = "+refConstant.getType(clazz));
        System.out.println("                 -> "+referencedMember.getDescriptor(referencedClass));
    }
}