1 /* <lambda>null2 * Copyright (C) 2019 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 18 package com.android.codegen 19 20 import com.github.javaparser.ast.CompilationUnit 21 22 /** 23 * Mixin for optionally shortening references based on existing imports 24 */ 25 interface ImportsProvider { 26 27 abstract val fileAst: CompilationUnit 28 29 val NonNull: String get() { return classRef("android.annotation.NonNull") } 30 val NonEmpty: String get() { return classRef("android.annotation.NonEmpty") } 31 val Nullable: String get() { return classRef("android.annotation.Nullable") } 32 val TextUtils: String get() { return classRef("android.text.TextUtils") } 33 val LinkedHashMap: String get() { return classRef("java.util.LinkedHashMap") } 34 val Collections: String get() { return classRef("java.util.Collections") } 35 val Preconditions: String get() { return classRef("com.android.internal.util.Preconditions") } 36 val ArrayList: String get() { return classRef("java.util.ArrayList") } 37 val DataClass: String get() { return classRef("com.android.internal.util.DataClass") } 38 val DataClassEnum: String get() { return classRef("com.android.internal.util.DataClass.Enum") } 39 val ParcelWith: String get() { return classRef("com.android.internal.util.DataClass.ParcelWith") } 40 val PluralOf: String get() { return classRef("com.android.internal.util.DataClass.PluralOf") } 41 val Each: String get() { return classRef("com.android.internal.util.DataClass.Each") } 42 val MaySetToNull: String get() { return classRef("com.android.internal.util.DataClass.MaySetToNull") } 43 val DataClassGenerated: String get() { return classRef("com.android.internal.util.DataClass.Generated") } 44 val DataClassSuppressConstDefs: String get() { return classRef("com.android.internal.util.DataClass.SuppressConstDefsGeneration") } 45 val DataClassSuppress: String get() { return classRef("com.android.internal.util.DataClass.Suppress") } 46 val GeneratedMember: String get() { return classRef("com.android.internal.util.DataClass.Generated.Member") } 47 val Parcelling: String get() { return classRef("com.android.internal.util.Parcelling") } 48 val Parcelable: String get() { return classRef("android.os.Parcelable") } 49 val Parcel: String get() { return classRef("android.os.Parcel") } 50 val UnsupportedAppUsage: String get() { return classRef("android.compat.annotation.UnsupportedAppUsage") } 51 52 /** 53 * Optionally shortens a class reference if there's a corresponding import present 54 */ 55 fun classRef(fullName: String): String { 56 57 val pkg = fullName.substringBeforeLast(".") 58 val simpleName = fullName.substringAfterLast(".") 59 if (fileAst.imports.any { imprt -> 60 imprt.nameAsString == fullName 61 || (imprt.isAsterisk && imprt.nameAsString == pkg) 62 }) { 63 return simpleName 64 } else { 65 val outerClass = pkg.substringAfterLast(".", "") 66 if (outerClass.firstOrNull()?.isUpperCase() == true) { 67 return classRef(pkg) + "." + simpleName 68 } 69 } 70 return fullName 71 } 72 73 /** @see classRef */ 74 fun memberRef(fullName: String): String { 75 val className = fullName.substringBeforeLast(".") 76 val methodName = fullName.substringAfterLast(".") 77 return if (fileAst.imports.any { 78 it.isStatic 79 && (it.nameAsString == fullName 80 || (it.isAsterisk && it.nameAsString == className)) 81 }) { 82 className.substringAfterLast(".") + "." + methodName 83 } else { 84 classRef(className) + "." + methodName 85 } 86 } 87 } 88 89 /** @see classRef */ classRefnull90inline fun <reified T : Any> ImportsProvider.classRef(): String { 91 return classRef(T::class.java.name) 92 }