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.text
18 
19 import com.android.tools.metalava.model.AnnotationAttribute
20 import com.android.tools.metalava.model.AnnotationItem
21 import com.android.tools.metalava.model.Codebase
22 import com.android.tools.metalava.model.DefaultAnnotationAttribute
23 
24 class TextBackedAnnotationItem(
25     override val codebase: Codebase,
26     source: String,
27     mapName: Boolean = true
28 ) : AnnotationItem {
29     private val qualifiedName: String?
30     private val full: String
31     private val attributes: List<AnnotationAttribute>
32 
33     init {
34         val index = source.indexOf("(")
35         val annotationClass = if (index == -1)
36             source.substring(1) // Strip @
37         else source.substring(1, index)
38 
39         qualifiedName = if (mapName) AnnotationItem.mapName(codebase, annotationClass) else annotationClass
40         full = when {
41             qualifiedName == null -> ""
42             index == -1 -> "@$qualifiedName"
43             else -> "@" + qualifiedName + source.substring(index)
44         }
45 
46         attributes = if (index == -1) {
47             emptyList()
48         } else {
49             DefaultAnnotationAttribute.createList(
50                 source.substring(index + 1, source.lastIndexOf(')'))
51             )
52         }
53     }
54 
qualifiedNamenull55     override fun qualifiedName(): String? = qualifiedName
56     override fun attributes(): List<AnnotationAttribute> = attributes
57     override fun toSource(): String = full
58 }
59