Home | History | Annotate | Download | only in heapdump
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      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 
     17 package com.android.ahat.heapdump;
     18 
     19 import com.android.tools.perflib.heap.ClassObj;
     20 import com.android.tools.perflib.heap.Field;
     21 import com.android.tools.perflib.heap.Instance;
     22 import java.util.Arrays;
     23 import java.util.Collection;
     24 import java.util.List;
     25 import java.util.Map;
     26 
     27 public class AhatClassObj extends AhatInstance {
     28   private String mClassName;
     29   private AhatClassObj mSuperClassObj;
     30   private AhatInstance mClassLoader;
     31   private FieldValue[] mStaticFieldValues;
     32 
     33   public AhatClassObj(long id) {
     34     super(id);
     35   }
     36 
     37   @Override void initialize(AhatSnapshot snapshot, Instance inst) {
     38     super.initialize(snapshot, inst);
     39 
     40     ClassObj classObj = (ClassObj)inst;
     41     mClassName = classObj.getClassName();
     42 
     43     ClassObj superClassObj = classObj.getSuperClassObj();
     44     if (superClassObj != null) {
     45       mSuperClassObj = snapshot.findClassObj(superClassObj.getId());
     46     }
     47 
     48     Instance loader = classObj.getClassLoader();
     49     if (loader != null) {
     50       mClassLoader = snapshot.findInstance(loader.getId());
     51     }
     52 
     53     Collection<Map.Entry<Field, Object>> fieldValues = classObj.getStaticFieldValues().entrySet();
     54     mStaticFieldValues = new FieldValue[fieldValues.size()];
     55     int index = 0;
     56     for (Map.Entry<Field, Object> field : fieldValues) {
     57       String name = field.getKey().getName();
     58       String type = field.getKey().getType().toString();
     59       Value value = snapshot.getValue(field.getValue());
     60       mStaticFieldValues[index++] = new FieldValue(name, type, value);
     61 
     62       if (field.getValue() instanceof Instance) {
     63         Instance ref = (Instance)field.getValue();
     64         if (ref.getNextInstanceToGcRoot() == inst) {
     65           value.asAhatInstance().setNextInstanceToGcRoot(this, "." + name);
     66         }
     67       }
     68     }
     69   }
     70 
     71   /**
     72    * Returns the name of the class this is a class object for.
     73    */
     74   public String getName() {
     75     return mClassName;
     76   }
     77 
     78   /**
     79    * Returns the superclass of this class object.
     80    */
     81   public AhatClassObj getSuperClassObj() {
     82     return mSuperClassObj;
     83   }
     84 
     85   /**
     86    * Returns the class loader of this class object.
     87    */
     88   public AhatInstance getClassLoader() {
     89     return mClassLoader;
     90   }
     91 
     92   /**
     93    * Returns the static field values for this class object.
     94    */
     95   public List<FieldValue> getStaticFieldValues() {
     96     return Arrays.asList(mStaticFieldValues);
     97   }
     98 
     99   @Override public boolean isClassObj() {
    100     return true;
    101   }
    102 
    103   @Override public AhatClassObj asClassObj() {
    104     return this;
    105   }
    106 
    107   @Override public String toString() {
    108     return mClassName;
    109   }
    110 
    111   @Override AhatInstance newPlaceHolderInstance() {
    112     return new AhatPlaceHolderClassObj(this);
    113   }
    114 }
    115 
    116