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
18 
19 import com.android.tools.metalava.model.visitors.ItemVisitor
20 import com.android.tools.metalava.model.visitors.TypeVisitor
21 
22 interface ParameterItem : Item {
23     /** The name of this field */
namenull24     fun name(): String
25 
26     /** The type of this field */
27     fun type(): TypeItem
28 
29     /** The containing method */
30     fun containingMethod(): MethodItem
31 
32     /** Index of this parameter in the parameter list (0-based) */
33     val parameterIndex: Int
34 
35     /**
36      * The public name of this parameter. In Kotlin, names are part of the
37      * public API; in Java they are not. In Java, you can annotate a
38      * parameter with {@literal @ParameterName("foo")} to name the parameter
39      * something (potentially different from the actual code parameter name).
40      */
41     fun publicName(): String?
42 
43     /**
44      * Returns whether this parameter has a default value. In Kotlin, this is supported
45      * directly; in Java, it's supported via a special annotation,
46      * {@literal @DefaultValue("source").
47      */
48     fun hasDefaultValue(): Boolean
49 
50     /**
51      * Returns the default value.
52      *
53      * **This method should only be called if [hasDefaultValue] returned true!** (This
54      * is necessary since the null return value is a valid default value separate from
55      * no default value specified.)
56      *
57      * The default value is the source string
58      * literal representation of the value, e.g. strings would be surrounded
59      * by quotes, Booleans are the strings "true" or "false", and so on.
60      */
61     fun defaultValue(): String?
62 
63     /**
64      * Whether this is a varargs parameter
65      */
66     fun isVarArgs(): Boolean
67 
68     override fun parent(): MethodItem? = containingMethod()
69 
70     override fun accept(visitor: ItemVisitor) {
71         if (visitor.skip(this)) {
72             return
73         }
74 
75         visitor.visitItem(this)
76         visitor.visitParameter(this)
77 
78         visitor.afterVisitParameter(this)
79         visitor.afterVisitItem(this)
80     }
81 
acceptTypesnull82     override fun acceptTypes(visitor: TypeVisitor) {
83         if (visitor.skip(this)) {
84             return
85         }
86 
87         val type = type()
88         visitor.visitType(type, this)
89         visitor.afterVisitType(type, this)
90     }
91 
requiresNullnessInfonull92     override fun requiresNullnessInfo(): Boolean {
93         return !type().primitive
94     }
95 
hasNullnessInfonull96     override fun hasNullnessInfo(): Boolean {
97         if (!requiresNullnessInfo()) {
98             return true
99         }
100 
101         return modifiers.hasNullnessInfo()
102     }
103 
104     // TODO: modifier list
105 }