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

« back to all changes in this revision

Viewing changes to src/proguard/classfile/editor/ConstantPoolEditor.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: ConstantPoolEditor.java,v 1.7 2004/11/20 15:41:24 eric Exp $
 
1
/* $Id: ConstantPoolEditor.java,v 1.10 2005/06/11 13:21:35 eric Exp $
2
2
 *
3
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
4
4
 *
5
 
 * Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu)
 
5
 * Copyright (c) 2002-2005 Eric Lafortune (eric@graphics.cornell.edu)
6
6
 *
7
7
 * This program is free software; you can redistribute it and/or modify it
8
8
 * under the terms of the GNU General Public License as published by the Free
29
29
 */
30
30
public class ConstantPoolEditor
31
31
{
 
32
    private static final boolean DEBUG = false;
 
33
 
 
34
 
32
35
    /**
33
36
     * Finds or creates a StringCpInfo constant pool entry with the given value,
34
37
     * in the given class file.
66
69
 
67
70
 
68
71
    /**
 
72
     * Finds or creates a FieldrefCpInfo constant pool entry for the given
 
73
     * class and field, in the given class file.
 
74
     * @return the constant pool index of the FieldrefCpInfo.
 
75
     */
 
76
    public int addFieldrefCpInfo(ProgramClassFile programClassFile,
 
77
                                 ClassFile        referencedClassFile,
 
78
                                 MemberInfo       referencedMemberInfo)
 
79
    {
 
80
        return addFieldrefCpInfo(programClassFile,
 
81
                                 referencedClassFile.getName(),
 
82
                                 referencedMemberInfo.getName(referencedClassFile),
 
83
                                 referencedMemberInfo.getDescriptor(referencedClassFile),
 
84
                                 referencedClassFile,
 
85
                                 referencedMemberInfo);
 
86
    }
 
87
 
 
88
 
 
89
    /**
69
90
     * Finds or creates a FieldrefCpInfo constant pool entry with the given
70
91
     * class name, field name, and descriptor, in the given class file.
71
92
     * @return the constant pool index of the FieldrefCpInfo.
75
96
                                 String           name,
76
97
                                 String           descriptor,
77
98
                                 ClassFile        referencedClassFile,
78
 
                                 MemberInfo       referencedMemberInfo,
79
 
                                 ClassFile[]      referencedClassFiles)
 
99
                                 MemberInfo       referencedMemberInfo)
80
100
    {
81
101
        return addFieldrefCpInfo(programClassFile,
82
102
                                 className,
83
103
                                 addNameAndTypeCpInfo(programClassFile,
84
104
                                                      name,
85
 
                                                      descriptor,
86
 
                                                      referencedClassFiles),
 
105
                                                      descriptor),
87
106
                                 referencedClassFile,
88
107
                                 referencedMemberInfo);
89
108
    }
121
140
                                 String           name,
122
141
                                 String           descriptor,
123
142
                                 ClassFile        referencedClassFile,
124
 
                                 MemberInfo       referencedMemberInfo,
125
 
                                 ClassFile[]      referencedClassFiles)
 
143
                                 MemberInfo       referencedMemberInfo)
126
144
    {
127
145
        return addFieldrefCpInfo(programClassFile,
128
146
                                 classIndex,
129
147
                                 addNameAndTypeCpInfo(programClassFile,
130
148
                                                      name,
131
 
                                                      descriptor,
132
 
                                                      referencedClassFiles),
 
149
                                                      descriptor),
133
150
                                 referencedClassFile,
134
151
                                 referencedMemberInfo);
135
152
    }
176
193
 
177
194
 
178
195
    /**
179
 
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
180
 
     * class name, method name, and descriptor, in the given class file.
181
 
     * @return the constant pool index of the MethodrefCpInfo.
182
 
     */
183
 
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
184
 
                                  String           className,
185
 
                                  String           name,
186
 
                                  String           descriptor,
187
 
                                  ClassFile        referencedClassFile,
188
 
                                  MemberInfo       referencedMemberInfo,
189
 
                                  ClassFile[]      referencedClassFiles)
