• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.cts.ctsprofiles;
18 
19 import org.apache.commons.math3.util.Pair;
20 
21 import java.util.List;
22 
23 /** Utility for generating package, class or method information. */
24 public final class Utils {
25 
26     /** Splits a class signature and returns the (package, class) pair. */
getPackageClass(String classSignature)27     public static Pair<String, String> getPackageClass(String classSignature) {
28         int splitPos = classSignature.lastIndexOf('.');
29         String className = classSignature.substring(splitPos + 1);
30         String packageName = "";
31         if (splitPos > 0) {
32             packageName = classSignature.substring(0, splitPos);
33         }
34         return Pair.create(packageName, className.replaceAll("\\$", "."));
35     }
36 
37     /** Splits a class name represented in ASM and returns the (package, class) pair. */
getPackageClassFromASM(String packageClass)38     public static Pair<String, String> getPackageClassFromASM(String packageClass) {
39         // The class name obtained from ASM includes package information and is separated by "/".
40         // For example, the class com.my.package.MyClass.NestClass is represented as
41         // com/my/package/MyClass$NestClass in ASM.
42         int splitPos = packageClass.lastIndexOf("/");
43         String packageName = "";
44         if (splitPos != -1) {
45             packageName = packageClass.substring(0, splitPos);
46         }
47         String className = packageClass.substring(splitPos + 1);
48         return Pair.create(
49                 packageName.replaceAll("/", "."),
50                 className.replaceAll("\\$", ".")
51         );
52     }
53 
54     /** Returns a class signature in the format {package}.{class}. */
getClassSignature(String packageName, String className)55     public static String getClassSignature(String packageName, String className) {
56         if (packageName.isEmpty()) {
57             return className;
58         }
59         return String.format("%s.%s", packageName, className);
60     }
61 
62     /** Returns a method signature in the format {method}({param1}, {param2}, ...). */
getMethodSignature(String methodName, List<String> paramTypes)63     public static String getMethodSignature(String methodName, List<String> paramTypes) {
64         return String.format("%s(%s)", methodName, String.join(", ", paramTypes));
65     }
66 
67     /**
68      * Returns a full method signature in the format
69      * {package}.{class}#{method}({param1}, {param2}, ...).
70      */
getMethodSignatureWithClass( String packageName, String className, String methodName, List<String> paramTypes)71     public static String getMethodSignatureWithClass(
72             String packageName, String className, String methodName, List<String> paramTypes) {
73         String classSignature = getClassSignature(packageName, className);
74         String methodSignature = getMethodSignature(methodName, paramTypes);
75         return String.format("%s#%s", classSignature, methodSignature);
76     }
77 }
78