1 /* 2 * Copyright (C) 2023 The Dagger Authors. 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 dagger.hilt.processor.internal 18 19 import com.google.auto.common.MoreTypes 20 import com.google.devtools.ksp.symbol.KSAnnotated 21 import com.google.devtools.ksp.symbol.KSDeclaration 22 import com.squareup.javapoet.ClassName 23 import dagger.spi.model.DaggerAnnotation 24 import dagger.spi.model.DaggerElement 25 import dagger.spi.model.DaggerProcessingEnv 26 import dagger.spi.model.DaggerProcessingEnv.Backend.JAVAC 27 import dagger.spi.model.DaggerProcessingEnv.Backend.KSP 28 import dagger.spi.model.DaggerType 29 30 hasAnnotationnull31fun DaggerType.hasAnnotation(className: ClassName): Boolean = 32 when (checkNotNull(backend())) { 33 JAVAC -> Processors.hasAnnotation(MoreTypes.asTypeElement(javac()), className) 34 KSP -> ksp().declaration.hasAnnotation(className.canonicalName()) 35 } 36 hasAnnotationnull37fun KSDeclaration.hasAnnotation(annotationName: String): Boolean = 38 annotations.any { 39 it.annotationType.resolve().declaration.qualifiedName?.asString().equals(annotationName) 40 } 41 hasAnnotationnull42fun DaggerElement.hasAnnotation(className: ClassName) = 43 when (checkNotNull(backend())) { 44 JAVAC -> Processors.hasAnnotation(javac(), className) 45 KSP -> ksp().hasAnnotation(className) 46 } 47 getQualifiedNamenull48fun DaggerAnnotation.getQualifiedName() = 49 when (checkNotNull(backend())) { 50 JAVAC -> MoreTypes.asTypeElement(javac().annotationType).qualifiedName.toString() 51 KSP -> ksp().annotationType.resolve().declaration.qualifiedName!!.asString() 52 } 53 hasAnnotationnull54private fun KSAnnotated.hasAnnotation(className: ClassName) = 55 annotations.any { 56 it.annotationType.resolve().declaration.qualifiedName!!.asString() == className.canonicalName() 57 } 58