1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2011 Ben Gruver
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.dexlib.Code.Analysis;
30
31import org.jf.dexlib.*;
32import org.jf.dexlib.Code.Format.Instruction22c;
33import org.jf.dexlib.Code.Instruction;
34import org.jf.dexlib.Code.InstructionWithReference;
35import org.jf.dexlib.Util.AccessFlags;
36
37import java.util.HashMap;
38
39public class SyntheticAccessorResolver {
40    public static final int METHOD = 0;
41    public static final int GETTER = 1;
42    public static final int SETTER = 2;
43
44    private final DexFileClassMap classMap;
45    private final HashMap<MethodIdItem, AccessedMember> resolvedAccessors = new HashMap<MethodIdItem, AccessedMember>();
46
47    public SyntheticAccessorResolver(DexFile dexFile) {
48        classMap = new DexFileClassMap(dexFile);
49    }
50
51    public static boolean looksLikeSyntheticAccessor(MethodIdItem methodIdItem) {
52        return methodIdItem.getMethodName().getStringValue().startsWith("access$");
53    }
54
55    public AccessedMember getAccessedMember(MethodIdItem methodIdItem) {
56        AccessedMember accessedMember = resolvedAccessors.get(methodIdItem);
57        if (accessedMember != null) {
58            return accessedMember;
59        }
60
61        ClassDefItem classDefItem = classMap.getClassDefByType(methodIdItem.getContainingClass());
62        if (classDefItem == null) {
63            return null;
64        }
65
66        ClassDataItem classDataItem = classDefItem.getClassData();
67        if (classDataItem == null) {
68            return null;
69        }
70
71        ClassDataItem.EncodedMethod encodedMethod = classDataItem.findDirectMethodByMethodId(methodIdItem);
72        if (encodedMethod == null) {
73            return null;
74        }
75
76        //A synthetic accessor will be marked synthetic
77        if ((encodedMethod.accessFlags & AccessFlags.SYNTHETIC.getValue()) == 0) {
78            return null;
79        }
80
81        Instruction[] instructions = encodedMethod.codeItem.getInstructions();
82
83        //TODO: add support for odexed formats
84        switch (instructions[0].opcode.format) {
85            case Format35c:
86            case Format3rc: {
87                //a synthetic method access should be either 2 or 3 instructions, depending on if the method returns
88                //anything or not
89                if (instructions.length < 2 || instructions.length > 3) {
90                    return null;
91                }
92                InstructionWithReference instruction = (InstructionWithReference)instructions[0];
93                Item referencedItem = instruction.getReferencedItem();
94                if (!(referencedItem instanceof  MethodIdItem)) {
95                    return null;
96                }
97                MethodIdItem referencedMethodIdItem = (MethodIdItem)referencedItem;
98
99                accessedMember = new AccessedMember(METHOD, referencedMethodIdItem);
100                resolvedAccessors.put(methodIdItem, accessedMember);
101                return accessedMember;
102            }
103            case Format22c: {
104                //a synthetic field access should be exactly 2 instructions. The set/put, and then the return
105                if (instructions.length != 2) {
106                    return null;
107                }
108                Instruction22c instruction = (Instruction22c)instructions[0];
109                Item referencedItem = instruction.getReferencedItem();
110                if (!(referencedItem instanceof FieldIdItem)) {
111                    return null;
112                }
113                FieldIdItem referencedFieldIdItem = (FieldIdItem)referencedItem;
114
115                if (instruction.opcode.setsRegister() || instruction.opcode.setsWideRegister()) {
116                    //If the instruction sets a register, that means it is a getter - it gets the field value and
117                    //stores it in the register
118                    accessedMember = new AccessedMember(GETTER, referencedFieldIdItem);
119                } else {
120                    accessedMember = new AccessedMember(SETTER, referencedFieldIdItem);
121                }
122
123                resolvedAccessors.put(methodIdItem, accessedMember);
124                return accessedMember;
125            }
126            default:
127                return null;
128        }
129    }
130
131    public static class AccessedMember {
132        public final int accessedMemberType;
133        public final Item accessedMember;
134
135        public AccessedMember(int accessedMemberType, Item accessedMember) {
136            this.accessedMemberType = accessedMemberType;
137            this.accessedMember = accessedMember;
138        }
139    }
140}
141