1 /* 2 * Copyright (C) 2017 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.internal.codegen; 18 19 import com.google.auto.common.MoreElements; 20 import com.google.auto.common.MoreTypes; 21 import dagger.internal.codegen.langmodel.DaggerTypes; 22 import dagger.model.Key; 23 import java.util.Optional; 24 import javax.lang.model.element.AnnotationMirror; 25 import javax.lang.model.element.ElementKind; 26 import javax.lang.model.element.Modifier; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.type.DeclaredType; 29 import javax.lang.model.type.TypeKind; 30 import javax.lang.model.type.TypeMirror; 31 import javax.lang.model.util.SimpleTypeVisitor6; 32 33 /** Utility methods related to {@link Key}s. */ 34 final class Keys { isValidMembersInjectionKey(Key key)35 static boolean isValidMembersInjectionKey(Key key) { 36 return !key.qualifier().isPresent() 37 && !key.multibindingContributionIdentifier().isPresent() 38 && key.type().getKind().equals(TypeKind.DECLARED); 39 } 40 41 /** 42 * Returns {@code true} if this is valid as an implicit key (that is, if it's valid for a 43 * just-in-time binding by discovering an {@code @Inject} constructor). 44 */ isValidImplicitProvisionKey(Key key, DaggerTypes types)45 static boolean isValidImplicitProvisionKey(Key key, DaggerTypes types) { 46 return isValidImplicitProvisionKey(key.qualifier(), key.type(), types); 47 } 48 49 /** 50 * Returns {@code true} if a key with {@code qualifier} and {@code type} is valid as an implicit 51 * key (that is, if it's valid for a just-in-time binding by discovering an {@code @Inject} 52 * constructor). 53 */ isValidImplicitProvisionKey( Optional<? extends AnnotationMirror> qualifier, TypeMirror type, final DaggerTypes types)54 static boolean isValidImplicitProvisionKey( 55 Optional<? extends AnnotationMirror> qualifier, TypeMirror type, final DaggerTypes types) { 56 // Qualifiers disqualify implicit provisioning. 57 if (qualifier.isPresent()) { 58 return false; 59 } 60 61 return type.accept( 62 new SimpleTypeVisitor6<Boolean, Void>(false) { 63 @Override 64 public Boolean visitDeclared(DeclaredType type, Void ignored) { 65 // Non-classes or abstract classes aren't allowed. 66 TypeElement element = MoreElements.asType(type.asElement()); 67 if (!element.getKind().equals(ElementKind.CLASS) 68 || element.getModifiers().contains(Modifier.ABSTRACT)) { 69 return false; 70 } 71 72 // If the key has type arguments, validate that each type argument is declared. 73 // Otherwise the type argument may be a wildcard (or other type), and we can't 74 // resolve that to actual types. 75 for (TypeMirror arg : type.getTypeArguments()) { 76 if (arg.getKind() != TypeKind.DECLARED) { 77 return false; 78 } 79 } 80 81 // Also validate that the key is not the erasure of a generic type. 82 // If it is, that means the user referred to Foo<T> as just 'Foo', 83 // which we don't allow. (This is a judgement call -- we *could* 84 // allow it and instantiate the type bounds... but we don't.) 85 return MoreTypes.asDeclared(element.asType()).getTypeArguments().isEmpty() 86 || !types.isSameType(types.erasure(element.asType()), type); 87 } 88 }, 89 null); 90 } 91 } 92