190
 
    {
191
 
        return addMethodrefCpInfo(programClassFile,
192
 
                                  className,
193
 
                                  addNameAndTypeCpInfo(programClassFile,
194
 
                                                       name,
195
 
                                                       descriptor,
196
 
                                                       referencedClassFiles),
197
 
                                  referencedClassFile,
198
 
                                  referencedMemberInfo);
199
 
    }
200
 
 
201
 
 
202
 
    /**
203
 
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
204
 
     * class name, method name, and descriptor, in the given class file.
205
 
     * @return the constant pool index of the MethodrefCpInfo.
206
 
     */
207
 
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
208
 
                                  String           className,
209
 
                                  int              nameAndTypeIndex,
210
 
                                  ClassFile        referencedClassFile,
211
 
                                  MemberInfo       referencedMemberInfo)
212
 
    {
213
 
        return addMethodrefCpInfo(programClassFile,
214
 
                                  addClassCpInfo(programClassFile,
215
 
                                                 className,
216
 
                                                 referencedClassFile),
217
 
                                  nameAndTypeIndex,
218
 
                                  referencedClassFile,
219
 
                                  referencedMemberInfo);
220
 
    }
221
 
 
222
 
 
223
 
    /**
224
 
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
225
 
     * class constant pool entry index, method name, and descriptor, in the
226
 
     * given class file.
227
 
     * @return the constant pool index of the MethodrefCpInfo.
228
 
     */
229
 
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
230
 
                                  int              classIndex,
231
 
                                  String           name,
232
 
                                  String           descriptor,
233
 
                                  ClassFile        referencedClassFile,
234
 
                                  MemberInfo       referencedMemberInfo,
235
 
                                  ClassFile[]      referencedClassFiles)
236
 
    {
237
 
        return addMethodrefCpInfo(programClassFile,
238
 
                                  classIndex,
239
 
                                  addNameAndTypeCpInfo(programClassFile,
240
 
                                                       name,
241
 
                                                       descriptor,
242
 
                                                       referencedClassFiles),
243
 
                                  referencedClassFile,
244
 
                                  referencedMemberInfo);
245
 
    }
246
 
 
247
 
 
248
 
    /**
249
 
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
250
 
     * class constant pool entry index and name and type constant pool entry index
251
 
     * the given class file.
252
 
     * @return the constant pool index of the MethodrefCpInfo.
253
 
     */
254
 
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
255
 
                                  int              classIndex,
256
 
                                  int              nameAndTypeIndex,
257
 
                                  ClassFile        referencedClassFile,
258
 
                                  MemberInfo       referencedMemberInfo)
259
 
    {
260
 
        CpInfo[] constantPool      = programClassFile.constantPool;
261
 
        int      constantPoolCount = programClassFile.u2constantPoolCount;
262
 
 
263
 
        // Check if the entry already exists.
264
 
        for (int index = 1; index < constantPoolCount; index++)
265
 
        {
266
 
            CpInfo cpInfo = constantPool[index];
267
 
 
268
 
            if (cpInfo != null &&
269
 
                cpInfo.getTag() == ClassConstants.CONSTANT_Methodref)
270
 
            {
271
 
                MethodrefCpInfo methodrefCpInfo = (MethodrefCpInfo)cpInfo;
272
 
                if (methodrefCpInfo.u2classIndex       == classIndex &&
273
 
                    methodrefCpInfo.u2nameAndTypeIndex == nameAndTypeIndex)
274
 
                {
275
 
                    return index;
276
 
                }
277
 
            }
278
 
        }
279
 
 
280
 
        return addCpInfo(programClassFile,
281
 
                         new MethodrefCpInfo(classIndex,
282
 
                                             nameAndTypeIndex,
283
 
                                             referencedClassFile,
284
 
                                             referencedMemberInfo));
285
 
    }
