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