1 /* 2 * Copyright 2012, 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.util; 33 34 import com.google.common.collect.ImmutableList; 35 import com.google.common.collect.ImmutableMap; 36 import com.google.common.collect.Maps; 37 import org.jf.dexlib2.AccessFlags; 38 import org.jf.dexlib2.Opcodes; 39 import org.jf.dexlib2.iface.ClassDef; 40 import org.jf.dexlib2.iface.Method; 41 import org.jf.dexlib2.iface.MethodImplementation; 42 import org.jf.dexlib2.iface.instruction.Instruction; 43 import org.jf.dexlib2.iface.instruction.ReferenceInstruction; 44 import org.jf.dexlib2.iface.reference.MethodReference; 45 import org.jf.dexlib2.iface.reference.Reference; 46 47 import javax.annotation.Nonnull; 48 import javax.annotation.Nullable; 49 import java.util.List; 50 import java.util.Map; 51 52 public class SyntheticAccessorResolver { 53 public static final int METHOD = 0; 54 public static final int GETTER = 1; 55 public static final int SETTER = 2; 56 public static final int POSTFIX_INCREMENT = 3; 57 public static final int PREFIX_INCREMENT = 4; 58 public static final int POSTFIX_DECREMENT = 5; 59 public static final int PREFIX_DECREMENT = 6; 60 public static final int ADD_ASSIGNMENT = 7; 61 public static final int SUB_ASSIGNMENT = 8; 62 public static final int MUL_ASSIGNMENT = 9; 63 public static final int DIV_ASSIGNMENT = 10; 64 public static final int REM_ASSIGNMENT = 11; 65 public static final int AND_ASSIGNMENT = 12; 66 public static final int OR_ASSIGNMENT = 13; 67 public static final int XOR_ASSIGNMENT = 14; 68 public static final int SHL_ASSIGNMENT = 15; 69 public static final int SHR_ASSIGNMENT = 16; 70 public static final int USHR_ASSIGNMENT = 17; 71 72 private final SyntheticAccessorFSM syntheticAccessorFSM; 73 private final Map<String, ClassDef> classDefMap; 74 private final Map<String, AccessedMember> resolvedAccessors = Maps.newConcurrentMap(); 75 76 public SyntheticAccessorResolver(@Nonnull Opcodes opcodes, @Nonnull Iterable<? extends ClassDef> classDefs) { 77 this.syntheticAccessorFSM = new SyntheticAccessorFSM(opcodes); 78 ImmutableMap.Builder<String, ClassDef> builder = ImmutableMap.builder(); 79 80 for (ClassDef classDef: classDefs) { 81 builder.put(classDef.getType(), classDef); 82 } 83 84 this.classDefMap = builder.build(); 85 } 86 87 public static boolean looksLikeSyntheticAccessor(String methodName) { 88 return methodName.startsWith("access$"); 89 } 90 91 @Nullable 92 public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) { 93 String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference); 94 95 AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor); 96 if (accessedMember != null) { 97 return accessedMember; 98 } 99 100 String type = methodReference.getDefiningClass(); 101 ClassDef classDef = classDefMap.get(type); 102 if (classDef == null) { 103 return null; 104 } 105 106 Method matchedMethod = null; 107 MethodImplementation matchedMethodImpl = null; 108 for (Method method: classDef.getMethods()) { 109 MethodImplementation methodImpl = method.getImplementation(); 110 if (methodImpl != null) { 111 if (methodReferenceEquals(method, methodReference)) { 112 matchedMethod = method; 113 matchedMethodImpl = methodImpl; 114 break; 115 } 116 } 117 } 118 119 if (matchedMethod == null) { 120 return null; 121 } 122 123 //A synthetic accessor will be marked synthetic 124 if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) { 125 return null; 126 } 127 128 List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions()); 129 130 131 int accessType = syntheticAccessorFSM.test(instructions); 132 133 if (accessType >= 0) { 134 AccessedMember member = 135 new AccessedMember(accessType, ((ReferenceInstruction)instructions.get(0)).getReference()); 136 resolvedAccessors.put(methodDescriptor, member); 137 return member; 138 } 139 return null; 140 } 141 142 public static class AccessedMember { 143 public final int accessedMemberType; 144 @Nonnull public final Reference accessedMember; 145 146 public AccessedMember(int accessedMemberType, @Nonnull Reference accessedMember) { 147 this.accessedMemberType = accessedMemberType; 148 this.accessedMember = accessedMember; 149 } 150 } 151 152 private static boolean methodReferenceEquals(@Nonnull MethodReference ref1, @Nonnull MethodReference ref2) { 153 // we already know the containing class matches 154 return ref1.getName().equals(ref2.getName()) && 155 ref1.getReturnType().equals(ref2.getReturnType()) && 156 ref1.getParameterTypes().equals(ref2.getParameterTypes()); 157 } 158 } 159