1 /* 2 * Copyright (C) 2020 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.root; 18 19 import com.google.auto.common.MoreElements; 20 import com.google.auto.value.AutoValue; 21 import com.squareup.javapoet.ClassName; 22 import dagger.hilt.processor.internal.ClassNames; 23 import dagger.hilt.processor.internal.ProcessorErrors; 24 import dagger.hilt.processor.internal.Processors; 25 import javax.annotation.processing.ProcessingEnvironment; 26 import javax.lang.model.element.Element; 27 import javax.lang.model.element.TypeElement; 28 29 /** Metadata class for {@code InternalTestRoot} annotated classes. */ 30 @AutoValue 31 abstract class TestRootMetadata { 32 33 /** Returns the {@link TypeElement} for the test class. */ testElement()34 abstract TypeElement testElement(); 35 36 /** Returns the {@link TypeElement} for the base application. */ baseElement()37 abstract TypeElement baseElement(); 38 39 /** Returns the {@link ClassName} for the test class. */ testName()40 ClassName testName() { 41 return ClassName.get(testElement()); 42 } 43 44 /** Returns the {@link ClassName} for the base application. */ baseAppName()45 ClassName baseAppName() { 46 return ClassName.get(baseElement()); 47 } 48 49 /** The name of the generated Hilt test application class for the given test name. */ appName()50 ClassName appName() { 51 return Processors.append(Processors.getEnclosedClassName(testName()), "_Application"); 52 } 53 54 /** The name of the generated Hilt test application class for the given test name. */ testInjectorName()55 ClassName testInjectorName() { 56 return Processors.append(Processors.getEnclosedClassName(testName()), "_GeneratedInjector"); 57 } 58 of(ProcessingEnvironment env, Element element)59 static TestRootMetadata of(ProcessingEnvironment env, Element element) { 60 61 TypeElement testElement = MoreElements.asType(element); 62 63 TypeElement baseElement = 64 env.getElementUtils().getTypeElement(ClassNames.MULTI_DEX_APPLICATION.toString()); 65 ProcessorErrors.checkState( 66 !Processors.hasAnnotation(element, ClassNames.ANDROID_ENTRY_POINT), 67 element, 68 "Tests cannot be annotated with @AndroidEntryPoint. Please use @HiltAndroidTest"); 69 70 ProcessorErrors.checkState( 71 Processors.hasAnnotation(element, ClassNames.HILT_ANDROID_TEST), 72 element, 73 "Tests must be annotated with @HiltAndroidTest"); 74 75 return new AutoValue_TestRootMetadata(testElement, baseElement); 76 } 77 } 78