SparseSwitchMethodItem.java revision 754b3c4dc009b7a02e39001560c3f0fd6a7cc2c0
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.baksmali.Adaptors.Format; 30 31import org.jf.baksmali.Adaptors.LabelMethodItem; 32import org.jf.baksmali.Adaptors.MethodDefinition; 33import org.jf.dexlib2.iface.instruction.SwitchElement; 34import org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload; 35import org.jf.util.IndentingWriter; 36import org.jf.baksmali.Renderers.IntegerRenderer; 37 38import java.io.IOException; 39import java.util.ArrayList; 40import java.util.List; 41 42public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> { 43 private final List<SparseSwitchTarget> targets; 44 45 public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, SparseSwitchPayload instruction) { 46 super(methodDef, codeAddress, instruction); 47 48 int baseCodeAddress = methodDef.getSparseSwitchBaseAddress(codeAddress); 49 50 targets = new ArrayList<SparseSwitchTarget>(); 51 if (baseCodeAddress >= 0) { 52 for (SwitchElement switchElement: instruction.getSwitchElements()) { 53 LabelMethodItem label = new LabelMethodItem(baseCodeAddress + switchElement.getOffset(), "sswitch_"); 54 targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label)); 55 } 56 } else { 57 //if we couldn't determine a base address, just use relative offsets rather than labels 58 for (SwitchElement switchElement: instruction.getSwitchElements()) { 59 targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset())); 60 } 61 } 62 } 63 64 @Override 65 public boolean writeTo(IndentingWriter writer) throws IOException { 66 writer.write(".sparse-switch\n"); 67 writer.indent(4); 68 for (SparseSwitchTarget target: targets) { 69 IntegerRenderer.writeTo(writer, target.getKey()); 70 writer.write(" -> "); 71 target.writeTargetTo(writer); 72 writer.write('\n'); 73 } 74 writer.deindent(4); 75 writer.write(".end sparse-switch"); 76 return true; 77 } 78 79 private static abstract class SparseSwitchTarget { 80 private final int key; 81 public SparseSwitchTarget(int key) { 82 this.key = key; 83 } 84 public int getKey() { return key; } 85 public abstract void writeTargetTo(IndentingWriter writer) throws IOException; 86 } 87 88 private static class SparseSwitchLabelTarget extends SparseSwitchTarget { 89 private final LabelMethodItem target; 90 public SparseSwitchLabelTarget(int key, LabelMethodItem target) { 91 super(key); 92 this.target = target; 93 } 94 95 public void writeTargetTo(IndentingWriter writer) throws IOException { 96 target.writeTo(writer); 97 } 98 } 99 100 private static class SparseSwitchOffsetTarget extends SparseSwitchTarget { 101 private final int target; 102 public SparseSwitchOffsetTarget(int key, int target) { 103 super(key); 104 this.target = target; 105 } 106 107 public void writeTargetTo(IndentingWriter writer) throws IOException { 108 if (target >= 0) { 109 writer.write('+'); 110 } 111 writer.printSignedIntAsDec(target); 112 } 113 } 114} 115