• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AnnotationTarget
22 import com.android.tools.metalava.model.Codebase
23 import com.android.tools.metalava.model.DefaultAnnotationAttribute
24 import com.android.tools.metalava.model.DefaultAnnotationItem
25 
26 class TextBackedAnnotationItem(
27     codebase: Codebase,
28     source: String,
29     mapName: Boolean = true
30 ) : DefaultAnnotationItem(codebase) {
31     private val originalName: String
32     private val qualifiedName: String?
33     private val full: String
34     private val attributes: List<AnnotationAttribute>
35 
36     init {
37         val index = source.indexOf("(")
38         val annotationClass = if (index == -1)
39             source.substring(1) // Strip @
40         else source.substring(1, index)
41 
42         originalName = annotationClass
43         qualifiedName = if (mapName) AnnotationItem.mapName(codebase, annotationClass) else annotationClass
44         full = when {
45             qualifiedName == null -> ""
46             index == -1 -> "@$qualifiedName"
47             else -> "@" + qualifiedName + source.substring(index)
48         }
49 
50         attributes = if (index == -1) {
51             emptyList()
52         } else {
53             DefaultAnnotationAttribute.createList(
54                 source.substring(index + 1, source.lastIndexOf(')'))
55             )
56         }
57     }
58 
originalNamenull59     override fun originalName(): String? = originalName
60     override fun qualifiedName(): String? = qualifiedName
61     override fun attributes(): List<AnnotationAttribute> = attributes
62     override fun toSource(target: AnnotationTarget): String = full
63 }
64