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     private final String mClassName;
28 
ClassInfo(Object obj)29     public ClassInfo(Object obj) {
30         this(obj.getClass());
31     }
32 
ClassInfo(Class<?> clazz)33     public ClassInfo(Class<?> clazz) {
34         this(clazz.getName());
35     }
36 
ClassInfo(String className)37     public ClassInfo(String className) {
38         mClassName = className;
39     }
40 
className()41     public String className() {
42         return mClassName;
43     }
44 
simpleName()45     public String simpleName() {
46         return getSimpleName(mClassName);
47     }
48 
getSimpleName(String name)49     private static String getSimpleName(String name) {
50         // First deal with inner classes
51         int dollar = name.lastIndexOf("$");
52         if (dollar > 0) {
53             return name.substring(dollar + 1); // strip the package name
54         }
55 
56         int dot = name.lastIndexOf(".");
57         if (dot > 0) {
58             return name.substring(dot + 1); // strip the package name
59         }
60         return name;
61     }
62 
63     @Override
toString()64     public String toString() {
65         return "Class{"
66                 + "className=" + className()
67                 + "}";
68     }
69 }
70