1 /* 2 * Copyright (C) 2021 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.queryable.info; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import java.io.Serializable; 23 import java.util.Objects; 24 25 /** 26 * Wrapper for information about a {@link Class}. 27 * 28 * <p>This is used instead of {@link Class} so that it can be easily serialized. 29 */ 30 public class ClassInfo implements Serializable, Parcelable { 31 32 private static final long serialVersionUID = 1; 33 34 private final String mClassName; 35 ClassInfo(Object obj)36 public ClassInfo(Object obj) { 37 this(obj.getClass()); 38 } 39 ClassInfo(Class<?> clazz)40 public ClassInfo(Class<?> clazz) { 41 this(clazz.getName()); 42 } 43 ClassInfo(String className)44 public ClassInfo(String className) { 45 mClassName = className; 46 } 47 ClassInfo(Parcel in)48 ClassInfo(Parcel in) { 49 mClassName = in.readString(); 50 } 51 className()52 public String className() { 53 return mClassName; 54 } 55 simpleName()56 public String simpleName() { 57 return getSimpleName(mClassName); 58 } 59 getSimpleName(String name)60 private static String getSimpleName(String name) { 61 // First deal with inner classes 62 int dollar = name.lastIndexOf("$"); 63 if (dollar > 0) { 64 return name.substring(dollar + 1); // strip the package name 65 } 66 67 int dot = name.lastIndexOf("."); 68 if (dot > 0) { 69 return name.substring(dot + 1); // strip the package name 70 } 71 return name; 72 } 73 74 @Override toString()75 public String toString() { 76 return "Class{" 77 + "className=" + className() 78 + "}"; 79 } 80 81 @Override describeContents()82 public int describeContents() { 83 return 0; 84 } 85 86 @Override writeToParcel(Parcel out, int flags)87 public void writeToParcel(Parcel out, int flags) { 88 out.writeString(mClassName); 89 } 90 91 public static final Parcelable.Creator<ClassInfo> CREATOR = 92 new Parcelable.Creator<ClassInfo>() { 93 public ClassInfo createFromParcel(Parcel in) { 94 return new ClassInfo(in); 95 } 96 97 public ClassInfo[] newArray(int size) { 98 return new ClassInfo[size]; 99 } 100 }; 101 102 @Override equals(Object o)103 public boolean equals(Object o) { 104 if (this == o) return true; 105 if (!(o instanceof ClassInfo)) return false; 106 ClassInfo classInfo = (ClassInfo) o; 107 return mClassName.equals(classInfo.mClassName); 108 } 109 110 @Override hashCode()111 public int hashCode() { 112 return Objects.hash(mClassName); 113 } 114 } 115