1 /*
2  * Copyright (C) 2010 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.apkcheck;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 
22 /**
23  * Container representing a method with parameters.
24  */
25 public class MethodInfo {
26     private String mName;
27     private String mReturn;
28     private String mNameAndDescriptor;
29     private ArrayList<String> mParameters;
30     private boolean mParametersNormalized;
31 
32     /**
33      * Constructs MethodInfo.  Tuck the method return type away for
34      * later construction of the signature.
35      */
MethodInfo(String name, String returnType)36     public MethodInfo(String name, String returnType) {
37         mName = name;
38         mReturn = returnType;
39         mParameters = new ArrayList<String>();
40     }
41 
42     /**
43      * Returns the method signature.  This is generated when needed.
44      */
getNameAndDescriptor()45     public String getNameAndDescriptor() {
46         if (mNameAndDescriptor == null) {
47             StringBuilder newSig = new StringBuilder(mName);
48             newSig.append(":(");
49             for (int i = 0; i < mParameters.size(); i++) {
50                 String humanType = mParameters.get(i);
51                 String sigType = TypeUtils.typeToDescriptor(humanType);
52                 newSig.append(sigType);
53             }
54             newSig.append(")");
55             newSig.append(TypeUtils.typeToDescriptor(mReturn));
56             mNameAndDescriptor = newSig.toString();
57         }
58         return mNameAndDescriptor;
59     }
60 
61     /**
62      * Adds a parameter to the method.  The "type" is a primitive or
63      * object type, formatted in human-centric form.  For now we just
64      * store it.
65      */
addParameter(String type)66     public void addParameter(String type) {
67         mParameters.add(type);
68         if (mNameAndDescriptor != null) {
69             System.err.println("WARNING: late add of params to method");
70             mNameAndDescriptor = null;      // force regen
71         }
72     }
73 
74     /**
75      * Normalizes the types in parameter lists to unambiguous binary form.
76      *
77      * The public API file must be fully parsed before calling here,
78      * because we need the full set of package names.
79      */
normalizeTypes(ApiList apiList)80     public void normalizeTypes(ApiList apiList) {
81         if (!mParametersNormalized) {
82             mReturn = TypeUtils.ambiguousToBinaryName(mReturn, apiList);
83 
84             for (int i = 0; i < mParameters.size(); i++) {
85                 String fixed = TypeUtils.ambiguousToBinaryName(mParameters.get(i),
86                         apiList);
87                 mParameters.set(i, fixed);
88             }
89 
90             mNameAndDescriptor = null;      // force regen
91             mParametersNormalized = true;
92         }
93     }
94 }
95 
96