1 /*
2  * Copyright (C) 2017 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.tools.metalava.model.visitors
18 
19 import com.android.tools.metalava.doclava1.ApiPredicate
20 import com.android.tools.metalava.model.ClassItem
21 import com.android.tools.metalava.model.Codebase
22 import com.android.tools.metalava.model.FieldItem
23 import com.android.tools.metalava.model.Item
24 import com.android.tools.metalava.model.MethodItem
25 import java.util.function.Predicate
26 
27 open class ApiVisitor(
28     /**
29      * Whether constructors should be visited as part of a [#visitMethod] call
30      * instead of just a [#visitConstructor] call. Helps simplify visitors that
31      * don't care to distinguish between the two cases. Defaults to true.
32      */
33     visitConstructorsAsMethods: Boolean = true,
34     /**
35      * Whether inner classes should be visited "inside" a class; when this property
36      * is true, inner classes are visited before the [#afterVisitClass] method is
37      * called; when false, it's done afterwards. Defaults to false.
38      */
39     nestInnerClasses: Boolean = false,
40 
41     /**
42      * Whether to include inherited fields too
43      */
44     val inlineInheritedFields: Boolean = true,
45 
46     /** Comparator to sort methods with, or null to use natural (source) order */
47     val methodComparator: Comparator<MethodItem>? = null,
48 
49     /** Comparator to sort fields with, or null to use natural (source) order */
50     val fieldComparator: Comparator<FieldItem>? = null,
51 
52     /** The filter to use to determine if we should emit an item */
53     val filterEmit: Predicate<Item>,
54     /** The filter to use to determine if we should emit a reference to an item */
55     val filterReference: Predicate<Item>,
56 
57     /**
58      * Whether the visitor should include visiting top-level classes that have
59      * nothing other than non-empty inner classes within.
60      * Typically these are not included in signature files, but when generating
61      * stubs we need to include them.
62      */
63     val includeEmptyOuterClasses: Boolean = false
64 ) : ItemVisitor(visitConstructorsAsMethods, nestInnerClasses) {
65     constructor(
66         codebase: Codebase,
67         /**
68          * Whether constructors should be visited as part of a [#visitMethod] call
69          * instead of just a [#visitConstructor] call. Helps simplify visitors that
70          * don't care to distinguish between the two cases. Defaults to true.
71          */
72         visitConstructorsAsMethods: Boolean = true,
73         /**
74          * Whether inner classes should be visited "inside" a class; when this property
75          * is true, inner classes are visited before the [#afterVisitClass] method is
76          * called; when false, it's done afterwards. Defaults to false.
77          */
78         nestInnerClasses: Boolean = false,
79 
80         /** Whether to ignore APIs with annotations in the --show-annotations list */
81 //        ignoreShown: Boolean = options.showUnannotated,
82         ignoreShown: Boolean = true,
83 
84         /** Whether to match APIs marked for removal instead of the normal API */
85         remove: Boolean = false,
86 
87         /** Comparator to sort methods with, or null to use natural (source) order */
88         methodComparator: Comparator<MethodItem>? = null,
89 
90         /** Comparator to sort fields with, or null to use natural (source) order */
91         fieldComparator: Comparator<FieldItem>? = null
92     ) : this(
93         visitConstructorsAsMethods, nestInnerClasses,
94         true, methodComparator,
95         fieldComparator,
96         ApiPredicate(codebase, ignoreShown = ignoreShown, matchRemoved = remove),
97         ApiPredicate(codebase, ignoreShown = true, ignoreRemoved = remove)
98     )
99 
100     // The API visitor lazily visits packages only when there's a match within at least one class;
101     // this property keeps track of whether we've already visited the current package
102     var visitingPackage = false
103 
includenull104     open fun include(cls: ClassItem): Boolean = cls.emit
105 }
106