286
 
 
287
 
 
288
 
    /**
289
196
     * Finds or creates a InterfaceMethodrefCpInfo constant pool entry with the
290
197
     * given class name, method name, and descriptor, in the given class file.
291
198
     * @return the constant pool index of the InterfaceMethodrefCpInfo.
295
202
                                           String           name,
296
203
                                           String           descriptor,
297
204
                                           ClassFile        referencedClassFile,
298
 
                                           MemberInfo       referencedMemberInfo,
299
 
                                           ClassFile[]      referencedClassFiles)
 
205
                                           MemberInfo       referencedMemberInfo)
300
206
    {
301
207
        return addInterfaceMethodrefCpInfo(programClassFile,
302
208
                                           className,
303
209
                                           addNameAndTypeCpInfo(programClassFile,
304
210
                                                                name,
305
 
                                                                descriptor,
306
 
                                                                referencedClassFiles),
 
211
                                                                descriptor),
307
212
                                                                referencedClassFile,
308
213
                                                                referencedMemberInfo);
309
214
    }
331
236
 
332
237
 
333
238
    /**
 
239
     * Finds or creates a InterfaceMethodrefCpInfo constant pool entry for the
 
240
     * given class and method, in the given class file.
 
241
     * @return the constant pool index of the InterfaceMethodrefCpInfo.
 
242
     */
 
243
    public int addInterfaceMethodrefCpInfo(ProgramClassFile programClassFile,
 
244
                                           ClassFile        referencedClassFile,
 
245
                                           MemberInfo       referencedMemberInfo)
 
246
    {
 
247
        return addInterfaceMethodrefCpInfo(programClassFile,
 
248
                                           referencedClassFile.getName(),
 
249
                                           referencedMemberInfo.getName(referencedClassFile),
 
250
                                           referencedMemberInfo.getDescriptor(referencedClassFile),
 
251
                                           referencedClassFile,
 
252
                                           referencedMemberInfo);
 
253
    }
 
254
 
 
255
 
 
256
    /**
334
257
     * Finds or creates a InterfaceMethodrefCpInfo constant pool entry with the
335
258
     * given class constant pool entry index, method name, and descriptor, in
336
259
     * the given class file.
341
264
                                           String           name,
342
265
                                           String           descriptor,
343
266
                                           ClassFile        referencedClassFile,
344
 
                                           MemberInfo       referencedMemberInfo,
345
 
                                           ClassFile[]      referencedClassFiles)
 
267
                                           MemberInfo       referencedMemberInfo)
346
268
    {
347
269
        return addInterfaceMethodrefCpInfo(programClassFile,
348
270
                                           classIndex,
349
271
                                           addNameAndTypeCpInfo(programClassFile,
350
272
                                                                name,
351
 
                                                                descriptor,
352
 
                                                                referencedClassFiles),
353
 
                                                                referencedClassFile,
354
 
                                                                referencedMemberInfo);
 
273
                                                                descriptor),
 
274
                                           referencedClassFile,
 
275
                                           referencedMemberInfo);
355
276
    }
356
277
 
357
278
 
396
317
 
397
318
 
398
319
    /**
 
320
     * Finds or creates a MethodrefCpInfo constant pool entry for the given
 
321
     * class and method, in the given class file.
 
322
     * @return the constant pool index of the MethodrefCpInfo.
 
323
     */
 
324
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
 
325
                                  ClassFile        referencedClassFile,
 
326
                                  MemberInfo       referencedMemberInfo)
 
327
    {
 
328
        return addMethodrefCpInfo(programClassFile,
 
329
                                  referencedClassFile.getName(),
 
330
                                  referencedMemberInfo.getName(referencedClassFile),
 
331
                                  referencedMemberInfo.getDescriptor(referencedClassFile),
 
332
                                  referencedClassFile,
 
333
                                  referencedMemberInfo);
 
334
    }
 
335
 
 
336
 
 
337
    /**
 
338
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
 
339
     * class name, method name, and descriptor, in the given class file.
 
340
     * @return the constant pool index of the MethodrefCpInfo.
 
341
     */
 
