• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.xprocessing;
18 
19 import static androidx.room.compiler.processing.compat.XConverters.toJavac;
20 
21 import androidx.room.compiler.processing.XArrayType;
22 import androidx.room.compiler.processing.XType;
23 import androidx.room.compiler.processing.compat.XConverters;
24 import com.squareup.javapoet.ClassName;
25 import javax.lang.model.type.TypeKind;
26 
27 // TODO(bcorso): Consider moving these methods into XProcessing library.
28 /** A utility class for {@link XType} helper methods. */
29 public final class XTypes {
30 
31   /** Returns {@code true} if the given type is a raw type of a parameterized type. */
isRawParameterizedType(XType type)32   public static boolean isRawParameterizedType(XType type) {
33     return isDeclared(type)
34         && type.getTypeArguments().isEmpty()
35         && !type.getTypeElement().getType().getTypeArguments().isEmpty();
36   }
37 
38   /** Returns the given {@code type} as an {@link XArrayType}. */
asArray(XType type)39   public static XArrayType asArray(XType type) {
40     return (XArrayType) type;
41   }
42 
43   /** Returns {@code true} if the raw type of {@code type} is equal to {@code className}. */
isTypeOf(XType type, ClassName className)44   public static boolean isTypeOf(XType type, ClassName className) {
45     return isDeclared(type) && type.getTypeElement().getClassName().equals(className);
46   }
47 
48   /** Returns {@code true} if the given type is a declared type. */
isWildcard(XType type)49   public static boolean isWildcard(XType type) {
50     return toJavac(type).getKind().equals(TypeKind.WILDCARD);
51   }
52 
53   /** Returns {@code true} if the given type is a declared type. */
isDeclared(XType type)54   public static boolean isDeclared(XType type) {
55     return type.getTypeElement() != null;
56   }
57 
58   /** Returns {@code true} if the given type is a type variable. */
isTypeVariable(XType type)59   public static boolean isTypeVariable(XType type) {
60     return XConverters.toJavac(type).getKind() == TypeKind.TYPEVAR;
61   }
62 
63   /**
64    * Returns {@code true} if {@code type1} is equivalent to {@code type2}.
65    */
areEquivalentTypes(XType type1, XType type2)66   public static boolean areEquivalentTypes(XType type1, XType type2) {
67     return type1.getTypeName().equals(type2.getTypeName());
68   }
69 
70   /** Returns {@code true} if the given type is a primitive type. */
isPrimitive(XType type)71   public static boolean isPrimitive(XType type) {
72     return XConverters.toJavac(type).getKind().isPrimitive();
73   }
74 
75   /** Returns {@code true} if the given type has type parameters. */
hasTypeParameters(XType type)76   public static boolean hasTypeParameters(XType type) {
77     return !type.getTypeArguments().isEmpty();
78   }
79 
XTypes()80   private XTypes() {}
81 }
82