~ubuntu-branches/ubuntu/wily/proguard/wily

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): komal Sukhani
  • Date: 2015-08-31 14:45:54 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20150831144554-v4gb9hzo8xldfky9
Tags: 5.2.1-1
* Team upload
* New upstream release
* Add new patches
  - correct_properties_file_path
  - enable_building_of_gradle_task
* Add gradle package in build depends
* d/control: Switch Vcs-Browser field to cgit

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3
3
 *             of Java bytecode.
4
4
 *
5
 
 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
 
5
 * Copyright (c) 2002-2015 Eric Lafortune @ GuardSquare
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
28
28
import proguard.classfile.instruction.*;
29
29
import proguard.classfile.instruction.visitor.InstructionVisitor;
30
30
import proguard.classfile.util.SimplifiedVisitor;
 
31
import proguard.classfile.visitor.ClassPrinter;
31
32
import proguard.util.ArrayUtil;
32
33
 
33
34
import java.util.Arrays;
67
68
    private int maximumCodeLength;
68
69
    private int codeLength;
69
70
    private int exceptionTableLength;
 
71
    private int lineNumberTableLength;
70
72
    private int level = -1;
71
73
 
72
74
    private byte[]  code                  = new byte[ClassConstants.TYPICAL_CODE_LENGTH];
76
78
    private final int[]   codeFragmentLengths  = new int[MAXIMUM_LEVELS];
77
79
    private final int[][] instructionOffsetMap = new int[MAXIMUM_LEVELS][ClassConstants.TYPICAL_CODE_LENGTH + 1];
78
80
 
79
 
    private ExceptionInfo[] exceptionTable = new ExceptionInfo[ClassConstants.TYPICAL_EXCEPTION_TABLE_LENGTH];
 
81
    private ExceptionInfo[]  exceptionTable  = new ExceptionInfo[ClassConstants.TYPICAL_EXCEPTION_TABLE_LENGTH];
 
82
    private LineNumberInfo[] lineNumberTable = new LineNumberInfo[ClassConstants.TYPICAL_LINE_NUMBER_TABLE_LENGTH];
80
83
 
81
84
    private int expectedStackMapFrameOffset;
82
85
 
124
127
     */
125
128
    public void reset()
