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.DebugItemType; 35 import org.jf.dexlib2.iface.debug.*; 36 import org.jf.dexlib2.iface.reference.StringReference; 37 import org.jf.dexlib2.iface.reference.TypeReference; 38 39 import javax.annotation.Nonnull; 40 import javax.annotation.Nullable; 41 42 public class DebugItemRewriter implements Rewriter<DebugItem> { 43 @Nonnull protected final Rewriters rewriters; 44 45 public DebugItemRewriter(@Nonnull Rewriters rewriters) { 46 this.rewriters = rewriters; 47 } 48 49 @Nonnull @Override public DebugItem rewrite(@Nonnull DebugItem value) { 50 switch (value.getDebugItemType()) { 51 case DebugItemType.START_LOCAL: 52 return new RewrittenStartLocal((StartLocal)value); 53 case DebugItemType.END_LOCAL: 54 return new RewrittenEndLocal((EndLocal)value); 55 case DebugItemType.RESTART_LOCAL: 56 return new RewrittenRestartLocal((RestartLocal)value); 57 default: 58 return value; 59 } 60 } 61 62 protected class BaseRewrittenLocalInfoDebugItem<T extends DebugItem & LocalInfo> implements DebugItem, LocalInfo { 63 @Nonnull protected T debugItem; 64 65 public BaseRewrittenLocalInfoDebugItem (@Nonnull T debugItem) { 66 this.debugItem = debugItem; 67 } 68 69 @Override public int getDebugItemType() { 70 return debugItem.getDebugItemType(); 71 } 72 73 @Override public int getCodeAddress() { 74 return debugItem.getCodeAddress(); 75 } 76 77 @Override @Nullable public String getName() { 78 return debugItem.getName(); 79 } 80 81 @Override @Nullable public String getType() { 82 return RewriterUtils.rewriteNullable(rewriters.getTypeRewriter(), debugItem.getType()); 83 } 84 85 @Override @Nullable public String getSignature() { 86 return debugItem.getSignature(); 87 } 88 } 89 90 protected class RewrittenStartLocal extends BaseRewrittenLocalInfoDebugItem<StartLocal> implements StartLocal { 91 public RewrittenStartLocal(@Nonnull StartLocal debugItem) { 92 super(debugItem); 93 } 94 95 @Override public int getRegister() { 96 return debugItem.getRegister(); 97 } 98 99 @Override @Nullable public StringReference getNameReference() { 100 return debugItem.getNameReference(); 101 } 102 103 @Override @Nullable public TypeReference getTypeReference() { 104 TypeReference typeReference = debugItem.getTypeReference(); 105 if (typeReference == null) { 106 return null; 107 } 108 109 return RewriterUtils.rewriteTypeReference(rewriters.getTypeRewriter(), typeReference); 110 } 111 112 @Override @Nullable public StringReference getSignatureReference() { 113 return debugItem.getSignatureReference(); 114 } 115 } 116 117 protected class RewrittenEndLocal extends BaseRewrittenLocalInfoDebugItem<EndLocal> implements EndLocal { 118 public RewrittenEndLocal(@Nonnull EndLocal instruction) { 119 super(instruction); 120 } 121 122 public int getRegister() { 123 return debugItem.getRegister(); 124 } 125 } 126 127 protected class RewrittenRestartLocal extends BaseRewrittenLocalInfoDebugItem<RestartLocal> 128 implements RestartLocal { 129 public RewrittenRestartLocal(@Nonnull RestartLocal instruction) { 130 super(instruction); 131 } 132 133 public int getRegister() { 134 return debugItem.getRegister(); 135 } 136 } 137 } 138