1 /* 2 * Copyright (C) 2019 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.aggregateddeps; 18 19 import static com.google.auto.common.Visibility.effectiveVisibilityOfElement; 20 21 import com.google.auto.common.MoreElements; 22 import com.google.auto.common.Visibility; 23 import com.google.auto.value.AutoValue; 24 import com.squareup.javapoet.ClassName; 25 import com.squareup.javapoet.TypeName; 26 import dagger.hilt.processor.internal.ClassNames; 27 import dagger.hilt.processor.internal.Processors; 28 import java.util.Optional; 29 import javax.lang.model.element.AnnotationMirror; 30 import javax.lang.model.element.Element; 31 import javax.lang.model.element.TypeElement; 32 import javax.lang.model.util.Elements; 33 34 /** PkgPrivateModuleMetadata contains a set of utilities for processing package private modules. */ 35 @AutoValue 36 abstract class PkgPrivateMetadata { 37 private static final String PREFIX = "HiltWrapper_"; 38 39 /** Returns the base class name of the elemenet. */ baseClassName()40 TypeName baseClassName() { 41 return TypeName.get(getTypeElement().asType()); 42 } 43 44 /** Returns TypeElement for the module element the metadata object represents */ getTypeElement()45 abstract TypeElement getTypeElement(); 46 47 /** 48 * Returns an optional @InstallIn AnnotationMirror for the module element the metadata object 49 * represents 50 */ getOptionalInstallInAnnotationMirror()51 abstract Optional<AnnotationMirror> getOptionalInstallInAnnotationMirror(); 52 53 /** Return the Type of this package private element. */ getAnnotation()54 abstract ClassName getAnnotation(); 55 56 /** Returns the expected genenerated classname for the element the metadata object represents */ generatedClassName()57 final ClassName generatedClassName() { 58 return Processors.prepend( 59 Processors.getEnclosedClassName(ClassName.get(getTypeElement())), PREFIX); 60 } 61 62 /** 63 * Returns an Optional PkgPrivateMetadata requiring Hilt processing, otherwise returns an empty 64 * Optional. 65 */ of(Elements elements, Element element, ClassName annotation)66 static Optional<PkgPrivateMetadata> of(Elements elements, Element element, ClassName annotation) { 67 // If this is a public element no wrapping is needed 68 if (effectiveVisibilityOfElement(element) == Visibility.PUBLIC) { 69 return Optional.empty(); 70 } 71 72 Optional<AnnotationMirror> installIn; 73 if (Processors.hasAnnotation(element, ClassNames.INSTALL_IN)) { 74 installIn = Optional.of(Processors.getAnnotationMirror(element, ClassNames.INSTALL_IN)); 75 } else if (Processors.hasAnnotation(element, ClassNames.TEST_INSTALL_IN)) { 76 installIn = Optional.of(Processors.getAnnotationMirror(element, ClassNames.TEST_INSTALL_IN)); 77 } else { 78 throw new IllegalStateException( 79 "Expected element to be annotated with @InstallIn: " + element); 80 } 81 82 if (annotation.equals(ClassNames.MODULE) 83 ) { 84 // Skip modules that require a module instance. Required by 85 // dagger (b/31489617) 86 if (Processors.requiresModuleInstance(elements, MoreElements.asType(element))) { 87 return Optional.empty(); 88 } 89 } 90 return Optional.of( 91 new AutoValue_PkgPrivateMetadata(MoreElements.asType(element), installIn, annotation)); 92 } 93 } 94