126
129
    {
127
 
        maximumCodeLength    = 0;
128
 
        codeLength           = 0;
129
 
        exceptionTableLength = 0;
130
 
        level                = -1;
 
130
        maximumCodeLength     = 0;
 
131
        codeLength            = 0;
 
132
        exceptionTableLength  = 0;
 
133
        lineNumberTableLength = 0;
 
134
        level                 = -1;
131
135
 
132
136
        // Make sure the instruction writer has at least the same buffer size
133
137
        // as the local arrays.
340
344
 
341
345
 
342
346
    /**
 
347
     * Inserts the given line number at the appropriate position in the line
 
348
     * number table.
 
349
     * @param lineNumberInfo the line number to be inserted.
 
350
     * @return the index where the line number was actually inserted.
 
351
     */
 
352
    public int insertLineNumber(LineNumberInfo lineNumberInfo)
 
353
    {
 
354
        return insertLineNumber(0, lineNumberInfo);
 
355
    }
 
356
 
 
357
 
 
358
    /**
 
359
     * Inserts the given line number at the appropriate position in the line
 
360
     * number table.
 
361
     * @param minimumIndex   the minimum index where the line number may be
 
362
     *                       inserted.
 
363
     * @param lineNumberInfo the line number to be inserted.
 
364
     * @return the index where the line number was inserted.
 
365
     */
 
366
    public int insertLineNumber(int minimumIndex, LineNumberInfo lineNumberInfo)
 
367
    {
 
368
        if (DEBUG)
 
369
        {
 
370
            print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
 
371
        }
 
372
 
 
373
        // Remap the line number right away.
 
374
        visitLineNumberInfo(null, null, null, lineNumberInfo);
 
375
 
 
376
        if (DEBUG)
 
377
        {
 
378
            System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
 
379
        }
 
380
 
 
381
        lineNumberTable =
 
382
            (LineNumberInfo[])ArrayUtil.extendArray(lineNumberTable,
 
383
                                                    lineNumberTableLength + 1);
 
384
 
 
385
        // Find the insertion index, starting from the end.
 
386
        // Don't insert before a negative line number, in case of a tie.
 
387
        int index = lineNumberTableLength++;
 
388
        while (index > minimumIndex &&
 
389
               (lineNumberTable[index - 1].u2startPC    >  lineNumberInfo.u2startPC ||
 
390
                lineNumberTable[index - 1].u2startPC    >= lineNumberInfo.u2startPC &&
 
391
                lineNumberTable[index - 1].u2lineNumber >= 0))
 
392
        {
 
393
            lineNumberTable[index] = lineNumberTable[--index];
 
394
        }
 
395
 
 
396
        lineNumberTable[index] = lineNumberInfo;
 
397
 
 
398
        return index;
 
399
    }
 
400
 
 
401
 
 
402
    /**
 
403
     * Appends the given line number to the line number table.
 
404
     * @param lineNumberInfo the line number to be appended.
 
405
     */
 
406
    public void appendLineNumber(LineNumberInfo lineNumberInfo)
 
407
    {
 
408
        if (DEBUG)
 
409
        {
 
410
            print("         ", "Line number ["+lineNumberInfo.u2startPC+"]");
 
411
        }
 
412
 
 
413
        // Remap the line number right away.
 
414
        visitLineNumberInfo(null, null, null, lineNumberInfo);
 
415
 
 
416
        if (DEBUG)
 
417
        {
 
418
            System.out.println(" -> ["+lineNumberInfo.u2startPC+"] line "+lineNumberInfo.u2lineNumber+(lineNumberInfo.getSource()==null ? "":" ["+lineNumberInfo.getSource()+"]"));
 
419
        }
 
420
 
 
421
        // Add the line number.
 
422
        lineNumberTable =
 
423
            (LineNumberInfo[])ArrayUtil.add(lineNumberTable,
 
424
                                            lineNumberTableLength++,
 
425
                                            lineNumberInfo);
 
426
    }
 
427
 
 
428
 
 
429
    /**
343
430
     * Wraps up the current code fragment, continuing with the previous one on
344
431
     * the stack.
345
432
     */
458
545
        stackSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
459
546
        variableSizeUpdater.visitCodeAttribute(clazz, method, codeAttribute);
460
547
 
461
 
        // Remap  the line number table and the local variable table.
 
548
        // Add a new line number table for the line numbers, if necessary.
 
549
        if (lineNumberTableLength > 0 &&
 
550
            codeAttribute.getAttribute(clazz, ClassConstants.ATTR_LineNumberTable) == null)
 
551
        {
 
552
            int attributeNameIndex =
 
553
                new ConstantPoolEditor((ProgramClass)clazz)
 
554
                    .addUtf8Constant(ClassConstants.ATTR_LineNumberTable);
 
555
 
 
556
            new AttributesEditor((ProgramClass)clazz, (ProgramMember)method, codeAttribute, false)
 
557
                .addAttribute(new LineNumberTableAttribute(attributeNameIndex, 0, null));
 
558
        }
 
559
 
 
560
        // Copy the line number table and the local variable table.
462
561
        codeAttribute.attributesAccept(clazz, method, this);
463
562
 
464
563
        // Remap the exception table (done before).
473
572
        instructionWriter.visitCodeAttribute(clazz, method, codeAttribute);
474
573
 
475
574
        level--;
 
575
 
 
576
        if (DEBUG)
 
577
        {
 
578
            codeAttribute.accept(clazz, method, new ClassPrinter());
 
579
        }
476
580
    }
477
581
 
478
582
 
494
598
 
495
599
    public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
496
600
    {
497
 
        // Remap all line number table entries.
498
 
        lineNumberTableAttribute.lineNumbersAccept(clazz, method, codeAttribute, this);
 
601
        // Didn't we get line number new definitions?
 
602
        if (lineNumberTableLength == 0)
 
603
        {
 
604
            // Remap all line number table entries of the existing table.
 
605
            lineNumberTableAttribute.lineNumbersAccept(clazz, method, codeAttribute, this);
 
606
        }
 
607
        else
 
608
        {
 
609
            // Remove line numbers with empty code blocks.
 
610
            // Actually, we'll do this elsewhere, to allow processing the
 
611
            // line numbers of inlined methods.
 
612
            //lineNumberTableLength =
 
613
            //    removeEmptyLineNumbers(lineNumberTable,
 
614
            //                           lineNumberTableLength,
 
615
            //                           codeAttribute.u4codeLength);
499
616
 
500
 
        // Remove line numbers with empty code blocks.
501
 
        lineNumberTableAttribute.u2lineNumberTableLength =
502
 
           removeEmptyLineNumbers(lineNumberTableAttribute.lineNumberTable,
503
 
                                  lineNumberTableAttribute.u2lineNumberTableLength,
504
 
                                  codeAttribute.u4codeLength);
 
617
            // Copy the line number table.
 
618
            lineNumberTableAttribute.lineNumberTable         = new LineNumberInfo[lineNumberTableLength];
 
619
            lineNumberTableAttribute.u2lineNumberTableLength = lineNumberTableLength;
 
620
            System.arraycopy(lineNumberTable, 0, lineNumberTableAttribute.lineNumberTable, 0, lineNumberTableLength);
 
621
        }
505
622
    }
506
623
 
507
 
 
508
624
    public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
509
625
    {
510
626
        // Remap all local variable table entries.