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.model.ClassItem
20 import com.android.tools.metalava.model.CompilationUnit
21 import com.android.tools.metalava.model.ConstructorItem
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 com.android.tools.metalava.model.PackageItem
26 import com.android.tools.metalava.model.ParameterItem
27 
28 open class ItemVisitor(
29     /**
30      * Whether constructors should be visited as part of a [#visitMethod] call
31      * instead of just a [#visitConstructor] call. Helps simplify visitors that
32      * don't care to distinguish between the two cases. Defaults to true.
33      */
34     val visitConstructorsAsMethods: Boolean = true,
35     /**
36      * Whether inner classes should be visited "inside" a class; when this property
37      * is true, inner classes are visited before the [#afterVisitClass] method is
38      * called; when false, it's done afterwards. Defaults to false.
39      */
40     val nestInnerClasses: Boolean = false,
41     /**
42      * Whether to skip empty packages
43      */
44     val skipEmptyPackages: Boolean = false
45 ) {
46 
skipnull47     open fun skip(item: Item): Boolean = false
48 
49     /** Visits the item. This is always called before other more specialized visit methods, such as [visitClass]. */
50     open fun visitItem(item: Item) {}
51 
visitCompilationUnitnull52     open fun visitCompilationUnit(unit: CompilationUnit) {}
visitPackagenull53     open fun visitPackage(pkg: PackageItem) {}
visitClassnull54     open fun visitClass(cls: ClassItem) {}
visitConstructornull55     open fun visitConstructor(constructor: ConstructorItem) {
56         if (visitConstructorsAsMethods) {
57             visitMethod(constructor)
58         }
59     }
60 
visitFieldnull61     open fun visitField(field: FieldItem) {}
visitMethodnull62     open fun visitMethod(method: MethodItem) {}
visitParameternull63     open fun visitParameter(parameter: ParameterItem) {}
64 
afterVisitItemnull65     open fun afterVisitItem(item: Item) {}
afterVisitPackagenull66     open fun afterVisitPackage(pkg: PackageItem) {}
afterVisitCompilationUnitnull67     open fun afterVisitCompilationUnit(unit: CompilationUnit) {}
afterVisitClassnull68     open fun afterVisitClass(cls: ClassItem) {}
afterVisitConstructornull69     open fun afterVisitConstructor(constructor: ConstructorItem) {
70         if (visitConstructorsAsMethods) {
71             afterVisitMethod(constructor)
72         }
73     }
74 
afterVisitFieldnull75     open fun afterVisitField(field: FieldItem) {}
afterVisitMethodnull76     open fun afterVisitMethod(method: MethodItem) {}
afterVisitParameternull77     open fun afterVisitParameter(parameter: ParameterItem) {}
78 }
79