342
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
 
343
                                  String           className,
 
344
                                  String           name,
 
345
                                  String           descriptor,
 
346
                                  ClassFile        referencedClassFile,
 
347
                                  MemberInfo       referencedMemberInfo)
 
348
    {
 
349
        return addMethodrefCpInfo(programClassFile,
 
350
                                  className,
 
351
                                  addNameAndTypeCpInfo(programClassFile,
 
352
                                                       name,
 
353
                                                       descriptor),
 
354
                                  referencedClassFile,
 
355
                                  referencedMemberInfo);
 
356
    }
 
357
 
 
358
 
 
359
    /**
 
360
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
 
361
     * class name, method name, and descriptor, in the given class file.
 
362
     * @return the constant pool index of the MethodrefCpInfo.
 
363
     */
 
364
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
 
365
                                  String           className,
 
366
                                  int              nameAndTypeIndex,
 
367
                                  ClassFile        referencedClassFile,
 
368
                                  MemberInfo       referencedMemberInfo)
 
369
    {
 
370
        return addMethodrefCpInfo(programClassFile,
 
371
                                  addClassCpInfo(programClassFile,
 
372
                                                 className,
 
373
                                                 referencedClassFile),
 
374
                                  nameAndTypeIndex,
 
375
                                  referencedClassFile,
 
376
                                  referencedMemberInfo);
 
377
    }
 
378
 
 
379
 
 
380
    /**
 
381
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
 
382
     * class constant pool entry index, method name, and descriptor, in the
 
383
     * given class file.
 
384
     * @return the constant pool index of the MethodrefCpInfo.
 
385
     */
 
386
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
 
387
                                  int              classIndex,
 
388
                                  String           name,
 
389
                                  String           descriptor,
 
390
                                  ClassFile        referencedClassFile,
 
391
                                  MemberInfo       referencedMemberInfo)
 
392
    {
 
393
        return addMethodrefCpInfo(programClassFile,
 
394
                                  classIndex,
 
395
                                  addNameAndTypeCpInfo(programClassFile,
 
396
                                                       name,
 
397
                                                       descriptor),
 
398
                                  referencedClassFile,
 
399
                                  referencedMemberInfo);
 
400
    }
 
401
 
 
402
 
 
403
    /**
 
404
     * Finds or creates a MethodrefCpInfo constant pool entry with the given
 
405
     * class constant pool entry index and name and type constant pool entry index
 
406
     * the given class file.
 
407
     * @return the constant pool index of the MethodrefCpInfo.
 
408
     */
 
409
    public int addMethodrefCpInfo(ProgramClassFile programClassFile,
 
410
                                  int              classIndex,
 
411
                                  int              nameAndTypeIndex,
 
412
                                  ClassFile        referencedClassFile,
 
413
                                  MemberInfo       referencedMemberInfo)
 
414
    {
 
415
        CpInfo[] constantPool      = programClassFile.constantPool;
 
416
        int      constantPoolCount = programClassFile.u2constantPoolCount;
 
417
 
 
418
        // Check if the entry already exists.
 
419
        for (int index = 1; index < constantPoolCount; index++)
 
420
        {
 
421
            CpInfo cpInfo = constantPool[index];
 
422
 
 
423
            if (cpInfo != null &&
 
424
                cpInfo.getTag() == ClassConstants.CONSTANT_Methodref)
 
425
            {
 
426
                MethodrefCpInfo methodrefCpInfo = (MethodrefCpInfo)cpInfo;
 
427
                if (methodrefCpInfo.u2classIndex       == classIndex &&
 
428
                    methodrefCpInfo.u2nameAndTypeIndex == nameAndTypeIndex)
 
429
                {
 
430
                    return index;
 
431
                }
 
432
            }
 
433
        }
 
434
 
 
435
        return addCpInfo(programClassFile,
 
436
                         new MethodrefCpInfo(classIndex,
 
437
                                             nameAndTypeIndex,
 
438
                                             referencedClassFile,
 
439
                                             referencedMemberInfo));
 
440
    }
 
