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 if (annotationSources.isEmpty()) { 49 return 50 } 51 52 val annotations = ArrayList<AnnotationItem>(annotationSources.size) 53 annotationSources.forEach { source -> 54 val index = source.indexOf('(') 55 val originalName = if (index == -1) source.substring(1) else source.substring(1, index) 56 val qualifiedName = AnnotationItem.mapName(codebase, originalName) 57 58 // @Deprecated is also treated as a "modifier" 59 if (qualifiedName == JAVA_LANG_DEPRECATED) { 60 setDeprecated(true) 61 } 62 63 val attributes = 64 if (index == -1) { 65 emptyList() 66 } else { 67 DefaultAnnotationAttribute.createList(source.substring(index + 1, source.lastIndexOf(')'))) 68 } 69 val codebase = codebase 70 val item = object : DefaultAnnotationItem(codebase) { 71 override val attributes: List<AnnotationAttribute> = attributes 72 override val originalName: String? = originalName 73 override val qualifiedName: String? = qualifiedName 74 override fun toSource(target: AnnotationTarget, showDefaultAttrs: Boolean): String = source 75 } 76 annotations.add(item) 77 } 78 this.annotations = annotations 79 } 80 81 override fun toString(): String { 82 val item = owner() 83 val writer = StringWriter() 84 ModifierList.write(writer, this, item, target = AnnotationTarget.SDK_STUBS_FILE) 85 return writer.toString() 86 } 87 } 88