1 /* 2 * Copyright (C) 2018 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.compatibility 20 import com.android.tools.metalava.model.psi.PsiModifierItem 21 22 open class DefaultModifierList( 23 override val codebase: Codebase, 24 protected var flags: Int = 0, 25 protected open var annotations: MutableList<AnnotationItem>? = null 26 ) : MutableModifierList { 27 private lateinit var owner: Item 28 setnull29 private operator fun set(mask: Int, set: Boolean) { 30 flags = if (set) { 31 flags or mask 32 } else { 33 flags and mask.inv() 34 } 35 } 36 isSetnull37 private fun isSet(mask: Int): Boolean { 38 return flags and mask != 0 39 } 40 annotationsnull41 override fun annotations(): List<AnnotationItem> { 42 return annotations ?: emptyList() 43 } 44 ownernull45 override fun owner(): Item { 46 return owner 47 } 48 setOwnernull49 fun setOwner(owner: Item) { 50 this.owner = owner 51 } 52 isPublicnull53 override fun isPublic(): Boolean { 54 return isSet(PUBLIC) 55 } 56 isProtectednull57 override fun isProtected(): Boolean { 58 return isSet(PROTECTED) 59 } 60 isPrivatenull61 override fun isPrivate(): Boolean { 62 return isSet(PRIVATE) 63 } 64 isStaticnull65 override fun isStatic(): Boolean { 66 return isSet(STATIC) 67 } 68 isAbstractnull69 override fun isAbstract(): Boolean { 70 return isSet(ABSTRACT) 71 } 72 isFinalnull73 override fun isFinal(): Boolean { 74 return isSet(FINAL) 75 } 76 isNativenull77 override fun isNative(): Boolean { 78 return isSet(NATIVE) 79 } 80 isSynchronizednull81 override fun isSynchronized(): Boolean { 82 return isSet(SYNCHRONIZED) 83 } 84 isStrictFpnull85 override fun isStrictFp(): Boolean { 86 return isSet(STRICT_FP) 87 } 88 isTransientnull89 override fun isTransient(): Boolean { 90 return isSet(TRANSIENT) 91 } 92 isVolatilenull93 override fun isVolatile(): Boolean { 94 return isSet(VOLATILE) 95 } 96 isDefaultnull97 override fun isDefault(): Boolean { 98 return isSet(DEFAULT) 99 } 100 isDeprecatednull101 fun isDeprecated(): Boolean { 102 return isSet(DEPRECATED) 103 } 104 isVarArgnull105 override fun isVarArg(): Boolean { 106 return isSet(VARARG) 107 } 108 isSealednull109 override fun isSealed(): Boolean { 110 return isSet(SEALED) 111 } 112 isInternalnull113 override fun isInternal(): Boolean { 114 return isSet(INTERNAL) 115 } 116 isInfixnull117 override fun isInfix(): Boolean { 118 return isSet(INFIX) 119 } 120 isSuspendnull121 override fun isSuspend(): Boolean { 122 return isSet(SUSPEND) 123 } 124 isOperatornull125 override fun isOperator(): Boolean { 126 return isSet(OPERATOR) 127 } 128 isInlinenull129 override fun isInline(): Boolean { 130 return isSet(INLINE) 131 } 132 setPublicnull133 override fun setPublic(public: Boolean) { 134 set(PUBLIC, public) 135 } 136 setProtectednull137 override fun setProtected(protected: Boolean) { 138 set(PROTECTED, protected) 139 } 140 setPrivatenull141 override fun setPrivate(private: Boolean) { 142 set(PRIVATE, private) 143 } 144 setStaticnull145 override fun setStatic(static: Boolean) { 146 set(STATIC, static) 147 } 148 setAbstractnull149 override fun setAbstract(abstract: Boolean) { 150 set(ABSTRACT, abstract) 151 } 152 setFinalnull153 override fun setFinal(final: Boolean) { 154 set(FINAL, final) 155 } 156 setNativenull157 override fun setNative(native: Boolean) { 158 set(NATIVE, native) 159 } 160 setSynchronizednull161 override fun setSynchronized(synchronized: Boolean) { 162 set(SYNCHRONIZED, synchronized) 163 } 164 setStrictFpnull165 override fun setStrictFp(strictfp: Boolean) { 166 set(STRICT_FP, strictfp) 167 } 168 setTransientnull169 override fun setTransient(transient: Boolean) { 170 set(TRANSIENT, transient) 171 } 172 setVolatilenull173 override fun setVolatile(volatile: Boolean) { 174 set(VOLATILE, volatile) 175 } 176 setDefaultnull177 override fun setDefault(default: Boolean) { 178 set(DEFAULT, default) 179 } 180 setInternalnull181 override fun setInternal(internal: Boolean) { 182 set(INTERNAL, internal) 183 } 184 setSealednull185 override fun setSealed(sealed: Boolean) { 186 set(SEALED, sealed) 187 } 188 setInfixnull189 override fun setInfix(infix: Boolean) { 190 set(INFIX, infix) 191 } 192 setOperatornull193 override fun setOperator(operator: Boolean) { 194 set(OPERATOR, operator) 195 } 196 setInlinenull197 override fun setInline(inline: Boolean) { 198 set(INLINE, inline) 199 } 200 201 override fun setVarArg(vararg: Boolean) { 202 set(VARARG, vararg) 203 } 204 setDeprecatednull205 fun setDeprecated(deprecated: Boolean) { 206 set(DEPRECATED, deprecated) 207 } 208 setSuspendnull209 fun setSuspend(suspend: Boolean) { 210 set(SUSPEND, suspend) 211 } 212 addAnnotationnull213 override fun addAnnotation(annotation: AnnotationItem) { 214 if (annotations == null) { 215 annotations = mutableListOf() 216 } 217 annotations?.add(annotation) 218 } 219 removeAnnotationnull220 override fun removeAnnotation(annotation: AnnotationItem) { 221 annotations?.remove(annotation) 222 } 223 clearAnnotationsnull224 override fun clearAnnotations(annotation: AnnotationItem) { 225 annotations?.clear() 226 } 227 isEmptynull228 override fun isEmpty(): Boolean { 229 return flags and DEPRECATED.inv() == 0 // deprecated isn't a real modifier 230 } 231 isPackagePrivatenull232 override fun isPackagePrivate(): Boolean { 233 return flags and (PUBLIC or PROTECTED or PRIVATE or INTERNAL) == 0 234 } 235 getAccessFlagsnull236 fun getAccessFlags(): Int { 237 return flags and (PUBLIC or PROTECTED or PRIVATE or INTERNAL) 238 } 239 240 /** Sets the given modifier */ setnull241 fun set(modifier: String) { 242 set(bit(modifier), true) 243 } 244 245 // Rename? It's not a full equality, it's whether an override's modifier set is significant equivalentTonull246 override fun equivalentTo(other: ModifierList): Boolean { 247 if (other is PsiModifierItem) { 248 val flags2 = other.flags 249 val mask = if (compatibility.includeSynchronized) COMPAT_EQUIVALENCE_MASK else EQUIVALENCE_MASK 250 251 val masked1 = flags and mask 252 val masked2 = flags2 and mask 253 val same = masked1 xor masked2 254 if (same == 0) { 255 return true 256 } else if (compatibility.hideDifferenceImplicit) { 257 if (same == FINAL && 258 // Only differ in final: not significant if implied by containing class 259 isFinal() && (owner as? MethodItem)?.containingClass()?.modifiers?.isFinal() == true) { 260 return true 261 } else if (same == DEPRECATED && 262 // Only differ in deprecated: not significant if implied by containing class 263 isDeprecated() && (owner as? MethodItem)?.containingClass()?.deprecated == true) { 264 return true 265 } 266 } 267 } 268 return false 269 } 270 271 companion object { 272 const val PUBLIC = 1 shl 0 273 const val PROTECTED = 1 shl 1 274 const val PRIVATE = 1 shl 2 275 const val STATIC = 1 shl 3 276 const val ABSTRACT = 1 shl 4 277 const val FINAL = 1 shl 5 278 const val NATIVE = 1 shl 6 279 const val SYNCHRONIZED = 1 shl 7 280 const val STRICT_FP = 1 shl 8 281 const val TRANSIENT = 1 shl 9 282 const val VOLATILE = 1 shl 10 283 const val DEFAULT = 1 shl 11 284 const val DEPRECATED = 1 shl 12 285 const val VARARG = 1 shl 13 286 const val SEALED = 1 shl 14 287 const val INTERNAL = 1 shl 15 288 const val INFIX = 1 shl 16 289 const val OPERATOR = 1 shl 17 290 const val INLINE = 1 shl 18 291 const val SUSPEND = 1 shl 19 292 bitnull293 private fun bit(modifier: String): Int { 294 return when (modifier) { 295 "public" -> PUBLIC 296 "protected" -> PROTECTED 297 "private" -> PRIVATE 298 "static" -> STATIC 299 "abstract" -> ABSTRACT 300 "final" -> FINAL 301 "native" -> NATIVE 302 "synchronized" -> SYNCHRONIZED 303 "strictfp" -> STRICT_FP 304 "transient" -> TRANSIENT 305 "volatile" -> VOLATILE 306 "default" -> DEFAULT 307 "deprecated" -> DEPRECATED 308 "vararg" -> VARARG 309 "sealed" -> SEALED 310 "internal" -> INTERNAL 311 "infix" -> INFIX 312 "operator" -> OPERATOR 313 "inline" -> INLINE 314 "suspend" -> SUSPEND 315 else -> error("Unsupported modifier $modifier") 316 } 317 } 318 319 /** 320 * Modifiers considered significant to include signature files (and similarly 321 * to consider whether an override of a method is different from its super implementation 322 */ 323 private const val EQUIVALENCE_MASK = PUBLIC or PROTECTED or PRIVATE or STATIC or ABSTRACT or 324 FINAL or TRANSIENT or VOLATILE or DEPRECATED or VARARG or 325 SEALED or INTERNAL or INFIX or OPERATOR or SUSPEND 326 327 private const val COMPAT_EQUIVALENCE_MASK = EQUIVALENCE_MASK or SYNCHRONIZED 328 } 329 }