Home | History | Annotate | Download | only in instruction
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) 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.instruction;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.attribute.CodeAttribute;
     25 import proguard.classfile.instruction.visitor.InstructionVisitor;
     26 
     27 /**
     28  * This Instruction represents a simple instruction without variable arguments
     29  * or constant pool references.
     30  *
     31  * @author Eric Lafortune
     32  */
     33 public class LookUpSwitchInstruction extends SwitchInstruction
     34 {
     35     public int[] cases;
     36 
     37 
     38     /**
     39      * Creates an uninitialized LookUpSwitchInstruction.
     40      */
     41     public LookUpSwitchInstruction() {}
     42 
     43 
     44     /**
     45      * Creates a new LookUpSwitchInstruction with the given arguments.
     46      */
     47     public LookUpSwitchInstruction(byte  opcode,
     48                                    int   defaultOffset,
     49                                    int[] cases,
     50                                    int[] jumpOffsets)
     51     {
     52         this.opcode        = opcode;
     53         this.defaultOffset = defaultOffset;
     54         this.cases         = cases;
     55         this.jumpOffsets   = jumpOffsets;
     56     }
     57 
     58 
     59     /**
     60      * Copies the given instruction into this instruction.
     61      * @param lookUpSwitchInstruction the instruction to be copied.
     62      * @return this instruction.
     63      */
     64     public LookUpSwitchInstruction copy(LookUpSwitchInstruction lookUpSwitchInstruction)
     65     {
     66         this.opcode        = lookUpSwitchInstruction.opcode;
     67         this.defaultOffset = lookUpSwitchInstruction.defaultOffset;
     68         this.cases         = lookUpSwitchInstruction.cases;
     69         this.jumpOffsets   = lookUpSwitchInstruction.jumpOffsets;
     70 
     71         return this;
     72     }
     73 
     74 
     75     // Implementations for Instruction.
     76 
     77     public Instruction shrink()
     78     {
     79         // There aren't any ways to shrink this instruction.
     80         return this;
     81     }
     82 
     83     protected void readInfo(byte[] code, int offset)
     84     {
     85         // Skip up to three padding bytes.
     86         offset += -offset & 3;
     87 
     88         // Read the two 32-bit arguments.
     89         defaultOffset       = readInt(code, offset); offset += 4;
     90         int jumpOffsetCount = readInt(code, offset); offset += 4;
     91 
     92         // Read the matches-offset pairs.
     93         cases       = new int[jumpOffsetCount];
     94         jumpOffsets = new int[jumpOffsetCount];
     95 
     96         for (int index = 0; index < jumpOffsetCount; index++)
     97         {
     98             cases[index]       = readInt(code, offset); offset += 4;
     99             jumpOffsets[index] = readInt(code, offset); offset += 4;
    100         }
    101     }
    102 
    103 
    104     protected void writeInfo(byte[] code, int offset)
    105     {
    106         // Write up to three padding bytes.
    107         while ((offset & 3) != 0)
    108         {
    109             writeByte(code, offset++, 0);
    110         }
    111 
    112         // Write the two 32-bit arguments.
    113         writeInt(code, offset, defaultOffset); offset += 4;
    114         writeInt(code, offset, cases.length);  offset += 4;
    115 
    116         // Write the matches-offset pairs.
    117         for (int index = 0; index < cases.length; index++)
    118         {
    119             writeInt(code, offset, cases[index]);       offset += 4;
    120             writeInt(code, offset, jumpOffsets[index]); offset += 4;
    121         }
    122     }
    123 
    124 
    125     public int length(int offset)
    126     {
    127         return 1 + (-(offset+1) & 3) + 8 + cases.length * 8;
    128     }
    129 
    130 
    131     public void accept(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, InstructionVisitor instructionVisitor)
    132     {
    133         instructionVisitor.visitLookUpSwitchInstruction(clazz, method, codeAttribute, offset, this);
    134     }
    135 }
    136