Home | History | Annotate | Download | only in rewriter
      1 /*
      2  * Copyright 2014, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.dexlib2.rewriter;
     33 
     34 import org.jf.dexlib2.Opcode;
     35 import org.jf.dexlib2.ReferenceType;
     36 import org.jf.dexlib2.iface.instruction.Instruction;
     37 import org.jf.dexlib2.iface.instruction.ReferenceInstruction;
     38 import org.jf.dexlib2.iface.instruction.formats.*;
     39 import org.jf.dexlib2.iface.reference.FieldReference;
     40 import org.jf.dexlib2.iface.reference.MethodReference;
     41 import org.jf.dexlib2.iface.reference.Reference;
     42 import org.jf.dexlib2.iface.reference.TypeReference;
     43 
     44 import javax.annotation.Nonnull;
     45 
     46 public class InstructionRewriter implements Rewriter<Instruction> {
     47     @Nonnull protected final Rewriters rewriters;
     48 
     49     public InstructionRewriter(@Nonnull Rewriters rewriters) {
     50         this.rewriters = rewriters;
     51     }
     52 
     53     @Nonnull @Override public Instruction rewrite(@Nonnull Instruction instruction) {
     54         if (instruction instanceof ReferenceInstruction) {
     55             switch (instruction.getOpcode().format) {
     56                 case Format20bc:
     57                     return new RewrittenInstruction20bc((Instruction20bc)instruction);
     58                 case Format21c:
     59                     return new RewrittenInstruction21c((Instruction21c)instruction);
     60                 case Format22c:
     61                     return new RewrittenInstruction22c((Instruction22c)instruction);
     62                 case Format31c:
     63                     return new RewrittenInstruction31c((Instruction31c)instruction);
     64                 case Format35c:
     65                     return new RewrittenInstruction35c((Instruction35c)instruction);
     66                 case Format3rc:
     67                     return new RewrittenInstruction3rc((Instruction3rc)instruction);
     68                 default:
     69                     throw new IllegalArgumentException();
     70             }
     71         }
     72         return instruction;
     73     }
     74 
     75     protected class BaseRewrittenReferenceInstruction<T extends ReferenceInstruction>
     76             implements ReferenceInstruction {
     77         @Nonnull protected T instruction;
     78 
     79         protected BaseRewrittenReferenceInstruction(@Nonnull T instruction) {
     80             this.instruction = instruction;
     81         }
     82 
     83         @Override @Nonnull public Reference getReference() {
     84             switch (getReferenceType()) {
     85                 case ReferenceType.TYPE:
     86                     return RewriterUtils.rewriteTypeReference(rewriters.getTypeRewriter(),
     87                             (TypeReference)instruction.getReference());
     88                 case ReferenceType.FIELD:
     89                     return rewriters.getFieldReferenceRewriter().rewrite((FieldReference)instruction.getReference());
     90                 case ReferenceType.METHOD:
     91                     return rewriters.getMethodReferenceRewriter().rewrite((MethodReference)instruction.getReference());
     92                 case ReferenceType.STRING:
     93                     return instruction.getReference();
     94                 default:
     95                     throw new IllegalArgumentException();
     96             }
     97         }
     98 
     99         @Override public int getReferenceType() {
    100             return instruction.getReferenceType();
    101         }
    102 
    103         @Override public Opcode getOpcode() {
    104             return instruction.getOpcode();
    105         }
    106 
    107         @Override public int getCodeUnits() {
    108             return instruction.getCodeUnits();
    109         }
    110     }
    111 
    112     protected class RewrittenInstruction20bc extends BaseRewrittenReferenceInstruction<Instruction20bc>
    113             implements Instruction20bc {
    114         public RewrittenInstruction20bc(@Nonnull Instruction20bc instruction) {
    115             super(instruction);
    116         }
    117 
    118         @Override public int getVerificationError() {
    119             return instruction.getVerificationError();
    120         }
    121     }
    122 
    123     protected class RewrittenInstruction21c extends BaseRewrittenReferenceInstruction<Instruction21c>
    124             implements Instruction21c {
    125         public RewrittenInstruction21c(@Nonnull Instruction21c instruction) {
    126             super(instruction);
    127         }
    128 
    129         public int getRegisterA() {
    130             return instruction.getRegisterA();
    131         }
    132     }
    133 
    134     protected class RewrittenInstruction22c extends BaseRewrittenReferenceInstruction<Instruction22c>
    135             implements Instruction22c {
    136         public RewrittenInstruction22c(@Nonnull Instruction22c instruction) {
    137             super(instruction);
    138         }
    139 
    140         public int getRegisterA() {
    141             return instruction.getRegisterA();
    142         }
    143 
    144         public int getRegisterB() {
    145             return instruction.getRegisterB();
    146         }
    147     }
    148 
    149     protected class RewrittenInstruction31c extends BaseRewrittenReferenceInstruction<Instruction31c>
    150             implements Instruction31c {
    151         public RewrittenInstruction31c(@Nonnull Instruction31c instruction) {
    152             super(instruction);
    153         }
    154 
    155         public int getRegisterA() {
    156             return instruction.getRegisterA();
    157         }
    158     }
    159 
    160     protected class RewrittenInstruction35c extends BaseRewrittenReferenceInstruction<Instruction35c>
    161             implements Instruction35c {
    162         public RewrittenInstruction35c(@Nonnull Instruction35c instruction) {
    163             super(instruction);
    164         }
    165 
    166         public int getRegisterC() {
    167             return instruction.getRegisterC();
    168         }
    169 
    170         public int getRegisterE() {
    171             return instruction.getRegisterE();
    172         }
    173 
    174         public int getRegisterG() {
    175             return instruction.getRegisterG();
    176         }
    177 
    178         public int getRegisterCount() {
    179             return instruction.getRegisterCount();
    180         }
    181 
    182         public int getRegisterD() {
    183             return instruction.getRegisterD();
    184         }
    185 
    186         public int getRegisterF() {
    187             return instruction.getRegisterF();
    188         }
    189     }
    190 
    191     protected class RewrittenInstruction3rc extends BaseRewrittenReferenceInstruction<Instruction3rc>
    192             implements Instruction3rc {
    193         public RewrittenInstruction3rc(@Nonnull Instruction3rc instruction) {
    194             super(instruction);
    195         }
    196 
    197         public int getStartRegister() {
    198             return instruction.getStartRegister();
    199         }
    200 
    201         public int getRegisterCount() {
    202             return instruction.getRegisterCount();
    203         }
    204     }
    205 }
    206