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.EncodedMethod encodedMethod = classDefItem.getClassData().findDirectMethodByMethodId(methodIdItem);
67        if (encodedMethod == null) {
68            return null;
69        }
70
71        //A synthetic accessor will be marked synthetic
72        if ((encodedMethod.accessFlags & AccessFlags.SYNTHETIC.getValue()) == 0) {
73            return null;
74        }
75
76        Instruction[] instructions = encodedMethod.codeItem.getInstructions();
77
78        //TODO: add support for odexed formats
79        switch (instructions[0].opcode.format) {
80            case Format35c:
81            case Format3rc: {
82                //a synthetic method access should be either 2 or 3 instructions, depending on if the method returns
83                //anything or not
84                if (instructions.length < 2 || instructions.length > 3) {
85                    return null;
86                }
87                InstructionWithReference instruction = (InstructionWithReference)instructions[0];
88                MethodIdItem referencedMethodIdItem = (MethodIdItem)instruction.getReferencedItem();
89
90                accessedMember = new AccessedMember(METHOD, referencedMethodIdItem);
91                resolvedAccessors.put(methodIdItem, accessedMember);
92                return accessedMember;
93            }
94            case Format22c: {
95                //a synthetic field access should be exactly 2 instructions. The set/put, and then the return
96                if (instructions.length != 2) {
97                    return null;
98                }
99                Instruction22c instruction = (Instruction22c)instructions[0];
100                FieldIdItem referencedFieldIdItem = (FieldIdItem)instruction.getReferencedItem();
101
102                if (instruction.opcode.setsRegister() || instruction.opcode.setsWideRegister()) {
103                    accessedMember = new AccessedMember(GETTER, referencedFieldIdItem);
104                } else {
105                    accessedMember = new AccessedMember(SETTER, referencedFieldIdItem);
106                }
107
108                resolvedAccessors.put(methodIdItem, accessedMember);
109                return accessedMember;
110            }
111            default:
112                return null;
113        }
114    }
115
116    public static class AccessedMember {
117        private final int accessedMemberType;
118        private final Item accessedMember;
119
120        public AccessedMember(int accessedMemberType, Item accessedMember) {
121            this.accessedMemberType = accessedMemberType;
122            this.accessedMember = accessedMember;
123        }
124
125        public int getAccessedMemberType() {
126            return accessedMemberType;
127        }
128
129        public Item getAccessedMember() {
130            return accessedMember;
131        }
132    }
133}
134