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.originatingelement; 18 19 import androidx.room.compiler.processing.XElement; 20 import androidx.room.compiler.processing.XElementKt; 21 import androidx.room.compiler.processing.XProcessingEnv; 22 import androidx.room.compiler.processing.XTypeElement; 23 import com.google.common.collect.ImmutableSet; 24 import com.squareup.javapoet.ClassName; 25 import dagger.hilt.processor.internal.BaseProcessingStep; 26 import dagger.hilt.processor.internal.ClassNames; 27 import dagger.hilt.processor.internal.ProcessorErrors; 28 import dagger.hilt.processor.internal.Processors; 29 import dagger.internal.codegen.xprocessing.XElements; 30 31 /** 32 * Processes the annotations annotated with {@link dagger.hilt.codegen.OriginatingElement} to check 33 * that they're only used on top-level classes and the value passed is also a top-level class. 34 */ 35 public final class OriginatingElementProcessingStep extends BaseProcessingStep { 36 OriginatingElementProcessingStep(XProcessingEnv env)37 public OriginatingElementProcessingStep(XProcessingEnv env) { 38 super(env); 39 } 40 41 @Override annotationClassNames()42 protected ImmutableSet<ClassName> annotationClassNames() { 43 return ImmutableSet.of(ClassNames.ORIGINATING_ELEMENT); 44 } 45 46 @Override processEach(ClassName annotation, XElement element)47 public void processEach(ClassName annotation, XElement element) { 48 ProcessorErrors.checkState( 49 XElementKt.isTypeElement(element) && Processors.isTopLevel(element), 50 element, 51 "@%s should only be used to annotate top-level types, but found: %s", 52 annotation.simpleName(), 53 XElements.toStableString(element)); 54 55 XTypeElement topLevelClassElement = 56 element 57 .getAnnotation(ClassNames.ORIGINATING_ELEMENT) 58 .getAsType("topLevelClass") 59 .getTypeElement(); 60 61 // TODO(bcorso): ProcessorErrors should allow us to point to the annotation too. 62 ProcessorErrors.checkState( 63 Processors.isTopLevel(topLevelClassElement), 64 element, 65 "@%s.topLevelClass value should be a top-level class, but found: %s", 66 annotation.simpleName(), 67 topLevelClassElement.getClassName()); 68 } 69 } 70