1/*
2 * Copyright (C) 2008 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.hit;
18
19import java.util.Set;
20
21public class RootObj extends Instance {
22    RootType mType = RootType.UNKNOWN;
23    int mIndex;
24    int mThread;
25
26    /*
27     * These two fields are only used by roots that are static
28     * fields of class objects
29     */
30    long mParent;
31    String mComment;
32
33    public RootObj(RootType type) {
34        this(type, 0, 0, null);
35    }
36
37    public RootObj(RootType type, long id) {
38        this(type, id, 0, null);
39    }
40
41    public RootObj(RootType type, long id, int thread, StackTrace stack) {
42        mType = type;
43        mId = id;
44        mThread = thread;
45        mStack = stack;
46    }
47
48    public final String getClassName(State state) {
49        ClassObj theClass;
50
51        if (mType == RootType.SYSTEM_CLASS) {
52            theClass = state.findClass(mId);
53        } else {
54            Instance instance = state.findReference(mId);
55
56            theClass = state.findClass(instance.mClassId);
57        }
58
59        if (theClass == null) {
60            return "no class defined!!";
61        }
62
63        return theClass.mClassName;
64    }
65
66    @Override
67    public final int getSize() {
68        Instance instance = null;
69
70        if (mType == RootType.SYSTEM_CLASS) {
71            instance = mHeap.mState.findClass(mId);
72        } else {
73            instance = mHeap.mState.findReference(mId);
74        }
75
76        if (instance == null) {
77            return 0;
78        }
79
80        return instance.getSize();
81    }
82
83    @Override
84    public final void visit(Set<Instance> resultSet, Filter filter) {
85        if (resultSet.contains(this)) {
86            return;
87        }
88
89        if (filter != null) {
90            if (filter.accept(this)) {
91                resultSet.add(this);
92            }
93        } else {
94            resultSet.add(this);
95        }
96    }
97
98    @Override
99    public final void resolveReferences(State state) {
100        //  Nothing to do here
101    }
102
103    @Override
104    public final String getTypeName() {
105        return "root " + mType.getName();
106    }
107
108    public final String toString() {
109        return String.format("%s@0x08x", mType.getName(), mId);
110    }
111}
112