441
 
 
442
 
 
443
    /**
 
444
     * Finds or creates a ClassCpInfo constant pool entry for the given class,
 
445
     * in the given class file.
 
446
     * @return the constant pool index of the ClassCpInfo.
 
447
     */
 
448
    public int addClassCpInfo(ProgramClassFile programClassFile,
 
449
                              ClassFile        referencedClassFile)
 
450
    {
 
451
        return addClassCpInfo(programClassFile,
 
452
                              referencedClassFile.getName(),
 
453
                              referencedClassFile);
 
454
    }
 
455
 
 
456
 
 
457
    /**
399
458
     * Finds or creates a ClassCpInfo constant pool entry with the given name,
400
459
     * in the given class file.
401
460
     * @return the constant pool index of the ClassCpInfo.
404
463
                              String           name,
405
464
                              ClassFile        referencedClassFile)
406
465
    {
407
 
        CpInfo[] constantPool        = programClassFile.constantPool;
 
466
        CpInfo[] constantPool      = programClassFile.constantPool;
408
467
        int      constantPoolCount = programClassFile.u2constantPoolCount;
409
468
 
410
469
        // Check if the entry already exists.
438
497
     */
439
498
    public int addNameAndTypeCpInfo(ProgramClassFile programClassFile,
440
499
                                    String           name,
441
 
                                    String           type,
442
 
                                    ClassFile[]      referencedClassFiles)
 
500
                                    String           type)
443
501
    {
444
 
        CpInfo[] constantPool        = programClassFile.constantPool;
 
502
        CpInfo[] constantPool      = programClassFile.constantPool;
445
503
        int      constantPoolCount = programClassFile.u2constantPoolCount;
446
504
 
447
505
        // Check if the entry already exists.
466
524
 
467
525
        return addCpInfo(programClassFile,
468
526
                         new NameAndTypeCpInfo(nameIndex,
469
 
                                               descriptorIndex,
470
 
                                               referencedClassFiles));
 
527
                                               descriptorIndex));
471
528
    }
472
529
 
473
530
 
474
531
    /**
475
 
     * Finds or creates an Utf8CpInfo constant pool entry for the given string,
 
532
     * Finds or creates a Utf8CpInfo constant pool entry for the given string,
476
533
     * in the given class file.
477
534
     * @return the constant pool index of the Utf8CpInfo.
478
535
     */
479
536
    public int addUtf8CpInfo(ProgramClassFile programClassFile,
480
537
                             String           string)
481
538
    {
482
 
        CpInfo[] constantPool        = programClassFile.constantPool;
 
539
        CpInfo[] constantPool      = programClassFile.constantPool;
483
540
        int      constantPoolCount = programClassFile.u2constantPoolCount;
484
541
 
485
542
        // Check if the entry already exists.
507
564
     * in the given class file.
508
565
     * @return the constant pool index for the added entry.
509
566
     */
510
 
    private int addCpInfo(ProgramClassFile programClassFile,
511
 
                          CpInfo           cpInfo)
 
567
    public int addCpInfo(ProgramClassFile programClassFile,
 
568
                         CpInfo           cpInfo)
512
569
    {
513
 
        CpInfo[] constantPool        = programClassFile.constantPool;
 
570
        CpInfo[] constantPool      = programClassFile.constantPool;
514
571
        int      constantPoolCount = programClassFile.u2constantPoolCount;
515
572
 
516
573
        // Make sure there is enough space for another constant pool entry.
523
580
            constantPool = programClassFile.constantPool;
524
581
        }
525
582
 
 
583
        if (DEBUG)
 
584
        {
 
585
            System.out.println(programClassFile.getName()+": adding ["+cpInfo.getClass().getName()+"] at index "+programClassFile.u2constantPoolCount);
 
586
        }
 
587
 
526
588
        // Create a new Utf8CpInfo for the given string.
527
589
        constantPool[programClassFile.u2constantPoolCount++] = cpInfo;
528
590