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 java.io.Serializable;
20 
21 /**
22  * Wrapper for information about a {@link Class}.
23  *
24  * <p>This is used instead of {@link Class} so that it can be easily serialized.
25  */
26 public class ClassInfo implements Serializable {
27 
28     private static final long serialVersionUID = 1;
29 
30     private final String mClassName;
31 
ClassInfo(Object obj)32     public ClassInfo(Object obj) {
33         this(obj.getClass());
34     }
35 
ClassInfo(Class<?> clazz)36     public ClassInfo(Class<?> clazz) {
37         this(clazz.getName());
38     }
39 
ClassInfo(String className)40     public ClassInfo(String className) {
41         mClassName = className;
42     }
43 
className()44     public String className() {
45         return mClassName;
46     }
47 
simpleName()48     public String simpleName() {
49         return getSimpleName(mClassName);
50     }
51 
getSimpleName(String name)52     private static String getSimpleName(String name) {
53         // First deal with inner classes
54         int dollar = name.lastIndexOf("$");
55         if (dollar > 0) {
56             return name.substring(dollar + 1); // strip the package name
57         }
58 
59         int dot = name.lastIndexOf(".");
60         if (dot > 0) {
61             return name.substring(dot + 1); // strip the package name
62         }
63         return name;
64     }
65 
66     @Override
toString()67     public String toString() {
68         return "Class{"
69                 + "className=" + className()
70                 + "}";
71     }
72 }
73