1 /* 2 * Copyright (C) 2018 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 package android.signature.cts; 17 18 /** 19 * Represents one class member parsed from the reader of dex signatures. 20 */ 21 public abstract class DexMember { 22 private final String mName; 23 private final String mClassDescriptor; 24 private final String mType; 25 private final String[] mFlags; 26 DexMember(String className, String name, String type, String[] flags)27 protected DexMember(String className, String name, String type, String[] flags) { 28 mName = name; 29 mClassDescriptor = className; 30 mType = type; 31 mFlags = flags; 32 } 33 getName()34 public String getName() { 35 return mName; 36 } 37 getDexClassName()38 public String getDexClassName() { 39 return mClassDescriptor; 40 } 41 getJavaClassName()42 public String getJavaClassName() { 43 return dexToJavaType(mClassDescriptor); 44 } 45 getDexType()46 public String getDexType() { 47 return mType; 48 } 49 getJavaType()50 public String getJavaType() { 51 return dexToJavaType(mType); 52 } 53 getHiddenapiFlags()54 public String[] getHiddenapiFlags() { 55 return mFlags; 56 } 57 58 /** 59 * Converts `type` to a Java type. 60 */ dexToJavaType(String type)61 protected static String dexToJavaType(String type) { 62 String javaDimension = ""; 63 while (type.startsWith("[")) { 64 javaDimension += "[]"; 65 type = type.substring(1); 66 } 67 68 String javaType = null; 69 if ("V".equals(type)) { 70 javaType = "void"; 71 } else if ("Z".equals(type)) { 72 javaType = "boolean"; 73 } else if ("B".equals(type)) { 74 javaType = "byte"; 75 } else if ("C".equals(type)) { 76 javaType = "char"; 77 } else if ("S".equals(type)) { 78 javaType = "short"; 79 } else if ("I".equals(type)) { 80 javaType = "int"; 81 } else if ("J".equals(type)) { 82 javaType = "long"; 83 } else if ("F".equals(type)) { 84 javaType = "float"; 85 } else if ("D".equals(type)) { 86 javaType = "double"; 87 } else if (type.startsWith("L") && type.endsWith(";")) { 88 javaType = type.substring(1, type.length() - 1).replace('/', '.'); 89 } else { 90 throw new IllegalStateException("Unexpected type " + type); 91 } 92 93 return javaType + javaDimension; 94 } 95 } 96