ClassDefinition.java revision 6ef13753e78bb7abc7e7683d5e533c3395d4a9b6
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2009 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.baksmali.Adaptors;
30
31import org.jf.dexlib.ClassDefItem;
32import org.jf.dexlib.TypeIdItem;
33import org.jf.dexlib.ClassDataItem;
34import org.jf.dexlib.EncodedArrayItem;
35import org.jf.dexlib.EncodedValue.EncodedValue;
36import org.jf.dexlib.util.AccessFlags;
37
38import java.util.List;
39import java.util.ArrayList;
40
41public class ClassDefinition {
42    private ClassDefItem classDefItem;
43    private ClassDataItem classDataItem;
44
45    public ClassDefinition(ClassDefItem classDefItem) {
46        this.classDefItem = classDefItem;
47        this.classDataItem = classDefItem.getClassData();
48    }
49
50    private List<String> accessFlags = null;
51    public List<String> getAccessFlags() {
52        if (accessFlags == null) {
53            accessFlags = new ArrayList<String>();
54
55            for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDefItem.getAccessFlags())) {
56                accessFlags.add(accessFlag.toString());
57            }
58        }
59        return accessFlags;
60    }
61
62    private String classType = null;
63    public String getClassType() {
64        if (classType == null) {
65            classType = classDefItem.getClassType().getTypeDescriptor();
66        }
67        return classType;
68    }
69
70    private String superType = null;
71    public String getSuperType() {
72        if (superType == null) {
73            superType = classDefItem.getSuperclass().getTypeDescriptor();
74        }
75        return superType;
76    }
77
78    private List<String> interfaces = null;
79    public List<String> getInterfaces() {
80        if (interfaces == null) {
81            interfaces = new ArrayList<String>();
82
83            List<TypeIdItem> interfaceList = classDefItem.getInterfaces();
84
85            if (interfaceList != null) {
86                for (TypeIdItem typeIdItem: interfaceList) {
87                    interfaces.add(typeIdItem.getTypeDescriptor());
88                }
89            }
90        }
91        return interfaces;
92    }
93
94    private List<FieldDefinition> staticFields = null;
95    public List<FieldDefinition> getStaticFields() {
96        if (staticFields == null) {
97            staticFields = new ArrayList<FieldDefinition>();
98
99            if (classDataItem != null) {
100
101                EncodedArrayItem encodedStaticInitializers = classDefItem.getStaticInitializers();
102
103                List<EncodedValue> staticInitializers;
104                if (encodedStaticInitializers != null) {
105                    staticInitializers = encodedStaticInitializers.getEncodedArray().getValues();
106                } else {
107                    staticInitializers = new ArrayList<EncodedValue>();
108                }
109
110                int i=0;
111                for (ClassDataItem.EncodedField field: classDataItem.getStaticFields()) {
112                    EncodedValue encodedValue = null;
113                    if (i < staticInitializers.size()) {
114                        encodedValue = staticInitializers.get(i);
115                    }
116                    staticFields.add(new FieldDefinition(field, encodedValue));
117                    i++;
118                }
119            }
120        }
121        return staticFields;
122    }
123
124    private List<FieldDefinition> instanceFields = null;
125    public List<FieldDefinition> getInstanceFields() {
126        if (instanceFields == null) {
127            instanceFields = new ArrayList<FieldDefinition>();
128
129            if (classDataItem != null) {
130                for (ClassDataItem.EncodedField field: classDataItem.getInstanceFields()) {
131                    instanceFields.add(new FieldDefinition(field));
132                }
133            }
134        }
135        return instanceFields;
136    }
137
138    private List<MethodDefinition> directMethods = null;
139    public List<MethodDefinition> getDirectMethods() {
140        if (directMethods == null) {
141            directMethods = new ArrayList<MethodDefinition>();
142
143            if (classDataItem != null) {
144                for (ClassDataItem.EncodedMethod method: classDataItem.getDirectMethods()) {
145                    directMethods.add(new MethodDefinition(method));
146                }
147            }
148        }
149        return directMethods;
150    }
151
152    private List<MethodDefinition> virtualMethods = null;
153    public List<MethodDefinition> getVirtualMethods() {
154        if (virtualMethods == null) {
155            virtualMethods = new ArrayList<MethodDefinition>();
156
157            if (classDataItem != null) {
158                for (ClassDataItem.EncodedMethod method: classDataItem.getVirtualMethods()) {
159                    virtualMethods.add(new MethodDefinition(method));
160                }
161            }
162        }
163        return virtualMethods;
164    }
165}
166