1 /* 2 * Copyright (C) 2015 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.base; 18 19 import static com.google.auto.common.MoreTypes.isType; 20 import static com.google.common.base.Preconditions.checkArgument; 21 import static dagger.internal.codegen.langmodel.DaggerTypes.isTypeOf; 22 import static dagger.internal.codegen.langmodel.DaggerTypes.unwrapType; 23 import static dagger.internal.codegen.xprocessing.XTypes.isTypeOf; 24 25 import androidx.room.compiler.processing.XType; 26 import com.google.auto.value.AutoValue; 27 import com.squareup.javapoet.ClassName; 28 import com.squareup.javapoet.TypeName; 29 import dagger.internal.codegen.javapoet.TypeNames; 30 import dagger.spi.model.Key; 31 import javax.lang.model.type.TypeMirror; 32 33 /** Information about a {@link java.util.Set} {@link TypeMirror}. */ 34 @AutoValue 35 public abstract class SetType { 36 private XType type; 37 38 /** The set type itself. */ typeName()39 abstract TypeName typeName(); 40 41 /** The set type itself. */ type()42 private XType type() { 43 return type; 44 } 45 46 /** {@code true} if the set type is the raw {@link java.util.Set} type. */ isRawType()47 public boolean isRawType() { 48 return type().getTypeArguments().isEmpty(); 49 } 50 51 /** Returns the element type. */ elementType()52 public XType elementType() { 53 return unwrapType(type()); 54 } 55 56 /** Returns {@code true} if {@link #elementType()} is of type {@code className}. */ elementsAreTypeOf(ClassName className)57 public boolean elementsAreTypeOf(ClassName className) { 58 return !isRawType() && isTypeOf(elementType(), className); 59 } 60 61 /** 62 * {@code T} if {@link #elementType()} is a {@code WrappingClass<T>}. 63 * 64 * @throws IllegalStateException if {@link #elementType()} is not a {@code WrappingClass<T>} 65 */ 66 // TODO(b/202033221): Consider using stricter input type, e.g. FrameworkType. unwrappedElementType(ClassName wrappingClass)67 public XType unwrappedElementType(ClassName wrappingClass) { 68 checkArgument( 69 elementsAreTypeOf(wrappingClass), 70 "expected elements to be %s, but this type is %s", 71 wrappingClass, 72 type()); 73 return unwrapType(elementType()); 74 } 75 76 /** {@code true} if {@code type} is a {@link java.util.Set} type. */ isSet(XType type)77 public static boolean isSet(XType type) { 78 return isTypeOf(type, TypeNames.SET); 79 } 80 81 /** {@code true} if {@code type} is a {@link java.util.Set} type. */ isSet(TypeMirror type)82 public static boolean isSet(TypeMirror type) { 83 return isType(type) && isTypeOf(TypeNames.SET, type); 84 } 85 86 /** {@code true} if {@code key.type()} is a {@link java.util.Set} type. */ isSet(Key key)87 public static boolean isSet(Key key) { 88 return isSet(key.type().xprocessing()); 89 } 90 91 /** 92 * Returns a {@link SetType} for {@code type}. 93 * 94 * @throws IllegalArgumentException if {@code type} is not a {@link java.util.Set} type 95 */ from(XType type)96 public static SetType from(XType type) { 97 checkArgument(isSet(type), "%s must be a Set", type); 98 SetType setType = new AutoValue_SetType(type.getTypeName()); 99 setType.type = type; 100 return setType; 101 } 102 103 /** 104 * Returns a {@link SetType} for {@code key}'s {@link Key#type() type}. 105 * 106 * @throws IllegalArgumentException if {@code key.type()} is not a {@link java.util.Set} type 107 */ from(Key key)108 public static SetType from(Key key) { 109 return from(key.type().xprocessing()); 110 } 111 } 112