summaryrefslogtreecommitdiff
path: root/hit/src/com/android/hit/ClassObj.java
blob: 1e3ac280d19cb400d90eb141bda3a0381fb96211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.hit;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class ClassObj extends Instance implements Comparable<ClassObj> {
    String mClassName;
    long mSuperclassId;

    String[] mFieldNames;
    int[] mFieldTypes;

    String[] mStaticFieldNames;
    int[] mStaticFieldTypes;
    byte[] mStaticFieldValues;

    ArrayList<Instance> mInstances = new ArrayList<Instance>();
    Set<ClassObj> mSubclasses = new HashSet<ClassObj>();

    int mSize;

    public ClassObj(long id, StackTrace stack, String className) {
        mId = id;
        mStack = stack;
        mClassName = className;
    }

    @Override
    public final void resolveReferences(State state) {
        ByteArrayInputStream bais =
            new ByteArrayInputStream(mStaticFieldValues);
        DataInputStream dis = new DataInputStream(bais);
        int[] types = mStaticFieldTypes;
        final int N = types.length;

        /*
         * Spin through the list of static fields, find all object references,
         * and list ourselves as a reference holder.  Also add them to
         * the list of root objects.
         */
        try {
            for (int i = 0; i < N; i++) {
                int type = types[i];
                int size = Types.getTypeSize(type);

                if (type == Types.OBJECT) {
                    long id;

                    if (size == 4) {
                        id = dis.readInt();
                    } else {
                        id = dis.readLong();
                    }

                    RootObj root = new RootObj(RootType.JAVA_STATIC, id);

                    if (id == 0) {
                        root.mComment = String.format(
                            "Static field %s:%s null",
                                mClassName,
                                mStaticFieldNames[i]);
                    } else {
                        Instance instance = state.findReference(id);

                        instance.addParent(this);

                        root.mComment = String.format(
                            "Static field %s:%s %s [%s] 0x%08x",
                                mClassName,
                                mStaticFieldNames[i],
                                instance.getTypeName(),
                                instance.mHeap.mName,
                                id);
                    }

                    mHeap.addRoot(root);
                } else {
                    dis.skipBytes(size);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        //  Lastly, add ourself as a subclass of our superclass
        if (mSuperclassId != 0) {
            ClassObj superclass = state.findClass(mSuperclassId);

            superclass.addSubclass(this);
        }
    }

    public final void addSubclass(ClassObj subclass) {
        mSubclasses.add(subclass);
    }

    public final void dumpSubclasses() {
        for (ClassObj subclass: mSubclasses) {
            System.out.println("     " + subclass.mClassName);
        }
    }

    public final String toString() {
        return mClassName.replace('/', '.');
    }

    public final void addInstance(Instance instance) {
        mInstances.add(instance);
    }

    public final void setSuperclassId(long id) {
        mSuperclassId = id;
    }

    public final void setFieldNames(String[] names) {
        mFieldNames = names;
    }

    public final void setFieldTypes(int[] types) {
        mFieldTypes = types;
    }

    public final void setStaticFieldNames(String[] names) {
        mStaticFieldNames = names;
    }

    public final void setStaticFieldTypes(int[] types) {
        mStaticFieldTypes = types;
    }

    public final void setStaticFieldValues(byte[] values) {
        mStaticFieldValues = values;
    }

    public final void dump() {
        System.out.println("+----------  ClassObj dump for: " + mClassName);

        System.out.println("+-----  Static fields");

        for (int i = 0; i < mStaticFieldNames.length; i++) {
            System.out.println(mStaticFieldNames[i] + ": "
                + mStaticFieldTypes[i]);
        }

        System.out.println("+-----  Instance fields");

        for (int i = 0; i < mFieldNames.length; i++) {
            System.out.println(mFieldNames[i] + ": " + mFieldTypes[i]);
        }
    }

    @Override
    public final String getTypeName() {
        return "class " + mClassName;
    }

    @Override
    public final void visit(Set<Instance> resultSet, Filter filter) {
        if (resultSet.contains(this)) {
            return;
        }

        if (filter != null) {
            if (filter.accept(this)) {
                resultSet.add(this);
            }
        } else {
            resultSet.add(this);
        }

        ByteArrayInputStream bais =
            new ByteArrayInputStream(mStaticFieldValues);
        DataInputStream dis = new DataInputStream(bais);
        int[] types = mStaticFieldTypes;
        final int N = types.length;
        State state = mHeap.mState;

        /*
         * Spin through the list of static fields, find all object references,
         * and visit them.
         */
        try {
            for (int i = 0; i < N; i++) {
                int type = types[i];
                int size = Types.getTypeSize(type);

                if (type == Types.OBJECT) {
                    long id;

                    if (size == 4) {
                        id = dis.readInt();
                    } else {
                        id = dis.readLong();
                    }

                    Instance instance = state.findReference(id);

                    if (instance != null) {
                        instance.visit(resultSet, filter);
                    }
                } else {
                    dis.skipBytes(size);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final int compareTo(ClassObj o) {
        return mClassName.compareTo(o.mClassName);
    }

    public final boolean equals(Object o) {
        if (! (o instanceof ClassObj)) {
            return false;
        }

        return 0 == compareTo((ClassObj) o);
    }
}