summaryrefslogtreecommitdiffstats
path: root/src/com/android/cts/apicoverage/ApiClass.java
blob: 73cea67d6c7bd7a1543a0675797948da2dd2b554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.cts.apicoverage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/** Representation of a class in the API with constructors and methods. */
class ApiClass implements Comparable<ApiClass>, HasCoverage {

    private static final String VOID = "void";

    private final String mName;

    private final boolean mDeprecated;

    private final boolean mAbstract;

    private final List<ApiConstructor> mApiConstructors = new ArrayList<ApiConstructor>();

    private final List<ApiMethod> mApiMethods = new ArrayList<ApiMethod>();

    private final String mSuperClassName;

    private ApiClass mSuperClass;

    /**
     * @param name The name of the class
     * @param deprecated true iff the class is marked as deprecated
     * @param classAbstract true iff the class is abstract
     * @param superClassName The fully qualified name of the super class
     */
    ApiClass(
            String name,
            boolean deprecated,
            boolean classAbstract,
            String superClassName) {
        mName = name;
        mDeprecated = deprecated;
        mAbstract = classAbstract;
        mSuperClassName = superClassName;
    }

    @Override
    public int compareTo(ApiClass another) {
        return mName.compareTo(another.mName);
    }

    @Override
    public String getName() {
        return mName;
    }

    public boolean isDeprecated() {
        return mDeprecated;
    }

    public String getSuperClassName() {
        return mSuperClassName;
    }

    public boolean isAbstract() {
        return mAbstract;
    }

    public void setSuperClass(ApiClass superClass) { mSuperClass = superClass; }

    public void addConstructor(ApiConstructor constructor) {
        mApiConstructors.add(constructor);
    }


    public Collection<ApiConstructor> getConstructors() {
        return Collections.unmodifiableList(mApiConstructors);
    }

    public void addMethod(ApiMethod method) {
        mApiMethods.add(method);
    }

    /** Look for a matching constructor and mark it as covered */
    public void markConstructorCovered(List<String> parameterTypes) {
        if (mSuperClass != null) {
            // Mark matching constructors in the superclass
            mSuperClass.markConstructorCovered(parameterTypes);
        }
        ApiConstructor apiConstructor = getConstructor(parameterTypes);
        if (apiConstructor != null) {
            apiConstructor.setCovered(true);
        }

    }

    /** Look for a matching method and if found and mark it as covered */
    public void markMethodCovered(String name, List<String> parameterTypes, String returnType) {
        if (mSuperClass != null) {
            // Mark matching methods in the super class
            mSuperClass.markMethodCovered(name, parameterTypes, returnType);
        }
        ApiMethod apiMethod = getMethod(name, parameterTypes, returnType);
        if (apiMethod != null) {
            apiMethod.setCovered(true);
        }
    }

    public Collection<ApiMethod> getMethods() {
        return Collections.unmodifiableList(mApiMethods);
    }

    public int getNumCoveredMethods() {
        int numCovered = 0;
        for (ApiConstructor constructor : mApiConstructors) {
            if (constructor.isCovered()) {
                numCovered++;
            }
        }
        for (ApiMethod method : mApiMethods) {
            if (method.isCovered()) {
                numCovered++;
            }
        }
        return numCovered;
    }

    public int getTotalMethods() {
        return mApiConstructors.size() + mApiMethods.size();
    }

    @Override
    public float getCoveragePercentage() {
        if (getTotalMethods() == 0) {
            return 100;
        } else {
            return (float) getNumCoveredMethods() / getTotalMethods() * 100;
        }
    }

    @Override
    public int getMemberSize() {
        return getTotalMethods();
    }

    private ApiMethod getMethod(String name, List<String> parameterTypes, String returnType) {
        for (ApiMethod method : mApiMethods) {
            boolean methodNameMatch = name.equals(method.getName());
            boolean parameterTypeMatch =
                    compareParameterTypes(method.getParameterTypes(), parameterTypes);
            boolean returnTypeMatch = compareType(method.getReturnType(), returnType);
            if (methodNameMatch && parameterTypeMatch && returnTypeMatch) {
                return method;
            }
        }
        return null;
    }

    /**
     * The method compares two lists of parameters. If the {@code apiParameterTypeList} contains
     * generic types, test parameter types are ignored.
     *
     * @param apiParameterTypeList The list of parameter types from the API
     * @param testParameterTypeList The list of parameter types used in a test
     * @return true iff the list of types are the same.
     */
    private static boolean compareParameterTypes(
            List<String> apiParameterTypeList, List<String> testParameterTypeList) {
        if (apiParameterTypeList.equals(testParameterTypeList)) {
            return true;
        }
        if (apiParameterTypeList.size() != testParameterTypeList.size()) {
            return false;
        }

        for (int i = 0; i < apiParameterTypeList.size(); i++) {
            String apiParameterType = apiParameterTypeList.get(i);
            String testParameterType = testParameterTypeList.get(i);
            if (!compareType(apiParameterType, testParameterType)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Compare class types.
     * @param apiType The type as reported by the api
     * @param testType The type as found used in a test
     * @return true iff the strings are equal,
     * or the apiType is generic and the test type is not void
     */
    private static boolean compareType(String apiType, String testType) {
        return apiType.equals(testType) ||
                isGenericType(apiType) && !testType.equals(VOID) ||
                isGenericArrayType(apiType) && isArrayType(testType) ;
    }

    /**
     * @return true iff the given parameterType is a generic type.
     */
    private static boolean isGenericType(String type) {
        return type.length() == 1 &&
                type.charAt(0) >= 'A' &&
                type.charAt(0) <= 'Z';
    }

    /**
     * @return true iff {@code type} ends with an [].
     */
    private static boolean isArrayType(String type) {
        return type.endsWith("[]");
    }

    /**
     * @return true iff the given parameterType is an array of generic type.
     */
    private static boolean isGenericArrayType(String type) {
        return type.length() == 3 && isGenericType(type.substring(0, 1)) && isArrayType(type);
    }

    private ApiConstructor getConstructor(List<String> parameterTypes) {
        for (ApiConstructor constructor : mApiConstructors) {
            if (compareParameterTypes(constructor.getParameterTypes(), parameterTypes)) {
                return constructor;
            }
        }
        return null;
    }
}