Home | History | Annotate | Download | only in cts
      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 
     26     protected DexMember(String className, String name, String type) {
     27         mName = name;
     28         mClassDescriptor = className;
     29         mType = type;
     30     }
     31 
     32     public String getName() {
     33         return mName;
     34     }
     35 
     36     public String getDexClassName() {
     37         return mClassDescriptor;
     38     }
     39 
     40     public String getJavaClassName() {
     41         return dexToJavaType(mClassDescriptor);
     42     }
     43 
     44     public String getDexType() {
     45         return mType;
     46     }
     47 
     48     public String getJavaType() {
     49         return dexToJavaType(mType);
     50     }
     51 
     52     /**
     53      * Converts `type` to a Java type.
     54      */
     55     protected static String dexToJavaType(String type) {
     56         String javaDimension = "";
     57         while (type.startsWith("[")) {
     58             javaDimension += "[]";
     59             type = type.substring(1);
     60         }
     61 
     62         String javaType = null;
     63         if ("V".equals(type)) {
     64             javaType = "void";
     65         } else if ("Z".equals(type)) {
     66             javaType = "boolean";
     67         } else if ("B".equals(type)) {
     68             javaType = "byte";
     69         } else if ("C".equals(type)) {
     70             javaType = "char";
     71         } else if ("S".equals(type)) {
     72             javaType = "short";
     73         } else if ("I".equals(type)) {
     74             javaType = "int";
     75         } else if ("J".equals(type)) {
     76             javaType = "long";
     77         } else if ("F".equals(type)) {
     78             javaType = "float";
     79         } else if ("D".equals(type)) {
     80             javaType = "double";
     81         } else if (type.startsWith("L") && type.endsWith(";")) {
     82             javaType = type.substring(1, type.length() - 1).replace('/', '.');
     83         } else {
     84             throw new IllegalStateException("Unexpected type " + type);
     85         }
     86 
     87         return javaType + javaDimension;
     88     }
     89 }
     90