1 /* <lambda>null2 * 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.JAVA_LANG_DEPRECATED 20 import com.android.tools.metalava.model.AnnotationAttribute 21 import com.android.tools.metalava.model.AnnotationItem 22 import com.android.tools.metalava.model.AnnotationTarget 23 import com.android.tools.metalava.model.Codebase 24 import com.android.tools.metalava.model.DefaultAnnotationAttribute 25 import com.android.tools.metalava.model.DefaultAnnotationItem 26 import com.android.tools.metalava.model.DefaultModifierList 27 import com.android.tools.metalava.model.ModifierList 28 import java.io.StringWriter 29 30 class TextModifiers( 31 override val codebase: Codebase, 32 flags: Int = PACKAGE_PRIVATE, 33 annotations: MutableList<AnnotationItem>? = null 34 ) : DefaultModifierList(codebase, flags, annotations) { 35 36 fun duplicate(): TextModifiers { 37 val annotations = this.annotations 38 val newAnnotations = 39 if (annotations == null || annotations.isEmpty()) { 40 null 41 } else { 42 annotations.toMutableList() 43 } 44 return TextModifiers(codebase, flags, newAnnotations) 45 } 46 47 fun addAnnotations(annotationSources: List<String>?) { 48 annotationSources ?: return 49 if (annotationSources.isEmpty()) { 50 return 51 } 52 53 val annotations = ArrayList<AnnotationItem>(annotationSources.size) 54 annotationSources.forEach { source -> 55 val index = source.indexOf('(') 56 val originalName = if (index == -1) source.substring(1) else source.substring(1, index) 57 val qualifiedName = AnnotationItem.mapName(codebase, originalName) 58 59 // @Deprecated is also treated as a "modifier" 60 if (qualifiedName == JAVA_LANG_DEPRECATED) { 61 setDeprecated(true) 62 } 63 64 val attributes = 65 if (index == -1) { 66 emptyList() 67 } else { 68 DefaultAnnotationAttribute.createList(source.substring(index + 1, source.lastIndexOf(')'))) 69 } 70 val codebase = codebase 71 val item = object : DefaultAnnotationItem(codebase) { 72 override fun attributes(): List<AnnotationAttribute> = attributes 73 override fun originalName(): String? = originalName 74 override fun qualifiedName(): String? = qualifiedName 75 override fun toSource(target: AnnotationTarget, showDefaultAttrs: Boolean): String = source 76 } 77 annotations.add(item) 78 } 79 this.annotations = annotations 80 } 81 82 override fun toString(): String { 83 val item = owner() 84 val writer = StringWriter() 85 ModifierList.write(writer, this, item, target = AnnotationTarget.SDK_STUBS_FILE) 86 return writer.toString() 87 } 88 } 89