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.Codebase
21 import com.android.tools.metalava.model.CompilationUnit
22 import com.android.tools.metalava.model.ConstructorItem
23 import com.android.tools.metalava.model.FieldItem
24 import com.android.tools.metalava.model.Item
25 import com.android.tools.metalava.model.MethodItem
26 import com.android.tools.metalava.model.PackageItem
27 import com.android.tools.metalava.model.ParameterItem
28 import com.android.tools.metalava.model.PropertyItem
29 
30 open class ItemVisitor(
31     /**
32      * Whether constructors should be visited as part of a [#visitMethod] call
33      * instead of just a [#visitConstructor] call. Helps simplify visitors that
34      * don't care to distinguish between the two cases. Defaults to true.
35      */
36     val visitConstructorsAsMethods: Boolean = true,
37     /**
38      * Whether inner classes should be visited "inside" a class; when this property
39      * is true, inner classes are visited before the [#afterVisitClass] method is
40      * called; when false, it's done afterwards. Defaults to false.
41      */
42     val nestInnerClasses: Boolean = false,
43     /**
44      * Whether to skip empty packages
45      */
46     val skipEmptyPackages: Boolean = false
47 ) {
48 
skipnull49     open fun skip(item: Item): Boolean = false
50 
51     /** Visits the item. This is always called before other more specialized visit methods, such as [visitClass]. */
52     open fun visitItem(item: Item) {}
53 
visitCodebasenull54     open fun visitCodebase(codebase: Codebase) {}
visitPackagenull55     open fun visitPackage(pkg: PackageItem) {}
visitCompilationUnitnull56     open fun visitCompilationUnit(unit: CompilationUnit) {}
visitClassnull57     open fun visitClass(cls: ClassItem) {}
visitConstructornull58     open fun visitConstructor(constructor: ConstructorItem) {
59         if (visitConstructorsAsMethods) {
60             visitMethod(constructor)
61         }
62     }
63 
visitFieldnull64     open fun visitField(field: FieldItem) {}
visitMethodnull65     open fun visitMethod(method: MethodItem) {}
visitParameternull66     open fun visitParameter(parameter: ParameterItem) {}
visitPropertynull67     open fun visitProperty(property: PropertyItem) {}
68 
afterVisitItemnull69     open fun afterVisitItem(item: Item) {}
afterVisitCodebasenull70     open fun afterVisitCodebase(codebase: Codebase) {}
afterVisitPackagenull71     open fun afterVisitPackage(pkg: PackageItem) {}
afterVisitCompilationUnitnull72     open fun afterVisitCompilationUnit(unit: CompilationUnit) {}
afterVisitClassnull73     open fun afterVisitClass(cls: ClassItem) {}
afterVisitConstructornull74     open fun afterVisitConstructor(constructor: ConstructorItem) {
75         if (visitConstructorsAsMethods) {
76             afterVisitMethod(constructor)
77         }
78     }
79 
afterVisitFieldnull80     open fun afterVisitField(field: FieldItem) {}
afterVisitMethodnull81     open fun afterVisitMethod(method: MethodItem) {}
afterVisitParameternull82     open fun afterVisitParameter(parameter: ParameterItem) {}
afterVisitPropertynull83     open fun afterVisitProperty(property: PropertyItem) {}
84 }
85